-
Notifications
You must be signed in to change notification settings - Fork 5
/
vanilla-query.js
936 lines (829 loc) · 20.6 KB
/
vanilla-query.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
/**
* @name vanilla-query
* Version: 0.5.1 (Thu, 04 May 2023 19:15:17 GMT)
*
* @author makesites
* Homepage: http://makesites.org/projects/vanilla-query
* @license Apache License, Version 2.0
*/
// stop processing if $ is already part of the namespace
//if( !$ )
(function(window, document) {
var vQuery = function(){
var self = this;
var $ = function( query ){
// find the el if passed a query
selector(query);
return self;
};
// copy methods from class
for(var i in this ){
$[i] = this[i];
}
return $;
};
// internal variables
var _selected = {};
// AJAX
vQuery.prototype.ajax = function(url, options){
// prerequisite(s)
if( !url ) return;
// fallback(s)
options = options || {};
// variables
var data;
var method = options.method || 'get';
var success = options.success || function(){};
var error = options.error || function(){};
// start request
var request = new XMLHttpRequest();
request.open(method, url, true);
// use tpe specific methods ?
if (method.toLowerCase() === 'get') {
request.send();
}
if (method.toLowerCase() === 'post') {
// Check if options.data is JSON, if not, send as application/x-www-form-urlencoded
try {
options.data = JSON.stringify(options.data);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
} catch (e) {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
}
request.send(options.data);
}
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
try {
data = JSON.parse(request.responseText);
} catch (e) {
data = request.responseText;
}
success(data);
} else {
error();
}
};
request.onerror = function(err) {
error(err);
};
};
// GET
// Example: $.get('//example.com', function (data) { });
vQuery.prototype.get = function(url, callback){
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = callback;
httpRequest.open('GET', url);
httpRequest.send();
};
// POST
// Example: $.post('//example.com', { username: username }, function (data) { })
vQuery.prototype.post = function(url, data, callback){
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = callback;
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send( vQuery.param( data ) );
};
vQuery.prototype.load = function(url, data, callback){
// prerequisite(s)
if( !url ) return;
// fallback(s)
data = data || {};
if( !isFunction(callback) ) callback = function(){};
var nodes = ( !isArray(_selected) ) ? [_selected] : _selected;
// ajax request
this.ajax( url, {
data: data,
dataType: 'html', // what's returned from the server
success: function( response ){
//
nodes.forEach(function(node){
node.innerHTML = response;
});
// continue;
callback();
}
});
//
};
// JSONP
// Example: $.getJSON('//example.com', function (data) { })
vQuery.prototype.getJSON = function(url, callback){
// save callback
window._success = callback;
var scr = document.createElement('script');
scr.src = url + '?callback=_success';
document.body.append(scr);
};
vQuery.prototype.parseJSON = function( str ){
// prereqiusite(s)
if( isObject(str) ) return str; /// already an object
if( !isString(str) ) return false; // alert error?
//
try {
var output = JSON.parse( str );
return output;
} catch (e) {
// error:
console.log("error", "JSON not valid");
}
};
// Param - convert a JSON object to a string of URL params
// Example: $.param( obj )
vQuery.prototype.param = function(data){
return Object.keys(data).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
}).join('&');
};
// attr - set attribute value
// Example: $( selector ).attr( key, value );
vQuery.prototype.attr = function(key, value){
if( !_selected ) return this;
var nodes = ( isElement(_selected) && !isArray(_selected) ) ? [ _selected ] : _selected;
var output = [];
//
nodes.forEach(function(el){
if( key ){
// set attributes
if( value && isString(value) ){
el.setAttribute(key, value);
output = this; // change of type...
} else if ( isObject(key) ){
var props = key;
for(var p in props){
el.setAttribute(p, props[p]);
output = this; // change of type...
}
} else if( isString(key) ){
output.push(el.attributes[key].value);
}
} else {
// return attributes
var atts = {};
el.attributes.forEach(function( property ){
var name = camelCase( property.name );
atts[name] = property.value;
});
output.push(atts);
}
});
return output;
};
vQuery.prototype.removeAttr = function(attr){
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.removeAttribute( attr );
});
} else {
var el = _selected;
el.removeAttribute( attr );
}
return this;
};
// Classes
// addClass
vQuery.prototype.addClass = function(names){
// check if string?
var classes = names.split(' ');
for( var i in classes ){
var name = classes[i];
_selected.classList.add(name);
}
return this;
};
// hasClass
vQuery.prototype.hasClass = function(name) {
//return new RegExp(' ' + name + ' ').test(' ' + _selected.className + ' ');
return _selected.classList.contains( name );
};
// toggleClass
vQuery.prototype.toggleClass = function(names){
// check if string?
var classes = names.split(' ');
for( var i in classes ){
var name = classes[i];
_selected.classList.toggle(name);
}
return this;
};
// toggleClass
vQuery.prototype.removeClass = function(names){
// check if string?
var classes = names.split(' ');
for( var i in classes ){
var name = classes[i];
_selected.classList.remove(name);
}
return this;
};
vQuery.prototype.type = function( el ){
el = el || _selected;
return Object.prototype.toString.call( el ).replace(/^\[object (.+)\]$/, '$1').toLowerCase();
};
vQuery.prototype.isEmptyObject = function(){
if( !_selected ) return;
return !_selected.hasChildNodes();
};
vQuery.prototype.isFunction = function( value ){
return isFunction( value );
};
vQuery.prototype.isArray = function( value ){
return isArray( value );
};
// Elements
vQuery.prototype.contains = function( el ){
// check for a match
return _selected.textContent.indexOf( el ) > -1;
};
vQuery.prototype.val = function( value ){
if( isString(value) ){
if( isArray(_selected) ){
_selected.forEach(function(i){
i.value = value;
});
} else {
_selected.value = value;
}
return this;
} else {
return _selected.value || 0;
}
};
//
vQuery.prototype.text = function( text ){
text = text || "";
var output = [];
if( isArray(_selected) ){
_selected.forEach(function( el ){
if( !text ){
output.push( el.textContent );
} else {
el.textContent = text;
}
});
} else {
var el = _selected;
if( !text ){
output.push( el.textContent );
} else {
el.textContent = text;
}
}
return contents ? this : output;
};
// Event methods
vQuery.prototype.ready = function( callback ){
if( callback && callback !== undefined && typeof callback === 'function' ){
// special condition for Chrome
//window.addEventListener('DOMFocusIn', callback, false);
document.addEventListener('DOMContentLoaded', callback, false);
} else {
// output error
console.log(".ready():", "Callback not valid");
}
};
// click
vQuery.prototype.click = function( callback ){
if( Array.isArray(_selected) && callback ){
_selected.forEach(function( el ){
el.addEventListener('click', callback);
});
} else if( callback ) {
var el = _selected;
el.addEventListener('click', callback); // this.on('click', callback);
} else {
this.trigger('click');
}
return this;
};
// trigger
vQuery.prototype.trigger = function( event ){
var el = _selected;
if( el.fireEvent ){
el.fireEvent('on' + event);
} else {
var e = document.createEvent('Events');
e.initEvent(event, true, false);
el.dispatchEvent(e);
}
return this;
};
vQuery.prototype.on = function( event, callback ){
// prerequisite(s)
if( !callback ) return this; // assume it's a function?
//
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.addEventListener( event, callback );
});
} else {
var el = _selected;
el.addEventListener( event, callback );
}
return this;
};
vQuery.prototype.off = function( event, callback ){
// prerequisite(s)
if( !callback ) return this; // assume it's a function?
//
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.removeEventListener( event, callback );
});
} else {
var el = _selected;
el.removeEventListener( event, callback );
}
return this;
};
// prepend
vQuery.prototype.prepend = function( node ){
// prerequisite: check if node is an element?
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.insertBefore( node, el.firstChild );
});
} else {
var el = _selected;
el.insertBefore( node, el.firstChild );
}
return this;
};
// append
vQuery.prototype.append = function( node ){
// prerequisite: check if node is an element?
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.append( node ); // appendChild(node);
});
} else {
var el = _selected;
el.append( node ); // appendChild(node);
}
return this;
};
vQuery.prototype.before = function( str ){
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.insertAdjacentHTML('beforebegin', str);
});
} else {
var el = _selected;
el.insertAdjacentHTML('beforebegin', str);
}
return this;
};
vQuery.prototype.after = function( str ){
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.insertAdjacentHTML('afterend', str);
});
} else {
var el = _selected;
el.insertAdjacentHTML('afterend', str);
}
return this;
};
vQuery.prototype.insertBefore = function( node ){
// prerequisite(s)
if( !_selected ) return this;
// prerequisite: check if node is an element?
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
node.parentNode.insertBefore(el, node.parentNode.firstChild);
});
} else {
var el = _selected;
node.parentNode.insertBefore(el, node.parentNode.firstChild);
}
return this;
};
vQuery.prototype.insertAfter = function( node ){
// prerequisite(s)
if( !_selected ) return this;
// prerequisite: check if node is an element?
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
node.parentNode.insertBefore(el, node.parentNode.nextSibling);
});
} else {
var el = _selected;
node.parentNode.insertBefore(el, node.parentNode.nextSibling);
}
return this;
};
vQuery.prototype.replaceWith = function( html ){
// prerequisite(s)
var replaced = [];
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
replaced.push( el.outerHTML );
el.outerHTML = html;
});
} else {
var el = _selected;
replaced.push( el.outerHTML );
el.outerHTML = html;
}
return replaced;
};
// Feature not supported yet
/*
vQuery.prototype.wrap = function( tag ){
// check tag
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.outerHTML = `${tag.match(/<(.|\n)*?>/g)[0]}${el.outerHTML}`;
});
} else {
var el = _selected;
el.outerHTML = `${tag.match(/<(.|\n)*?>/g)[0]}${el.outerHTML}`;
}
return this;
};
*/
// clone
vQuery.prototype.clone = function( callback ){
return _selected.cloneNode(true); // also replace _selected with cloned?
};
vQuery.prototype.remove = function(){
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.parentNode.removeChild(el);
});
} else {
var el = _selected;
el.parentNode.removeChild(el);
}
return this;
};
// empty
vQuery.prototype.empty = function( callback ){
while(_selected.firstChild){ _selected.removeChild(_selected.firstChild); } // or: el.innerHTML = '';
};
vQuery.prototype.unique = function( items ){
var array = items || _selected || null;
var unique = Array.from( new Set( array ) );
if( items ){
return unique;
} else {
_selected = unique;
return this;
}
};
// html
vQuery.prototype.html = function( html ){
var el = getEl();
// two states
if( html ){
el.innerHTML = html;
return this;
} else {
return el.innerHTML;
}
};
vQuery.prototype.parseHTML = function( str ){
var html = document.implementation.createHTMLDocument();
html.body.innerHTML = str ;
return html.body.children;
};
// Array manipulation
vQuery.prototype.map = function( cb ){
// prerequisite(s)
if( !isFunction(cb) || !_selected ) return this;
Array.prototype.map.call( _selected, cb );
return this;
};
// String manipulation
vQuery.prototype.trim = function( str ){
if( !isString(str) ) return false;
return str.trim();
};
// internal selector method
var selector = function( query, root ){
// fallbacks
if( !root || !isElement(root) ) root = document;
var el = null;
// first off if it's not a string assume it's already an element
if( typeof query !== "string" ){
el = query;
} else if( isTag(query) ){
// check if it's a tag
var tag = query.substring(1, query.length-1);
el = document.createElement( tag );
} else if( isId(query) ){
// check if it's an id
var id = query.substr(1);
el = root.getElementById( id );
} else {
// run a basic query
el = root.querySelectorAll(query);
}
// save element(s) for chaining commands
_selected = el;
return this;
};
// show
vQuery.prototype.show = function(){
// prerequisite(s)
if( !_selected ) return this;
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.style.display = ''; // display = 'block
});
} else {
var el = _selected;
el.style.display = ''; // display = 'block
}
return this;
};
// hide
vQuery.prototype.hide = function(){
// prerequisite(s)
if( !_selected ) return this;
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
el.style.display = 'none';
});
} else {
var el = _selected;
el.style.display = 'none';
}
return this;
};
// toggle
vQuery.prototype.toggle = function (elem) {
// prerequisite(s)
if( !_selected ) return this;
var self = this;
// multiple items
if( Array.isArray(_selected) ){
_selected.forEach(function( el ){
// If the element is visible, hide it
if( window.getComputedStyle(el).display === 'block' ){
self.hide();
} else {
self.show();
}
});
} else {
var el = _selected;
// If the element is visible, hide it
if( window.getComputedStyle(el).display === 'block' ){
self.hide();
} else {
self.show();
}
}
return this;
};
// css
vQuery.prototype.css = function(key, value){
// prerequisite(s)
if( !isElement( _selected ) ) return this;
//
if( isString(key) ){
_selected.style[ camelCase(key) ] = value;
} else if( isObject(key) ){
var styles = key;
for( var k in styles ){
var v = styles[k];
_selected.style[ camelCase(k) ] = v;
}
} else {
// return styles (check if element first?)
return getComputedStyle( _selected );
}
return this;
};
vQuery.prototype.position = function(){
if( !_selected ) return false;
// expect one element
return { left: _selected.offsetLeft, top: _selected.offsetTop };
};
vQuery.prototype.height = function(){
if( !_selected ) return 0; // error log?
var height = _selected.offsetHeight || 0;
return height;
};
vQuery.prototype.width = function(){
if( !_selected ) return 0; // error log?
var width = _selected.offsetWidth || 0;
return width;
};
vQuery.prototype.offset = function(){
var pos = _selected.getBoundingClientRect();
var offset = {
top: pos.top + document.body.scrollTop,
left: pos.left + document.body.scrollLeft
};
return offset;
};
vQuery.prototype.offsetParent = function(){
if( !_selected ) return false; // error log?
return _selected.offsetParent;
};
// each
// Example: $("a").each(function( el ){ });
vQuery.prototype.each = function( a, b ){
// check variables
var el, callback;
if( typeof a == "function"){
callback = a;
el = getEl();
} else {
el = a;
callback = ( b ) ? b : function(){};
}
[].forEach.call(el, callback );
//el.forEach( callback );
};
vQuery.prototype.find = function(query){
if( !isElement(query) ){
if( query.indexOf(',') > -1 ){
var _query = query.split(',');
var nodes = [];
_query.forEach(function(i){
var subset = [];
if(nodes.length){
nodes.forEach(function( node ){
subset = selector(i, node);
subset.forEach(function(m){
nodes.push(m);
});
});
} else {
subset = selector(i, _selected);
subset.forEach(function(n){
nodes.push(n);
});
}
});
_selected = nodes;
} else {
_selected = selector(query, _selected);
}
} else {
// replace selected
_selected = query;
}
return this;
};
vQuery.prototype.get = function(i){
return ( isArray(_selected) ) ? _selected[i] : null;
};
// parent
vQuery.prototype.parent = function( query ){
return _selected.parentNode;
};
vQuery.prototype.parents = function( el ){
var parent = _selected.parentNode;
var parents = [];
//
while( parent ){
parents.unshift(parent);
parent = parent.parentNode;
}
if( el ){
var parents_with_el = [];
var valid_parents = [];
parents.forEach(function( node ){
parents_with_el = slice( node.querySelectorAll( el ) ); // query());
parents_with_el.forEach(function( p ){
valid_parents.push( p );
});
});
_selected = this.unique( valid_parents );
} else {
_selected = parents;
}
return this;
};
vQuery.prototype.children = function(el){
var children = slice( _selected.children ); //
if (el) {
var children_with_el = [];
var valid_children = [];
children.forEach(function( node ){
children_with_el = slice( node.querySelectorAll( el ) ); // query()
children_with_el.forEach(function( p ){
valid_children.push( p );
});
});
_selected = this.unique( valid_children );
} else {
_selected = children;
}
return this;
};
vQuery.prototype.siblings = function(){
var nodes = _selected.parentNode.children;
var siblings = Array.prototype.filter.call(nodes, function ( child ){
return child !== _selected;
});
var _selected = siblings;
return this;
};
// is
vQuery.prototype.is = function( query ){
// flags
var flag = null;
switch( query ){
case ":empty":
flag = (!_selected.hasChildNodes());
break;
// more flags...
}
// check query
if( _selected && query && flag === null ){
var el = selector( query );
flag = (el === _selected);
}
return flag;
};
// next
vQuery.prototype.next = function(){
return _selected.nextSibling; // .nextElementSibling
};
// prev
vQuery.prototype.prev = function(){
return _selected.previousSibling; // .previousElementSibling
};
// filter (partial support)
vQuery.prototype.filter = function( query ){
if( query == ":first" ){
return ( Array.isArray(_selected) ) ? _selected[0] : _selected;
}
// test if element(s)
if( isFunction( query ) ){
var el = Array.prototype.filter.call(_selected, query);
_selected = el; // update selected?
}
return this;
};
// Utils
// Underscore Mixin: camelCase()
// Source: https://gist.github.com/tracend/5530356
var camelCase = function( string ){
return string.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
};
var isTag = function( string ){
return string.search(/^<\w+>$/) === 0;
};
var isId = function( string ){
return string.search(/^#\w+$/) === 0;
};
var isElement = function( el ){
return el instanceof Element || el[0] instanceof Element;
};
// return one item from the _selected stack
var getEl = function( i ){
i = i || 0;
// more sophisticated logic here?
return _selected[i] || _selected;
};
// find an element in the dom
var findEl = function( element ){
return isElement(element) ? element : document.querySelectorAll( element)[0];
};
var slice = function( nodes ){
return Array.prototype.slice.call( nodes );
};
// Common.js extend method: https://github.com/commons/common.js
var extend = function(destination, source) {
for (var property in source) {
if (source[property] && source[property].constructor && source[property].constructor === Object) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
};
// Types
var isArray = function( value ){
return Array.isArray( value );
};
var isFunction = function( fn ){
return (fn instanceof Function);
};
var isObject = function( value ){
return value && typeof value === 'object' && value.constructor === Object;
};
var isString = function( value ){
return typeof value === 'string' || value instanceof String;
};
window.vQuery = new vQuery();
// condition the attachment to the $
window.$ = window.vQuery;
})(this.window, this.document);