-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight-query.js
More file actions
1285 lines (1102 loc) · 33.1 KB
/
light-query.js
File metadata and controls
1285 lines (1102 loc) · 33.1 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
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
/* exported $ */
/* global AjaxSender */
/**
* The AjaxSender class
* @external AjaxSender
* @see {@link https://zenoo.github.io/ajax-sender/AjaxSender.html}
*/
/**
* Events holder
* @type {Object.<String, Object[]>}
* @private
*/
const __$_events__ = {}; // eslint-disable-line camelcase
/**
* Default elements display value
* @type {Object.<String, String>}
* @private
*/
const __$_default_display_inline__ = [ // eslint-disable-line camelcase
'a',
'i',
'script',
'abbr',
'iframe',
'select',
'acronym',
'img',
'slot',
'audio',
'input',
'small',
'b',
'ins',
'span',
'bdi',
'kbd',
'strong',
'bdo',
'label',
'sub',
'big',
'map',
'sup',
'br',
'mark',
'svg',
'button',
'meter',
'template',
'canvas',
'noscript',
'textarea',
'cite',
'object',
'time',
'code',
'output',
'u',
'data',
'picture',
'tt',
'datalist',
'progress',
'var',
'del',
'q',
'video',
'dfn',
'ruby',
'wbr',
'em',
's',
'embed',
'samp'
];
/**
* LightQuery holder class
* @type {Element[]}
* @extends Array
*/
class _$ extends Array{
/**
*
* @param {Element|NodeList|Array|String|Document|Window|Function|_$} parameter The parameter to initialize the _$ object with
* @param {Element} [context] Potential query context
*/
constructor(parameter, context){
super();
if(parameter instanceof Function){
window.addEventListener('load', () => {
Reflect.apply(parameter, null, []);
});
}else{
this.push(..._$._STD(parameter, context));
}
}
/**
* Standardizes any input to an Element array
* @param {Element|NodeList|Array|String|Document|Window|_$} parameter Element to standardize
* @param {Element} [context] Potential query context
* @returns {Element[]} Resulting Element array
* @private
*/
static _STD(parameter, context){
const result = [];
// Element | Document | Window passed as a parameter
if(parameter instanceof Element || parameter instanceof Document || parameter instanceof Window){
result.push(parameter);
// Array-like passed as a parameter
}else if(parameter instanceof _$ || parameter instanceof NodeList || parameter instanceof Array){
result.push(...parameter);
// String passed as a parameter
}else if(typeof parameter == 'string'){
// Valid selector
try {
// Check the context before querying globally
const elements = (context || document).querySelectorAll(parameter);
result.push(...elements);
// Invalid selector
} catch (error) {
const template = document.createElement('template');
// Try to create a DOM from the string
template.innerHTML = parameter;
result.push(...template.content.childNodes);
}
}
return result;
}
/**
* Converts element(s) to a DocumentFragment
* @param {Element[]} elements
* @returns {DocumentFragment}
* @private
*/
static _toFragment(elements){
const
fragment = document.createDocumentFragment(),
elementArray = [...elements];
while(elementArray[0]){
fragment.appendChild(elementArray.shift());
}
return fragment;
}
/**
* Add elements to the current _$ elements
* @param {Element|NodeList|Array|String|_$} parameter Element(s) to add
* @param {Element} [context] Context of the potential query
* @returns {_$} The current object
*/
add(parameter, context){
if(parameter instanceof _$){
this.push(...parameter);
}else{
this.push(..._$._STD(parameter, context));
}
return this;
}
/**
* Add class(es) to each element
* @param {String} parameter Space separated classes to add
* @returns {_$} The current object
*/
addClass(parameter){
this.forEach(item => {
item.classList.add(...parameter.split(/\s+/));
});
return this;
}
/**
* Insert content after each element
* @param {Element|NodeList|Array|String|_$|Element[]|NodeList[]|Array[]|String[]|_$[]} elements Elements to be inserted
* @returns {_$} The current object
*/
after(...elements){
this.forEach(item => {
let previousElement = item;
elements.forEach(newElement => {
previousElement = _$.insert(newElement, 'after', previousElement);
});
});
return this;
}
/**
* Append content to the end of each element
* @param {Element[]|NodeList[]|Array[]|String[]|_$[]} elements Elements to be appended
* @returns {_$} The current object
*/
append(...elements){
this.forEach(item => {
elements.forEach(newElement => {
_$.insert(newElement, 'end', item);
});
});
return this;
}
/**
* Append each element to the end of the targets
* @param {Element|NodeList|Array|String|_$} targets Elements to be appended to
* @returns {_$} The current object
*/
appendTo(targets){
_$.insert(this, 'end', targets);
return this;
}
/**
* Set/Get an attribute for each element
* @param {String} name The attribute name
* @param {String|Number|null} [value] The attribute value
* @returns {_$|String} The current object or the value of the attribute
*/
attr(name, value){
// Getter
if(typeof value === 'undefined'){
return this.length ? this[0].getAttribute(name) : null;
}
// Setter
this.forEach(item => {
item.setAttribute(name, value);
});
return this;
}
/**
* Insert content before each element
* @param {Element[]|NodeList[]|Array[]|String[]|_$[]} elements Elements to be inserted
* @returns {_$} The current object
*/
before(...elements){
// For each current element
this.forEach(item => {
// For each new element
elements.forEach(newElement => {
_$.insert(newElement, 'before', item);
});
});
return this;
}
/**
* Force the focus out of each element
* @returns {_$} The current object
*/
blur(){
this.forEach(item => {
item.blur();
});
return this;
}
/**
* Get the children of each element
* @param {String} [selector] An optional filter
* @returns {_$} The current object's children
*/
children(selector){
const children = [];
this.forEach(item => {
[...item.children].forEach(child => {
if(!selector || child.matches(selector)) children.push(child);
});
});
return new _$([...new Set(children)]);
}
/**
* Clone each element
* @param {Boolean} [deep=true] Deep clone the elements ?
* @returns {_$} A clone of the previous object
*/
clone(deep=true){
return new _$(this.map(item => item.cloneNode(deep)));
}
/**
* Get the closest (self-included) parent matching the selector for each element
* @param {String} selector The selector
* @returns {_$} A new LightQuery object
*/
closest(selector){
return new _$(this.map(item => item.closest(selector)).filter(item => item));
}
/**
* Get the children of each element (including text nodes)
* @returns {_$} A LightQuery object containing the child nodes
*/
contents(){
return new _$(this.reduce((acc, item) => {
acc.push(...item.childNodes);
return acc;
}, []));
}
/**
* Set/Get a CSS property
* @param {String|Object} parameter The CSS property name or an object containing every CSS properties to be changed
* @param {String} [value] The CSS property value
* @returns {_$} The current object
*/
css(parameter, value){
// Simple String way
if(typeof parameter == 'string'){
if(value){
this.forEach(item => {
item.style[parameter] = value;
});
}else{
return this.length ? this[0].style[parameter] || getComputedStyle(this[0])[parameter] : null;
}
// Object way
}else{
this.forEach(item => {
Object.entries(parameter).forEach(([key, val]) => {
item.style[key] = val;
});
});
}
return this;
}
/**
* Remove each element from the DOM, to be reused later
* @returns {_$} The current object
*/
detach(){
this.forEach(item => {
item.parentElement.removeChild(item);
});
return this;
}
/**
* Iterate over each element
* Inside this method, `this` corresponds to the current element
* @param {Function} callback The callback function
* @returns {_$} The current object
*/
each(callback){
this.forEach((item, index) => {
Reflect.apply(callback, item, [index, item]);
});
return this;
}
/**
* Remove all child nodes of each element
* @returns {_$} The current object
*/
empty(){
this.forEach(item => {
item.innerHTML = '';
});
return this;
}
/**
* Get the Nth element (a negative N starts counting from the end)
* @param {Number} index The element index
* @returns {_$} The Nth element's object
*/
eq(index){
return this.length ? new _$(this[index >= 0 ? index : this.length + index]) : new _$([]);
}
/**
* Filter elements from a selector or a function returning a Boolean
* @param {String|Function} parameter A selector or a filtering function
* @returns {_$} The filtered object
*/
filter(parameter){
const filtered = [];
this.forEach((item, index) => {
// Filter by function
if(parameter instanceof Function){
if(Reflect.apply(parameter, item, [item, index])) filtered.push(item);
// Filter by selector
}else if(item.matches(parameter)) filtered.push(item);
});
return new _$([...new Set(filtered)]);
}
/**
* Find descendants of each element corresponding to the selector
* @param {String} selector The selector
* @returns {_$} The corresponding descendants' object
*/
find(selector){
const descendants = [];
this.forEach(item => {
descendants.push(...item.querySelectorAll(selector));
});
return new _$([...new Set(descendants)]);
}
/**
* Get the first element
* @returns {_$} The first element's object
*/
first(){
return this.eq(0);
}
/**
* Get one or every element
* @param {Number} [index] The element index (null to get every element)
* @returns {Element|Element[]} The requested element(s)
*/
get(index){
return index ? this[index] : [...this];
}
/**
* Reduce the elements based on a descendant selector or descendant element
* @param {String|Element} parameter The selector or element to reduce with
* @returns {_$} The corresponding elements' object
*/
has(parameter){
// Reduce by element
if(parameter instanceof Element){
return new _$(this.filter(item => item.contains(parameter)));
}
//Reduce by selector
return new _$(this.filter(item => item.querySelector(parameter)));
}
/**
* Determine if at least one element contains the given class
* @param {String} className The class name
* @returns {Boolean} `true` if an element contains the class, `false` otherwise
*/
hasClass(className){
return !!this.filter(item => item.classList.contains(className)).length;
}
/**
* Get the computed height of the first element
* @returns {Number} The computed height of the first element (px)
*/
height(){
return this.length ? this[0] instanceof Document || this[0] instanceof Window ? document.documentElement.clientHeight : this[0].clientHeight : null;
}
/**
* Hide each element
* @returns {_$} The current object
*/
hide(){
this.forEach(item => {
item.style.display = 'none';
});
return this;
}
/**
* Get the HTML of the first element or Set each element's HTML
* @param {String} [html] The HTML to set
* @returns {_$} The current object
*/
html(html){
// Get
if(!html){
return this.length ? this[0].innerHTML : null;
}
// Set
this.forEach(item => {
item.innerHTML = html;
});
return this;
}
/**
* Get the first element's index in relation to its siblings
* @returns {Number} The index
*/
index(){
return this.length ? [...this[0].parentElement.children].indexOf(this[0]) : null;
}
/**
* Shorthand to insert element(s) relative to other(s)
* @param {Element|NodeList|Array|String|_$} toInsert The elements to insert
* @param {String} position The position of the new elements
* @param {Element|NodeList|Array|String|_$} relativeElements The elements to position from
* @returns {_$} A new LightQuery object containing the inserted nodes
*/
static insert(toInsert, position, relativeElements){
const elements = [];
_$._STD(relativeElements).forEach(relative => {
const
newElements = _$._STD(toInsert),
newNode = _$._toFragment(newElements);
elements.push(...newElements);
if(position == 'before'){
relative.parentNode.insertBefore(newNode, relative);
}else if(position == 'start'){
relative.insertBefore(newNode, relative.firstChild);
}else if(position == 'end'){
relative.appendChild(newNode);
}else if(position == 'after'){
relative.parentNode.insertBefore(newNode, relative.nextElementSibling);
}
});
return new _$(elements);
}
/**
* Insert each element after the target(s)
* @param {Element|NodeList|Array|String|_$} target The target(s)
* @returns {_$} The current object
*/
insertAfter(target){
_$.insert(this, 'after', target);
return this;
}
/**
* Insert each element before the target(s)
* @param {Element|NodeList|Array|String|_$} target The target(s)
* @returns {_$} The current object
*/
insertBefore(target){
_$.insert(this, 'before', target);
return this;
}
/**
* Check if at least one of the elements matches the target
* @param {String|Function|Element|_$} target The target or Function to match against
* @returns {Boolean} `true` if at least one of the elements matches the target, `false` otherwise
*/
is(target){
if(target instanceof _$){
return !!this.filter(item => target.includes(item)).length;
}else if(target instanceof Function){
return !!this.filter((item, index) => Reflect.apply(target, item, [index, item])).length;
}else if(target instanceof Element){
return !!this.filter(item => target.isSameNode(item)).length;
}
return !!this.filter(item => item.matches(target)).length;
}
/**
* Callback function used for the XHR error
*
* @callback errorCallback
* @param {XMLHttpRequest} response The XHR response
*/
/**
* Callback function used for the XHR success
*
* @callback successCallback
* @param {Object} response The XHR response
*/
/**
* Send an AJAX request
* @param {String|Object} parameter URL of the request or settings object
* @param {Object} [settings] Settings object
* @param {Object|FormData} [settings.data] Request data
* @param {String} [settings.dataType=json] Response data type
* @param {errorCallback} [settings.error] Callback for the error event
* @param {Object.<String, String>} [settings.headers] Request headers
* @param {String} [settings.method=GET] Request method
* @param {successCallback} [settings.success] Callback for the success event
* @param {String} [settings.url] Request URL
* @returns {XMLHttpRequest} The XHR request
*/
static ajax(parameter, settings){
// Load AjaxSender
new Promise(resolve => {
if(typeof AjaxSender == 'function'){
resolve();
}else{
const script = document.createElement('script');
script.src = 'https://gitcdn.link/repo/Zenoo/ajax-sender/v0.1.7/AjaxSender.min.js';
script.onload = () => {
resolve();
};
document.querySelector('head').appendChild(script);
}
}).then(() => {
// AjaxSender is ready here
if(!settings){
settings = parameter;
parameter = settings.url;
}
return new AjaxSender(parameter, {
method: settings.method || 'GET',
data: settings.data,
responseType: settings.dataType || 'json',
header: settings.headers,
load: settings.success,
error: settings.error
}).xhr;
});
}
/**
* Shorthand for sending a GET AJAX request
* @param {String} url URL of the request or settings object
* @param {Object} [data] Request data
* @param {successCallback} [success] Callback for the success event
* @param {String} [dataType] Response data type
* @returns {XMLHttpRequest} The XHR request
*/
static get(url, data, success, dataType){
return _$.ajax(url, {
data,
success,
dataType
});
}
/**
* Get the last element
* @returns {_$} The last element's object
*/
last(){
return this.eq(this.length - 1);
}
/**
* Get next immediate sibling. If a selector is provided, doesn't return the sibling if it doesn't match
* @param {String} [selector] The sibling selector
* @returns {_$} The next immediate sibling
*/
next(selector){
return this.map(item => selector ? item.nextElementSibling.matches(selector) ? item.nextElementSibling : null : item.nextElementSibling).filter(Boolean);
}
/**
* Get next siblings. If a selector is provided, doesn't return the siblings if they don't match
* @param {String} [selector] The sibling selector
* @returns {_$} The next siblings
*/
nextAll(selector){
const nextSiblings = [];
this.forEach(item => {
const
siblings = [...item.parentElement.children],
index = siblings.indexOf(item);
nextSiblings.push(...siblings.slice(index + 1, siblings.length));
});
return new _$([...new Set(selector ? nextSiblings.filter(sibling => sibling.matches(selector)) : nextSiblings)]);
}
/**
* Remove elements matching the target from the current object
* @param {Element|NodeList|Array|String|Function|_$} target The target
* @returns {_$} Object containing elements not matching the target
*/
not(target){
const unmatched = [];
this.forEach((item, index) => {
if(target instanceof Function){
if(!Reflect.apply(target, item, [index, item])) unmatched.push(item);
}else if(target instanceof _$){
if(!target.includes(item)) unmatched.push(item);
}else if(!_$._STD(target).includes(item)) unmatched.push(item);
});
return new _$([...new Set(unmatched)]);
}
/**
* Remove an event handler
* @param {String} events The events to stop listening to
* @param {String} [selector] The selector matching the one used with {@link _$#on}
* @param {Function} [handler] The handler used with {@link _$#on}
* @returns {_$} The current object
*/
off(events, selector, handler){
const eventList = events.split(/\s+/);
const eventsToRemove = [];
// Gather the events to remove
eventList.forEach(event => {
if(selector){
// .off(events, selector, handler)
if(handler){
// eslint-disable-next-line camelcase
eventsToRemove.push(...__$_events__[event].filter(stored => this.includes(stored.element) && stored.selector == selector && stored.handler == handler));
}else{
// .off(events, selector)
// eslint-disable-next-line camelcase
eventsToRemove.push(...__$_events__[event].filter(stored => this.includes(stored.element) && stored.selector == selector));
}
}else{
// .off(events)
// eslint-disable-next-line camelcase
eventsToRemove.push(...__$_events__[event].filter(stored => this.includes(stored.element)));
}
});
eventsToRemove.forEach(event => {
// Remove the events from the DOM
event.element.removeEventListener(event.eventName, event.silentHandler);
// Remove the events from the datastore
// eslint-disable-next-line camelcase
__$_events__[event.eventName] = __$_events__[event.eventName].filter(stored => stored != event);
});
return this;
}
/**
* Get the coordinates of the first element
* @returns {DOMRect} Object containing the coordinates of the first element. *Use `.left`, `.top`*
*/
offset(){
return this.length ? this[0].getBoundingClientRect() : null;
}
/**
* Add an event handler
* @param {String} events The events to start listening to
* @param {String} [selector] The selector used for event delegation
* @param {Function} handler The handler for the event(s)
* @param {Object} [data] The data to be passed the the handler
* @returns {_$} The current object
*/
on(events, selector, handler, data){
const eventList = events.split(/\s+/);
if(selector instanceof Function){
data = handler;
handler = selector;
selector = null;
}
eventList.forEach(event => {
this.forEach(item => {
const silentHandler = e => {
if(selector){
const target = e.target.closest(selector);
if(item.contains(target)){
e.data = data;
Reflect.apply(handler, target, [e]);
}
}else{
e.data = data;
Reflect.apply(handler, item, [e]);
}
};
// Attach event
item.addEventListener(event, silentHandler);
// Store event in datastore
if(!__$_events__[event]) __$_events__[event] = []; // eslint-disable-line camelcase
__$_events__[event].push({ // eslint-disable-line camelcase
eventName: event,
element: item,
selector,
handler,
silentHandler
});
});
});
return this;
}
/**
* Get the parent of each element. If a selector is passed, filter those parents
* @param {String} [selector] The parent selector
* @returns {_$} The parent(s)
*/
parent(selector){
return new _$([...new Set(selector ? this.map(item => item.parentElement.matches(selector) ? item.parentElement : null).filter(Boolean) : this.map(item => item.parentElement))]);
}
/**
* Get the ancestors of each element. If a selector is passed, filter those parents
* @param {String} [selector] The parents selector
* @returns {_$} The parent(s)
*/
parents(selector){
const parents = new Set();
this.forEach(item => {
let parent = item.parentElement;
while(parent){
if(selector){
if(parent.matches(selector)) parents.add(parent);
}else{
parents.add(parent);
}
parent = parent.parentElement;
}
});
return new _$([...parents]);
}
/**
* Prepend content to the end of each element
* @param {Element[]|NodeList[]|Array[]|String[]|_$[]} elements Elements to be prepended
* @returns {_$} The current object
*/
prepend(...elements){
this.forEach(item => {
_$.insert(elements.reduce((acc, element) => acc.add(element), new _$()), 'start', item);
});
return this;
}
/**
* Prepend each element before the targets
* @param {Element|NodeList|Array|String|_$} targets Elements to be prepended to
* @returns {_$} The current object
*/
prependTo(targets){
_$.insert(this, 'start', targets);
return this;
}
/**
* Get previous immediate sibling. If a selector is provided, doesn't return the sibling if it doesn't match
* @param {String} [selector] The sibling selector
* @returns {_$} The previous immediate sibling
*/
prev(selector){
return this.map(item => selector ? item.previousElementSibling.matches(selector) ? item.previousElementSibling : null : item.previousElementSibling).filter(Boolean);
}
/**
* Get previous siblings. If a selector is provided, doesn't return the siblings if they don't match
* @param {String} [selector] The sibling selector
* @returns {_$} The previous siblings
*/
prevAll(selector){
const previousSiblings = [];
this.forEach(item => {
const
siblings = [...item.parentElement.children],
index = siblings.indexOf(item);
previousSiblings.push(...siblings.slice(0, index));
});
return new _$([...new Set(selector ? previousSiblings.filter(sibling => sibling.matches(selector)) : previousSiblings)]);
}
/**
* Remove each element from the DOM
* @returns {_$} The current object
*/
remove(){
this.forEach(item => {
item.remove();
});
return this;
}
/**
* Remove an attribute from each element
* @param {String} attribute Attribute name
* @returns {_$} The current object
*/
removeAttr(attribute){
this.forEach(item => {
item.removeAttribute(attribute);
});
return this;
}
/**
* Remove class(es) from each element
* @param {String} classes Space separated classes to remove
* @returns {_$} The current object
*/
removeClass(classes){
this.forEach(item => {
item.classList.remove(...classes.split(/\s+/));
});
return this;
}
/**
* Replace each target with each element
* @param {Element|NodeList|Array|String|_$} targets The targets
* @returns {_$} The current object
*/
replaceAll(targets){
_$._STD(targets).forEach(target => {
this.insertAfter(target);
target.remove();
});
return this;
}
/**
* Replace each element with the new content
* @param {Element|NodeList|Array|String|_$} newContent The new content
* @returns {_$} The current object
*/
replaceWith(newContent){
this.forEach(item => {
_$.insert(newContent, 'before', item);
item.remove();
});
return this;
}
/**
* Get the horizontal scroll value of the first element or set the horizontal scroll value for each element
* @param {Number} [value] The new horizontal scroll value
* @returns {Number|_$} The horizontal scroll value of the first element or the current object
*/
scrollLeft(value){
// Get
if(isNaN(value)){
return this.length
? this[0] instanceof Document
? this[0].scrollingElement.scrollLeft
: this[0] instanceof Window
? this[0].document.scrollingElement.scrollLeft
: this[0].scrollLeft
: null;
}