-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgquery.js
1557 lines (1394 loc) · 53.3 KB
/
gquery.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
// =================================================
//
// gQuery.js v1.5.6
// (c) 2020-present, JU Chengren (Ganxiaozhe)
// Contact: [email protected]
// Released under the MIT License.
// gquery.cn/license
//
// [fn]
// init,push,each,find,is,exist,eq,parent,next
// text,html,ohtml,val,width,height,css,offset
// remove,empty,prepend,append,before,after
// attr,removeAttr,data,removeData
// hasClass,addClass,removeClass,toggleClass
// show,hide,animate,fadeIn,fadeOut,fadeToggle
// slideUp,slideDown,slideToggle,on,one,off,trigger
// click,select,load,wait
//
// [extend fn]
// isPlainObject,isWindow,isNode
// each,copy,fetch
// [extend array]
// unique,finder,has
// [extend event]
// add,remove
// [extend get]
// browserSpec,queryParam,json
// [extend parse]
// html,json
// [extend cookie]
// set,get,remove
// [extend storage]
// local,set,get,remove,clear,push
// [extend sessionStorage]
// local,set,get,remove,clear,push
// [extend chain]
// chain
// [extend date]
// parse,calc
// [extend date prototype]
// init,calc,initDate,format,diff,ago
//
// =================================================
;(function(global, factory){
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = window || self, global.gQuery = global.$ = factory());
if(!!window.ActiveXObject || "ActiveXObject" in window){
window.location.href = 'https://gquery.cn/kill-ie?back='+(window.location.href);
}
console.log('%c gQuery 1.5.6 %c gquery.cn \n','color: #fff; background: #030307; padding:5px 0; margin-top: 1em;','background: #efefef; color: #333; padding:5px 0;');
}(window, function(){
'use strict';
let gQuery = function(selector, context){
return new gQuery.fn.init(selector, context);
};
/* -------------------------------------
* gQuery - prototype chain
* ------------------------------------- */
gQuery.fn = gQuery.prototype = {
constructor: gQuery,
gquery: '1.5.6',
init: function(sel, opts){
let to = typeof sel, elems = [];
switch(to){
case 'function':
if (document.readyState != 'loading'){sel();} else {
document.addEventListener('DOMContentLoaded', sel);
}
return;
case 'object':
if(gQuery.isWindow(sel)){elems = [window];break;}
if(sel.gquery!==undefined){elems = sel;break;}
if(Array.isArray(sel)){
sel.map((val)=>{
$.isNode(val) && elems.push(val);
});
} else {elems.push(sel);}
break;
case 'string':
try{elems = document.querySelectorAll(sel);} catch(err){
elems = $.parse.html(sel);
};
break;
}
switch(opts){
case 'push':
let oi = this.length;
for(let i = elems.length-1; i >= 0; i--){
let rp = true;
for(let ii = this.length-1; ii>=0; ii--){
this[ii]===elems[i] && (rp=false);
}
rp && (this.length+=1, this[oi+i] = elems[i]);
}
break;
default:
this.length = elems.length;
for (let i = elems.length - 1; i >= 0; i--) {this[i] = elems[i];}
}
return this;
},
push: function(sel){
return this.init(sel, 'push');
},
each: function(arr, callback){
callback || (callback = arr, arr = this);
for(let i = 0,len = arr.length; i < len; i++){
if(callback.call(arr[i], i, arr[i-1]) == false){break;}
}
return this;
},
/**
* sel CSS3 选择器
* selfContain 查询是否包含自身
* onlyChildren 只查询子元素
*/
find: function(sel, selfContain, onlyChildren){
let finder = Object.create(this), fArr = [], i;
this.each(function(idx){
if(!$.isNode(this)){return;}
selfContain && $(this).is(sel) && fArr.push(this);
let elems = onlyChildren ? this.children : this.querySelectorAll(sel);
for (i = 0; i < elems.length; i++) {
if(onlyChildren && !$(elems[i]).is(sel)){
continue;
}
fArr.push(elems[i]);
}
});
fArr = $.array.unique(fArr);
finder.length = fArr.length;
for (i = fArr.length - 1; i >= 0; i--) {finder[i] = fArr[i];}
return finder;
},
is: function(sel){
let r = false;
this.each(function(idx){
let _mat = (
this.matches || this.matchesSelector ||
this.msMatchesSelector || this.mozMatchesSelector ||
this.webkitMatchesSelector || this.oMatchesSelector
);
if(typeof _mat==='function' && _mat.call(this, sel)){r = true;return false;}
});
return r;
},
exist: function(){
let r = false;
this.each(function(idx){
if(document.body.contains(this)){r = true;return false;}
});
return r;
},
eq: function(idx){return $(this[idx]);},
parent: function(sel, force){
let finder = Object.create(this), fArr = [], i;
this.each(function(idx){
let node = this.parentNode;
while(sel && node && !$(node).is(sel)){
if(!force){
node = undefined;break;
}
node = node.parentNode;
}
node&&fArr.push(node);
});
fArr = $.array.unique(fArr);
finder.length = fArr.length;
for (i = fArr.length - 1; i >= 0; i--) {finder[i] = fArr[i];}
return finder;
},
next: function(sel){
let finder = Object.create(this), fArr = [], elem, i;
this.each(function(idx){
elem = this.nextElementSibling;
if( elem!==null && (!sel || elem.matches(sel)) ){
fArr.push(elem);
}
delete finder[idx];
});
fArr = $.array.unique(fArr);
finder.length = fArr.length;
for (i = fArr.length - 1; i >= 0; i--) {finder[i] = fArr[i];}
return finder;
},
remove: function(sel, onlyChildren){
onlyChildren===undefined && (onlyChildren=false);
let rthis = ( sel === undefined ? this : this.find(sel, false, onlyChildren) );
rthis.each(function(){
if(this.parentNode===null){return true;}
this.parentNode.removeChild(this);
});
return this;
},
empty: function(sel){
let rthis = ( sel === undefined ? this : this.find(sel) );
return rthis.each(function(){
while(this.firstChild){this.removeChild(this.firstChild);}
});
},
text: function(val){
return gqHandle.thvEach.call(this,'innerText',val);
},
html: function(val){
return gqHandle.thvEach.call(this,'innerHTML',val);
},
val: function(val){
return gqHandle.thvEach.call(this,'value',val);
},
ohtml: function(val){
return gqHandle.thvEach.call(this,'outerHTML',val);
},
width: function(val){
if(val!==undefined){
isNaN(val) || (val += 'px');
return this.each(function(){this.style.width = val;});
}
let totalWidth = [], iw;
this.each(function(){
iw = this.offsetWidth;
if(iw===undefined){
iw = typeof this.getBoundingClientRect==='function' ? this.getBoundingClientRect().width.toFixed(2) : 0;
}
totalWidth.push(parseFloat(iw));
});
return (totalWidth.length>1 ? totalWidth : totalWidth[0]);
},
height: function(val){
if(val!==undefined){
isNaN(val) || (val += 'px');
return this.each(function(){this.style.height = val;});
}
let totalHeight = [], ih;
this.each(function(){
ih = this.offsetHeight;
if(ih===undefined){
ih = typeof this.getBoundingClientRect==='function' ? this.getBoundingClientRect().height.toFixed(2) : 0;
}
totalHeight.push(parseFloat(ih));
});
return (totalHeight.length>1 ? totalHeight : totalHeight[0]);
},
offset: function(opts){
if(typeof opts === 'object'){
return this.each(function(){
for(let key in opts){
isNaN(opts[key]) || (opts[key]+='px');
this.style[key] = opts[key];
}
});
}
let rect = this[0].getBoundingClientRect(), spos = {
top: document.body.scrollTop==0?document.documentElement.scrollTop:document.body.scrollTop,
left: document.body.scrollLeft==0?document.documentElement.scrollLeft:document.body.scrollLeft
};
// true
opts && (spos.top=0, spos.left=0);
let $this = $(this[0]);
let data = {
top: rect.top + spos.top,
left: rect.left + spos.left,
height: $this.height(),
width: $this.width(),
};
let wrapH = opts ? window.innerHeight : document.body.offsetHeight,
wrapW = opts ? window.innerWidth : document.body.offsetWidth;
data.bottom = wrapH - data.top - data.height;
data.right = wrapW - data.left - data.width;
return data;
},
append: function(elem){
return gqHandle.pend.call(this, 'appendChild', elem);
},
prepend: function(elem){
return gqHandle.pend.call(this, 'insertBefore', elem);
},
insertBefore: function(elem){
return gqHandle.pend.call($(elem), 'before', this);
},
before: function(elem){
return gqHandle.pend.call(this, 'before', elem);
},
after: function(elem){
return gqHandle.pend.call(this, 'after', elem);
},
attr: function(attrs, val){
if(val === undefined && typeof attrs === 'string') {
let resArr = [], attr;this.each(function(){
attr = this.getAttribute(attrs);attr===null&&(attr=undefined);
resArr.push( attr );
});
return (resArr.length>1 ? resArr : resArr[0]);
}
if(typeof attrs === 'object'){
return this.each(function(){
for(let idx in attrs){
this.setAttribute&&this.setAttribute(idx, attrs[idx]);
}
});
}
return this.each(function(){
this.setAttribute&&this.setAttribute(attrs, val);
});
},
removeAttr: function(attr){
attr = attr.split(' ');
return this.each(function(){
attr.map(v=>this.removeAttribute(v));
});
},
data: function(keys, val){
if(typeof keys !== 'object' && val === undefined) {
let resArr = [];this.each(function(){
this.gQueryData===undefined&&(this.gQueryData={});
typeof keys === 'string' ? resArr.push( this.gQueryData[keys] ) : resArr.push( this.gQueryData );
});
return (resArr.length>1 ? resArr : resArr[0]);
}
return this.each(function(){
this.gQueryData===undefined&&(this.gQueryData={});
if(typeof keys === 'object'){
for(let idx in keys){this.gQueryData[idx] = keys[idx];}
} else {this.gQueryData[keys] = val;}
});
},
removeData: function(key){
return this.each(function(){
this.gQueryData===undefined&&(this.gQueryData={});
delete this.gQueryData[key];
});
},
hasClass: function(cls){
cls = cls.split(' ');
let res = true;
this.each(function(){
cls.map(v=>{
this.classList.contains(v) || (res = false);
});
});
return res;
},
addClass: function(cls){
cls = cls.split(' ');
return this.each(function(){
cls.map(v=>this.classList.add(v));
});
},
removeClass: function(cls){
cls = cls.split(' ');
return this.each(function(){
cls.map(v=>this.classList.remove(v));
});
},
toggleClass: function(cls){
cls = cls.split(' ');
return this.each(function(){
cls.map(v=>this.classList.toggle(v));
});
},
css: function(styles, val){
if(val !== undefined){
return this.each(function(){
setProperty(this, styles, val);
});
}
if(typeof styles === 'string'){
let _css, resArr=[];
this.each(function(){
resArr.push( getComputedStyle(this)[styles] );
});
return (resArr.length>1 ? resArr : resArr[0]);
}
return this.each(function(){
for(let style in styles){
setProperty(this, style, styles[style]);
}
});
function setProperty(obj, prop, val){
let re = /!important\s?$/, thr = '';
re.test(val) && (thr = 'important', val = val.replace(re, ''));
obj.style.setProperty(prop, val, thr);
}
},
show: function(disp){
return this.each(function(){
this.style.display = '';
if(getComputedStyle(this)['display'] == 'none'){
this.style.display = disp||'block';
}
});
},
hide: function(){
return this.each(function(){this.style.display='none'});
},
animate: function(props, opts, callback){
typeof opts === undefined && (opts = 500);
if(typeof opts!=='object'){
opts = {duration:parseInt(opts)};
}
opts.duration || (opts.duration = 500);
opts.timing || (opts.timing = 'linear');
opts.delay || (opts.delay = 0);
return this.each(function(){
let ani = {
elem:this,
callback:callback,
props:props,
calc:$.animation.preCalc(props, this),
opts:opts,
start:null
};
function add(){
$.animation.array.push(ani);
$.animation.start();
};
opts.delay>0 ? setTimeout(add, opts.delay) : add();
});
},
stop: function(){
return this.each(function(){
this.__gQueryStop = true;
});
},
fadeIn: function(dur, callback){
dur || (dur=500);
return this.each(function(){
$(this).show().stop().animate({opacity:1}, dur, function(){
callback && callback.call(this);
});
});
},
fadeOut: function(dur, callback){
dur || (dur=500);
return this.each(function(){
$(this).stop().animate({opacity:0}, dur, function(){
this.style.display = 'none';
callback && callback.call(this);
});
});
},
fadeToggle: function(dur, callback){
dur || (dur=500);
typeof callback === 'function' || (callback=function(){});
return this.each(function(){
this.style.display=='none' ? $(this).fadeIn(dur, callback) : $(this).fadeOut(dur, callback);
});
},
slideUp: function(dur, callback){
dur || (dur=500);
return this.each(function(){
$(this).stop().animate({height:'0px'}, dur, function(){
this.style.display = 'none';
callback && callback.call(this);
});
});
},
slideDown: function(dur, callback){
dur || (dur=500);
return this.each(function(){
let orgHeight = $(this).css('height');
let $that = $(this).css({display:'', height:''});
let newHeight = this.offsetHeight;
this.style.height = orgHeight;
$that.stop().animate({height:newHeight+'px'}, dur, function(){
callback && callback.call(this);
});
});
},
slideToggle: function(dur, callback){
dur || (dur=500);typeof callback === 'function' || (callback=function(){});
return this.each(function(){
this.style.display=='none' ? $(this).slideDown(dur, callback) : $(this).slideUp(dur, callback);
});
},
on: function(evtName, selector, fn, opts){
[evtName, selector, fn, opts] = gqHandle.onArgs(arguments);
// 处理事件委托
if(selector){
opts.capture===undefined&&(opts.capture=true);
}
let appoint = function(inFn, name){
let isMouse = $.array.finder([
// 不包括 mousedown\mouseup\touchstart\touchend
'mouseenter','mouseleave',
'mousemove','mouseover','mouseout'
], name);
return selector ? function(e){
let nodes = this.querySelectorAll(selector),
tgtNode = false, i;
for (i = nodes.length - 1; i >= 0; i--) {
if(tgtNode!==false){break;}
if(isMouse){
nodes[i]===e.target && (tgtNode = nodes[i]);
} else {
nodes[i].contains(e.target) && (tgtNode = nodes[i]);
}
}
tgtNode && ( inFn.call(tgtNode, e) );
} : inFn;
}, cfn;
if(typeof fn === 'function'){
cfn = appoint(fn, evtName);
return this.each(function(){
$.event.add(this, evtName, cfn, opts);
});
}
return this.each(function(){
for(let evt in evtName){
cfn = appoint(evtName[evt], evt);
$.event.add(this, evt, cfn, opts);
}
});
},
one: function(evtName, selector, fn, opts){
[evtName, selector, fn, opts] = gqHandle.onArgs(arguments);
opts.once = true;
return this.on(evtName, selector, fn, opts);
},
off: function(evts, opts){
opts===undefined && (opts = false);
evts || (evts='*');
evts = evts.split(' ');
return this.each(function(){
evts.map(evt=>$.event.remove(this, evt, opts));
});
},
trigger: function(evts, params){
params || (params={});
evts = evts.split(' ');
let ctmEvts = evts.map(name=>new CustomEvent(name, {detail: params}));
return this.each(function(){
ctmEvts.map(evt=>this.dispatchEvent(evt));
});
},
click: function(fn){
if(typeof fn === 'function'){
return this.each(function(){$.event.add(this,'click',fn);});
} else {
return this.trigger('click');
}
},
select: function(){
switch( this[0].tagName.toLowerCase() ){
case 'input':case 'textarea':
this[0].select();break;
default:window.getSelection().selectAllChildren(this[0]);
}
return this;
},
/**
* load('https://gquery.cn #hero')
*/
load: function(url, data, func){
let _this = this, up = url.trim().split(' ');
typeof data === 'function' && (func=data, data=false);
$.fetch(up[0], data, 'text').then(function(resp){
if(up.length>1){
up.splice(0, 1);
_this.html( $(resp).find(up.join(' '), true).html() );
} else {
_this.html( resp );
}
typeof func === 'function' && func.call( _this );
});
},
extend: function(obj){
for(let idx in obj){
typeof obj[idx] === "object" ?
this[idx] = $.extend(true, this[idx], obj[idx]) :
this[idx] = obj[idx];
}
return this;
}
};
gQuery.fn.init.prototype = gQuery.fn;
let gqHandle = {
thvEach: function(prop, val){
let isArr = Array.isArray(val);
if(val === undefined || (isArr && val.length==0) ) {
let resArr = [];
this.each(function(){
resArr.push(this[prop]);
});
return (isArr ? resArr : resArr.join(''));
}
return isArr ? this.each(function(idx){
this[prop] = val[idx];
}) : this.each(function(){this[prop] = val;});
},
pend: function(prop, elem){
let elems = typeof elem === 'string' ? $.parse.html(elem) : (
elem.gquery ? elem : [elem]
);
return this.each(function(){
let elen = elems.length, i, el;
for(i = 0; i < elen; i++){
el = elems[i].cloneNode(true);
prop=='insertBefore' ? this[prop](el, this.firstChild) : this[prop](el);
}
});
},
onArgs: function(args){
let evtName = args[0], selector = args[1], fn = args[2], opts = args[3];
(args.length==3 && typeof fn !== 'function') && (opts = fn,fn = selector,selector = false);
if(args.length==2){
if(typeof selector === 'function'){
fn = selector, selector = false;
} else if(typeof selector === 'object'){opts = selector, selector = false;}
}
typeof opts === 'object' || (opts={});
return [evtName, selector, fn, opts];
}
};
/**
* gQuery wait
* @author Matthew Lee [email protected]
* @author Ganxiaozhe [email protected]
*/
function gQueryDummy($real, delay, _fncQueue){
let dummy = this;
this._fncQueue = (typeof _fncQueue === 'undefined') ? [] : _fncQueue;
this._delayCompleted = false;
this._$real = $real;
// 1: $(sel).fn().wait(ms).fn();
// 2: $(sel).fn().wait(promise).fn();
// 3: $(sel).fn().wait(event).fn();
if (typeof delay === 'number' && delay >= 0 && delay < Infinity){
this.timeoutKey = window.setTimeout(function(){
dummy._performDummyQueueActions();
}, delay);
} else if (delay !== null && typeof delay === 'object' && typeof delay.promise === 'function'){
delay.then(function(){
dummy._performDummyQueueActions();
});
} else if (typeof delay === 'string'){
$real.one(delay, function(){
dummy._performDummyQueueActions();
});
} else return $real;
}
gQueryDummy.prototype._addToQueue = function(fnc, arg){
// 当影子函数被调用时,将函数名称及参数放入队列以便稍后执行
this._fncQueue.unshift({fnc: fnc, arg: arg});
if (this._delayCompleted){
return this._performDummyQueueActions();
} else {return this;}
};
gQueryDummy.prototype._performDummyQueueActions = function(){
// 列队操作
// 若遇到另一个 “wait”,则将剩余堆栈传递给新的 gQueryDummy
this._delayCompleted = true;
let next;
while (this._fncQueue.length > 0){
next = this._fncQueue.pop();
if (next.fnc === 'wait') {
next.arg.push(this._fncQueue);
return this._$real = this._$real[next.fnc].apply(this._$real, next.arg);
}
this._$real = this._$real[next.fnc].apply(this._$real, next.arg);
}
return this;
};
gQuery.fn.wait = function(delay, _queue){
return new gQueryDummy(this, delay, _queue);
};
gQuery.waitUpdate = function(){
// 为 gQueryDummy 添加 gQuery 的所有的方法
// 跳过非函数方法和 Object.prototype
for (let fnc in gQuery.fn){
if (typeof gQuery.fn[fnc] !== 'function' || !gQuery.fn.hasOwnProperty(fnc)){
continue;
}
gQueryDummy.prototype[fnc] = (function(fnc){
return function(){
let arg = Array.prototype.slice.call(arguments);
return this._addToQueue(fnc, arg);
};
})(fnc);
};
};
gQuery.waitUpdate();
gQuery.debugger = false;
/* -------------------------------------
* gQuery - extend
* ------------------------------------- */
gQuery.extend = function(obj){
if(arguments.length==1){
for(let idx in obj){
typeof obj[idx] === "object" ?
this[idx] = $.extend(true, this[idx], obj[idx]) :
this[idx] = obj[idx];
}
gQuery.waitUpdate();
return this;
}
let deep = false, length = arguments.length, i = 1,
name, options, src, copy, clone, copyIsArray,
target = arguments[0] || {};
if (typeof target == 'boolean') {
deep = target;target = arguments[i] || {};i++;
}
if (typeof target !== "object") {target = {};}
for (; i < length; i++) {
options = arguments[i];
if(options == null){continue;}
for (name in options) {
src = target[name];
copy = options[name];
// 解决循环引用
if (target === copy) {continue;}
// 要递归的对象必须是 plainObject 或者数组
if ( deep && copy && (gQuery.isPlainObject(copy) || (copyIsArray = Array.isArray(copy))) ) {
// 要复制的对象属性值类型需要与目标属性值相同
if (copyIsArray) {
copyIsArray = false;
clone = src && Array.isArray(src) ? src : [];
} else {
clone = src && gQuery.isPlainObject(src) ? src : {};
}
target[name] = gQuery.extend(deep, clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
return target;
};
gQuery.each = function(object, callback){
if(typeof object === 'object'){
for(let i in object){
if(callback.call(object[i], i, object[i]) === false){
break;
}
}
return true;
}
[].every.call(object, function(v, i){
return callback.call(v, i, v) === false ? false : true;
});
};
gQuery.copy = function(str){
if(typeof str==='object'){str = $(str).text();}
$('body').append("<textarea id='gQuery-copyTemp'>"+str+"</textarea>");
$('#gQuery-copyTemp').select();document.execCommand("Copy");
$('#gQuery-copyTemp').remove();
};
/**
* Fetch
* @url 传入请求地址:String或请求实例:Object
* @data 传入 String 或 Object
* @bodyMH 对 Response 的解析方法:String
*/
gQuery.fetch = function(url, data, bodyMH){
let _mh = {method: 'GET'};
typeof url === 'object' && (_mh = $.extend(_mh, url), url = url.url);
if(typeof data === 'object'){
_mh.method = 'POST';
if(Object.prototype.toString.call(data)=='[object FormData]'){
_mh.body = data;
} else {
_mh.body = new FormData();
for(let nm in data){_mh.body.append(nm, data[nm]);}
}
} else if(typeof data === 'string'){
typeof bodyMH === 'string' ? (_mh.method = 'POST', _mh.body = data) : (bodyMH = data);
}
if(!bodyMH){return fetch(url, _mh);}
let copyRes;
return fetch(url, _mh).then(res => {
if(!res.ok){throw new Error('Network response was not ok.');}
gQuery.debugger && (copyRes = res.clone());
return res[bodyMH]();
}).catch(err => {
if(copyRes && copyRes.ok){
copyRes.text().then(rsp=>{
console.error(rsp);
});
}
throw new Error(err);
});
};
gQuery.global = (typeof window !== 'undefined' ? window : global);
gQuery.isWindow = function(obj){
return Object.prototype.toString.call(obj)==='[object Window]';
};
gQuery.isNode = function(obj){
let str = Object.prototype.toString.call(obj);
return (str.indexOf('HTML')>-1 && str.indexOf('Element')>-1) ? true : false;
};
gQuery.isPlainObject = function(obj){
let prototype;
return Object.prototype.toString.call(obj) === '[object Object]'
&& (prototype = Object.getPrototypeOf(obj), prototype === null ||
prototype == Object.getPrototypeOf({}));
};
gQuery.ui = "Missing gQuery UI components.";
/** -------------------------------------
* Array
* ------------------------------------- */
gQuery.array = {
unique: function(arr, typ){
let j = {};
if( typ=='node' || $.isNode(arr[0]) ){
return arr.filter(function(item, index, arr) {
return arr.indexOf(item, 0) === index;
});
}
arr.forEach(function(v){
let vtyp = typeof v, vv=v;
if(vtyp==='object'){v = JSON.stringify(v);}
j[v + '::' + vtyp] = vv;
});
return Object.keys(j).map(function(v){return j[v];});
},
finder: function(arr, finder, opts){
typeof opts === 'object' || (opts = {});
opts.limit === undefined && (opts.limit=1);
let isObj = (typeof finder === 'object'), resame, resArr = [];
for (let i = 0; i < arr.length; i++) {
if(isObj){
resame = true;
for(let obj in finder){
arr[i][obj]==finder[obj] || (resame = false);
}
resame && resArr.push( {index:i, array:arr[i]} );
if(opts.limit>0 && resArr.length>=opts.limit){break;}
} else {
arr[i]==finder && resArr.push( {index:i,array:arr[i]} );
if(opts.limit>0 && resArr.length>=opts.limit){break;}
}
}
if(opts.array){return resArr;}
return resArr.length>1 ? resArr : resArr[0];
},
has: function(arr, finder){
let fdr = this.finder(arr, finder);
return fdr!==undefined;
}
};
gQuery.event = {
add: function(obj, name, fn, opts){
typeof opts === 'object' || (opts={});
opts.capture === undefined && (opts.capture=false);
let flag = name.split('.');
let evtName = flag.splice(0,1);
flag.length>0 && (opts.__flag = {});
flag.map(f=>{opts.__flag[f]=true;});
let fnDummy = function(e){
if(opts.once===true){
$.event.remove(obj, name, opts);
}
return fn.call(obj, e);
};
let events = obj.gQueryEvents, evtObj = {fn:fnDummy, opts:opts};
if(events===undefined){
events = {[evtName]:[ evtObj ]};
} else {
if(typeof events[evtName] !== 'object'){
events[evtName] = [evtObj];
} else {
events[evtName].push(evtObj);
}
}
obj.gQueryEvents = events;
let event = events[evtName][ events[evtName].length-1 ];
obj.addEventListener(evtName, event.fn, event.opts);
},
remove: function(obj, evtName, opts){
let events = obj.gQueryEvents, flag = evtName.split('.'), i;
evtName = flag.splice(0, 1);
if(events===undefined){return;}
if(evtName=='*'){
Object.keys(events).map(evt=>revent(evt, true));return;
}
if(typeof events[evtName]!=='object'){return;}
revent(evtName);
function revent(evt, forceFilter){
let fns = events[evt];
for (i = fns.length - 1; i >= 0; i--) {
/*
* 默认过滤状态;
* 传入 flag 时,过滤无 flag 的对象,留下有 flag 的对象;
* 未传入 flag 时,过滤有 flag 的对象,留下无 flag 的对象。
*/
if(!forceFilter){
let filter=1, flagO=fns[i].opts.__flag || {};
if(flag.length<1 && Object.keys(flagO).length<1){filter=0;}
flag.map(f=>{flagO[f] && (filter=0);});
if(filter){continue;}
}
obj.removeEventListener(evt, fns[i].fn, fns[i].opts);
events[evt].splice(i, 1);
events[evt].length<1 && (delete events[evt]);
}
}
}
};
/** -------------------------------------
* Get
* ------------------------------------- */
gQuery.get = {
browserSpec: function(){
let ua = navigator.userAgent, tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];