UNPKG

4.08 kBJavaScriptView Raw
1import assign from "./assign";
2import isObject from "./isObject";
3import uuid from "./uuid";
4import RESET from "./RESET";
5
6/**
7 @desc Recursive function that resets nested Object configs.
8 @param {Object} obj
9 @param {Object} defaults
10 @private
11*/
12function nestedReset(obj, defaults) {
13 if (isObject(obj)) {
14 for (var nestedKey in obj) {
15 if ({}.hasOwnProperty.call(obj, nestedKey) && !nestedKey.startsWith("_")) {
16 var defaultValue = defaults && isObject(defaults) ? defaults[nestedKey] : undefined;
17 if (obj[nestedKey] === RESET) {
18 obj[nestedKey] = defaultValue;
19 }
20 else if (isObject(obj[nestedKey])) {
21 nestedReset(obj[nestedKey], defaultValue);
22 }
23 }
24 }
25 }
26}
27
28/**
29 @class BaseClass
30 @summary An abstract class that contains some global methods and functionality.
31*/
32var BaseClass = function BaseClass() {
33 this._locale = "en-US";
34 this._on = {};
35 this._uuid = uuid();
36};
37
38/**
39 @memberof BaseClass
40 @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.
41 @param {Object} [*value*]
42 @chainable
43*/
44BaseClass.prototype.config = function config (_) {
45 if (!this._configDefault) {
46 var config = {};
47 for (var k in this.__proto__) {
48 if (k.indexOf("_") !== 0 && !["config", "constructor", "render"].includes(k)) {
49 var v = this[k]();
50 config[k] = isObject(v) ? assign({}, v) : v;
51 }
52 }
53 this._configDefault = config;
54 }
55 if (arguments.length) {
56 for (var k$1 in _) {
57 if ({}.hasOwnProperty.call(_, k$1) && k$1 in this) {
58 var v$1 = _[k$1];
59 if (v$1 === RESET) {
60 if (k$1 === "on") { this._on = this._configDefault[k$1]; }
61 else { this[k$1](this._configDefault[k$1]); }
62 }
63 else {
64 nestedReset(v$1, this._configDefault[k$1]);
65 this[k$1](v$1);
66 }
67 }
68 }
69 return this;
70 }
71 else {
72 var config$1 = {};
73 for (var k$2 in this.__proto__) { if (k$2.indexOf("_") !== 0 && !["config", "constructor", "render"].includes(k$2)) { config$1[k$2] = this[k$2](); } }
74 return config$1;
75 }
76};
77
78/**
79 @memberof BaseClass
80 @desc If *value* is specified, sets the locale to the specified string and returns the current class instance. This method supports the locales defined in [d3plus-format](https://github.com/d3plus/d3plus-format/blob/master/src/locale.js). In another case, you can define an Object with a custom locale.
81 @param {Object|String} [*value* = "en-US"]
82 @chainable
83 @example
84 {
85 separator: "",
86 suffixes: ["y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "B", "t", "q", "Q", "Z", "Y"],
87 grouping: [3],
88 delimiters: {
89 thousands: ",",
90 decimal: "."
91 },
92 currency: ["$", ""]
93 }
94*/
95BaseClass.prototype.locale = function locale (_) {
96 return arguments.length ? (this._locale = _, this) : this._locale;
97};
98
99/**
100 @memberof BaseClass
101 @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.
102 @param {String} [*typenames*]
103 @param {Function} [*listener*]
104 @chainable
105 @example <caption>By default, listeners apply globally to all objects, however, passing a namespace with the class name gives control over specific elements:</caption>
106new Plot
107.on("click.Shape", function(d) {
108 console.log("data for shape clicked:", d);
109})
110.on("click.Legend", function(d) {
111 console.log("data for legend clicked:", d);
112})
113*/
114BaseClass.prototype.on = function on (_, f) {
115 return arguments.length === 2 ? (this._on[_] = f, this) : arguments.length ? typeof _ === "string" ? this._on[_] : (this._on = Object.assign({}, this._on, _), this) : this._on;
116};
117
118export default BaseClass;
119
120//# sourceMappingURL=BaseClass.js.map
\No newline at end of file