This repository was archived by the owner on Jan 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
649 lines (601 loc) · 20 KB
/
Copy pathcore.js
File metadata and controls
649 lines (601 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*!
* Contains all the common JS functions Rengn needs.
*/
(function (root) {
'use strict';
/**
* Add a class to an element.
* @param {Node} element - Element to add the class to.
* @param {string} className - Name of class to add.
*/
var addClass = function (element, className) {
var node = get(element);
if (node) {
node.classList.add(className);
}
};
/**
* Deep copy of an object, by value not by ref.
* @param {Object} src - Object to copy
* @returns {Object} New copy of the object
*/
var clone = function (src) {
if (isNull(src)) {
return src;
}
var cpy;
if (isArray(src)) {
return src.map(function (x) { return clone(x); });
}
if (isDate(src)) {
return new Date(src.getTime());
}
if (src instanceof RegExp) {
cpy = new RegExp(src.source);
cpy.global = src.global;
cpy.ignoreCase = src.ignoreCase;
cpy.multiline = src.multiline;
cpy.lastIndex = src.lastIndex;
return cpy;
}
if ($.isObject(src)) {
cpy = {};
// copy dialog pototype over definition.
for (var prop in src) {
if (src.hasOwnProperty(prop)) {
cpy[prop] = clone(src[prop]);
}
}
return cpy;
}
return src;
};
/**
* Get closest parent that matches the selector.
* @param {string} selector - ID, class name, tag name, or data attribute to find.
* @param {Node} node - Node to start search from.
* @returns {Node} Matched node or null.
*/
var closest = function (selector, node) {
var firstChar = selector.charAt(0);
var tSelector = selector.substr(1);
var lowerSelector = selector.toLowerCase();
while (node !== document) {
node = node.parentNode;
if (!node) {
return null;
}
// If selector is a class
if (firstChar === '.' && node.classList && node.classList.contains(tSelector)) {
return node;
}
// If selector is an ID
if (firstChar === '#' && node.id === tSelector) {
return node;
}
// If selector is a data attribute
if (firstChar === '[' && node.hasAttribute(selector.substr(1, selector.length - 2))) {
return node;
}
// If selector is a tag
if (node.tagName && node.tagName.toLowerCase() === lowerSelector) {
return node;
}
}
return null;
};
/**
* Coalesce value and defValue.
* @param {*} value - First value to check.
* @param {*} defValue - Default value.
* @returns {*} Value if it is not null, else defValue.
*/
var coalesce = function (value, defValue) {
return isNull(value) ? defValue : value;
};
/**
* Create a dom node from an html string. Expects a single root element.
* @param {string} html - HTML content for the node.
* @returns {Node} New DOM node.
*/
var createNode = function (html) {
var node = document.createElement('div');
node.innerHTML = html;
return node.children[0];
};
/**
* Create a debounce handler to prevent a function from being called too frequently.
* @param {Function} fn - Function to debounce.
* @param {number} wait - Milliseconds to wait between running the function.
* @returns {Function} A closure wrapping the passed in function.
*/
var debounce = function (fn, wait) {
var timeout, args, context, timestamp;
return function () {
context = this;
args = [].slice.call(arguments, 0);
timestamp = new Date();
var later = function () {
var last = new Date() - timestamp;
if (last < wait) {
// if the latest call was less that the wait period ago then we reset the timeout to wait for the difference
timeout = setTimeout(later, wait - last);
} else {
// if not we can null out the timer and run the latest
timeout = null;
fn.apply(context, args);
}
};
// we only need to set the timer now if one isn't already running
if (!timeout) {
timeout = setTimeout(later, wait);
}
};
};
/**
* Destroy an object.
* @param {Object} obj - Object to destroy.
*/
var destroy = function (obj) {
if (isNull(obj)) {
return;
}
if (obj.destroy) {
obj.destroy();
}
obj = null;
};
/**
* Deep equality comparer for objects.
* @param {Object} x - First object to compare.
* @param {Object} y - Object to compare to x.
* @returns {bool} True if objects are equal.
*/
var equals = function (x, y) {
var p;
for (p in y) {
if (isUndefined(x[p])) {
return false;
}
}
for (p in y) {
if (y[p]) {
switch (typeof y[p]) {
case 'object':
if (!equals(y[p], x[p])) {
return false;
} break;
case 'function':
if (isUndefined(x[p]) || (p !== 'equals' && y[p].toString() !== x[p].toString())) {
return false;
}
break;
default:
if (y[p] !== x[p]) {
return false;
}
}
} else if (x[p]) {
return false;
}
}
for (p in x) {
if (isUndefined(y[p])) {
return false;
}
}
return true;
};
/**
* Recursively merge multiple objects, combining values of arguments into first argument. Rightmost values take precedence.
* @returns {*} Updated first argument.
*/
var extend = function () {
var l = arguments.length, key, i;
var result = l > 0 ? arguments[0] : {};
if (isNull(result)) {
result = {};
}
for (i = 1; i < l; i++) {
for (key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
if (isNull(arguments[i][key])) {
result[key] = null;
} else if (isArray(arguments[i][key])) {
result[key] = arguments[i][key].map(function (x) { return extend({}, x); });
} else if (isDate(arguments[i][key])) {
result[key] = new Date(arguments[i][key].getTime());
} else if (isNode(arguments[i][key])) {
result[key] = arguments[i][key].cloneNode();
} else if (isObject(arguments[i][key])) {
result[key] = extend(result[key] || {}, arguments[i][key]);
} else {
result[key] = arguments[i][key];
}
}
}
}
return result;
};
/**
* Get an object from an array where the obj[key]===value.
* @param {*[]} arr - Array to search in.
* @param {string} key - Property name to check.
* @param {*} value - Value to find.
* @returns {*} Array value that matches or null.
*/
var findByKey = function (arr, key, value) {
if (!arr || isNull(key)) {
return;
}
var i = arr.length - 1;
while (i > -1) {
if (arr[i][key] === value) {
arr[i]._i = i;
return arr[i];
}
i--;
}
return null;
};
/**
* Get an element matching selector.
* @param {string} selector - ID, class name, or any valid query selector.
* @param {Node} container - Only search within this node.
* @returns {Node} Matched node.
*/
var get = function (selector, container) {
if (typeof selector !== 'string') {
return selector;
}
if (container) {
return container.querySelector(selector);
}
var sel = selector.charAt(0);
var simple = selector.indexOf(' ', 1) === -1 && selector.indexOf('.', 1) === -1;
if (sel === '#' && simple) {
return document.getElementById(selector.substr(1));
} else if (sel === '.' && simple) {
return document.getElementsByClassName(selector.substr(1));
} else {
return document.querySelector(selector);
}
};
/**
* Get all elements matching selector.
* @param {string} selector - ID, class name, or any valid query selector.
* @param {Node} container - Only search within this node.
* @returns {Node[]} Non-live array of matched nodes.
*/
var getAll = function (selector, container) {
var list;
if (selector.charAt(0) === '.' && selector.indexOf(',') === -1) {
list = (container || document).getElementsByClassName(selector.substr(1));
} else {
list = (container || document).querySelectorAll(selector);
}
return Array.prototype.slice.call(list);
};
/**
* Check if an element has a class assigned to it.
* @param {Node} element - Element to check.
* @param {string} className - Name of class to look for.
* @returns {bool} True if the element has the class.
*/
var hasClass = function (element, className) {
var node = get(element);
return node && node.classList && node.classList.contains(className);
};
/**
* Check if variable has a value.
* @param {*} val - Value to check.
* @returns {bool} True if the object has a value greater than zero.
*/
var hasPositiveValue = function (val) {
return hasValue(val) && val > 0;
};
/**
* Check if variable has a value.
* @param {*} val - Value to check.
* @returns {bool} True if the object is not null, undefined, or zero length.
*/
var hasValue = function (val) {
return !(isNull(val) || val.length === 0);
};
/**
* Hide an element.
* @param {Node} element - Element to hide.
* @param {bool} maintainLayout - Maintain the spacing of the element if true, default to false.
*/
var hide = function (element, maintainLayout) {
var node = get(element);
if (node) {
if (coalesce(maintainLayout, false)) {
addClass(node, 'invisible');
} else {
addClass(node, 'hidden');
}
}
};
/**
* Check if a variable is an array.
* @param {*} x - Variable to check the type of.
* @returns {bool} True if x is an array.
*/
var isArray = function (x) {
return !isNull(x) && x.constructor === Array;
};
/**
* Check if a variable is a date.
* @param {*} x - Variable to check the type of.
* @returns {bool} True if x is a date.
*/
var isDate = function (x) {
return !isNull(x) && x.getMonth && !isNaN(x.getTime());
};
/**
* Check if a variable is a function.
* @param {*} x - Variable to check the type of.
* @returns {bool} True if x is a function.
*/
var isFunction = function (x) {
return typeof x === 'function';
};
/**
* Check if a variable is a DOM node.
* @param {*} x - Variable to check the type of.
* @returns {bool} True if x is a node.
*/
var isNode = function (x) {
return !isNull(x) && x.nodeType === 1 && x.nodeName;
};
/**
* Check if a variable is undefined or null.
* @param {*} x - Variable to check the value of.
* @returns {bool} True if x is undefined or null.
*/
var isNull = function (x) {
return isUndefined(x) || x === null;
};
/**
* Check if a variable is a number.
* @param {*} x - Variable to check the value of.
* @returns {bool} True if x is a number.
*/
var isNumber = function (x) {
return typeof x === 'number' && !isNaN(x);
};
/**
* Check if a variable is an object.
* @param {*} x - Variable to check the type of.
* @returns {bool} True if x is an object.
*/
var isObject = function (x) {
return typeof x === 'object';
};
/**
* Check if a variable is a string.
* @param {*} x - Variable to check the type of.
* @returns {bool} True if x is a string.
*/
var isString = function (x) {
return typeof x === 'string';
};
/**
* Check if a variable is undefined.
* @param {*} x - Variable to check the value of.
* @returns {bool} True if x is undefined.
*/
var isUndefined = function (x) {
return typeof x === 'undefined';
};
/**
* Verify if an element would be matched by a selector.
* @param {Node} element - Node to compare the selector to.
* @param {string} selector - Valid CSS selector.
* @returns {bool} True if the element matches the selector.
*/
var matches = function (element, selector) {
var p = Element.prototype;
var f = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector || function (s) {
return [].indexOf.call(getAll(s), this) !== -1;
};
return f.call(element, selector);
};
/**
* Do nothing.
*/
var noop = function () { };
/**
* Remove an event from an element.
* @param {Node} element - Element to remove the event from.
* @param {string} event - Event name to remove.
* @param {Function} fn - Function to remove.
* @param {bool} useCapture - Dispatch to this listener before any before it.
*/
var off = function (element, event, fn, useCapture) {
var node = get(element);
if (node) {
node.removeEventListener(event, fn, useCapture);
}
};
/**
* Attach an event to an element.
* @param {Node} element - Element to attach the event to.
* @param {string} event - Event name to attach.
* @param {Function} fn - Function to run when the event fires.
* @param {bool} useCapture - Dispatch to this listener before any before it.
*/
var on = function (element, event, fn, useCapture) {
var node = get(element);
if (node) {
node.addEventListener(event, fn, useCapture);
}
};
/**
* Set a function to run onChange, and run it immediately if needed.
* @param {Node} element - Element to attach the event to.
* @param {Function} changeFn - Function to run.
* @param {bool} immediate - Run function immediately.
*/
var onChange = function (element, changeFn, immediate) {
var node = get(element);
if (node) {
on(node, 'change', changeFn);
if (coalesce(immediate, true)) {
changeFn.call(node);
}
}
};
/**
* Run afunction when page is loaded
* @param {Function} fn - Function to run.
*/
var ready = function (fn) {
// Sanity check
if (!isFunction(fn)) {
return;
}
// If document is already loaded, run method
if (document.readyState === 'complete') {
fn();
}
// Otherwise, wait until document is loaded
document.addEventListener('DOMContentLoaded', fn, false);
};
/**
* Remove a class from an element.
* @param {Node} element - Element to remove the class from.
* @param {string} className - Name of class to remove.
*/
var removeClass = function (element, className) {
var node = get(element);
if (node) {
node.classList.remove(className);
}
};
/**
* Set the text content of a node.
* @param {Node} element - Element to update.
* @param {string} text - New text content.
*/
var setText = function (element, text) {
var node = get(element);
if (node) {
node.textContent = text;
}
};
/**
* Show a hidden element.
* @param {Node} element - Element to show.
*/
var show = function (element) {
var node = get(element);
if (node) {
removeClass(node, 'invisible');
removeClass(node, 'hidden');
}
};
/**
* Get element style, or set multiple styles for an element at once.
* @param {Node} element - Element to get/set styles for.
* @param {Object|undefined} styles - Object with styleName:value, or undefined if getting.
* @param {bool} overwrite - Overwrite existing styles if true, else merge.
* @returns {string|undefined} Returns the element style if styles param is empty, else undefined.
*/
var style = function (element, styles, overwrite) {
var node = get(element);
if (node) {
if (!isNull(styles)) {
node.style.cssText = _toCSS($.coalesce(overwrite, false) ? extend(_parseCss(node.style.cssText), _toLowerKeys(styles)) : styles);
} else {
return node.style.cssText;
}
}
return;
};
/**
* Change the property names of an object to lowercase.
* @param {Object} obj - Object to change properties of.
* @returns {Object} New object with all lowercase property names.
*/
var _toLowerKeys = function (obj) {
if (isNull(obj)) {
return {};
}
var key, keys = Object.keys(obj), i = keys.length, newObj = {};
while (i--) {
key = keys[i];
newObj[key.toLowerCase()] = obj[key];
}
return newObj;
};
/**
* Convert a string of CSS settings into an object.
* @param {string} cssText - CSS list.
* @returns {Object} Object with styleName:value.
*/
var _parseCss = function (cssText) {
var regex = /([\w-]*)\s*:\s*([^;]*)/g;
var match, properties = {};
while ((match = regex.exec(cssText))) {
properties[match[1].toLowerCase()] = match[2];
}
return properties;
};
/**
* Convert an object to a string of CSS settings.
* @param {Object} obj - Object with styleName:value.
* @returns {string} CSS list.
*/
var _toCSS = function (obj) {
if (isNull(obj)) {
return '';
}
var key, keys = Object.keys(obj), i = keys.length, css = '';
while (i--) {
key = keys[i];
if (!isNull(obj[key])) {
css += key + ': ' + obj[key] + '; ';
}
}
return css;
};
root.$ = {
addClass: addClass,
clone: clone,
closest: closest,
coalesce: coalesce,
createNode: createNode,
debounce: debounce,
destroy: destroy,
equals: equals,
extend: extend,
get: get,
getAll: getAll,
findByKey: findByKey,
hasClass: hasClass,
hasPositiveValue: hasPositiveValue,
hasValue: hasValue,
hide: hide,
isArray: isArray,
isDate: isDate,
isFunction: isFunction,
isNode: isNode,
isNull: isNull,
isNumber: isNumber,
isObject: isObject,
isString: isString,
isUndefined: isUndefined,
matches: matches,
noop: noop,
off: off,
on: on,
onChange: onChange,
ready: ready,
removeClass: removeClass,
setText: setText,
show: show,
style: style
};
}(this));