1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | if (typeof Object.assign !== "function") {
|
9 | Object.defineProperty(Object, "assign", {
|
10 | value: function assign(target) {
|
11 | "use strict";
|
12 | if (target === null) {
|
13 | throw new TypeError("Cannot convert undefined or null to object");
|
14 | }
|
15 |
|
16 | var to = Object(target);
|
17 |
|
18 | for (var index = 1; index < arguments.length; index++) {
|
19 | var nextSource = arguments[index];
|
20 |
|
21 | if (nextSource !== null) {
|
22 | for (var nextKey in nextSource) {
|
23 | if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
24 | to[nextKey] = nextSource[nextKey];
|
25 | }
|
26 | }
|
27 | }
|
28 | }
|
29 | return to;
|
30 | },
|
31 | writable: true,
|
32 | configurable: true
|
33 | });
|
34 | }
|
35 |
|
36 | if (!Array.prototype.includes) {
|
37 | Object.defineProperty(Array.prototype, "includes", {
|
38 | value: function includes(searchElement, fromIndex) {
|
39 |
|
40 | var o = Object(this);
|
41 |
|
42 | var len = o.length >>> 0;
|
43 |
|
44 | if (len === 0) return false;
|
45 |
|
46 | var n = fromIndex | 0;
|
47 |
|
48 | var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
|
49 |
|
50 | function sameValueZero(x, y) {
|
51 | return x === y || typeof x === "number" && typeof y === "number" && isNaN(x) && isNaN(y);
|
52 | }
|
53 |
|
54 | while (k < len) {
|
55 | if (sameValueZero(o[k], searchElement)) {
|
56 | return true;
|
57 | }
|
58 | k++;
|
59 | }
|
60 |
|
61 | return false;
|
62 | }
|
63 | });
|
64 | }
|
65 |
|
66 | (function (global, factory) {
|
67 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-selection'), require('d3-transition'), require('d3-array'), require('d3-collection')) :
|
68 | typeof define === 'function' && define.amd ? define('d3plus-common', ['exports', 'd3-selection', 'd3-transition', 'd3-array', 'd3-collection'], factory) :
|
69 | (factory((global.d3plus = {}),global.d3Selection,global.d3Transition,global.d3Array,global.d3Collection));
|
70 | }(this, (function (exports,d3Selection,d3Transition,d3Array,d3Collection) { 'use strict';
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function accessor(key, def) {
|
85 | if (def === void 0) { return function (d) { return d[key]; }; }
|
86 | return function (d) { return d[key] === void 0 ? def : d[key]; };
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function isObject(item) {
|
95 | return item && typeof item === "object" && !Array.isArray(item) && item !== void 0 ? true : false;
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | function assign() {
|
108 | var objects = [], len = arguments.length;
|
109 | while ( len-- ) objects[ len ] = arguments[ len ];
|
110 |
|
111 |
|
112 | var target = objects[0];
|
113 | var loop = function ( i ) {
|
114 |
|
115 | var source = objects[i];
|
116 |
|
117 | Object.keys(source).forEach(function (prop) {
|
118 |
|
119 | var value = source[prop];
|
120 |
|
121 | if (isObject(value)) {
|
122 |
|
123 | if (target.hasOwnProperty(prop) && isObject(target[prop])) { target[prop] = assign({}, target[prop], value); }
|
124 | else { target[prop] = value; }
|
125 |
|
126 | }
|
127 | else if (Array.isArray(value)) {
|
128 |
|
129 | if (target.hasOwnProperty(prop) && Array.isArray(target[prop])) {
|
130 |
|
131 | var targetArray = target[prop];
|
132 |
|
133 | value.forEach(function (sourceItem, itemIndex) {
|
134 |
|
135 | if (itemIndex < targetArray.length) {
|
136 | var targetItem = targetArray[itemIndex];
|
137 |
|
138 | if (Object.is(targetItem, sourceItem)) { return; }
|
139 |
|
140 | if (isObject(targetItem) && isObject(sourceItem) || Array.isArray(targetItem) && Array.isArray(sourceItem)) {
|
141 | targetArray[itemIndex] = assign({}, targetItem, sourceItem);
|
142 | }
|
143 | else { targetArray[itemIndex] = sourceItem; }
|
144 |
|
145 | }
|
146 | else { targetArray.push(sourceItem); }
|
147 |
|
148 | });
|
149 | }
|
150 | else { target[prop] = value; }
|
151 |
|
152 | }
|
153 | else { target[prop] = value; }
|
154 |
|
155 | });
|
156 | };
|
157 |
|
158 | for (var i = 1; i < objects.length; i++) loop( i );
|
159 |
|
160 | return target;
|
161 |
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function attrize(e, a) {
|
171 | if ( a === void 0 ) a = {};
|
172 |
|
173 | for (var k in a) { if ({}.hasOwnProperty.call(a, k)) { e.attr(k, a[k]); } }
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | function s() {
|
182 | return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function uuid() {
|
190 | return ("" + (s()) + (s()) + "-" + (s()) + "-" + (s()) + "-" + (s()) + "-" + (s()) + (s()) + (s()));
|
191 | }
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | var RESET = "D3PLUS-COMMON-RESET";
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function nestedReset(obj, defaults) {
|
206 | if (isObject(obj)) {
|
207 | for (var nestedKey in obj) {
|
208 | if ({}.hasOwnProperty.call(obj, nestedKey) && !nestedKey.startsWith("_")) {
|
209 | var defaultValue = defaults && isObject(defaults) ? defaults[nestedKey] : undefined;
|
210 | if (obj[nestedKey] === RESET) {
|
211 | obj[nestedKey] = defaultValue;
|
212 | }
|
213 | else if (isObject(obj[nestedKey])) {
|
214 | nestedReset(obj[nestedKey], defaultValue);
|
215 | }
|
216 | }
|
217 | }
|
218 | }
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | var BaseClass = function BaseClass() {
|
226 | this._on = {};
|
227 | this._uuid = uuid();
|
228 | };
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | BaseClass.prototype.config = function config (_) {
|
237 | var this$1 = this;
|
238 |
|
239 | if (!this._configDefault) {
|
240 | var config = {};
|
241 | for (var k in this$1.__proto__) {
|
242 | if (k.indexOf("_") !== 0 && !["config", "constructor", "render"].includes(k)) {
|
243 | var v = this$1[k]();
|
244 | config[k] = isObject(v) ? assign({}, v) : v;
|
245 | }
|
246 | }
|
247 | this._configDefault = config;
|
248 | }
|
249 | if (arguments.length) {
|
250 | for (var k$1 in _) {
|
251 | if ({}.hasOwnProperty.call(_, k$1) && k$1 in this$1) {
|
252 | var v$1 = _[k$1];
|
253 | if (v$1 === RESET) {
|
254 | if (k$1 === "on") { this$1._on = this$1._configDefault[k$1]; }
|
255 | else { this$1[k$1](this$1._configDefault[k$1]); }
|
256 | }
|
257 | else {
|
258 | nestedReset(v$1, this$1._configDefault[k$1]);
|
259 | this$1[k$1](v$1);
|
260 | }
|
261 | }
|
262 | }
|
263 | return this;
|
264 | }
|
265 | else {
|
266 | var config$1 = {};
|
267 | 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](); } }
|
268 | return config$1;
|
269 | }
|
270 | };
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | BaseClass.prototype.on = function on (_, f) {
|
288 | return arguments.length === 2 ? (this._on[_] = f, this) : arguments.length ? typeof _ === "string" ? this._on[_] : (this._on = Object.assign({}, this._on, _), this) : this._on;
|
289 | };
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | function closest(n, arr) {
|
298 | if ( arr === void 0 ) arr = [];
|
299 |
|
300 | if (!arr || !(arr instanceof Array) || !arr.length) { return undefined; }
|
301 | return arr.reduce(function (prev, curr) { return Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev; });
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | function configPrep(config, type, nest) {
|
312 | if ( config === void 0 ) config = this._shapeConfig;
|
313 | if ( type === void 0 ) type = "shape";
|
314 | if ( nest === void 0 ) nest = false;
|
315 |
|
316 |
|
317 | var newConfig = {duration: this._duration, on: {}};
|
318 |
|
319 | var wrapFunction = function (func) { return function (d, i, s) {
|
320 | while (d.__d3plus__) {
|
321 | i = d.i;
|
322 | d = d.data || d.feature;
|
323 | }
|
324 | return func(d, i, s);
|
325 | }; };
|
326 |
|
327 | var parseEvents = function (newObj, on) {
|
328 |
|
329 | for (var event in on) {
|
330 |
|
331 | if ({}.hasOwnProperty.call(on, event) && !event.includes(".") || event.includes(("." + type))) {
|
332 |
|
333 | newObj.on[event] = wrapFunction(on[event]);
|
334 |
|
335 | }
|
336 |
|
337 | }
|
338 |
|
339 | };
|
340 |
|
341 | var keyEval = function (newObj, obj) {
|
342 |
|
343 | for (var key in obj) {
|
344 |
|
345 | if ({}.hasOwnProperty.call(obj, key)) {
|
346 |
|
347 | if (key === "on") { parseEvents(newObj, obj[key]); }
|
348 | else if (typeof obj[key] === "function") {
|
349 | newObj[key] = wrapFunction(obj[key]);
|
350 | }
|
351 | else if (typeof obj[key] === "object" && !(obj instanceof Array)) {
|
352 | newObj[key] = {on: {}};
|
353 | keyEval(newObj[key], obj[key]);
|
354 | }
|
355 | else { newObj[key] = obj[key]; }
|
356 |
|
357 | }
|
358 |
|
359 | }
|
360 |
|
361 | };
|
362 |
|
363 | keyEval(newConfig, config);
|
364 | if (this._on) { parseEvents(newConfig, this._on); }
|
365 | if (nest && config[nest]) {
|
366 | keyEval(newConfig, config[nest]);
|
367 | if (config[nest].on) { parseEvents(newConfig, config[nest].on); }
|
368 | }
|
369 |
|
370 | return newConfig;
|
371 |
|
372 | }
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | function constant(value) {
|
386 | return function constant() {
|
387 | return value;
|
388 | };
|
389 | }
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | function elem(selector, p) {
|
404 |
|
405 |
|
406 | p = Object.assign({}, {
|
407 | condition: true,
|
408 | enter: {},
|
409 | exit: {},
|
410 | parent: d3Selection.select("body"),
|
411 | transition: d3Transition.transition().duration(0),
|
412 | update: {}
|
413 | }, p);
|
414 |
|
415 | var className = (/\.([^#]+)/g).exec(selector),
|
416 | id = (/#([^\.]+)/g).exec(selector),
|
417 | tag = (/^([^.^#]+)/g).exec(selector)[1];
|
418 |
|
419 | var elem = p.parent.selectAll(selector.includes(":") ? selector.split(":")[1] : selector)
|
420 | .data(p.condition ? [null] : []);
|
421 |
|
422 | var enter = elem.enter().append(tag).call(attrize, p.enter);
|
423 |
|
424 | if (id) { enter.attr("id", id[1]); }
|
425 | if (className) { enter.attr("class", className[1]); }
|
426 |
|
427 | elem.exit().transition(p.transition).call(attrize, p.exit).remove();
|
428 |
|
429 | var update = enter.merge(elem);
|
430 | update.transition(p.transition).call(attrize, p.update);
|
431 |
|
432 | return update;
|
433 |
|
434 | }
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | function objectMerge(objects, aggs) {
|
450 | if ( aggs === void 0 ) aggs = {};
|
451 |
|
452 |
|
453 | var availableKeys = new Set(d3Array.merge(objects.map(function (o) { return d3Collection.keys(o); }))),
|
454 | newObject = {};
|
455 |
|
456 | availableKeys.forEach(function (k) {
|
457 | var values = objects.map(function (o) { return o[k]; });
|
458 | var value;
|
459 | if (aggs[k]) { value = aggs[k](values); }
|
460 | else {
|
461 | var types = values.map(function (v) { return v || v === false ? v.constructor : v; }).filter(function (v) { return v !== void 0; });
|
462 | if (!types.length) { value = undefined; }
|
463 | else if (types.indexOf(Array) >= 0) {
|
464 | value = d3Array.merge(values.map(function (v) { return v instanceof Array ? v : [v]; }));
|
465 | value = Array.from(new Set(value));
|
466 | if (value.length === 1) { value = value[0]; }
|
467 | }
|
468 | else if (types.indexOf(String) >= 0) {
|
469 | value = Array.from(new Set(values));
|
470 | if (value.length === 1) { value = value[0]; }
|
471 | }
|
472 | else if (types.indexOf(Number) >= 0) { value = d3Array.sum(values); }
|
473 | else if (types.indexOf(Object) >= 0) { value = objectMerge(values.filter(function (v) { return v; })); }
|
474 | else {
|
475 | value = Array.from(new Set(values.filter(function (v) { return v !== void 0; })));
|
476 | if (value.length === 1) { value = value[0]; }
|
477 | }
|
478 | }
|
479 | newObject[k] = value;
|
480 | });
|
481 |
|
482 | return newObject;
|
483 |
|
484 | }
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 | function parseSides(sides) {
|
492 | var values;
|
493 | if (typeof sides === "number") { values = [sides]; }
|
494 | else { values = sides.split(/\s+/); }
|
495 |
|
496 | if (values.length === 1) { values = [values[0], values[0], values[0], values[0]]; }
|
497 | else if (values.length === 2) { values = values.concat(values); }
|
498 | else if (values.length === 3) { values.push(values[1]); }
|
499 |
|
500 | return [
|
501 | "top",
|
502 | "right",
|
503 | "bottom",
|
504 | "left"
|
505 | ].reduce(function (acc, direction, i) {
|
506 | var value = parseFloat(values[i]);
|
507 | acc[direction] = value || 0;
|
508 | return acc;
|
509 | }, {});
|
510 | }
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 | function prefix() {
|
517 | if ("-webkit-transform" in document.body.style) { return "-webkit-"; }
|
518 | else if ("-moz-transform" in document.body.style) { return "-moz-"; }
|
519 | else if ("-ms-transform" in document.body.style) { return "-ms-"; }
|
520 | else if ("-o-transform" in document.body.style) { return "-o-"; }
|
521 | else { return ""; }
|
522 | }
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 | function stylize(e, s) {
|
531 | if ( s === void 0 ) s = {};
|
532 |
|
533 | for (var k in s) { if ({}.hasOwnProperty.call(s, k)) { e.style(k, s[k]); } }
|
534 | }
|
535 |
|
536 | exports.accessor = accessor;
|
537 | exports.assign = assign;
|
538 | exports.attrize = attrize;
|
539 | exports.BaseClass = BaseClass;
|
540 | exports.closest = closest;
|
541 | exports.configPrep = configPrep;
|
542 | exports.constant = constant;
|
543 | exports.elem = elem;
|
544 | exports.isObject = isObject;
|
545 | exports.merge = objectMerge;
|
546 | exports.parseSides = parseSides;
|
547 | exports.prefix = prefix;
|
548 | exports.RESET = RESET;
|
549 | exports.stylize = stylize;
|
550 | exports.uuid = uuid;
|
551 |
|
552 | Object.defineProperty(exports, '__esModule', { value: true });
|
553 |
|
554 | })));
|
555 |
|