<commit_before>var _ = require('underscore');
var L = require('leaflet');
var Backbone = require('backbone');
var config = require('cdb.config');
var log = require('cdb.log');
var Model = require('../core/model');
var util = require('../core/util');
var Layers = require('./map/layers');
var sanitize = require('../core/sanitize');
var GeometryFactory = require('./geometry-models/geometry-factory');

var Map = Model.extend({
  defaults: {
    attribution: [config.get('cartodb_attributions')],
    center: [0, 0],
    zoom: 4,
    minZoom: 0,
    maxZoom: 20,
    scrollwheel: true,
    drag: true,
    keyboard: !util.supportsTouch(), // #cartodb.js/1652
    provider: 'leaflet',
    popupsEnabled: true,
    isFeatureInteractivityEnabled: false
  },

  initialize: function (attrs, options) {
    options = options || {};

    if (!options.layersFactory) throw new Error('layersFactory is required');
    this._layersFactory = options.layersFactory;

    attrs = attrs || {};

    this.layers = options.layersCollection || new Layers();
    this.geometries = new Backbone.Collection();
    this.errors = new Backbone.Collection();

    var center = attrs.center || this.defaults.center;
    if (typeof center === 'string') {
      center = JSON.parse(center);
    }
    this.set({
      center: center,
      original_center: center
    });

    if (attrs.bounds) {
      this.set({
        view_bounds_sw: attrs.bounds[0],
        view_bounds_ne: attrs.bounds[1]
      });
      this.unset('bounds');
    } else {
      this.set({
        zoom: attrs.zoom || this.defaults.zoom
      });
    }

    this.layers.bind('reset', this._updateAttributions, this);
    this.layers.bind('add', this._updateAttributions, this);
    this.layers.bind('remove', this._updateAttributions, this);
    this.layers.bind('change:attribution', this._updateAttributions, this);
    this.layers.bind('reset', this._onLayersResetted, this);

    this._updateAttributions();
  },

  _onLayersResetted: function () {
    if (this.layers.size() >= 1) {
      this._adjustZoomtoLayer(this.layers.first());
    }
  },

  _updateAttributions: function () {
    var defaultCartoDBAttribution = this.defaults.attribution[0];
    var attributions = _.chain(this.layers.models)
      .map(function (layer) { return sanitize.html(layer.get('attribution')); })
      .reject(function (attribution) { return attribution === defaultCartoDBAttribution; })
      .compact()
      .uniq()
      .value();

    attributions.push(defaultCartoDBAttribution);

    this.set('attribution', attributions);
  },

  // PUBLIC API METHODS

  addError: function (error) {
    if (!error.type) {
      throw new Error('Error must have a type property.');
    }

    var inCollection = this.getError(error.type);
    return inCollection || this.errors.add(error);
  },

  getError: function (type) {
    return this.errors.findWhere({ type: type });
  },

  removeError: function (type) {
    var error = this.getError(type);
    return this.collection.remove(error);
  },

  moveCartoDBLayer: function (from, to) {
    var layerMoved = this.layers.moveCartoDBLayer(from, to);
    if (layerMoved) {
      this.trigger('cartodbLayerMoved', {}, this);
    }
  },

  createCartoDBLayer: function (attrs, options) {
    this._checkProperties(attrs, ['sql|source', 'cartocss']);<commit_msg>Trigger an error with the specified type in addError<commit_after>var _ = require('underscore');
var L = require('leaflet');
var Backbone = require('backbone');
var config = require('cdb.config');
var log = require('cdb.log');
var Model = require('../core/model');
var util = require('../core/util');
var Layers = require('./map/layers');
var sanitize = require('../core/sanitize');
var GeometryFactory = require('./geometry-models/geometry-factory');

var Map = Model.extend({
  defaults: {
    attribution: [config.get('cartodb_attributions')],
    center: [0, 0],
    zoom: 4,
    minZoom: 0,
    maxZoom: 20,
    scrollwheel: true,
    drag: true,
    keyboard: !util.supportsTouch(), // #cartodb.js/1652
    provider: 'leaflet',
    popupsEnabled: true,
    isFeatureInteractivityEnabled: false
  },

  initialize: function (attrs, options) {
    options = options || {};

    if (!options.layersFactory) throw new Error('layersFactory is required');
    this._layersFactory = options.layersFactory;

    attrs = attrs || {};

    this.layers = options.layersCollection || new Layers();
    this.geometries = new Backbone.Collection();
    this.errors = new Backbone.Collection();

    var center = attrs.center || this.defaults.center;
    if (typeof center === 'string') {
      center = JSON.parse(center);
    }
    this.set({
      center: center,
      original_center: center
    });

    if (attrs.bounds) {
      this.set({
        view_bounds_sw: attrs.bounds[0],
        view_bounds_ne: attrs.bounds[1]
      });
      this.unset('bounds');
    } else {
      this.set({
        zoom: attrs.zoom || this.defaults.zoom
      });
    }

    this.layers.bind('reset', this._updateAttributions, this);
    this.layers.bind('add', this._updateAttributions, this);
    this.layers.bind('remove', this._updateAttributions, this);
    this.layers.bind('change:attribution', this._updateAttributions, this);
    this.layers.bind('reset', this._onLayersResetted, this);

    this._updateAttributions();
  },

  _onLayersResetted: function () {
    if (this.layers.size() >= 1) {
      this._adjustZoomtoLayer(this.layers.first());
    }
  },

  _updateAttributions: function () {
    var defaultCartoDBAttribution = this.defaults.attribution[0];
    var attributions = _.chain(this.layers.models)
      .map(function (layer) { return sanitize.html(layer.get('attribution')); })
      .reject(function (attribution) { return attribution === defaultCartoDBAttribution; })
      .compact()
      .uniq()
      .value();

    attributions.push(defaultCartoDBAttribution);

    this.set('attribution', attributions);
  },

  // PUBLIC API METHODS

  addError: function (error) {
    var type = error.type;

    if (!type) {
      throw new Error('Error must have a type property.');
    }

    this.trigger('error:' + type, error);

    var inCollection = this.getError(type);
    return inCollection || this.errors.add(error);
  },

  getError: function (type) {
    return this.errors.findWhere({ type: type });
  },

  removeError: function (type) {
    var error = this.getError(type);
    return this.errors.remove(error);
  },

  moveCartoDBLayer: function (from, to) {
    var layerMoved = this.layers.moveCartoDBLayer(from, to);
    if (layerMoved) {
      this.trigger('cartodbLayerMoved', {}, this);
    }
  },

  createCartoDBLayer: function (attrs, options) {
    this._checkProperties(attrs, ['sql|source', 'cartocss']);
{'fraction_non_alphanumeric': 0.15234720992028344, 'fraction_numerical': 0.004133451431945674, 'mean_word_length': 3.3457344451571522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1014852', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2067, 'n_words': 439}