<commit_before>var _ = require('underscore');
var Backbone = require('backbone');
var LegendManager = require('./legend-manager');
var linkLayerInfowindow = require('./link-layer-infowindow');
var linkLayerTooltip = require('./link-layer-tooltip');
var layerTypesAndKinds = require('../data/layer-types-and-kinds');
var Notifier = require('../components/notifier/notifier');
var checkAndBuildOpts = require('../helpers/required-opts');
var NotificationErrorMessageHandler = require('../editor/layers/notification-error-message-handler');
var basemapProvidersAndCategories = require('../data/basemap-providers-and-categories');

var REQUIRED_OPTS = [
  'analysisDefinitionNodesCollection',
  'editFeatureOverlay',
  'layerDefinitionsCollection',
  'legendDefinitionsCollection',
  'diDashboardHelpers'
];

var LAYER_TYPE_TO_LAYER_CREATE_METHOD = {
  'cartodb': 'createCartoDBLayer',
  'gmapsbase': 'createGMapsBaseLayer',
  'plain': 'createPlainLayer',
  'tiled': 'createTileLayer',
  'torque': 'createTorqueLayer',
  'wms': 'createWMSLayer',
  'raster_tileo': 'createRasterTileoLayer'
};

var CARTODBJS_TO_CARTODB_ATTRIBUTE_MAPPINGS = {
  'layer_name': ['table_name_alias', 'table_name']
};

var BLACKLISTED_LAYER_DEFINITION_ATTRS = {
  'all': [ 'letter', 'kind' ],
  'Tiled': [ 'category', 'selected', 'highlighted' ],
  'CartoDB': [ 'color', 'letter' ],
  'torque': [ 'color', 'letter' ]
};

/**
 *  Only manage **LAYER** actions between Deep-Insights (CARTO.js) and Builder
 *
 */

var LayersIntegration = {
  track: function (options) {
    checkAndBuildOpts(options, REQUIRED_OPTS, this);

    this._visMap = this._diDashboardHelpers.visMap();

    this._layerDefinitionsCollection.each(this._linkLayerErrors, this);
    this._layerDefinitionsCollection.on('add', this._onLayerDefinitionAdded, this);
    this._layerDefinitionsCollection.on('sync', this._onLayerDefinitionSynced, this);
    this._layerDefinitionsCollection.on('change', this._onLayerDefinitionChanged, this);
    this._layerDefinitionsCollection.on('remove', this._onLayerDefinitionRemoved, this);
    this._layerDefinitionsCollection.on('layerMoved', this._onLayerDefinitionMoved, this);
    this._layerDefinitionsCollection.on('baseLayerChanged', this._onBaseLayerChanged, this);

    this._layerDefinitionsCollection.each(function (layerDefModel) {
      LegendManager.track(layerDefModel);

      linkLayerInfowindow(layerDefModel, this._visMap);
      linkLayerTooltip(layerDefModel, this._visMap);

      if (layerDefModel.has('source')) {
        this._resetStylesIfNoneApplied(layerDefModel);
      }
    }, this);

    // In order to sync layer selector and layer visibility
    this._diDashboardHelpers.getLayers().on('change:visible', function (layer, visible) {
      var layerDefModel = this._layerDefinitionsCollection.findWhere({id: layer.id});
      if (layerDefModel) {
        if (layerDefModel.get('visible') !== visible) {
          layerDefModel.save({visible: visible});
        }
      }
    }, this);

    return this;
  },

  _onLayerDefinitionRemoved: function (m) {
    if (!m.isNew()) {
      var layer = this._diDashboardHelpers.getLayer(m.id);
      layer && layer.remove();
    }
  },

  _onLayerDefinitionMoved: function (m, from, to) {
    this._diDashboardHelpers.moveCartoDBLayer(from, to);
  },

  _createLayer: function (layerDefModel, options) {
    options = options || {};
    var attrs = JSON.parse(JSON.stringify(layerDefModel.attributes)); // deep clone
    attrs = this._adaptAttrsToCDBjs(layerDefModel.get('type'), attrs);

    // create the legends for the new layer
    var legends = this._legendDefinitionsCollection.findByLayerDefModel(layerDefModel);
    if (legends.length > 0) {
      attrs.legends = _.map(legends, function (legend) {
        return legend.toJSON();
      });
    }
    attrs.type = (attrs.layer_name.indexOf('_raster') >= 0) ? 'raster_tileo' : attrs.type;
    var createMethodName = LAYER_TYPE_TO_LAYER_CREATE_METHOD[attrs.type.toLowerCase()];
    if (!createMethodName) throw new Error('no create method name found for type ' + attrs.type);

    if (attrs.source) {
      // Make sure the analysis is created first
      var nodeDefModel = this._analysisDefinitionNodesCollection.get(attrs.source);
      // Dependency with another integration
      this.trigger('onLayerCreation', nodeDefModel);
    }

    var layerPosition = this._layerDefinitionsCollection.indexOf(layerDefModel);<commit_msg>FIX: check we are handling a layer<commit_after>var _ = require('underscore');
var Backbone = require('backbone');
var LegendManager = require('./legend-manager');
var linkLayerInfowindow = require('./link-layer-infowindow');
var linkLayerTooltip = require('./link-layer-tooltip');
var layerTypesAndKinds = require('../data/layer-types-and-kinds');
var Notifier = require('../components/notifier/notifier');
var checkAndBuildOpts = require('../helpers/required-opts');
var NotificationErrorMessageHandler = require('../editor/layers/notification-error-message-handler');
var basemapProvidersAndCategories = require('../data/basemap-providers-and-categories');

var REQUIRED_OPTS = [
  'analysisDefinitionNodesCollection',
  'editFeatureOverlay',
  'layerDefinitionsCollection',
  'legendDefinitionsCollection',
  'diDashboardHelpers'
];

var LAYER_TYPE_TO_LAYER_CREATE_METHOD = {
  'cartodb': 'createCartoDBLayer',
  'gmapsbase': 'createGMapsBaseLayer',
  'plain': 'createPlainLayer',
  'tiled': 'createTileLayer',
  'torque': 'createTorqueLayer',
  'wms': 'createWMSLayer',
  'raster_tileo': 'createRasterTileoLayer'
};

var CARTODBJS_TO_CARTODB_ATTRIBUTE_MAPPINGS = {
  'layer_name': ['table_name_alias', 'table_name']
};

var BLACKLISTED_LAYER_DEFINITION_ATTRS = {
  'all': [ 'letter', 'kind' ],
  'Tiled': [ 'category', 'selected', 'highlighted' ],
  'CartoDB': [ 'color', 'letter' ],
  'torque': [ 'color', 'letter' ]
};

/**
 *  Only manage **LAYER** actions between Deep-Insights (CARTO.js) and Builder
 *
 */

var LayersIntegration = {
  track: function (options) {
    checkAndBuildOpts(options, REQUIRED_OPTS, this);

    this._visMap = this._diDashboardHelpers.visMap();

    this._layerDefinitionsCollection.each(this._linkLayerErrors, this);
    this._layerDefinitionsCollection.on('add', this._onLayerDefinitionAdded, this);
    this._layerDefinitionsCollection.on('sync', this._onLayerDefinitionSynced, this);
    this._layerDefinitionsCollection.on('change', this._onLayerDefinitionChanged, this);
    this._layerDefinitionsCollection.on('remove', this._onLayerDefinitionRemoved, this);
    this._layerDefinitionsCollection.on('layerMoved', this._onLayerDefinitionMoved, this);
    this._layerDefinitionsCollection.on('baseLayerChanged', this._onBaseLayerChanged, this);

    this._layerDefinitionsCollection.each(function (layerDefModel) {
      LegendManager.track(layerDefModel);

      linkLayerInfowindow(layerDefModel, this._visMap);
      linkLayerTooltip(layerDefModel, this._visMap);

      if (layerDefModel.has('source')) {
        this._resetStylesIfNoneApplied(layerDefModel);
      }
    }, this);

    // In order to sync layer selector and layer visibility
    this._diDashboardHelpers.getLayers().on('change:visible', function (layer, visible) {
      var layerDefModel = this._layerDefinitionsCollection.findWhere({id: layer.id});
      if (layerDefModel) {
        if (layerDefModel.get('visible') !== visible) {
          layerDefModel.save({visible: visible});
        }
      }
    }, this);

    return this;
  },

  _onLayerDefinitionRemoved: function (m) {
    if (!m.isNew()) {
      var layer = this._diDashboardHelpers.getLayer(m.id);
      layer && layer.remove();
    }
  },

  _onLayerDefinitionMoved: function (m, from, to) {
    this._diDashboardHelpers.moveCartoDBLayer(from, to);
  },

  _createLayer: function (layerDefModel, options) {
    options = options || {};
    var attrs = JSON.parse(JSON.stringify(layerDefModel.attributes)); // deep clone
    attrs = this._adaptAttrsToCDBjs(layerDefModel.get('type'), attrs);

    // create the legends for the new layer
    var legends = this._legendDefinitionsCollection.findByLayerDefModel(layerDefModel);
    if (legends.length > 0) {
      attrs.legends = _.map(legends, function (legend) {
        return legend.toJSON();
      });
    }
    attrs.type = (attrs.layer_name && attrs.layer_name.indexOf('_raster') >= 0) ? 'raster_tileo' : attrs.type;
    var createMethodName = LAYER_TYPE_TO_LAYER_CREATE_METHOD[attrs.type.toLowerCase()];
    if (!createMethodName) throw new Error('no create method name found for type ' + attrs.type);

    if (attrs.source) {
      // Make sure the analysis is created first
      var nodeDefModel = this._analysisDefinitionNodesCollection.get(attrs.source);
      // Dependency with another integration
      this.trigger('onLayerCreation', nodeDefModel);
    }

    var layerPosition = this._layerDefinitionsCollection.indexOf(layerDefModel);
{'fraction_non_alphanumeric': 0.13361730747674028, 'fraction_numerical': 0.0004483802264320144, 'mean_word_length': 4.6183879093199, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1377490', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2694, 'n_words': 486}