<commit_before>import * as utils from 'base/utils';
import DataConnected from 'models/dataconnected';
import EventSource from 'base/events';

/*!
 * HOOK MODEL
 */

var Hook = DataConnected.extend({

  //some hooks can be important. like axis x and y
  //that means, if X or Y doesn't have data at some point, we can't show markers
  _important: false,

  dataConnectedChildren: ['use', 'which'],
  
  
  init: function(name, value, parent, binds, persistent) {
    var _this = this;
    this._super(name, value, parent, binds, persistent);
    this.on('ready', this.hookReady.bind(this));
  },
  
  hookReady: function(){
    this.buildScale();
    this.validate();
  },
  
  buildScale: function(){
    //overloaded by specific hook models, like axis and color
  },

  /**
   * After complete model tree is created, this allows models to listen to eachother. 
   */
  setInterModelListeners: function() {
    var spaceRefs = this._parent.getSpace(this);

    //check what we want to hook this model to
    utils.forEach(spaceRefs, name => {
      //hook with the closest prefix to this model
      this._space[name] = this.getClosestModel(name);
      //if hooks change, this should load again
      this._space[name].on('dataConnectedChange', this.handleDataConnectedChange.bind(this));
      this._space[name].on('filterChange', this.handleFilterChange.bind(this));
    });
    this.getClosestModel('locale').on('dataConnectedChange', this.handleDataConnectedChange.bind(this));
  },

  setWhich: function(newValue) {

    var obj = { which: newValue };
    var conceptProps = this.dataSource.getConceptprops(newValue);

    if(conceptProps.use) obj.use = conceptProps.use;

    if(conceptProps.scales) {
      obj.scaleType = conceptProps.scales[0];
    }

    if(this.getType() === 'axis' || this.getType() === 'size') {
      obj.domainMin = null;
      obj.domainMax = null;
      obj.zoomedMin = null;
      obj.zoomedMax = null;
    }
    this.scale = null;

    this.set(obj);
  },
  
  setScaleType: function(newValue) {
    this.scale = null;
    this.set({"scaleType": newValue});
  },

  preloadData: function() {
    var dataModel = (this.data) ? this.data : 'data';
    this.dataSource = this.getClosestModel(dataModel);
    return this._super();
  },

  /**
   * Hooks loads data, models ask children to load data
   * Basically, this method:
   * loads is theres something to be loaded:
   * does not load if there's nothing to be loaded
   * @param {Object} options (includes splashScreen)
   * @returns defer
   */
  loadData: function(opts = {}) {

    // then start loading data

    if(!this.which) return Promise.resolve();

    this.trigger('hook_change');

    var query = this.getQuery(opts.splashScreen);

    //useful to check if in the middle of a load call
    this._loadCall = true;

    this.setReady(false);

    utils.timeStamp('Vizabi Model: Loading Data: ' + this._id);

    var parsers = this._getAllParsers();
    var dataPromise = this.dataSource.load(query, parsers);

    dataPromise.then(
      this.afterLoad.bind(this),
      this.loadError.bind(this)
    );

    return dataPromise;

  },

  handleDataConnectedChange: function(evt) {
    //defer is necessary because other events might be queued.
    //load right after such events
    utils.defer(() => {
      this.startLoading().then(
        undefined,
        err => utils.warn(err)
      );
    });
  },

  handleFilterChange: function(evt) {
    //defer is necessary because other events might be queued.
    //load right after such events
    utils.defer(() => {
      this.setReady(false);
      this.startLoading().then(
        () => {
          this.set({
            domainMin: null,
            domainMax: null,
            zoomedMin: null,
            zoomedMax: null
          }, null, false);
        },
        err => utils.warn(err)
      );
    });
  },

  _isLoading: function() {
    return (!this._loadedOnce || this._loadCall);
  },

  /**
   * executes after data has actually been loaded
   */
  afterLoad: function(dataId) {
    this._dataId = dataId;
    utils.timeStamp('Vizabi Model: Data loaded: ' + this._id);
  },

  loadError: function() {
      utils.warn('Problem with query: ', JSON.stringify(query));
  },

  /**
   * gets query that this model/hook needs to get data<commit_msg>Revert commit c60671e3a6b94990c23ea43288805132871666c9 Add entities show update connection to hook scale reset. Refactor line chart (#2256)<commit_after>import * as utils from 'base/utils';
import DataConnected from 'models/dataconnected';
import EventSource from 'base/events';

/*!
 * HOOK MODEL
 */

var Hook = DataConnected.extend({

  //some hooks can be important. like axis x and y
  //that means, if X or Y doesn't have data at some point, we can't show markers
  _important: false,

  dataConnectedChildren: ['use', 'which'],
  
  
  init: function(name, value, parent, binds, persistent) {
    var _this = this;
    this._super(name, value, parent, binds, persistent);
    this.on('ready', this.hookReady.bind(this));
  },
  
  hookReady: function(){
    this.buildScale();
    this.validate();
  },
  
  buildScale: function(){
    //overloaded by specific hook models, like axis and color
  },

  /**
   * After complete model tree is created, this allows models to listen to eachother. 
   */
  setInterModelListeners: function() {
    var spaceRefs = this._parent.getSpace(this);

    //check what we want to hook this model to
    utils.forEach(spaceRefs, name => {
      //hook with the closest prefix to this model
      this._space[name] = this.getClosestModel(name);
      //if hooks change, this should load again
      this._space[name].on('dataConnectedChange', this.handleDataConnectedChange.bind(this));
    });
    this.getClosestModel('locale').on('dataConnectedChange', this.handleDataConnectedChange.bind(this));
  },

  setWhich: function(newValue) {

    var obj = { which: newValue };
    var conceptProps = this.dataSource.getConceptprops(newValue);

    if(conceptProps.use) obj.use = conceptProps.use;

    if(conceptProps.scales) {
      obj.scaleType = conceptProps.scales[0];
    }

    if(this.getType() === 'axis' || this.getType() === 'size') {
      obj.domainMin = null;
      obj.domainMax = null;
      obj.zoomedMin = null;
      obj.zoomedMax = null;
    }
    this.scale = null;

    this.set(obj);
  },
  
  setScaleType: function(newValue) {
    this.scale = null;
    this.set({"scaleType": newValue});
  },

  preloadData: function() {
    var dataModel = (this.data) ? this.data : 'data';
    this.dataSource = this.getClosestModel(dataModel);
    return this._super();
  },

  /**
   * Hooks loads data, models ask children to load data
   * Basically, this method:
   * loads is theres something to be loaded:
   * does not load if there's nothing to be loaded
   * @param {Object} options (includes splashScreen)
   * @returns defer
   */
  loadData: function(opts = {}) {

    // then start loading data

    if(!this.which) return Promise.resolve();

    this.trigger('hook_change');

    var query = this.getQuery(opts.splashScreen);

    //useful to check if in the middle of a load call
    this._loadCall = true;

    this.setReady(false);

    utils.timeStamp('Vizabi Model: Loading Data: ' + this._id);

    var parsers = this._getAllParsers();
    var dataPromise = this.dataSource.load(query, parsers);

    dataPromise.then(
      this.afterLoad.bind(this),
      this.loadError.bind(this)
    );

    return dataPromise;

  },

  handleDataConnectedChange: function(evt) {
    //defer is necessary because other events might be queued.
    //load right after such events
    utils.defer(() => {
      this.startLoading().then(
        undefined,
        err => utils.warn(err)
      );
    });
  },

  _isLoading: function() {
    return (!this._loadedOnce || this._loadCall);
  },

  /**
   * executes after data has actually been loaded
   */
  afterLoad: function(dataId) {
    this._dataId = dataId;
    utils.timeStamp('Vizabi Model: Data loaded: ' + this._id);
  },

  loadError: function() {
      utils.warn('Problem with query: ', JSON.stringify(query));
  },

  /**
   * gets query that this model/hook needs to get data
{'fraction_non_alphanumeric': 0.12798343886994643, 'fraction_numerical': 0.004627374573794447, 'mean_word_length': 2.9240324892498806, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 12, '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': '188743', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2499, 'n_words': 674}