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)) {
|
209 | if (obj[nestedKey] === RESET) {
|
210 | obj[nestedKey] = defaults[nestedKey];
|
211 | }
|
212 | else if (isObject(obj[nestedKey])) {
|
213 | nestedReset(obj[nestedKey], defaults[nestedKey]);
|
214 | }
|
215 | }
|
216 | }
|
217 | }
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | var BaseClass = function BaseClass() {
|
225 | this._on = {};
|
226 | this._uuid = uuid();
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | BaseClass.prototype.config = function config (_) {
|
236 | var this$1 = this;
|
237 |
|
238 | if (!this._configDefault) {
|
239 | var config = {};
|
240 | for (var k in this$1.__proto__) {
|
241 | if (k.indexOf("_") !== 0 && !["config", "constructor", "render"].includes(k)) {
|
242 | var v = this$1[k]();
|
243 | config[k] = isObject(v) ? assign({}, v) : v;
|
244 | }
|
245 | }
|
246 | this._configDefault = config;
|
247 | }
|
248 | if (arguments.length) {
|
249 | for (var k$1 in _) {
|
250 | if ({}.hasOwnProperty.call(_, k$1) && k$1 in this$1) {
|
251 | var v$1 = _[k$1];
|
252 | if (v$1 === RESET) {
|
253 | if (k$1 === "on") { this$1._on = this$1._configDefault[k$1]; }
|
254 | else { this$1[k$1](this$1._configDefault[k$1]); }
|
255 | }
|
256 | else {
|
257 | nestedReset(v$1, this$1._configDefault[k$1]);
|
258 | this$1[k$1](v$1);
|
259 | }
|
260 | }
|
261 | }
|
262 | return this;
|
263 | }
|
264 | else {
|
265 | var config$1 = {};
|
266 | 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](); } }
|
267 | return config$1;
|
268 | }
|
269 | };
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | BaseClass.prototype.on = function on (_, f) {
|
287 | return arguments.length === 2 ? (this._on[_] = f, this) : arguments.length ? typeof _ === "string" ? this._on[_] : (this._on = Object.assign({}, this._on, _), this) : this._on;
|
288 | };
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | function closest(n, arr) {
|
297 | if ( arr === void 0 ) arr = [];
|
298 |
|
299 | if (!arr || !(arr instanceof Array) || !arr.length) { return undefined; }
|
300 | return arr.reduce(function (prev, curr) { return Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev; });
|
301 | }
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | function configPrep(config, type, nest) {
|
311 | if ( config === void 0 ) config = this._shapeConfig;
|
312 | if ( type === void 0 ) type = "shape";
|
313 | if ( nest === void 0 ) nest = false;
|
314 |
|
315 |
|
316 | var newConfig = {duration: this._duration, on: {}};
|
317 |
|
318 | var wrapFunction = function (func) { return function (d, i, s) {
|
319 | while (d.__d3plus__) {
|
320 | i = d.i;
|
321 | d = d.data || d.feature;
|
322 | }
|
323 | return func(d, i, s);
|
324 | }; };
|
325 |
|
326 | var parseEvents = function (newObj, on) {
|
327 |
|
328 | for (var event in on) {
|
329 |
|
330 | if ({}.hasOwnProperty.call(on, event) && !event.includes(".") || event.includes(("." + type))) {
|
331 |
|
332 | newObj.on[event] = wrapFunction(on[event]);
|
333 |
|
334 | }
|
335 |
|
336 | }
|
337 |
|
338 | };
|
339 |
|
340 | var keyEval = function (newObj, obj) {
|
341 |
|
342 | for (var key in obj) {
|
343 |
|
344 | if ({}.hasOwnProperty.call(obj, key)) {
|
345 |
|
346 | if (key === "on") { parseEvents(newObj, obj[key]); }
|
347 | else if (typeof obj[key] === "function") {
|
348 | newObj[key] = wrapFunction(obj[key]);
|
349 | }
|
350 | else if (typeof obj[key] === "object" && !(obj instanceof Array)) {
|
351 | newObj[key] = {on: {}};
|
352 | keyEval(newObj[key], obj[key]);
|
353 | }
|
354 | else { newObj[key] = obj[key]; }
|
355 |
|
356 | }
|
357 |
|
358 | }
|
359 |
|
360 | };
|
361 |
|
362 | keyEval(newConfig, config);
|
363 | if (this._on) { parseEvents(newConfig, this._on); }
|
364 | if (nest && config[nest]) {
|
365 | keyEval(newConfig, config[nest]);
|
366 | if (config[nest].on) { parseEvents(newConfig, config[nest].on); }
|
367 | }
|
368 |
|
369 | return newConfig;
|
370 |
|
371 | }
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | function constant(value) {
|
385 | return function constant() {
|
386 | return value;
|
387 | };
|
388 | }
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 | function elem(selector, p) {
|
403 |
|
404 |
|
405 | p = Object.assign({}, {
|
406 | condition: true,
|
407 | enter: {},
|
408 | exit: {},
|
409 | parent: d3Selection.select("body"),
|
410 | transition: d3Transition.transition().duration(0),
|
411 | update: {}
|
412 | }, p);
|
413 |
|
414 | var className = (/\.([^#]+)/g).exec(selector),
|
415 | id = (/#([^\.]+)/g).exec(selector),
|
416 | tag = (/^([^.^#]+)/g).exec(selector)[1];
|
417 |
|
418 | var elem = p.parent.selectAll(selector.includes(":") ? selector.split(":")[1] : selector)
|
419 | .data(p.condition ? [null] : []);
|
420 |
|
421 | var enter = elem.enter().append(tag).call(attrize, p.enter);
|
422 |
|
423 | if (id) { enter.attr("id", id[1]); }
|
424 | if (className) { enter.attr("class", className[1]); }
|
425 |
|
426 | elem.exit().transition(p.transition).call(attrize, p.exit).remove();
|
427 |
|
428 | var update = enter.merge(elem);
|
429 | update.transition(p.transition).call(attrize, p.update);
|
430 |
|
431 | return update;
|
432 |
|
433 | }
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | function objectMerge(objects, aggs) {
|
449 | if ( aggs === void 0 ) aggs = {};
|
450 |
|
451 |
|
452 | var availableKeys = new Set(d3Array.merge(objects.map(function (o) { return d3Collection.keys(o); }))),
|
453 | newObject = {};
|
454 |
|
455 | availableKeys.forEach(function (k) {
|
456 | var values = objects.map(function (o) { return o[k]; });
|
457 | var value;
|
458 | if (aggs[k]) { value = aggs[k](values); }
|
459 | else {
|
460 | var types = values.map(function (v) { return v || v === false ? v.constructor : v; }).filter(function (v) { return v !== void 0; });
|
461 | if (!types.length) { value = undefined; }
|
462 | else if (types.indexOf(Array) >= 0) {
|
463 | value = d3Array.merge(values.map(function (v) { return v instanceof Array ? v : [v]; }));
|
464 | value = Array.from(new Set(value));
|
465 | if (value.length === 1) { value = value[0]; }
|
466 | }
|
467 | else if (types.indexOf(String) >= 0) {
|
468 | value = Array.from(new Set(values));
|
469 | if (value.length === 1) { value = value[0]; }
|
470 | }
|
471 | else if (types.indexOf(Number) >= 0) { value = d3Array.sum(values); }
|
472 | else if (types.indexOf(Object) >= 0) { value = objectMerge(values.filter(function (v) { return v; })); }
|
473 | else {
|
474 | value = Array.from(new Set(values.filter(function (v) { return v !== void 0; })));
|
475 | if (value.length === 1) { value = value[0]; }
|
476 | }
|
477 | }
|
478 | newObject[k] = value;
|
479 | });
|
480 |
|
481 | return newObject;
|
482 |
|
483 | }
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | function prefix() {
|
490 | if ("-webkit-transform" in document.body.style) { return "-webkit-"; }
|
491 | else if ("-moz-transform" in document.body.style) { return "-moz-"; }
|
492 | else if ("-ms-transform" in document.body.style) { return "-ms-"; }
|
493 | else if ("-o-transform" in document.body.style) { return "-o-"; }
|
494 | else { return ""; }
|
495 | }
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 | function stylize(e, s) {
|
504 | if ( s === void 0 ) s = {};
|
505 |
|
506 | for (var k in s) { if ({}.hasOwnProperty.call(s, k)) { e.style(k, s[k]); } }
|
507 | }
|
508 |
|
509 | exports.accessor = accessor;
|
510 | exports.assign = assign;
|
511 | exports.attrize = attrize;
|
512 | exports.BaseClass = BaseClass;
|
513 | exports.closest = closest;
|
514 | exports.configPrep = configPrep;
|
515 | exports.constant = constant;
|
516 | exports.elem = elem;
|
517 | exports.isObject = isObject;
|
518 | exports.merge = objectMerge;
|
519 | exports.prefix = prefix;
|
520 | exports.RESET = RESET;
|
521 | exports.stylize = stylize;
|
522 | exports.uuid = uuid;
|
523 |
|
524 | Object.defineProperty(exports, '__esModule', { value: true });
|
525 |
|
526 | })));
|
527 |
|