Gapminder/vizabi

View on GitHub
src/models/stack.js

Summary

Maintainability
A
1 hr
Test Coverage
import * as utils from "base/utils";
import Hook from "models/hook";

/*
 * VIZABI Stack Model
 */

const palettes = {
  "ALL": "all",
  _default: "none"
};

const StackModel = Hook.extend({

  /**
   * Default values for this model
   */
  getClassDefaults() {
    const defaults = {
      use: null,
      which: null,
      merge: false,
      spaceRef: null
    };
    return utils.deepExtend(this._super(), defaults);
  },

  /**
   * Initializes the stack hook
   * @param {Object} values The initial values of this model
   * @param parent A reference to the parent model
   * @param {Object} bind Initial events to bind
   */
  init(name, values, parent, bind) {

    this._type = "model";

    this._super(name, values, parent, bind);
  },

  /**
   * Validates a color hook
   */
  validate() {
    //there must be no scale
    if (this.scale) this.scale = null;

    //use must not be "indicator"
    if (this.use === "indicator") {
      utils.warn("stack model: use must not be 'indicator'. Resetting use to 'constant' and which to '" + palettes._default);
      this.use = "constant";
      this.which = palettes._default;
    }

    //if use is "constant"
    if (this.use === "constant" && utils.values(palettes).indexOf(this.which) == -1) {
      utils.warn("stack model: the requested value '" + this.which + "' is not allowed. resetting to '" +
        palettes._default);
      this.which = palettes._default;
      this.spaceRef = null;
    }
  },

  /**
   * Get the above constants
   */
  getPalettes() {
    return palettes;
  },

  // Stack model only gets synced with discrete models
  _receiveSyncModelUpdate(sourceMdl) {
    const conceptType = sourceMdl.getConceptprops().concept_type;
    if (["entity_set", "entity_domain"].includes(conceptType) && this.use !== "constant") {
      this._super(sourceMdl);
    }
  },

  /**
   * There must be no scale
   */
  buildScale() {}

});

export default StackModel;