-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathselector.js
1819 lines (1684 loc) · 70.1 KB
/
selector.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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// selector.js - micro library as a jQuery replacement
// https://github.com/Websilk/Selector
// copyright 2017 by Mark Entingh
(function () {
//global variables
if (!window.selector) { window.selector = '$';}
const tru = true;
const fals = false;
const doc = document;
const pxStyles = ['top', 'right', 'bottom', 'left', 'width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight', 'fontSize'],
pxStylesPrefix = ['border', 'padding', 'margin'],
listeners = []; //used for capturing event listeners from $('').on
//listeners = [{ elem: null, events: [{ name: '', list: [[selector, event]] }] }];
const classMatch = /^\.[\w-]*$/,
singlet = /^\w+$/;
let ajaxQueue = [];
let ajaxWait = false;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Internal functions //////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* selector
* @constructor
*/
function select(sel) {
//main function, instantiated via $(sel)
if (sel) { this.push(query(document, sel)); }
return this;
}
function query(context, selector) {
//gets a list of elements from a CSS selector
if (context == null) { return [];}
let elems = [];
if (isType(selector,4)) {
//elements are already defined instead of using a selector /////////////////////////////////////
elems = isType(selector, 5) ? selector : [selector];
} else if (selector != null && selector != '') {
//only use vanilla Javascript to select DOM elements based on a CSS selector (Chrome 1, IE 9, Safari 3.2, Firefox 3.5, Opera 10)
context = context || doc;
const el = (
classMatch.test(selector) ?
context.getElementsByClassName(selector.slice(1)) :
singlet.test(selector) ?
context.getElementsByTagName(selector) :
context.querySelectorAll(selector)
);
//convert node list into array
for (let i = el.length; i--; elems.unshift(el[i])) { };
}
return elems;
}
function isDescendant(parent, child) {
//checks if element is child of another element
let node = child;
while (node != null) {
node = node.parentNode;
if (node == parent) {
return tru;
}
}
return fals;
}
function styleName(str) {
//gets the proper style name from shorthand string
//for example: border-width translates to borderWidth;
if (str.indexOf('-') < 0) { return str; }
const name = str.split('-');
if(name.length > 1){name[1] = name[1][0].toUpperCase() + name[1].substr(1);}
return name.join('');
}
function setStyle(e, n, val) {
//properly set a style for an element
if (e.nodeName == '#text') { return; }
let v = val;
//check for empty value
if (v === '' || v === null) {
e.style[n] = v == '' ? null : v; return;
}
//check for numbers that should be using 'px';
if ((Number(v) == v || v === 0) && v.toString().indexOf('%') < 0) {
if (pxStyles.indexOf(n) >= 0) {
v = val + 'px';
} else if (pxStylesPrefix.some(function (a) { return n.indexOf(a) == 0 }) === true) {
v = val + 'px';
}
}
//last resort, set style to string value\
e.style[n] = v;
}
function getObj(obj) {
//get a string from object (either string, number, or function)
if (obj == null) { return null; }
if (isType(obj, 1)) {
//handle object as string
return obj;
}
if (isType(obj, 5)) {
//handle object as array
return obj[0];
}
if (isType(obj, 6)) {
//handle object as function (get value from object function execution)
return getObj(obj());
}
return obj;
}
function diffArray(arr, remove) {
return arr.filter(function (el) {
return !remove.includes(el);
});
}
function isArrayThen(obj, arrayFunc) {
if (isType(obj, 5)) {
//handle content as array
for(let x in obj){
arrayFunc.call(this,obj[x]);
}
return tru;
}
return fals;
}
function isType(obj, type) {
if(obj != null){
switch (type) {
case 0: return tru; //anything
case 1: return typeof (obj) == 'string'; //string
case 2: return typeof (obj) == 'boolean'; //boolean
case 3: return !isNaN(parseFloat(obj)) && isFinite(obj); //number
case 4: return typeof (obj) == 'object'; //object
case 5: return typeof obj.splice === 'function'; //array
case 6: return typeof obj == 'function'; //function
}
}
return fals;
}
function normalizeArgs(types, args) {
let results = [],
a = [].slice.call(args), //convert arguments object into array
step = types.length - 1,
req, skip;
for (let x = a.length-1; x >= 0; x--) {
for (let i = step; i >= 0; i--) {
skip = fals;
if (types[i].o == tru) {
//make sure there are enough arguments
//left over for required types
req = 0;
for (let t = 0; t <= i; t++) {
if (types[t].o == fals) { req++;}
}
skip = req > x;
}
if (skip == fals) { skip = !isType(a[x], types[i].t) && a[x] != null; }
results[i] = !skip ? a[x] : null;
step = i - 1;
if (!skip || a[x] == null) { break; }
}
}
return results;
}
function insertContent(obj, elements, stringFunc, objFunc) {
//checks type of object and execute callback functions depending on object type
const type = isType(obj, 1);
if (type == tru) {
for (let x = 0; x < elements.length; x++) {
stringFunc(elements[x]);
}
} else {
for (let x = 0; x < elements.length; x++) {
objFunc(elements[x]);
}
}
return this;
}
function clone(elems) {
let n = new select(null);
return n.push(elems);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//prototype functions that are accessable by $(selector) //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
select.prototype = {
add: function (elems) {
//Add new (unique) elements to the existing elements array
let obj = getObj(elems);
if (!obj) { return this; }
if (obj.elements) { obj = obj.elements; }
for (let x in obj) {
//check for duplicates
if (this.indexOf(obj[x]) < 0) {
//element is unique
this.push(obj[x]);
}
}
return this;
},
addClass: function (classes) {
//Add class name to each of the elements in the collection.
//Multiple class names can be given in a space-separated string.
if (this.length > 0) {
const classList = classes.split(' ');
for (let x = 0; x < this.length; x++) {
let e = this[x];
//alter classname for each element in selector
if (e.className) {
let className = e.className;
const list = className.split(' ');
for (let c in classList) {
if (list.indexOf(classList[c]) < 0) {
//class doesn't exist in element classname list
className += ' ' + classList[c];
}
}
//finally, change element classname if it was altered
e.className = className;
} else {
e.className = classes;
}
}
}
return this;
},
after: function (content) {
//Add content to the DOM after each elements in the collection.
//The content can be an HTML string, a DOM node or an array of nodes.
let obj = getObj(content);
if (isArrayThen(obj, this.after) || obj == null) { return this; }
insertContent(obj, this,
function (e) { e.insertAdjacentHTML('afterend', obj); },
function (e) { e.parentNode.insertBefore(obj, e.nextSibling); }
);
return this;
},
animate: function (props, options) {
if (typeof (Velocity) != 'undefined') {
Velocity(this, props, options);
}
return this;
},
append: function (content) {
//Append content to the DOM inside each individual element in the collection.
//The content can be an HTML string, a DOM node or an array of nodes.
let obj = getObj(content);
if (isArrayThen.call(this, obj, this.append) || obj == null) { return this; }
insertContent(obj, this,
function (e) { e.insertAdjacentHTML('beforeend', obj); },
function (e) { e.appendChild(obj); }
);
return this;
},
attr: function (name, val) {
//Read or set DOM attributes. When no value is given, reads
//specified attribute from the first element in the collection.
//When value is given, sets the attribute to that value on each element
//in the collection. When value is null, the attribute is removed = function(like with removeAttr).
//Multiple attributes can be set by passing an object with name-value pairs.
let n = getObj(name), v = getObj(val);
if (isType(n, 5)) {
//get array of attribute values from first element
let attrs = {};
for (let p in n) {
attrs[n[p]] = this.length > 0 ? this[0].getAttribute(n[p]) : attrs[n[p]] = '';
}
return attrs;
} else {
if (v != null) {
//set single attribute value to all elements in list
for (let x = 0; x < this.length; x++) {
this[x].setAttribute(n, v);
}
} else {
//get single attribute value from first element in list
return this.length > 0 ? this[0].getAttribute(n) : '';
}
}
return this;
},
before: function (content) {
//Add content to the DOM before each element in the collection.
//The content can be an HTML string, a DOM node or an array of nodes.
let obj = getObj(content);
if (isArrayThen(obj, this.before) || obj == null) { return this; }
insertContent(obj, this,
function (e) { e.insertAdjacentHTML('beforebegin', obj); },
function (e) { e.parentNode.insertBefore(obj, e); }
);
return this;
},
children: function (sel) {
//Get immediate children of each element in the current collection.
//If selector is given, filter the results to only include ones matching the CSS select.
let elems = [];
let seltype = 0;
if (sel != null) {
if (isType(sel, 3)) {
seltype = 1;
} else {
seltype = 2;
}
}
this.each(function (i, e) {
if (seltype == 1) {
//get child from index
elems.push(e.children[sel]);
} else {
for (let x = 0; x < e.children.length; x++) {
if (!seltype) { //no selector
elems.push(e.children[x]);
} else if (seltype == 2) { //match selector
if (e.matches(sel)) {
elems.push(e.children[x]);
}
}
}
}
});
return clone(elems);
},
closest: function (selector) {
//Traverse upwards from the current element to find the first element that matches the select.
return this;
},
css: function (params) {
//Read or set CSS properties on DOM elements. When no value is given,
//returns the CSS property from the first element in the collection.
//When a value is given, sets the property to that value on each element of the collection.
//Multiple properties can be retrieved at once by passing an array of property names.
//Multiple properties can be set by passing an object to the method.
//When a value for a property is blank = function(empty string, null, or undefined), that property is removed.
//When a unitless number value is given, "px" is appended to it for properties that require units.
if (isType(params, 4)) {
let haskeys = fals;
for (let x in params) {
//if params is an object with key/value pairs, apply styling to elements\
haskeys = tru;
this.each(function (i, e) {
setStyle(e, x, params[x]);
});
}
if (haskeys) { return this; }
if (isType(params, 5)) {
//if params is an array of style names, return an array of style values
let vals = [];
this.each(function (i, e) {
let props = new Object();
params.forEach(function (param) {
const prop = e.style[styleName(param)];
if (prop) { props[param] = prop; }
});
vals.push(props);
});
return vals;
}
} else if (isType(params, 1)) {
const name = styleName(params);
const arg = arguments[1];
if (isType(arg, 1)) {
//set a single style property if two string arguments are supplied (key, value);
this.each(function (i, e) {
setStyle(e, name, arg);
});
} else {
//if params is a string, return a single style property
if (this.length > 0) {
if (this.length == 1) {
//return a string value for one element
return this[0].style[name];
} else {
//return an array of strings for multiple elements
let vals = [];
this.each(function (i, e) {
let val = e.style[name];
if (val == null) { val = ''; }
vals.push(val);
});
return vals;
}
}
}
}
return this;
},
each: function (func) {
//Iterate through every element of the collection. Inside the iterator function,
//this keyword refers to the current item = function(also passed as the second argument to the function).
//If the iterator select.prototype.returns 0, iteration stops.
for (let x = 0; x < this.length; x++) {
func.call(this, x, this[x]);
}
return this;
},
empty: function (func) {
//Clear DOM contents of each element in the collection.
this.each(function (i, e) {
e.innerHTML = '';
});
return this;
},
eq: function (index) {
//Reduce the set of matched elements to the one at the specified index
let elems = [];
if (index > this.length - 1) {
//out of bounds
elems = [];
} else if (index < 0) {
//negetive index
if (index * -1 >= this.length) {
elems = [];
} else {
elems = [this[(this.length - 1) + index]];
}
} else {
elems = [this[index]];
}
return clone(elems);
},
filter: function (sel) {
//Filter the collection to contain only items that match the CSS select.
//If a select.prototype.is given, return only elements for which the select.prototype.returns a truthy value.
let elems = [];
if (isType(sel, 6)) {
//filter a boolean function
for (let i = 0; i < this.length; i++) {
if (sel.call(this[i], i, this[i]) == tru) { elems.push(this[i]); }
}
} else {
//filter selector string
const found = query(document, sel);
if (found.length > 0) {
this.each(function (i, e) {
if (found.indexOf(e) >= 0) {
//make sure no duplicates are being added to the array
if (elems.indexOf(e) < 0) { elems.push(e); }
}
});
}
}
return clone(elems);
},
find: function (sel) {
//Find elements that match CSS selector executed in scope of nodes in the current collection.
let elems = [];
if (this.length > 0) {
this.each(function (i, e) {
const found = query(e, sel);
if (found.length > 0) {
found.forEach(function (a) {
//make sure no duplicates are being added to the array
if (elems.indexOf(a) < 0) { elems.push(a); }
});
}
});
}
return clone(elems);
},
first: function () {
//the first element found in the selector
let elems = [];
if (this.length > 0) {
elems = [this[0]];
}
return clone(elems);
},
get: function (index) {
//Get all elements or a single element from the current collection.
//When no index is given, returns all elements in an ordinary array.
//When index is specified, return only the element at that position.
return this[index || 0];
},
has: function (selector) {
//Filter the current collection to include only elements that have
//any number of descendants that match a selector, or that contain a specific DOM node.
let elems = [];
if (this.length > 0) {
this.each(function (i, e) {
if (query(e, selector).length > 0) {
if (elems.indexOf(e) < 0) { elems.push(e); }
}
});
}
return clone(elems);
},
hasClass: function (classes) {
//Check if any elements in the collection have the specified class.
let classList;
if (isType(classes, 5)) {
classList = classes;
} else if (isType(classes, 1)) {
classList = classes.split(' ');
}
for (let x = 0; x < this.length; x++) {
const n = this[x].className || '';
if (isType(n, 1)) {
const classNames = n.split(' ');
if (classNames.length > 0) {
if (
classList.every(function (a) {
return classNames.some(function (b) { return a == b; });
})
) {
return tru;
}
}
}
}
return fals;
},
height: function (val) {
//Get the height of the first element in the collection;
//or set the height of all elements in the collection.
//this function differs from jQuery as it doesn't care
//about box-sizing & border when returning the height
//of an element (when val is not specified).
//It simply returns vanilla js offsetHeight (as it should);
let obj = getObj(val);
if (isType(obj, 1)) {
const n = parseFloat(obj);
if (!isNaN(n)) { obj = n; } else {
//height is string
this.each(function (i, e) {
if (e != window && e != document) {
e.style.height = obj;
}
});
return this;
}
} else if (obj == null) {
if (this.length > 0) {
//get height from first element
const elem = this[0];
if (elem == window) {
return window.innerHeight;
} else if (elem == document) {
const body = document.body;
const html = document.documentElement;
return Math.max(
body.offsetHeight,
body.scrollHeight,
html.clientHeight,
html.offsetHeight,
html.scrollHeight
);
} else {
return elem.clientHeight;
}
}
} else {
//height is a number
if (obj == 0) {
this.each(function (i, e) {
e.style.height = 0;
});
} else {
this.each(function (i, e) {
e.style.height = obj + 'px';
});
}
}
return this;
},
hide: function () {
//Hide elements in this collection by setting their display CSS property to none.
this.each(function (i, e) {
e.style.display = 'none';
});
return this;
},
hover: function () {
const args = normalizeArgs([
{ t: 1, o: tru }, //0: selector = string
{ t: 0, o: tru }, //1: data = anything
{ t: 6, o: fals }, //2: onEnter = function
{ t: 6, o: fals } //3: onLeave = function
], arguments);
let entered = fals;
this.on('mouseenter', args[0], args[1], function (e) {
if (!entered) {
entered = tru;
if (args[2]) { args[2](e); }
}
});
this.on('mouseleave', args[0], args[1], function (e) {
let p = e.target.parentNode, f = fals;
while (p != null) {
if (p == this) {
f = tru; break;
}
p = p.parentNode;
}
if (!f) {
entered = fals;
if (args[3]) { args[3](e); }
}
});
},
html: function (content) {
//Get or set HTML contents of elements in the collection.
//When no content given, returns innerHTML of the first element.
//When content is given, use it to replace contents of each element.
let obj = getObj(content);
if (obj == null) {
if (this.length > 0) {
return this[0].innerHTML;
} else {
return '';
}
} else {
this.each(function (i, e) {
e.innerHTML = obj;
});
}
return this;
},
/**
* @suppress {checkTypes}
*/
index: function (e) {
//Get the position of an element. When no element is given,
//returns position of the current element among its siblings.
//When an element is given, returns its position in the current collection.
//Returns -1 if not found.
let i = -1;
if (this.length > 0) {
const elem = e ? e : this[0];
const p = elem.parentNode;
let c;
if (p) {
c = p.children;
if ([].indexOf) {
return [].indexOf.call(c, elem);
} else {
//fallback for older browsers
for (let x = 0; x < c.length; x++) {
if (c[x] == elem) {
return x;
}
}
}
}
}
return i;
},
innerHeight: function (height) {
//Get the current computed inner height (including padding but not border) for the
//first element in the set of matched elements or set the inner height of every matched element
let obj = getObj(height);
if (obj == null) {
//get inner height of first element (minus padding)
if (this.length > 0) {
const e = this[0];
const style = getComputedStyle(e);
let padtop = parseFloat(style.paddingTop);
let padbot = parseFloat(style.paddingBottom);
if (isNaN(padtop)) { padtop = 0; }
if (isNaN(padbot)) { padbot = 0; }
return e.clientHeight - (padtop + padbot);
}
} else {
//set height of elements
return this.height(height);
}
},
innerWidth: function (width) {
//Get the current computed inner width (including padding but not border) for the
//first element in the set of matched elements or set the inner width of every matched element
let obj = getObj(width);
if (obj == null) {
//get inner width of first element (minus padding)
if (this.length > 0) {
const e = this[0];
const style = getComputedStyle(e);
let padright = parseFloat(style.paddingRight);
let padleft = parseFloat(style.paddingLeft);
if (isNaN(padright)) { padright = 0; }
if (isNaN(padleft)) { padleft = 0; }
return e.clientWidth - (padright + padleft);
}
} else {
//set width of elements
return this.width(width);
}
},
is: function (selector) {
//Check if all the elements of the current collection matches the CSS select.
if (this.length > 0) {
const self = this;
let obj = getObj(selector);
for (let x = 0; x < this.length; x++) {
switch (obj) {
case ':focus':
if (this[x] == document.activeElement) {
return tru;
}
break;
default:
const q = query(this[x].parentNode, obj);
if (q.some(function (a) { return a == self[0] })) {
return tru;
}
break;
}
}
}
return fals;
},
last: function () {
//Get the last element of the current collection.
let elems = [];
if (this.length > 0) {
elems = [this[this.length - 1]];
}
return clone(elems);
},
map: function (func) { //func(index, element)
//Iterate through every element of the collection. Inside the iterator function,
//this keyword refers to the current item = function(also passed as the second argument to the function).
//If the iterator select.prototype.returns 0, iteration stops.
let mapped = [];
for (let x = 0; x < this.length; x++) {
mapped.push(func(x, this[x]));
}
return mapped;
},
next: function (selector) {
//Get the next sibling optionally filtered by selector of each element in the collection.
let elems = [];
this.each(function (i, e) {
let el = e.nextSibling;
if (selector && el) {
//use selector
const q = query(e.parentNode, selector);
while (el != null) {
if (el.nodeName != '#text') {
if (q.some(function (s) { return s == el })) {
elems.push(el);
break;
}
}
el = el.nextSibling;
}
} else if (el) {
//no selector
while (el != null) {
if (el.nodeName != '#text') {
elems.push(el);
break;
}
el = el.nextSibling;
}
}
});
return clone(elems);
},
nextAll: function (selector) {
//Get all siblings below current sibling optionally filtered by selector of each element in the collection.
let elems = [];
if (selector) {
//use selector
this.each(function (i, e) {
const q = query(e, selector);
let n = e.nextSibling;
while (n) {
while (n.nodeName == '#text') {
n = n.nextSibling;
if (!n) { break; }
}
if (!n) { break; }
if (q.some(function (s) { return s == n; })) { elems.push(n); }
n = n.nextSibling;
}
});
} else {
//no selector
this.each(function (i, e) {
let n = e.nextSibling;
while (n) {
while (n.nodeName == '#text') {
n = n.nextSibling;
if (!n) { break; }
}
if (!n) { break; }
elems.push(n);
n = n.nextSibling;
}
});
}
return clone(elems);
},
not: function (selector) {
//Filter the current collection to get a new collection of elements that don't match the CSS select.
//If another collection is given instead of selector, return only elements not present in it.
//If a select.prototype.is given, return only elements for which the select.prototype.returns a falsy value.
//Inside the function, the this keyword refers to the current element.
const sel = getObj(selector);
let elems = this;
//check if selector is an array (of selectors)
if (isType(sel, 5)) {
sel.forEach(function (s) {
const q = query(document, s);
elems = diffArray(elems, q);
});
this.push(elems);
return this;
}
return clone(diffArray(elems, query(document, sel)));
},
off: function () {
//remove an event handler
let args = normalizeArgs([
{ t: 1, o: fals }, //0: event = string
{ t: 1, o: tru }, //1: selector = string (optional)
{ t: 6, o: fals } //2: handler = function
], arguments);
if (arguments.length > 1 && typeof arguments[1] == 'undefined') {
return;
}
this.each(function (i, e) {
for (let x = 0; x < listeners.length; x++) {
if (listeners[x].elem == e) {
//found element in listeners array, now find specific function (func)
const item = listeners[x];
if (args[2] == null) {
//if no function specified, remove all listeners for a specific event
if (args[0] == null) {
//remove all events and functions for element from listener
for (let y = 0; y < item.events.length; y++) {
const ev = item.events[y];
for (let z = 0; z < ev.list.length; z++) {
e.removeEventListener(ev.name, ev.list[z][1], tru);
}
}
listeners.splice(x, 1);
} else {
//remove all functions (for event) from element in listener
for (let y = 0; y < item.events.length; y++) {
if (item.events[y].name == args[0]) {
const ev = item.events[y];
for (let z = 0; z < ev.list.length; z++) {
e.removeEventListener(args[0], ev.list[z][1], tru);
}
listeners[x].events.splice(y, 1);
if (listeners[x].events.length == 0) {
//remove element from listeners array since no more events exist for the element
listeners.splice(x, 1);
}
break;
}
}
}
} else {
//remove specific listener based on event & function
for (let y = 0; y < item.events.length; y++) {
if (item.events[y].name == args[0]) {
//remove specific event from element in listeners array
const ev = item.events[y];
for (let z = 0; z < ev.list.length; z++) {
if (ev.list[z][1] && ev.list[z][1].toString() === args[2].toString() //check function match
&& ev.list[z][0] == args[1]) { //check selector match
e.removeEventListener(args[0], args[2], tru);
listeners[x].events[y].list.splice(z, 1);
break;
}
}
if (listeners[x].events[y].list.length == 0) {
//remove event from element list array since no more functions exist for the event
listeners[x].events.splice(y, 1);
if (listeners[x].events.length == 0) {
//remove element from listeners array since no more events exist for the element
listeners.splice(x, 1);
}
}
break;
}
}
}
break;
}
}
});
return this;
},
offset: function () {
//Get position of the element in the document.
//Returns an object with properties: top, left, width and height.
//When given an object with properties left and top, use those values to
//position each element in the collection relative to the document.
if (this.length > 0) {
const box = this[0].getBoundingClientRect();
return {
left: box.left + document.body.scrollLeft,
top: box.top + document.body.scrollTop
};
}
return { left: 0, top: 0 };
},
offsetParent: function () {
//Find the first ancestor element that is positioned,
//meaning its CSS position value is "relative", "absolute"" or "fixed".
if (this.length > 0) {
return this[0].offsetParent;
}
return null;
},
on: function () {
//Attach an event handler function for one or more events to the selected elements.
let args = normalizeArgs([
{ t: 1, o: fals }, //0: event = string
{ t: 1, o: tru }, //1: selector = string (optional)
{ t: 0, o: tru }, //2: data = anything (optional)
{ t: 6, o: fals } //3: handler = function
], arguments);
const events = args[0].replace(/\s/g, '').split(',');
for (let i = 0; i < events.length; i++) {
const ev = events[i];
if (ev == "hover") {
this.hover(args[1], args[2], args[3], args[3]);
} else {
this.each(function (i, e) {
let params = [args[1], args[3]];
if (args[1] != null && args[1] != '') {
function delegate(el) {
const sels = query(e, args[1]);
for (let x = 0; x < sels.length; x++) {
if (el.target == sels[x]) {
args[3].apply(sels[x], [].slice.call(arguments));
}
}
}
params = [args[1], delegate];