-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightquery.js
1233 lines (986 loc) · 38.6 KB
/
lightquery.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
/////////////////////////HELPERS//////////////////////////
Function.prototype.clone4lightquery = function() {
var newfun = new Function('return ' + this.toString())();
for(var key in this)
newfun[key] = this[key];
Object.assign(newfun.prototype, this.prototype);
return newfun;
};
const lqHelpers = {
spacedListString: {
regex: /(\S)\s+(\S)/g,
replacement: "$1 $2",
splitter: " "
},
css_variables: {
regex: {
trailing: /^--\w+(?:\w|-)*$/,
no_trailing: /^\w+(?:\w|-)*$/
}
},
functions: {
valid_name_regex: /^[$µA-Z_][0-9A-Z_$µ]*$/i,
valid_firstChar_regex: /^[$A-Z_]$/i,
invalid_otherChar_regex: /[^0-9A-Z_$]/ig
},
plugin: {},
constructorLQ: {
baseName: "lightqueryObject",
nameRegex: /^lightqueryObject(\d*)$/
},
arrayLike: {},
array: {}
};
lqHelpers.spacedListString.toArray = (str)=>{
return str
.replace(lqHelpers.spacedListString.regex, lqHelpers.spacedListString.replacement)
.split(lqHelpers.spacedListString.splitter);
};
lqHelpers.functions.newName = (str)=>{
if(lqHelpers.functions.valid_name_regex.test(str))
return str;
if(! (lqHelpers.functions.valid_firstChar_regex.test("" + str.charAt(0))))
return lqHelpers.functions.newName("$"+str);
return str.replace(lqHelpers.functions.invalid_otherChar_regex, "_");
};
lqHelpers.plugin.isValidPluginType = (str)=>{
return (typeof str == typeof "abc42") && (str==="instance" || str==="global");
};
lqHelpers.arrayLike.toArray = (arrlike)=>Array.prototype.slice.call(arrlike);
lqHelpers.array.empty = (arr)=>(arr.length==0 || arr==[] || arr==new Array());
///////////////////////////////////////////////////////////
//// CONSTRUCTOR
/**@class lightqueryObject
*Instantiate a lightqueryObject
*@param {string | object | function} selector - the selector
*@return {lightqueryObject | lightquery | object}
*/
const lightqueryObject = function obj(selector){
if(obj.caller === this.lightqueryID){
if(typeof selector != typeof (x=>x*x)){
this.selector = selector;
this.asNode = this.lightqueryID.getNode(selector);
this.length = ( ((this.asNode && this.asNode!=this.selector) || (typeof this.asNode == typeof [])) && (this.asNode.length || this.asNode instanceof NodeList) )
? this.asNode.length
: (this.selector ? 1 : 0);
for(let i = 0 ; i < this.length ; i+=1)
if(this.asNode)
this[i] = (this.asNode[i] || this.selector);
}else
return this.lightqueryID(document).ready(selector);
}else{
// return null;
return Object.create(null);
}
};
//// ARRAYLIKE HANDLING
lightqueryObject.prototype = Object.create( Array.prototype );
lightqueryObject.prototype.constructor = lightqueryObject;
//// METHODS
/**@memberof lightqueryObject @method eq
*Restrict the selection set to the item at the given index
*@param {int} index - the given index
*@return {lightqueryObject | null}
*/
lightqueryObject.prototype.eq = function(index){
if(index >= 0 && index < this.length)
return this[index] ? this.lightqueryID(this[index]) : null;
else
return null;
};
/**@memberof lightqueryObject @method val
*Retrieves the value property of the first element (or set the value of all of them)
*@param {string} [optional] value - the new value of the value property
*@return {value | lightqueryObject | null}
*/
lightqueryObject.prototype.val = function(value){
if(value)
this.forEach((e)=>{
e.value = value;
});
else
if(this[0])
return this[0].value;
else
return null;
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method add
*Adds elements to the selection set based on a selector
*@param {string | HTMLelement | DOMelement | etc} Aselector - the selector to the elements to add to the set
*@return {lightqueryObject}
*/
lightqueryObject.prototype.add = function(Aselector){
if(typeof Aselector == typeof "abc42"){
this.selector += `, ${Aselector}`;
return this.lightqueryID(this.selector);
}else{
this.unshift(Aselector);
return this.lightqueryID(this.selector);
}
};
/**@memberof lightqueryObject @method hasClass
*Tests whether or not the first element of the set has a certain class in its class list
*@param {string} className - the class to test
*@return {bool}
*/
lightqueryObject.prototype.hasClass = function(className){//unique class
return this[0].classList.contains(className) || undefined;
};
/**@memberof lightqueryObject @method addClass
*Adds classes to elements of the set that didn't already had them
*@param {string} classNames - a string containing all class names ("hover classe undeux trois" is valid example)
*@return {lightqueryObject}
*/
lightqueryObject.prototype.addClass = function(classNames){//multiple classes
const classes = lqHelpers.spacedListString.toArray(classNames);
const that = this;
classes.forEach((classe)=>{
that.forEach((elem)=>{
if(! lightquery(elem).hasClass(classe))
elem.classList.add(classe);
});
});
return this.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method removeClass
*Removes classes from elements of the set that had them
*@param {string} classNames - a string containing all class names ("hover classe undeux trois" is a valid example)
*@return {lightqueryObject}
*/
lightqueryObject.prototype.removeClass = function(classNames){//multiple classes
const classes = lqHelpers.spacedListString.toArray(classNames);
const that = this;
classes.forEach((classe)=>{
that.forEach((elem)=>{
if(lightquery(elem).hasClass(classe))
elem.classList.remove(classe);
});
});
return this.lightqueryID(that.selector);
};
/**@memberof lightqueryObect @method toggleClass
*Toggles classes on all the elements of the set
*@param {string} classNames - a string containing all class names ("hover classe undeux trois" is a valid example)
*@return {lightqueryObject}
*/
lightqueryObject.prototype.toggleClass = function(classNames){//multiple classes
const classes = lqHelpers.spacedListString.toArray(classNames);
const that = this;
classes.forEach((classe)=>{
that.forEach((e)=>{
const µe = that.lightqueryID(e);
if(µe.hasClass(classe))
µe.removeClass(classe);
else
µe.addClass(classe);
});
});
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method html
*Getter/Setter of the HTML content of the selected element
*@param {string} [optional] HTMLstring - being the string to which the HTML content will be set
*@return if HTMLstring is provided: the lightquery object the method was called on
*else: the HTML content of the first element
*/
lightqueryObject.prototype.html = function(HTMLstring){
if(HTMLstring)
this.asNode.forEach((e)=>e.innerHTML = HTMLstring);
else
if(this[0])
return this[0].innerHTML;
else
return null;
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method each
*Apply a functions to each element of the set
*@param {function} func - the functions to apply (will receive current index, current element and full array)
*@return {lightqueryObject}
*
*@warning the 'this' keyword is bound to the current item at each call
*/
lightqueryObject.prototype.each = function(func){
if(func){
const list = this;
for(let i = 0 ; i < list.length ; i+=1)
func.call(list[i], i, list[i], list);
}
return this.lightqueryID(this.selector);
// calls the functions with : index, element, array
};
/**@memberof lightqueryObject @method on
*Add a listener to the given event(s)
*@param {string} eventName - a string containing all events's names (//!\\ 'onclick' is passed as 'click', etc...)
*@param {function} func [default: null] - the functions to bind to the event
*@return {lightqueryObject}
*
*@warning the 'this' keyword is bound to the selection set during the functions call (as Nodes, not a lightqueryObject)
*/
lightqueryObject.prototype.on = function(eventName, func){
if(typeof func == typeof (x=>x))
this.forEach((e)=>{
for( let event of lqHelpers.spacedListString.toArray(eventName) ){
if(e.addEventListener)
e.addEventListener(event, func.bind(e));
else if(e.attachEvent) //Because "sometimes", Microsoft feels the need to be a bitch
e.attachEvent(`on${event}`, func.bind(e));
}
});
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method off
*Remove a listener from the given event(s)
*@param {string} eventName - a string containing all events's names (//!\\ 'onclick' is passed as 'click', etc...)
*@param {function} func - the functions that was bound to the event using one of lightquery's event binding methods
*@return {lightqueryObject}
*
*@warning the 'this' keyword is bound to the selection set during the functions call (as Nodes, not a lightqueryObject)
*/
lightqueryObject.prototype.off = function(eventName, func){
if(typeof func == typeof (x=>x))
this.forEach((e)=>{
for( let event of lqHelpers.spacedListString.toArray(eventName) ){
if(e.removeEventListener)
e.removeEventListener(event, func.bind(e));
else if(e.detachEvent) //Because "sometimes", Microsoft feels the need to be a bitch
e.detachEvent(`on${event}`, func.bind(e));
}
});
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method trigger
*Trigger the given event(s) (and the listeners bound to it)
*@param {string} eventName - a string containing all events's names (//!\\ 'onclick' is passed as 'click', etc...)
*@param {object} options - the custom options used for triggering the event
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.trigger = function(eventName, options){
const that = this;
if(typeof eventName == typeof "abc42"){
this.forEach((e)=>{
for( let event of lqHelpers.spacedListString.toArray(eventName) ){
let eventObject;
const eventOptions = that.lightqueryID.extend({target: e}, options);
if(window.CustomEvent)
eventObject = new window.CustomEvent(event, eventOptions);
else{
eventObject = document.createEvent("CustomEvent");
eventObject.initCustomEvent(event, true, true, eventOptions);
}
e.dispatchEvent(eventObject);
}
});
}
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method click
*Shorthand syntax to add an event listener to the onclik event
*@param {function} [func=null] - the functions to bind to the event
*@return {lightqueryObject}
*
*@warning the 'this' keyword is bound to the selection set during the functions call (as Nodes, not a lightqueryObject)
*/
lightqueryObject.prototype.click = function(func){
if(func)
return this.on("click", func.bind(this.asNode));
else
return this.trigger("click");
};
/**@memberof lightqueryObject @method hover
*Shorthand syntax to handle the hover events (onmouseenter and onmouseleave)
*@param {function} funcEnter [default: null] - the functions for the onmouseenter event
*@param {function} funcLeav [default: null] - the functions for the onmouseleave event
*
*@return {lightqueryObject}
*
*@warning the 'this' keyword is bound to the selection set during the functions call (as Nodes, not a lightqueryObject)
*/
lightqueryObject.prototype.hover = function(funcEnter, funcLeave){
return this
.on("mouseleave", funcLeave.bind(this.asNode) || null)
.on("mouseenter", funcEnter.bind(this.asNode) || null);
};
/**@memberof lightqueryObject @method css
*Getter/Setter for css properties
*@param {string | object} propertyNames - a string containing all properties' names/object
*ex: "background color backgroundColor boxShadow" is valid
*@param {value} [optionnal] value - the value to which set each CSSproperty of each element of the set
*
*@return {value | lightqueryObject | null}
*/
lightqueryObject.prototype.css = function(propertyNames, value){
const that = this;
if(typeof propertyNames == typeof {}){
that.forEach((e)=>{
for(let key in propertyNames){
e.style[key] = propertyNames[key];
}
});
}else{
const properties = lqHelpers.spacedListString.toArray(propertyNames);
if(value)
properties.forEach((property)=>{
that.forEach((e)=>{
e.style[property] = value;
});
});
else
if(this[0])
return window.getComputedStyle(this[0])[properties[0]];
else
return null;
}
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method cssVar
*Get/set instance CSS variables
*@param {string} variable - name of the CSS variable
*@param {string | number} val [optional] - new value for the CSS variable
*
*@return {lightqueryObject | string}
*/
lightqueryObject.prototype.cssVar = function(variable, val){
let varname = variable;
if(lqHelpers.css_variables.regex.no_trailing.test(varname))
varname = `--${varname}`;
if(lqHelpers.css_variables.regex.trailing.test(varname))
if(val==val && (typeof val == typeof "abc" || typeof val == typeof 42)){//undefined, null and NaN check + allow only string or numbers
this.forEach(e=>e.style.setProperty(varname, val));
}else{
return window.getComputedStyle(this[0]).getPropertyValue(varname);
}
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method attr
*Getter/setter for HTML attributes
*@param {string} attrNames - a string containing all attributes' names
*ex: "id class src alt placeholder" is valid
*@param {value} [optional] value - the value to which set all of these attributes (for every elements)
*
*@return {value | lightqueryObject | null}
*/
lightqueryObject.prototype.attr = function(attrNames, value){
const names = lqHelpers.spacedListString.toArray(attrNames);
const that = this;
if(value)
names.forEach((name)=>{
that.forEach((e)=>{
e.attributes[name].value = value;
});
});
else
if(this[0])
return this[0].attributes[names[0]].value;
else
return null;
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObjet @method prop
*Getter/setter for DOM/custom properties
*@param {string} propNames - a string containing all properties' names
*ex: "innerHTML my_custom_prop length" is valid
*@param {value} [optional] value - the value to which set all of these properties (for every elements)
*
*@return {value | lightqueryObject | null}
*/
lightqueryObject.prototype.prop = function(propNames, value){
const names = lqHelpers.spacedListString.toArray(propNames);
const that = this;
if(value)
names.forEach((name)=>{
that.forEach((e)=>{
e[name] = value;
});
});
else
if(this[0])
return this[0][names[0]];
else
return null;
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObjet @method data
*Getter/setter for custom data attributes
*@param {string} dataNames - a string containing all data attributes' names
*ex: "head link img" is valid (data-head, data-link and data-img)
*@param {value} [optional] value - the value to which set all of these data attributes (for every elements)
*
*@return {value | lightqueryObject | null}
*/
lightqueryObject.prototype.data = function(dataNames, value){
const names = lqHelpers.spacedListString.toArray(dataNames);
const that = this;
if(value)
names.forEach((name)=>{
that.forEach((e)=>{
e.dataset[name] = value;
});
});
else
if(this[0])
return this[0].dataset[name];
else
return null;
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method hasProp
*Determines whether the first element of the set has the said property
*@param {string} name - the name of that property
*
*@return {boolean}
*/
lightqueryObject.prototype.hasProp = function(name){
if(this[0])
return this[0].hasOwnProperty(name);
else
return false;
};
/**@memberof lightqueryObject @method hasAttr
*Determines whether the first element of the set has the said attribute
*@param {string} name - the name of that attribute
*
*@return {boolean}
*/
lightqueryObject.prototype.hasAttr = function(name){
return this[0] && this[0].hasAttribute(name);
};
/**@memberof lightqueryObject @method hasData
*Determines whether the first element of the set has the said data
*@param {string} name - the name of that data
*
*@return {boolean}
*/
lightqueryObject.prototype.hasData = function(name){
return this[0] && (name in this[0].dataset);
};
/**@memberof lightqueryObject @method removeProp
*Removes properties from all the elements of the set
*@param {string} propNames - a string containing all properties' names
*ex: "innerHTML dataset length"
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.removeProp = function(propNames){
const props = lqHelpers.spacedListString.toArray(propNames);
const that = this;
props.forEach((prop)=>{
that.forEach((e)=>{
if(that.lightqueryID(e).hasProp(prop))
delete e[prop];
});
});
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method removeAttr
*Removes attributes from all the elements of the set
*@param {string} attrNames - a string containing all attributes' names
*ex: "id class src alt"
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.removeAttr = function(attrNames){
const attrs = lqHelpers.spacedListString.toArray(attrNames);
const that = this;
attrs.forEach((attr)=>{
that.forEach((e)=>{
if(that.lightqueryID(e).hasAttr(attr))
e.attributes.removeNamedItem(attr);
});
});
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method removeData
*Removes datas from all the elements of the set
*@param {string} dataNames - a string containing all datas' names
*ex: "high low medium thin" for data-high, data-low, data-medium and data-thin
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.removeData = function(dataNames){
const names = lqHelpers.spacedListString.toArray(dataNames);
const that = this;
names.forEach((name)=>{
that.forEach((e)=>{
if(that.lightqueryID(e).hasData(name))
delete e.dataset[name];
});
});
return that.lightqueryID(that.selector);
};
/**@memberof lightqueryObject @method closest
*Get the closest element matching the selector for each element of the matched set
*@param {string} selector - a string selector
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.closest = function(selector){
if(typeof selector == typeof "abc42"){
const closestArr = this.map(e=>e.closest(selector));
return this.lightqueryID(closestArr);
}
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method children
*Get all the children (that match the selector, if given one) from each node
*@param {string} [selector=undefined] - a CSS selector to restrict the children
*
*@return {lightqueryObject | null}
*/
lightqueryObject.prototype.children = function(selector){
let res = this.reduce((acc, elem)=>{
//get children
if(elem.children)
acc.push.apply(acc, lqHelpers.arrayLike.toArray(elem.children));
return acc;
}, []).reduce((elem, index, arr)=>{ //NOTE: should have been filter
//only get distinct elements
return arr.indexOf(elem) == index;
});
if(lqHelpers.array.empty(res))
return null;
if(selector)
res = res.filter(e=>e.matches(selector));
if(lqHelpers.array.empty(res))
return null;
return this.lightqueryID(res);
};
/**@memberof lightqueryObject @method parent
*Get the parent of each element of the matched set
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.parent = function(){
//get all parents(if none get null) and remove null values
let parentArr = this.map(elem=>(elem.parentNode)?elem.parentNode:null)
.filter(elem=>elem!=null);
//return early if there were absolutely no parents
if(lqHelpers.array.empty(parentArr))
return null;
//keep distinct elements
parentArr = parentArr.filter((elem, index, arr)=>{
return arr.indexOf(elem) == index;
});
//return early if there were absolutely no parents
if(lqHelpers.array.empty(parentArr))
return this.lightqueryID([]);
return this.lightqueryID(parentArr);
};
/**@memberof lightqueryObject @method parents
*Get all predecessors(parents) of each element of the matched set (filtered by selector if one is given)
*@param {string} [selector=undefined] - the selector used for filtering
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.parents = function(selector){
let parentsArr = this.map(e=>{
//grab all predecessors
let curr = e;
let res = [];
while(curr.parentElement){
res.push(curr.parentElement);
curr = curr.parentElement;
}
return res;
}).reduce((acc, elem)=>{
//array of arrays of elements -> array of elements
acc.push(...elem);
return acc;
}, []).filter((elem, index, arr)=>{
//keep'em distinct
return arr.indexOf(elem) == index;
});
//null handling
if(lqHelpers.array.empty(parentsArr))
return null;
//filter handling
if(selector)
parentsArr = parentsArr.filter(e=>e.matches(selector));
//null handling
if(lqHelpers.array.empty(parentsArr))
return this.lightqueryID([]);
return this.lightqueryID(parentsArr);
};
/**@memberof lightqueryObject @method find
*Get all descendants (filtered by selector if one is given) of each elements of the matched set
*@param {string} [selector=undefined] - the selector used to filter
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.find = function(selector){
selector = selector || "*";
desc = [];
if(typeof selector === "string")
desc = this
.map(e=>e.querySelectorAll(selector))//get descendants
.reduce((acc, e)=>{//Array(Array(Element)) -> Array(Element)
acc.push(...e);
return acc;
}, [])
.filter(//keep distinct elements
(e,i,a)=>a.indexOf(e)==i
);
return this.lightqueryID(desc);
};
/**@memberof lightqueryObject @method first
*Get the first element of the matched set
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.first = function(){
return this.eq(0);
};
/**@memberof lightqueryObject @method last
*Get the last element of the matched set
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.last = function(){
return this.eq( this.length - 1 );
};
/**@memberof lightqueryObject @method has
*Reduce the set to the elements that have at least one descendant that matches the selector
*@param {string} selector - the selector used to reduce the set
*
*@return {lightqueryObject | null}
*/
lightqueryObject.prototype.has = function(selector){
if(typeof selector != typeof "abc42")
return null;
const l = this.lightqueryID;
const arr = this.filter(e=>l(e).children(selector)); //NOTE: Should have been find
if(lqHelpers.array.empty(arr))
return null;
return this.lightqueryID(arr);
};
/**@memberof lightqueryObject @method ready
*Add an event listener that will be triggered when the DOM is fully loaded
*@param {function} func - the function passed as the event handler
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.ready = function(func){
if(this.selector == window.document){
if(document.addEventListener)
document.addEventListener("DOMContentLoaded", func, false);
else if(window.addEventListener)
window.addEventListener("load", func, false);
else if(document.attachEvent)
document.attachEvent("onreadystatechange", func);
else if(window.attachEvent)
window.attachEvent("onload", func);
else
throw new Error("Why the heck is this computer still working anyway ? (no support for DOM loaded events ...)");
}
return this.lightqueryID(this.selector);
};
/**@memberof lightqueryObject @method Filter
*Filter the set of matched elements according to a predicate
*@param {function} func - the predicate used to filter
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.Filter = function(func){
if(typeof func != typeof (x=>x))
throw new TypeError(`${this.lightqueryID.fname}().Filter expected a function as its parameter`);
return this.lightqueryID( this.filter(func) );
};
/**@memberof lightqueryObject @method Map
*Map the set of matched elements according to a function
*@param {function} func - the funciton used to map
*
*@return {lightqueryObject}
*/
lightqueryObject.prototype.Map = function(func){
if(typeof func != typeof(x=>x))
throw new TypeError(`${this.lightqueryID.fname}().Map expected a function as its parameter`);
return this.lightqueryID( this.map(func) );
};
/**@memberof lightqueryObject @method Reduce
*Performs a reduction on the set of matched elements according to the reducer
*@param {function} reducer - the function used for the reduction
*@param {?} accumulator - the initial value of the accumulator
*/
lightqueryObject.prototype.Reduce = function(reducer, accumulator){
if(typeof reducer != typeof(x=>x))
throw new TypeError(`${this.lightqueryID.fname}().Reduce expected a function as its first parameter`);
return this.lightqueryID( this.reduce(reducer, accumulator) );
};
//// THE OG LightQuery
/**@function lightquery
*Get a lightquery object from a selector / add an event listener that will be triggered when the DOM is fully loaded
*@param {string | function | object} selector - the selector/function
*
*@return {lightqueryObject | lightquery | Object}
*/
const lightquery = function f(selector){
return new f.createLightqueryObject(selector);
};
lightquery.fname = "lightquery";
//// CLASS METHOD
/**@memberof lightquery @method appendNode
*Append an HTML node to a set of DOM nodes from a selector
*@param {string | object} targetSelector - the selector of the parent(s) to append the content to
*@param {string} AppendedElementType - a string representing the HTML tag of the appended node (ex: to append a <p>, pass "p")
*@param {string | other} content - the content of the appended tag
*
*@return {lightqueryObject | undefined}
*/
lightquery.appendNode = function(targetSelector, AppendedElementType, content){
if(targetSelector && AppendedElementType && content){
const parent = document.querySelectorAll(targetSelector);
for(let elem of parent){
const node = document.createElement(AppendedElementType);
node.innerHTML = content;
elem.appendChild(node);
}
return this(targetSelector);
}
return undefined;
};
/**@memberof lightquery @method getNode
*get nodes from a selector
*@param {string | object} targetSelector - the selector
*
*@return {NodeList | selector}
*/
lightquery.getNode = function(targetSelector){
if(typeof targetSelector != typeof "abc123")
return targetSelector;
else
return document.querySelectorAll(targetSelector);
};
/**@memberof lightquery @method cssVar
*Get/set global CSS variables
*@param {string} variable - name of the CSS variable
*@param {string | number} val [optional] - new value for the CSS variable
*
*@return {lightquery | string}
*/
lightquery.cssVar = function(variable, val){
let varname = variable;
if(lqHelpers.css_variables.regex.no_trailing.test(varname))
varname = `--${varname}`;
if(lqHelpers.css_variables.regex.trailing.test(varname))
if(val==val && (typeof val == typeof "abc" || typeof val == typeof 42)){//undefined, null and NaN check + allow only string or numbers
document.body.style.setProperty(varname, val);
}else{
return window.getComputedStyle(document.body).getPropertyValue(varname);
}
return this;
};
/**@memberof lightquery @method extend
*Extend the initial object by attributing (override) the properties of the other objects to it
*@param {object} initialObject - The initial object
*@param {objects} otherObjects - any amount of other objects to use for the assignment
*(ex: lightquery.extend({}, {}, {}, {}))
*
*@return {object}
*/
lightquery.extend = function(initialObject, ...otherObjects){
return Object.assign(initialObject, ...otherObjects);
};
/**@memberof lightquery @method ready
*Add an event listener that will be triggered when the DOM is fully loaded
*@param {function} func - the event handler function
*
*@return {lightquery}
*/
lightquery.ready = function(func){
return this(document).ready(func);
};