UNPKG

3.11 kBJavaScriptView Raw
1function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
3/**
4 @function configPrep
5 @desc Preps a config object for d3plus data, and optionally bubbles up a specific nested type. When using this function, you must bind a d3plus class' `this` context.
6 @param {Object} [config = this._shapeConfig] The configuration object to parse.
7 @param {String} [type = "shape"] The event classifier to user for "on" events. For example, the default event type of "shape" will apply all events in the "on" config object with that key, like "click.shape" and "mouseleave.shape", in addition to any gloval events like "click" and "mouseleave".
8 @param {String} [nest] An optional nested key to bubble up to the parent config level.
9*/
10export default function configPrep() {
11 var _this = this;
12
13 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._shapeConfig;
14 var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "shape";
15 var nest = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
16 var newConfig = {
17 duration: this._duration,
18 on: {}
19 };
20
21 var wrapFunction = function wrapFunction(func) {
22 return function (d, i, s) {
23 var parent;
24
25 while (d.__d3plus__) {
26 if (parent) d.__d3plusParent__ = parent;
27 parent = d;
28 i = d.i;
29 d = d.data || d.feature;
30 }
31
32 return func.bind(_this)(d, i, s || parent);
33 };
34 };
35
36 var parseEvents = function parseEvents(newObj, on) {
37 for (var event in on) {
38 if ({}.hasOwnProperty.call(on, event) && !event.includes(".") || event.includes(".".concat(type))) {
39 newObj.on[event] = wrapFunction(on[event]);
40 }
41 }
42 };
43
44 var arrayEval = function arrayEval(arr) {
45 return arr.map(function (d) {
46 if (d instanceof Array) return arrayEval(d);else if (_typeof(d) === "object") return keyEval({}, d);else if (typeof d === "function") return wrapFunction(d);else return d;
47 });
48 };
49
50 var keyEval = function keyEval(newObj, obj) {
51 for (var key in obj) {
52 if ({}.hasOwnProperty.call(obj, key)) {
53 if (key === "on") parseEvents(newObj, obj[key]);else if (typeof obj[key] === "function") {
54 newObj[key] = wrapFunction(obj[key]);
55 } else if (obj[key] instanceof Array) {
56 newObj[key] = arrayEval(obj[key]);
57 } else if (_typeof(obj[key]) === "object") {
58 newObj[key] = {
59 on: {}
60 };
61 keyEval(newObj[key], obj[key]);
62 } else newObj[key] = obj[key];
63 }
64 }
65 };
66
67 keyEval(newConfig, config);
68 if (this._on) parseEvents(newConfig, this._on);
69
70 if (nest && config[nest]) {
71 keyEval(newConfig, config[nest]);
72 if (config[nest].on) parseEvents(newConfig, config[nest].on);
73 }
74
75 return newConfig;
76}
\No newline at end of file