UNPKG

3.3 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._on = {};
34 this._uuid = uuid();
35};
36
37/**
38 @memberof BaseClass
39 @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.
40 @param {Object} [*value*]
41 @chainable
42*/
43BaseClass.prototype.config = function config (_) {
44 var this$1 = this;
45
46 if (!this._configDefault) {
47 var config = {};
48 for (var k in this$1.__proto__) {
49 if (k.indexOf("_") !== 0 && !["config", "constructor", "render"].includes(k)) {
50 var v = this$1[k]();
51 config[k] = isObject(v) ? assign({}, v) : v;
52 }
53 }
54 this._configDefault = config;
55 }
56 if (arguments.length) {
57 for (var k$1 in _) {
58 if ({}.hasOwnProperty.call(_, k$1) && k$1 in this$1) {
59 var v$1 = _[k$1];
60 if (v$1 === RESET) {
61 if (k$1 === "on") { this$1._on = this$1._configDefault[k$1]; }
62 else { this$1[k$1](this$1._configDefault[k$1]); }
63 }
64 else {
65 nestedReset(v$1, this$1._configDefault[k$1]);
66 this$1[k$1](v$1);
67 }
68 }
69 }
70 return this;
71 }
72 else {
73 var config$1 = {};
74 for (var k$2 in this$1.__proto__) { if (k$2.indexOf("_") !== 0 && !["config", "constructor", "render"].includes(k$2)) { config$1[k$2] = this$1[k$2](); } }
75 return config$1;
76 }
77};
78
79/**
80 @memberof BaseClass
81 @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.
82 @param {String} [*typenames*]
83 @param {Function} [*listener*]
84 @chainable
85 @example <caption>By default, listeners apply globally to all objects, however, passing a namespace with the class name gives control over specific elements:</caption>
86new Plot
87.on("click.Shape", function(d) {
88 console.log("data for shape clicked:", d);
89})
90.on("click.Legend", function(d) {
91 console.log("data for legend clicked:", d);
92})
93*/
94BaseClass.prototype.on = function on (_, f) {
95 return arguments.length === 2 ? (this._on[_] = f, this) : arguments.length ? typeof _ === "string" ? this._on[_] : (this._on = Object.assign({}, this._on, _), this) : this._on;
96};
97
98export default BaseClass;
99
100//# sourceMappingURL=BaseClass.js.map
\No newline at end of file