UNPKG

d3plus-common

Version:

Common functions and methods used across D3plus modules.

201 lines (192 loc) 8.7 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import assign from "./assign.js"; import findLocale from "./findLocale.js"; import isObject from "./isObject.js"; import uuid from "./uuid.js"; import RESET from "./RESET.js"; import dictionaries from "./locales/index.js"; /** @desc Recursive function that resets nested Object configs. @param {Object} obj @param {Object} defaults @private */ function nestedReset(obj, defaults) { if (isObject(obj)) { for (var nestedKey in obj) { if ({}.hasOwnProperty.call(obj, nestedKey) && !nestedKey.startsWith("_")) { var defaultValue = defaults && isObject(defaults) ? defaults[nestedKey] : undefined; if (obj[nestedKey] === RESET) { if (defaultValue) obj[nestedKey] = defaultValue;else delete obj[nestedKey]; } else if (isObject(obj[nestedKey])) { nestedReset(obj[nestedKey], defaultValue); } } } } } /** * @desc finds all prototype methods of a class and it's parent classes * @param {*} obj * @private */ function getAllMethods(obj) { var props = []; do { props = props.concat(Object.getOwnPropertyNames(obj)); obj = Object.getPrototypeOf(obj); } while (obj && obj !== Object.prototype); return props.filter(function (e) { return e.indexOf("_") !== 0 && !["config", "constructor", "parent", "render"].includes(e); }); } /** @class BaseClass @summary An abstract class that contains some global methods and functionality. */ var BaseClass = /*#__PURE__*/function () { /** @memberof BaseClass @desc Invoked when creating a new class instance, and sets any default parameters. @private */ function BaseClass() { var _this = this; _classCallCheck(this, BaseClass); this._locale = "en-US"; this._on = {}; this._parent = {}; this._translate = function (d) { var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this._locale; var dictionary = dictionaries[locale]; return dictionary && dictionary[d] ? dictionary[d] : d; }; this._uuid = uuid(); } /** @memberof BaseClass @desc If *value* is specified, sets the methods that correspond to the key/value pairs and returns this class. If *value* is not specified, returns the current configuration. @param {Object} [*value*] @chainable */ _createClass(BaseClass, [{ key: "config", value: function config(_) { var _this2 = this; if (!this._configDefault) { var config = {}; getAllMethods(this.__proto__).forEach(function (k) { var v = _this2[k](); if (v !== _this2) config[k] = isObject(v) ? assign({}, v) : v; }); this._configDefault = config; } if (arguments.length) { for (var k in _) { if ({}.hasOwnProperty.call(_, k) && k in this) { var v = _[k]; if (v === RESET) { if (k === "on") this._on = this._configDefault[k];else this[k](this._configDefault[k]); } else { nestedReset(v, this._configDefault[k]); this[k](v); } } } return this; } else { var _config = {}; getAllMethods(this.__proto__).forEach(function (k) { _config[k] = _this2[k](); }); return _config; } } /** @memberof BaseClass @desc Sets the locale used for all text and number formatting. This method supports the locales defined in [d3plus-format](https://github.com/d3plus/d3plus-format/blob/master/src/locale.js). The locale can be defined as a complex Object (like in d3plus-format), a locale code (like "en-US"), or a 2-digit language code (like "en"). If a 2-digit code is provided, the "findLocale" function is used to identify the most approximate locale from d3plus-format. @param {Object|String} [*value* = "en-US"] @chainable @example { separator: "", suffixes: ["y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "B", "t", "q", "Q", "Z", "Y"], grouping: [3], delimiters: { thousands: ",", decimal: "." }, currency: ["$", ""] } */ }, { key: "locale", value: function locale(_) { return arguments.length ? (this._locale = findLocale(_), this) : this._locale; } /** @memberof BaseClass @desc Adds or removes a *listener* to each object for the specified event *typenames*. If a *listener* is not specified, returns the currently assigned listener for the specified event *typename*. Mirrors the core [d3-selection](https://github.com/d3/d3-selection#selection_on) behavior. @param {String} [*typenames*] @param {Function} [*listener*] @chainable @example <caption>By default, listeners apply globally to all objects, however, passing a namespace with the class name gives control over specific elements:</caption> new Plot .on("click.Shape", function(d) { console.log("data for shape clicked:", d); }) .on("click.Legend", function(d) { console.log("data for legend clicked:", d); }) */ }, { key: "on", value: function on(_, f) { return arguments.length === 2 ? (this._on[_] = f, this) : arguments.length ? typeof _ === "string" ? this._on[_] : (this._on = Object.assign({}, this._on, _), this) : this._on; } /** @memberof Viz @desc If *value* is specified, sets the parent config used by the wrapper and returns the current class instance. @param {Object} [*value*] @chainable */ }, { key: "parent", value: function parent(_) { return arguments.length ? (this._parent = _, this) : this._parent; } /** @memberof BaseClass @desc Defines how informational text strings should be displayed. By default, this function will try to find the string in question (which is the first argument provided to this function) inside of an internally managed translation Object. If you'd like to override to use custom text, simply pass this method your own custom formatting function. @param {Function} [*value*] @chainable @example <caption>For example, if we wanted to only change the string "Back" and allow all other string to return in English:</caption> .translate(function(d) { return d === "Back" ? "Get outta here" : d; }) */ }, { key: "translate", value: function translate(_) { return arguments.length ? (this._translate = _, this) : this._translate; } /** @memberof Viz @desc If *value* is specified, sets the config method for each shape and returns the current class instance. @param {Object} [*value*] @chainable */ }, { key: "shapeConfig", value: function shapeConfig(_) { return arguments.length ? (this._shapeConfig = assign(this._shapeConfig, _), this) : this._shapeConfig; } }]); return BaseClass; }(); export { BaseClass as default };