<commit_before>var ACTIVE_LOCALE = window.ACTIVE_LOCALE;
if (ACTIVE_LOCALE !== 'en') {
  require('moment/locale/' + ACTIVE_LOCALE);
}
var Locale = require('../../locale/index');
var Polyglot = require('node-polyglot');
var polyglot = new Polyglot({
  locale: ACTIVE_LOCALE, // Needed for pluralize behaviour
  phrases: Locale[ACTIVE_LOCALE]
});
window._t = polyglot.t.bind(polyglot);

var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var cdb = require('cartodb.js');
var deepInsights = require('cartodb-deep-insights.js');
var ConfigModel = require('./data/config-model');
var EditorMapView = require('./editor/editor-map-view');
var MapDefinitionModel = require('./data/map-definition-model');
var AnalysisDefinitionNodesCollection = require('./data/analysis-definition-nodes-collection');
var AnalysisDefinitionsCollection = require('./data/analysis-definitions-collection');
var LayerDefinitionsCollection = require('./data/layer-definitions-collection');
var WidgetDefinitionsCollection = require('./data/widget-definitions-collection');
var VisDefinitionModel = require('./data/vis-definition-model');
var createEditorMenuTabPane = require('./components/tab-pane/create-editor-menu-tab-pane');
var editorPaneTemplate = require('./editor/editor-pane.tpl');
var editorPaneIconItemTemplate = require('./editor/editor-pane-icon.tpl');
var ModalsServiceModel = require('./components/modals/modals-service-model');
var OnboardingsServiceModel = require('./components/onboardings/onboardings-service-model');
var BuilderOnboardingLauncher = require('./components/onboardings/builder/launcher');
var viewFactory = require('./components/view-factory');
var UserModel = require('./data/user-model');
var UserNotifications = require('./data/user-notifications');
var OrganizationUsers = require('./data/organization-users-collection');
var EditorModel = require('./data/editor-model');
var UserActions = require('./data/user-actions');
var ImporterManager = require('./components/background-importer/background-importer');
var BackgroundPollingModel = require('./data/editor-background-polling-model');
var StyleManager = require('./editor/style/style-manager');
var DeepInsightsIntegrations = require('./deep-insights-integrations');
var Notifier = require('./components/notifier/notifier');
var FeedbackButtonView = require('./editor/feedback/feedback-button-view');
var SettingsOptions = require('./data/map-settings');
var SettingsView = require('./editor/editor-settings-view');
var OverlaysCollection = require('./data/overlays-definition-collection');
var MapcapsCollection = require('./data/mapcaps-collection');
var PrivacyCollection = require('./components/modals/publish/privacy-collection');
var CreatePrivacyOptions = require('./components/modals/publish/create-privacy-options');
var UserGroupFetcher = require('./data/users-group-fetcher');
var State = require('./data/state-definition-model');

// JSON data passed from entry point (editor/visualizations/show.html):
var vizJSON = window.vizJSON;
var stateJSON = window.stateJSON;
var userData = window.userData;
var frontendConfig = window.frontendConfig;
var visualizationData = window.visualizationData;
var layersData = window.layersData;
var analysesData = window.analysesData;
var builderNotifications = window.builderNotifications;
var basemaps = window.basemaps;

cdb.god = new Backbone.Model(); // To be replaced

var configModel = new ConfigModel(
  _.defaults(
    {
      base_url: userData.base_url,
      api_key: userData.api_key
    },
    frontendConfig
  )
);

var userNotifications = new UserNotifications(builderNotifications, {
  key: 'builder',
  configModel: configModel
});

var userModel = new UserModel(userData, {
  configModel: configModel
});

if (userModel.isInsideOrg()) {
  userModel.usersOrganization = new OrganizationUsers(null, {
    organizationId: userModel._organizationModel.id,
    currentUserId: userModel.id,
    configModel: configModel
  });

  userModel.usersOrganization.fetch();
}

var editorModel = new EditorModel();

var visDefinitionModel = new VisDefinitionModel(visualizationData, {
  configModel: configModel
});
var modals = new ModalsServiceModel();
var onboardings = new OnboardingsServiceModel();

BuilderOnboardingLauncher.init({
  onboardings: onboardings,
  userModel: userModel,
  userNotifications: userNotifications
});

BuilderOnboardingLauncher.launch();

UserGroupFetcher.track({
  userModel: userModel,
  configModel: configModel,
  acl: visDefinitionModel.getPermissionModel().acl
});

// Setup and create the vis (map, layers etc.) + dashboard (widgets)
// from the given vizJSON, but disabling legends
vizJSON = _.extend(
  vizJSON, {
    legends: false
  }
);
deepInsights.createDashboard('#dashboard', vizJSON, {
  apiKey: configModel.get('api_key'),
  no_cdn: false,
  cartodb_logo: false,
  renderMenu: false,
  show_empty_infowindow_fields: true,
  state: stateJSON
}, function (error, dashboard) {
  if (error) {
    console.error('Dashboard has some errors:', error);
  }

  var mapId = visualizationData.map_id;

  var analysisDefinitionNodesCollection = new AnalysisDefinitionNodesCollection(null, {
    configModel: configModel
  });

  var analysisDefinitionsCollection = new AnalysisDefinitionsCollection(analysesData, {
    configModel: configModel,
    analysisDefinitionNodesCollection: analysisDefinitionNodesCollection,
    vizId: visDefinitionModel.id
  });

  var layerDefinitionsCollection = new LayerDefinitionsCollection(null, {
    configModel: configModel,
    analysisDefinitionNodesCollection: analysisDefinitionNodesCollection,
    mapId: mapId
  });

  var vis = dashboard.getMap();<commit_msg>Patch vizJSON to prepend username.<commit_after>var ACTIVE_LOCALE = window.ACTIVE_LOCALE;
if (ACTIVE_LOCALE !== 'en') {
  require('moment/locale/' + ACTIVE_LOCALE);
}
var Locale = require('../../locale/index');
var Polyglot = require('node-polyglot');
var polyglot = new Polyglot({
  locale: ACTIVE_LOCALE, // Needed for pluralize behaviour
  phrases: Locale[ACTIVE_LOCALE]
});
window._t = polyglot.t.bind(polyglot);

var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var cdb = require('cartodb.js');
var deepInsights = require('cartodb-deep-insights.js');
var ConfigModel = require('./data/config-model');
var EditorMapView = require('./editor/editor-map-view');
var MapDefinitionModel = require('./data/map-definition-model');
var AnalysisDefinitionNodesCollection = require('./data/analysis-definition-nodes-collection');
var AnalysisDefinitionsCollection = require('./data/analysis-definitions-collection');
var LayerDefinitionsCollection = require('./data/layer-definitions-collection');
var WidgetDefinitionsCollection = require('./data/widget-definitions-collection');
var VisDefinitionModel = require('./data/vis-definition-model');
var createEditorMenuTabPane = require('./components/tab-pane/create-editor-menu-tab-pane');
var editorPaneTemplate = require('./editor/editor-pane.tpl');
var editorPaneIconItemTemplate = require('./editor/editor-pane-icon.tpl');
var ModalsServiceModel = require('./components/modals/modals-service-model');
var OnboardingsServiceModel = require('./components/onboardings/onboardings-service-model');
var BuilderOnboardingLauncher = require('./components/onboardings/builder/launcher');
var viewFactory = require('./components/view-factory');
var UserModel = require('./data/user-model');
var UserNotifications = require('./data/user-notifications');
var OrganizationUsers = require('./data/organization-users-collection');
var EditorModel = require('./data/editor-model');
var UserActions = require('./data/user-actions');
var ImporterManager = require('./components/background-importer/background-importer');
var BackgroundPollingModel = require('./data/editor-background-polling-model');
var StyleManager = require('./editor/style/style-manager');
var DeepInsightsIntegrations = require('./deep-insights-integrations');
var Notifier = require('./components/notifier/notifier');
var FeedbackButtonView = require('./editor/feedback/feedback-button-view');
var SettingsOptions = require('./data/map-settings');
var SettingsView = require('./editor/editor-settings-view');
var OverlaysCollection = require('./data/overlays-definition-collection');
var MapcapsCollection = require('./data/mapcaps-collection');
var PrivacyCollection = require('./components/modals/publish/privacy-collection');
var CreatePrivacyOptions = require('./components/modals/publish/create-privacy-options');
var UserGroupFetcher = require('./data/users-group-fetcher');
var State = require('./data/state-definition-model');
var SQLUtils = require('./helpers/sql-utils');

// JSON data passed from entry point (editor/visualizations/show.html):
var vizJSON = window.vizJSON;
var stateJSON = window.stateJSON;
var userData = window.userData;
var frontendConfig = window.frontendConfig;
var visualizationData = window.visualizationData;
var layersData = window.layersData;
var analysesData = window.analysesData;
var builderNotifications = window.builderNotifications;
var basemaps = window.basemaps;

cdb.god = new Backbone.Model(); // To be replaced

var configModel = new ConfigModel(
  _.defaults(
    {
      base_url: userData.base_url,
      api_key: userData.api_key
    },
    frontendConfig
  )
);

var userNotifications = new UserNotifications(builderNotifications, {
  key: 'builder',
  configModel: configModel
});

var userModel = new UserModel(userData, {
  configModel: configModel
});

if (userModel.isInsideOrg()) {
  userModel.usersOrganization = new OrganizationUsers(null, {
    organizationId: userModel._organizationModel.id,
    currentUserId: userModel.id,
    configModel: configModel
  });

  userModel.usersOrganization.fetch();
}

var editorModel = new EditorModel();

var visDefinitionModel = new VisDefinitionModel(visualizationData, {
  configModel: configModel
});
var modals = new ModalsServiceModel();
var onboardings = new OnboardingsServiceModel();

BuilderOnboardingLauncher.init({
  onboardings: onboardings,
  userModel: userModel,
  userNotifications: userNotifications
});

BuilderOnboardingLauncher.launch();

UserGroupFetcher.track({
  userModel: userModel,
  configModel: configModel,
  acl: visDefinitionModel.getPermissionModel().acl
});

// Setup and create the vis (map, layers etc.) + dashboard (widgets)
// from the given vizJSON, but disabling legends
vizJSON = _.extend(
  vizJSON, {
    legends: false
  }
);

// prepend username to tablename in queries in order to fix map instantiation
// in map created from a shared dataset
var patchQuery = function (o) {
  var sql = o.params.query;
  var tableNameSet = o.options.table_name.split('.');
  var isOwner = tableNameSet.length === 1;

  if (!isOwner) {
    o.params.query = SQLUtils.prependTableName(sql, tableNameSet[1], tableNameSet[0]);
  }
};

// Patch the vizJSON analyses
_.each(vizJSON.analyses, patchQuery);

// Patch the analysesData
_.each(analysesData, function (analysis) {
  var analysisNode = analysis.analysis_definition;
  patchQuery(analysisNode);
});

deepInsights.createDashboard('#dashboard', vizJSON, {
  apiKey: configModel.get('api_key'),
  no_cdn: false,
  cartodb_logo: false,
  renderMenu: false,
  show_empty_infowindow_fields: true,
  state: stateJSON
}, function (error, dashboard) {
  if (error) {
    console.error('Dashboard has some errors:', error);
  }

  var mapId = visualizationData.map_id;

  var analysisDefinitionNodesCollection = new AnalysisDefinitionNodesCollection(null, {
    configModel: configModel
  });

  var analysisDefinitionsCollection = new AnalysisDefinitionsCollection(analysesData, {
    configModel: configModel,
    analysisDefinitionNodesCollection: analysisDefinitionNodesCollection,
    vizId: visDefinitionModel.id
  });

  var layerDefinitionsCollection = new LayerDefinitionsCollection(null, {
    configModel: configModel,
    analysisDefinitionNodesCollection: analysisDefinitionNodesCollection,
    mapId: mapId
  });

  var vis = dashboard.getMap();
{'fraction_non_alphanumeric': 0.12882927637025998, 'fraction_numerical': 0.00024838549428713363, 'mean_word_length': 5.655096418732782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6070176', 'n_tokens_mistral': 3443, 'n_tokens_neox': 3350, 'n_words': 721}