forked from jikeytang/jikeytang.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjing.js
1515 lines (1314 loc) · 45.8 KB
/
jing.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
/**
* @author: 豪情
* @see: <a href="mailto:jikeytang@gmail.com">豪情</a>
* @time: 2014-4-19 下午5:45
* @info:
*/
;(function(win, undefined){
var document = win.document,
loc = win.location,
docEle = document.documentElement,
arr = [],
slice = arr.slice,
concat = arr.concat,
push = arr.push,
indexOf = arr.indexOf,// js 1.6新增加的方法
version = '1.0',
class2type = {},
toString = class2type.toString,
hasOwn = class2type.hasOwnProperty,
isIE6 = !win.XMLHttpRequest && !win.opera,
support = {},
_ = { // 私有方法集合
isArraylike : function(obj){
var length = obj.length,
type = $.type(obj);
if(type == 'function' && $.isWindow(obj)){
return false;
}
if(obj.nodeType === 1 && length){
return true;
}
// http://bbs.csdn.net/topics/390413500
return type === 'array' || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj;
}
};
win.$ = win.Jing = $ = function(selector, context){ // 力量的源泉源
return new $.fn.init(selector, context);
}
$.fn = $.prototype = {
jing : version,
constructor : $,
sort : arr.sort,
push : push,
splice : arr.splice,
context : null,
selector : '',
length : 0,
init : function(selector, context){
var obj = null,
context = context || document,
select = '';
// $(''), $(null), $(undefined), $(false)
if(!selector){
return this;
}
// HTML strings
if(typeof selector === 'string'){
if(selector.charAt(0) === '<' && selector.charAt(selector.length - 1) === '>' && selector.length >= 3){
// 不能处理多标签的情况,如<p><span></span></p>
// return this.setArray([context.createElement(selector.slice(1, -2))]);
var div = document.createElement('div');
div.innerHTML = selector;
return this.setArray(this.makeArray(div.childNodes));
}
} else if(selector.nodeType || selector == win){ // $(window,document,document.body,tag)
this.context = this[0] = selector;
this.selector = selector;
this.length = 1;
return this;
}
selector = selector.toString();
// $("#id")
if(selector.indexOf('#') == 0){
obj = context.getElementById(selector.slice(1));
} else if(selector.indexOf('.') == 0){ // $(".className")
obj = this.getElementsByClassName(context, selector.slice(1));
} else { // $("tagName")
obj = context.getElementsByTagName(selector);
}
this.context = document;
this.selector = selector;
return this.setArray(this.makeArray(obj));
},
toArray : function(){
return slice.call(this);
},
/**
* 如果num为负则返回this.length+num值;如果num为空,直接返回一个数组元素
* @param num
*/
get : function(num){
return num != null ? (num < 0 ? this[num + this.length] : this[num]) : slice.call(this);
},
pushStack : function(elems){
var ret = $.merge(this.constructor(), elems);
ret.prevObject = this;
ret.context = this.context;
return ret;
},
setArray : function(obj){
this.length = 0;
Array.prototype.push.apply(this, obj);
return this;
},
// 把传入参数变成数组
makeArray : function(arr) {
var ret = [];
if( arr != null ){
var i = arr.length;
// 单个元素,但window, string、 function有 'length'的属性,加其它的判断
if (i == null || arr.split || arr.setInterval || arr.call) {
ret[0] = arr;
} else {
try {
ret = Array.prototype.slice.call(arr);
} catch (e) {
while (i) ret[--i] = arr[i]; // Clone数组
}
}
}
return ret;
},
getElementsByClassName : function(context, name){
if(context.getElementsByClassName){
return context.getElementsByClassName(name);
} else {
var i = 0,
res = [],
child = null,
len = all.length,
all = context.all,
regex = new RegExp('(^|\\s)' + name + '(\\s|$)');
for( ; i < len; i++){
child = all[i];
if(regex.test(child.className)){
res.push(child);
}
}
return res;
}
},
each : function(callback, args){
return $.each(this, callback, args);
},
map : function(callback){
return this.pushStack($.map(this, function(elem, i){
return callback.call(elem, i, elem);
}));
},
slice : function(){
return this.pushStack(slice.apply(this, arguments));
},
first : function(){
return this.eq(0);
},
last : function(){
return this.eq(-1);
},
eq : function(i){
var len = this.length,
j = +i + (i < 0 ? len : 0);
return this.pushStack(j >= 0 && j < len ? [this[j]] : []);
},
end : function(){
return this.prevObject || this.constructor(null);
}
}
$.fn.init.prototype = $.fn;
$.extend = $.fn.extend = function(){
var target = arguments[0] || {},
length = arguments.length,
options,
name,
i = 1;
if(length == i){
target = this;
--i;
}
for( ; i < length; i++){
if((options = arguments[i]) != null){
for(name in options){
if(options[name] != undefined){
target[name] = options[name];
}
}
}
}
return target;
}
var rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi;
_.fcamelCase = function(all, letter){
return letter.toUpperCase();
}
// 常用工具方法
$.extend({
// 非重复标志位
expando : 'Jing' + (version + Math.random()).replace(/\D/g, ''),
// 空函数快捷方式
noop : function(){},
isFunction : function(obj){
return $.type(obj) === 'function';
},
isArray : Array.isArray || function(obj){
return $.type(obj) === 'array';
},
// 是否为窗口
isWindow : function(obj){
return obj != null && obj == obj.window;
},
trim : function(text){
return text.replace(/^\s+|\s+$/g, '');
},
type : function(obj){
return typeof obj === 'object' || typeof obj === 'function' ? class2type[toString.call(obj)] || 'object' : typeof obj;
},
each : function(obj, callback){
var value,
i = 0,
length = obj.length,
isArray = _.isArraylike(obj);
if(isArray){ // 如果是数组
for( ; i < length; i++){
value = callback.call(obj[i], i, obj[i]);
if(value === false){
break;
}
}
} else { // 如果不是数组
for(i in obj){
value = callback.call(obj[i], i, obj[i]);
if(value === false){
break;
}
}
}
return obj;
},
makeArray : function(arr, results){
var ret = results || [];
if(arr != null){
if(_.isArraylike(Object(arr))){
$.merge(ret,
typeof arr === 'string' ?
[arr] : arr
);
} else {
push.call(ret, arr);
}
}
return ret;
},
/**
* 确定elem在数组中的位置,从0开始计数(如果没有找到则返回 -1 )。
* @param elem
* @param arr
* @param i 开始位置
*/
inArray : function(elem, arr, i){
var len;
if(arr){
if(indexOf){
return indexOf.call(elem, arr, i);
}
len = arr.length;
i = i ? i < 0 ? Math.max(0, len + i) : i : 0;
for( ; i < len; i++){
if(i in arr && arr[i] == elem){
return i;
}
}
}
return -1;
},
/**
* 合并两个参数
* @param first
* @param end
*/
merge : function(first, second){
var len = second.length,
i = first.length,
j = 0;
while(j < len){
first[i++] = second[j++];
}
first.length = i;
return first;
},
map : function(elems, callback, arg){
var i = 0,
value,
ret = [],
length = elems.length,
isArray = _.isArraylike(elems);
if(isArray){
for( ; i < length; i++){
value = callback(elems[i], i, arg);
if(value != null){
ret.push(value);
}
}
} else {
for(i in elems){
value = callback(elems[i], i, arg);
if(value != null){
ret.push(value);
}
}
}
return concat.apply([], ret);
},
clone : function(elem, dataAndEvents){
var clone;
clone = elem.cloneNode(true);
return clone;
},
/**
* 加强版文档碎片处理
* @param elems 要添加的元素
* @param context 文档上下文
* @param scripts 是否有script
* @param selection
* @returns {*|DocumentFragment}
*/
buildFragment : function(elems, context, scripts, selection){
var i = 0,
nodes = [],
elem = null,
l = elems.length,
safe = document.createDocumentFragment();
for( ; i < l; i++){
elem = elems[i];
$.merge(nodes, elem);
}
i = 0;
while((elem = nodes[i++])){
safe.appendChild(elem);
}
return safe;
},
cleanData : function(elems, acceptData){
},
/**
* 中划线转为大写
* @param string
* @returns {XML|string}
*/
camelCase : function(string){
return string.replace(rmsPrefix, 'ms-').replace(rdashAlpha, _.fcamelCase);
},
// 全局计数器对象
guid : 1,
now : function(){
return +(new Date());
},
support : support
});
/**
* 多功能处理函数,setter,getter混合处理
* @param elems
* @param fn
* @param key
* @param value
* @param chainable
* @param emptyGet
* @param raw // value值是否为函数的开关。如果是值,直接传递;如果是函数,则回调
* @returns {*}
*/
_.access = function(elems, fn, key, value, chainable, emptyGet, raw){
var i = 0,
length = elems.length,
bulk = key == null;
if($.type(key) === 'object'){ // 传递参数如{ color : 'red', fontSize : '20px' }
chainable = true;
for(i in key){
_.access(elems, fn, i, key[i], true, emptyGet, raw);
}
} else if(value !== undefined){ // 设置data,即set
chainable = true;
if(!$.isFunction(value)){
raw = true; // value不是函数,则raw设为true
}
if(bulk){ // data操作时必然true
}
if(fn){ // fn存在,开始正式执行fn
for(; i < length; i++){
fn(elems[i], key, raw ? value : value.call(elems[i], i, fn(elems[i], key)));
}
}
}
return chainable ? elems : bulk ? fn.call(elems) : length ? fn(elems[0], key) : emptyGet;
}
// DOM常规操作
$.fn.extend({
append : function(){
return this.domManip(arguments, function(elem){
if(this.nodeType == 1 || this.nodeType == 11 || this.nodeType == 9){
this.appendChild(elem);
}
});
},
prepend : function(){
return this.domManip(arguments, function(elem){
if(this.nodeType == 1 || this.nodeType == 11 || this.nodeType == 9){
this.insertBefore(elem, this.firstChild);
}
});
},
before : function(){
return this.domManip(arguments, function(elem){
if(this.parentNode){
this.parentNode.insertBefore(elem, this);
}
});
},
after : function(){
return this.domManip(arguments, function(elem){
if(this.parentNode){
this.parentNode.insertBefore(elem, this.nextSibling);
}
});
},
/**
* dom处理,将args转换为dom元素,并放在一个文档碎片中,
* 执行callback,实现真正的回调插入操作
* @param args
* @param callback
*/
domManip : function(args, callback){
args = concat.apply([], args);
var i = 0,
l = this.length,
fragment,
first,
node;
if(l){
fragment = $.buildFragment(args, this[0].ownerDocument, false, this);
first = fragment.firstChild;
/*var div = document.createElement('div');
div.innerHTML = (args.length === 1 ? args.get() : args);
node = div.childNodes[0];
div = null;*/
for( ; i < l; i++){
callback.call(this[i], first, i);
}
}
return this;
},
clone : function(){
return this.map(function(){
return $.clone(this);
});
},
empty : function(){
var i = 0,
elem;
for( ; (elem = this[i]) != null; i++){
while(elem.firstChild){
elem.removeChild(elem.firstChild);
}
}
return this;
},
html : function(value){
if(value === undefined){
return this[0] && this[0].nodeType === 1 ? $.trim(this[0].innerHTML) : null;
} else if(typeof value === 'string') {
try{
for(var i = 0, l = this.length; i < l; i++){
if(this[i].nodeType === 1){
this[i].innerHTML = value;
}
}
} catch(e) {
this.empty().append(value);
}
} else {
this.empty().append(value);
}
return this;
}
});
// dom反向操作
$.each({
appendTo : 'append', prependTo : 'prepend',
insertBefore : 'before', insertAfter : 'after', replaceAll : 'replaceWith'
}, function(name, original){
$.fn[name] = function(selector){
var i = 0,
ret = [],
elem = null,
insert = $(selector),
last = insert.length - 1;
for( ; i <= last; i++){
elem = i === last ? this : this.clone();
$(insert[i])[original](elem);
push.apply(ret, elem.get());
}
return this.pushStack(ret);
}
});
// 'Boolean Number String Function Array Date RegExp Object Error'.split(' ')
$.each(['Boolean', 'Number', 'String', 'Function', 'Array', 'Date', 'RegExp', 'Object', 'Error'], function(i, name){
class2type['[object ' + name + ']'] = name.toLowerCase();
});
// className 相关操作
var rnotwhite = (/\S+/g);
$.fn.extend({
addClass : function(value){
var i = 0,
j,
cur = '',
elem = null,
classes = [],
clazz = '',
len = this.length,
finalValue = '',
proceed = typeof value === 'string' && value;
if(proceed){
classes = value.match(rnotwhite);
for( ; i < len; i++){
elem = this[i];
cur = elem.nodeType === 1 && (elem.className ? ' ' + elem.className + ' ' : ' ');
if(cur){
j = 0;
while((clazz = classes[j++])){
if(cur.indexOf(' ' + clazz + ' ') < 0){
cur += clazz + ' ';
}
}
finalValue = $.trim(cur);
if(elem.className !== finalValue){ // 为了防止重绘
elem.className = finalValue;
}
}
}
}
return this;
},
removeClass : function(value){
var i = 0,
j,
cur = '',
elem = null,
classes = [],
clazz = '',
len = this.length,
finalValue = '',
proceed = arguments.length === 0 || typeof value === 'string' && value;
if(proceed){
classes = value.match(rnotwhite);
for( ; i < len; i++){
elem = this[i];
cur = elem.nodeType === 1 && (elem.className ? ' ' + elem.className + ' ' : ' ');
if(cur){
j = 0;
while((clazz = classes[j++])){
while(cur.indexOf(' ' + clazz + ' ') >= 0){
cur = cur.replace(' ' + clazz + ' ', ' ');
}
}
finalValue = value ? $.trim(cur) : '';
if(elem.className !== finalValue){ // 为了防止重绘
elem.className = finalValue;
}
}
}
}
return this;
},
toggleClass : function(){
},
hasClass : function(selector){
var i = 0,
className = ' ' + selector + ' ',
l = this.length;
for( ; i < l; i++){
if(this[i].nodeType === 1 && (' ' + this[i].className + ' ').indexOf(className) >= 0){
return true;
}
}
return false;
}
});
// 浏览器探测基本方法
_.uaMatch = function(ua){
ua = ua.toLowerCase();
var match = /(chrome)[\/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec( ua ) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
/(msie) ([\w.]+)/.exec( ua ) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) || [];
return {
browser : match[1] || '',
version : match[2] || '0'
}
}
var browser = {},
matched = _.uaMatch(navigator.userAgent);
if(matched.browser){
browser[matched.browser] = true;
browser.version = matched.version
}
if(browser.chrome){
browser.webkit = true;
} else if(browser.webkit){
browser.safari = true;
}
$.browser = browser;
var rposition = /^(top|right|bottom|left)$/;
if(win.getComputedStyle){
_.getStyles = function(elem){
return elem.ownerDocument.defaultView.getComputedStyle(elem, null);
}
_.curCSS = function(elem, name, computed){
}
} else {
}
_.vendorPropName = function(style, name){
if(name in style){
return name;
}
}
// css静态方法
$.extend({
cssHooks : {
opacity : {
get : function(elem, computed){
if(computed){
var ret = _.curCSS(elem, 'opacity');
return ret === '' ? '1' : ret;
}
}
}
},
// 一个常量值,以下所有常量列表中是不需要自动添加'px'单位
cssNumber : {
'columnCount' : true,
'fillOpacity' : true,
'fontWeight' : true,
'lineHeight' : true,
'opacity' : true,
'order' : true,
'orphans' : true,
'windows' : true,
'zIndex' : true,
'zoom' : true
},
cssProps : {
'float' : support.cssFloat ? 'cssFloat' : 'styleFloat'
},
style : function(elem, name, value, extra){
// 忽略不存在的节点,文本节点,注释节点
if(!elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style){
return ;
}
var ret,
type,
hooks,
style = elem.style,
origName = $.camelCase(name);
name = $.cssProps[origName] || ($.cssProps[origName] = _.vendorPropName(style, origName));
hooks = $.cssHooks[name] || $.cssHooks[origName];
if(value != undefined){
type = typeof value;
if(type === 'number' && !$.cssNumber[origName]){
value += 'px';
}
if(!hooks || !('set' in hooks) || (value = hooks.set(elem, value, extra)) != undefined){
try{
style[name] = '';
style[name] = value;
} catch(e) {
}
}
} else {
if(!hooks || !('get' in hooks) || (value = hooks.get(elem, value, extra)) != undefined){
return ret;
}
return style[name];
}
},
css : function(elem, name, extra, styles){
var num,
hooks,
val,
style = elem.style,
origName = $.camelCase(name);
name = $.cssProps[origName] || ($.cssProps[origName] = _.vendorPropName(style, origName));
hooks = $.cssHooks[name] || $.cssHooks[origName];
if(hooks && 'get' in hooks){
val = hooks.get(elem, true, extra);
}
if(val === undefined){
val = _.curCSS(elem, name, styles);
}
return val;
}
});
$.each(['height', 'width'], function(i, name){
$.cssHooks[name] = {
get : function(elem, computed, extra){
if(computed){
return elem['offset' + name.charAt(0).toUpperCase() + name.slice(1)];
}
},
set : function(elem, value, extra){
return value;
}
}
});
_.isHidden = function(elem, el){
elem = el || elem;
return $.css(elem, 'display') === 'none';
}
// css原型方法
$.fn.extend({
css : function(name, value){
return _.access(this, function(elem, name, value){
var i = 0,
len;
return value !== undefined ? $.style(elem, name, value) : $.css(elem, name);
}, name, value, arguments.length > 1);
},
show : function(){
},
hide : function(){
},
toggle : function(){
}
});
// 检测相关
(function(){
var a = null,
div = document.createElement('div');
div.innerHTML = '<a href="a">a</a>';
a = div.getElementsByTagName('a')[0];
a.style.cssText = 'float:left;opacity:.5;';
support.cssFloat = !!a.style.cssFloat;
a = div = null;
}());
// 尺寸大小
$.each({ Height : 'height', Width : 'width' }, function(name, type){
$.each({ padding : 'inner' + name, content : type, '' : 'outer'+ name }, function(defaultExtra, funcName){
$.fn[funcName] = function(margin, value){
var chainable = arguments.length && (defaultExtra || typeof margin != 'boolean'),
extra = defaultExtra || (margin === true || value === true ? 'margin' : 'border');
return _.access(this, function(elem, type, value){
var doc;
if($.isWindow(elem)){
return elem.document.documentElement['client' + name];
}
if(elem.nodeType === 9){
doc = elem.documentElement;
return Math.max(elem.body['scroll' + name], doc['scroll' + name], elem.body['offset' + name], doc['offset' + name], doc['client' + name]);
}
return value === undefined ? $.css(elem, type, extra) : $.style(elem, type, value, extra);
}, type, chainable ? margin : undefined, chainable, null);
}
});
});
var strundefined = typeof undefined;
var rformElems = /^(?:input|select|textarea)$/i,
rkeyEvent = /^key/,
rmouseEvent = /^(?:mouse|contextmenu)|click/,
rfocusMorph = /^(?:focusinfocus|focusoutblur)$/,
rtypenamespace = /^([^.]*)(?:\.(.+)|)$/;
// 事件
$.event = {
global : {},
/**
* 给选中元素注册事件处理程序
* @param elem
* @param types
* @param hanlder
* @param data
* @param selector
*/
add : function(elem, types, handler, data, selector){
var t,
tmp,
type,
events,
handlers,
origType,
handleObj,
namespaces,
handleObjIn,
elemData = $._data(elem), // 1. 在$.cahce缓存中获取存储的事件句柄对象,如果没就新建elemData
eventHandle; // 不仅仅只是只是充当一个回调函数的角色,而是一个实现了EventListener接口的对象
if(handler.handler){
handleObjIn = handler;
handler = handleObjIn.handler;
selector = handleObjIn.selector;
}
// 第二步:创建编号
if(!handler.guid){
// 用来寻找或者删除handler,因为这个东东是缓存在缓存对象上的,没有直接跟元素节点发生关联
handler.guid = $.guid++;
}
if(!(events = elemData.events)){
events = elemData.events = {};
}
// 第三步:分解事件名与句柄
if(!(eventHandle = elemData.handle)){
eventHandle = elemData.handle = function(e){
return typeof $ !== strundefined && (!e || $.event.triggered !== e.type) ?
$.event.dispatch.apply(eventHandle.elem, arguments) :
undefined;
}
eventHandle.elem = elem;
}
// 第四步: 填充事件名与事件句柄
types = (types || '').match(rnotwhite) || [''];
t = types.length;
while(t--){
tmp = rtypenamespace.exec(types[t]) || [];
type = origType = tmp[1];
namespaces = (tmp[2] || '').split('.').sort();
if(!type){
continue;
}
handleObj = $.extend({
type : type,
origType : origType,
data : data,
handler : handler,
guid : handler.guid,
selector : selector,
needsContext : selector,
namespace : namespaces.join('.')
}, handleObjIn);
if(!(handlers = events[type])){
handlers = events[type] = [];
handlers.delegateCount = 0;
if(elem.addEventListener){
elem.addEventListener(type, eventHandle, false);
} else if(elem.attachEvent){
elem.attachEvent('on' + type, eventHandle);
}
}
if(selector){
handlers.splice(handlers.delegateCount++, 0, handleObj);
} else {
handlers.push(handleObj);
}
// 表示事件曾经使用过,用于事件优化
$.event.global[type] = type;
}
elem = null;
},
remove : function(){
},
trigger : function(){
},
/**
* 分派(执行)事件处理函数
*/
dispatch : function(event){
var i,
j,
args = slice.call(arguments),
handlerQueue = [],
handlers = ($._data(this, 'events') || {})[event.type] || [],
handleObj;
args[0] = event;
event.delegateTarget = this;
handlerQueue = $.event.handlers.call(this, event, handlers);
handlers[0].handler.apply(handlerQueue[0].elem, args);
},