-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
742 lines (611 loc) · 14.9 KB
/
index.js
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/**
* Here is dom manipulation utils
* exodus from jQuery!!
* with little help from my functions
* author Heeju Jeong([email protected])
* some functions referred by http://youmightnotneedjquery.com/
* v 1.2.0 (Apr 09 2015)
*/
'use strict';
module.exports = {
"isHTMLElement": isHTMLElement,
"isHTMLCollection": isHTMLCollection,
"isDocument": isDocument,
"isWindow": isWindow,
"isDOMElement": isDOMElement,
"isContains": isContains,
"is": equals,
"equals": equals,
"contains": isContains,
"clone": clone,
//selection
"domSelect": domSelect,
"select": domSelect,
//class control
"hasClass": hasClass,
"addClass": addClass,
"removeClass": removeClass,
"toggleClass": toggleClass,
//attribute control
"attr": attr,
"removeAttr": removeAttr,
//style control
"css": css,
"getBgColor": getBgColor,
//dimensions
"width": width,
"innerWidth": innerWidth,
"outerWidth": outerWidth,
"height": height,
"innerHeight": innerHeight,
"outerHeight": outerHeight,
"getRect": getRect,
//offset
"offset": offset,
"position": position,
//DOM insertions & removals
"appendTo": appendTo,
"prependTo": prependTo,
"append": append,
"prepend": prepend,
"insertBefore": insertBefore,
"insertAfter": insertAfter,
"before": before,
"after": after,
"remove": remove,
"empty": empty
};
/**
* @param {HTMLElement} el
* @return {Boolean}
*/
function isHTMLElement(el) {
return (el) instanceof HTMLElement;
}
function isHTMLCollection(el) {
return (el) instanceof HTMLCollection;
}
function isDocument(el) {
return el === document;
}
function isWindow(el) {
return el === window;
}
function isDOMElement(el) {
return isHTMLElement(el) || isDocument(el) || isWindow(el);
}
/**
* @param {string} selector
* @param {[HTMLElement]} context
* @return {HTMLCollection or HTMLElement or null}
*/
function domSelect(selector, context) {
var matches = selector.match(/([#\.])?([\w-]+)/i);
if (!matches || !matches.length) {
return null;
}
if (matches[0] !== selector) {
matches[1] = 'querySelector';
matches[2] = selector;
}
var method = {
"#": "getElementById",
".": "getElementsByClassName",
"undefined": "getElementsByTagName",
"querySelector": "querySelectorAll"
}[matches[1]];
if (context === undefined || isHTMLElement(context) === false) {
context = document;
}
var el;
try {
el = context[method](matches[2]);
}
catch (e) {
el = null
}
if (el && el.length !== undefined) {
if (el.length === 0) {
el = null;
}
else if (el.length === 1) {
el = el[0];
}
}
return el;
}
function equals(el, target) {
if (el === undefined || target === undefined || isHTMLElement(el) === false) {
return;
}
if (isDOMElement(target) === true) {
return el === target;
}
}
/**
* @param {[HTMLElement]} el
* @param {[HTMLElement]} parent optional
* @return {Boolean}
*/
function isContains(el, child) {
if (el === undefined || isDOMElement(el) === false) {
return false;
}
if (child && isDOMElement(child) === true) {
return el !== child && el.contains(child);
}
else {
return el.ownerDocument.contains(el);
}
}
/**
* [clone description]
* @param {HTMLElement} el
* @return {null or Cloned HTMLElement} [description]
*/
function clone(el) {
if (el === undefined || isDOMElement(el) === false) {
return null;
}
return el.cloneNode(true);
}
/**
* [hasClass description]
* @param {[HTMLElement]} el [description]
* @param {[string]} className [description]
* @return {Boolean} [description]
*/
function hasClass(el, className) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
if (el.classList) {
return el.classList.contains(className);
}
else {
return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
}
}
/**
* [addClass description]
* @param {[HTMLElement]} el [description]
* @param {[string]} className [description]
*/
function addClass(el, className) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
el.classList.add(className);
// if (el.classList) {
// el.classList.add(className);
// }
// else {
// el.className += ' ' + className;
// }
}
/**
* [removeClass description]
* @param {[HTMLElement]} el [description]
* @param {[type]} className [description]
* @return {[type]} [description]
*/
function removeClass(el, className) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
el.classList.remove(className);
// if (el.classList) {
// el.classList.remove(className);
// }
// else {
// el.className = el.className.replace(
// new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' '
// );
// }
return el;
}
/**
* [toggleClass description]
* @param {[HTMLElement]} el [description]
* @param {[type]} className [description]
* @return {[type]} [description]
*/
function toggleClass(el, className, state) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
if (state !== undefined && typeof state === 'function') {
state = state(el);
}
if (state === true) {
return addClass(el, className);
}
else if (state === false) {
return removeClass(el, className);
}
el.classList.toggle(className);
// if (el.classList) {
// el.classList.toggle(className);
// }
// else {
// var classes = el.className.split(' ');
// var index = classes.indexOf(className);
// if (index >= 0) {
// classes.splice(index, 1);
// }
// else {
// classes.push(className);
// }
// el.className = classes.join(' ');
// }
return el;
}
/**
* [attr description]
* @param {[HTMLElement]} el [description]
* @param {[type]} name [description]
* @param {[type]} value [description]
* @return {[type]} [description]
*/
function attr(el, name, value) {
if (el === undefined || isHTMLElement(el) === false || name === undefined) {
return;
}
//get value
if (value === undefined) {
return el.getAttribute(name);
}
//set value
else {
el.setAttribute(name, value);
return el;
}
}
/**
* [removeAttr description]
* @param {[HTMLElement]} el [description]
* @param {[type]} name [description]
* @return {[type]} [description]
*/
function removeAttr(el, name) {
if (el === undefined || isHTMLElement(el) === false || name === undefined) {
return;
}
return el.removeAttribute(name);
}
/**
* [css description]
* @param {[HTMLElement]} el [description]
* @param {[type]} name [description]
* @param {[type]} value [description]
* @return {[type]} [description]
*/
function css(el, name, value) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
//get
if (name === undefined) {
return getComputedStyle(el);
}
if (value === undefined && name.constructor !== Object) {
var styles = getComputedStyle(el);
if (typeof name === 'string') {
return styles[toCamelCase(name)];
}
else if (Array.isArray(name)) {
var result = {};
for (var i = 0; i < name.length; i++) {
result[name[i]] = styles[toCamelCase(name[i])];
}
return result;
}
else {
return styles;
}
}
//set
var setStyles = {};
if (name.constructor === Object) {
setStyles = name;
}
if (typeof name === 'string') {
setStyles[name] = value;
}
for (var key in setStyles) {
if (typeof setStyles[key] === 'number' && key.match('index') === null) {
setStyles[key] += 'px';
}
el.style[toCamelCase(key)] = setStyles[key];
}
}
function getBgColor(el) {
var bgc = '';
do {
bgc = css(el, 'background-color');
el = el.parentNode;
} while (bgc === 'transparent' || bgc === 'rgba(0, 0, 0, 0)');
return bgc || 'transparent';
}
function width(el, value) {
if (el === undefined || isHTMLElement(el) === false) {
return null;
}
if (value !== undefined && value !== true) {
value = parseInt(value);
if (typeof value === 'number') {
return css(el, 'width', value + 'px');
}
}
var style = getComputedStyle(el)
, width = el.offsetWidth
- parseInt(style.borderLeftWidth)
- parseInt(style.borderRightWidth);
// include padding
if (value === true) {
return width;
}
return width
- parseInt(style.paddingLeft)
- parseInt(style.paddingRight);
}
function innerWidth(el) {
return width(el, true);
}
function outerWidth(el, withMargin) {
if (el === undefined || isHTMLElement(el) === false) {
return null;
}
var width = el.offsetWidth;
if (withMargin === true) {
var style = getComputedStyle(el);
width += parseInt(style.marginLeft) + parseInt(style.marginRight);
}
return width;
}
/**
* [height description]
* @param {[type]} el [description]
* @param {true or number} value [description]
* @return {[type]} [description]
*/
function height(el, value) {
if (el === undefined || isHTMLElement(el) === false) {
return null;
}
if (typeof value === 'number') {
return css(el, 'height', value + 'px');
}
var style = getComputedStyle(el)
, height = el.offsetHeight
- parseInt(style.borderTopWidth)
- parseInt(style.borderBottomWidth);
// include padding
if (value === true) {
return height;
}
return height
- parseInt(style.paddingTop)
- parseInt(style.paddingBottom);
}
function innerHeight(el) {
return height(el, true);
}
function outerHeight(el, withMargin) {
if (el === undefined || isHTMLElement(el) === false) {
return null;
}
var height = el.offsetHeight;
if (withMargin === true) {
var style = getComputedStyle(el);
height += parseInt(style.marginTop) + parseInt(style.marginBottom);
}
return height;
}
/**
* get real rect dimentions
* @param {[type]} el [description]
* @return {[type]} [description]
*/
function getRect(el) {
return el.getBoundingClientRect();
}
/**
* [offset description]
* @param {[type]} el [description]
* @param {Boolean} isRelation include scrolled position
* @return {[type]} [description]
*/
function offset(el, isRelation) {
if (el === undefined || isHTMLElement(el) === false) {
return {};
}
var rect = getRect(el);
if (isRelation === true) {
return {
top: rect.top,
left: rect.left
}
}
return {
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft
}
}
function position(el) {
if (el === undefined || isHTMLElement(el) === false) {
return {};
}
return {
top: el.offsetTop,
left: el.offsetLeft
}
}
/**
* [appendTo description]
* @param {[HTMLElement]} el [description]
* @param {[HTMLElement]} parent [description]
* @return {[type]} [description]
*/
function appendTo(el, parent) {
if (el === undefined || isHTMLElement(el) === false ||
parent === undefined || isHTMLElement(parent) === false) {
return;
}
return parent.appendChild(el);
}
/**
* [prepend description]
* @param {[HTMLElement]} el [description]
* @param {[HTMLElement]} child [description]
* @return {[type]} [description]
*/
function prependTo(el, parent) {
if (el === undefined || isHTMLElement(el) === false ||
parent === undefined || isHTMLElement(parent) === false) {
return;
}
return insertBefore(el, parent.firstChild);
}
/**
* [insertBefore description]
* @param {[HTMLElement]} el [description]
* @param {[HTMLElement]} target [description]
* @return {[type]} [description]
*/
function insertBefore(el, target) {
if (el === undefined || isHTMLElement(el) === false ||
target === undefined || isHTMLElement(target) === false) {
return;
}
if (target.insertAdjacentElement) {
return target.insertAdjacentElement('beforeBegin', el);
}
else {
return target.parentNode.insertBefore(el, target);
}
}
/**
* [insertAfter description]
* @param {[HTMLElement]} el [description]
* @param {[HTMLElement]} target [description]
* @return {[type]} [description]
*/
function insertAfter(el, target) {
if (el === undefined || isHTMLElement(el) === false ||
target === undefined || isHTMLElement(target) === false) {
return;
}
if (target.insertAdjacentElement) {
return target.insertAdjacentElement('afterend', el);
}
else {
if (target.parentNode.lastChild == target) {
appendTo(el, target.parentNode);
} else {
insertBefore(el, target.nextSibling);
}
}
}
/**
* [appendTo description]
* @param {[HTMLElement]} el [description]
* @param {[HTMLElement]} parent [description]
* @return {[type]} [description]
*/
function append(el, child) {
return appendTo(child, el);
}
/**
* [prependTo description]
* @param {[HTMLElement]} el [description]
* @param {[HTMLElement]} parent [description]
* @return {[type]} [description]
*/
function prepend(el, child) {
return prependTo(child, el);
}
/**
* [before description]
* @param {[HTMLElement]} target [description]
* @param {[HTMLElement]} el [description]
* @return {[type]} [description]
*/
function before(el, target) {
return insertBefore(target, el);
}
/**
* [after description]
* @param {[HTMLElement]} target [description]
* @param {[HTMLElement]} el [description]
* @return {[type]} [description]
*/
function after(el, target) {
return insertAfter(target, el);
}
/**
* [remove description]
* @param {[HTMLElement]} el [description]
* @return {[type]} [description]
*/
function remove(el) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
if (el.parentNode) {
el.parentNode.removeChild(el);
}
}
/**
* [empty description]
* @param {[HTMLElement]} el [description]
* @return {[type]} [description]
*/
function empty(el) {
if (el === undefined || isHTMLElement(el) === false) {
return;
}
el.textContent = "";
return el;
}
// utils
/**
* [toCamelCase description]
* @param {string} str [description]
* @return {[type]} [description]
*/
function toCamelCase(str) {
return str.replace(/[\s_-](\w)/gi, function(r, l){
return l.toUpperCase();
});
}
/**
* [iteration description]
* @param {[type]} collection [description]
* @param {[type]} func [description]
* @return {[type]} [description]
*/
function iteration(collection, func) {
if (isHTMLCollection(collection) === false) {
if (isHTMLElement(el) === true) {
return func(collection);
}
return;
}
var result = func(collection[0])
, resultISElement = false
, returnValue;
if (isHTMLElement(result) === true) {
resultISElement = true;
returnValue = collection;
} else {
returnValue = [result];
}
for (var i = 1; i < collection.length; i++) {
result = func(collection[i]);
if (resultISElement === false) {
returnValue.push(result)
}
}
return returnValue;
}