<commit_before>var _ = require('underscore');
var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
var StackLayoutView = require('../../../../../../components/stack-layout/stack-layout-view');
var ColumnListView = require('../../column-list-view');
var quantificationMethodItemTemplate = require('../quantification-method-item.tpl');
var InputRampContentView = require('./input-ramp-content-view');
var rampList = require('cartocolor');
var DefaultFillSettings = require('../../default-fill-settings.json');

module.exports = CoreView.extend({
  initialize: function (opts) {
    this._settings = DefaultFillSettings['color-ramps'];

    this._setupModel();
    this._initBinds();
    this._initViews();
  },

  render: function () {
    this.clearSubViews();
    this.$el.empty();

    this.$el.append(this._stackLayoutView.render().$el);

    return this;
  },

  _setupModel: function () {
    var options = {};

    if (!this.model.get('quantification')) {
      options.quantification = this._settings.quantifications.items[this._settings.quantifications.defaultIndex];
    }

    if (!this.model.get('bins')) {
      options.bins = this._settings.bins.items[this._settings.bins.defaultIndex];
    }

    if (+this.model.get('bins') > +this._settings.bins.items[this._settings.bins.items.length - 1]) {
      options.bins = this._settings.bins.items[this._settings.bins.items.length - 1];
    }

    this.model.set(options);
  },

  _initBinds: function () {
    var range = this.model.get('range');

    this.model.bind('change:bins', this._updateRange, this);
    this.model.bind('change:attribute', this.render, this);
    this.model.bind('change:quantification', this._onChangeQuantification, this);

    this.model.unset('fixed');

    if (!range || !this._isValidRange(range)) {
      this.model.set('range', this._getDefaultRamp());
    }
  },

  // range minimun size should be 2
  _isValidRange: function (range) {
    return range && range.length > 1 || false;
  },

  _initViews: function () {
    var self = this;

    var collectionOptions = [
      {
        createStackView: function (stackLayoutModel, opts) {
          return self._createInputRampContentView(stackLayoutModel, opts).bind(self);
        }
      }
    ];

    if (!this.options.hideTabs || !_.contains(this.options.hideTabs, 'quantification')) {
      collectionOptions.push(
        {
          createStackView: function (stackLayoutModel, opts) {
            return self._createQuantificationListView(stackLayoutModel, opts).bind(self);
          }
        }
      );
    }

    if (!this.options.hideTabs || !_.contains(this.options.hideTabs, 'bins')) {
      collectionOptions.push(
        {
          createStackView: function (stackLayoutModel, opts) {
            return self._createBinsListView(stackLayoutModel, opts).bind(self);
          }
        }
      );
    }

    var stackViewCollection = new Backbone.Collection(collectionOptions);

    this._stackLayoutView = new StackLayoutView({
      collection: stackViewCollection
    });
  },

  _updateRange: function () {
    var previousBins = this.model.previous('bins');

    if (!previousBins) {
      return;
    }

    var previousRamps = _.find(rampList, function (ramp) {
      var previousRamp = ramp[previousBins];
      var range = this.model.get('range');
      return previousRamp && range && previousRamp.join('').toLowerCase() === range.join('').toLowerCase();
    }, this);

    if (previousRamps) {
      var bins = this.model.get('bins');
      this.model.set('range', previousRamps[bins]);
    }
  },

  _createInputRampContentView: function (stackLayoutModel, opts) {
    var view = new InputRampContentView({
      stackLayoutModel: stackLayoutModel,
      model: this.model
    });
<commit_msg>Added subview to main view, and using listenTo<commit_after>var _ = require('underscore');
var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
var StackLayoutView = require('../../../../../../components/stack-layout/stack-layout-view');
var ColumnListView = require('../../column-list-view');
var quantificationMethodItemTemplate = require('../quantification-method-item.tpl');
var InputRampContentView = require('./input-ramp-content-view');
var rampList = require('cartocolor');
var DefaultFillSettings = require('../../default-fill-settings.json');

module.exports = CoreView.extend({
  initialize: function (opts) {
    this._settings = DefaultFillSettings['color-ramps'];

    this._setupModel();
    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this.$el.empty();
    this._initViews();
    return this;
  },

  _setupModel: function () {
    var options = {};

    if (!this.model.get('quantification')) {
      options.quantification = this._settings.quantifications.items[this._settings.quantifications.defaultIndex];
    }

    if (!this.model.get('bins')) {
      options.bins = this._settings.bins.items[this._settings.bins.defaultIndex];
    }

    if (+this.model.get('bins') > +this._settings.bins.items[this._settings.bins.items.length - 1]) {
      options.bins = this._settings.bins.items[this._settings.bins.items.length - 1];
    }

    this.model.set(options);
  },

  _initBinds: function () {
    var range = this.model.get('range');

    this.listenTo(this.model, 'change:bins', this._updateRange);
    this.listenTo(this.model, 'change:attribute', this.render);
    this.listenTo(this.model, 'change:quantification', this._onChangeQuantification);

    this.model.unset('fixed');

    if (!range || !this._isValidRange(range)) {
      this.model.set('range', this._getDefaultRamp());
    }
  },

  // range minimun size should be 2
  _isValidRange: function (range) {
    return range && range.length > 1 || false;
  },

  _initViews: function () {
    var self = this;

    var collectionOptions = [
      {
        createStackView: function (stackLayoutModel, opts) {
          return self._createInputRampContentView(stackLayoutModel, opts).bind(self);
        }
      }
    ];

    if (!this.options.hideTabs || !_.contains(this.options.hideTabs, 'quantification')) {
      collectionOptions.push(
        {
          createStackView: function (stackLayoutModel, opts) {
            return self._createQuantificationListView(stackLayoutModel, opts).bind(self);
          }
        }
      );
    }

    if (!this.options.hideTabs || !_.contains(this.options.hideTabs, 'bins')) {
      collectionOptions.push(
        {
          createStackView: function (stackLayoutModel, opts) {
            return self._createBinsListView(stackLayoutModel, opts).bind(self);
          }
        }
      );
    }

    var stackViewCollection = new Backbone.Collection(collectionOptions);

    this._stackLayoutView = new StackLayoutView({
      collection: stackViewCollection
    });

    this.$el.append(this._stackLayoutView.render().$el);
    this.addView(this._stackLayoutView);
  },

  _updateRange: function () {
    var previousBins = this.model.previous('bins');

    if (!previousBins) {
      return;
    }

    var previousRamps = _.find(rampList, function (ramp) {
      var previousRamp = ramp[previousBins];
      var range = this.model.get('range');
      return previousRamp && range && previousRamp.join('').toLowerCase() === range.join('').toLowerCase();
    }, this);

    if (previousRamps) {
      var bins = this.model.get('bins');
      this.model.set('range', previousRamps[bins]);
    }
  },

  _createInputRampContentView: function (stackLayoutModel, opts) {
    var view = new InputRampContentView({
      stackLayoutModel: stackLayoutModel,
      model: this.model
    });

{'fraction_non_alphanumeric': 0.1511400651465798, 'fraction_numerical': 0.001042345276872964, 'mean_word_length': 3.176278563656148, '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': '3879233', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2229, 'n_words': 384}