-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.js
2365 lines (1768 loc) · 47.3 KB
/
C.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
/*
* Cxy JavaScript Library
* Create time 2011-05-15 9:28
* Copyright (c) 2011 design by cxy.
* mysite http://www.jdoi.net/
*/
/*
* @progress
* 1.0.3 - 20120904 - 添加命名空间 request; 原有的ajax, getJ, get, getX, post 移植到 request上
* 2.0.0 - 20130124 - 基本重构 C ,引入 sizzle, 逐渐组件化
* 20130211 - 内部不再做dom节点查找,所有传递均采用 Sizzle查找的dom对象
* 20130322 - 修改 include 方法,支持异步/同步加载,去除 use 方法,去除 plugin 属性
* 文件加载不再以功能模块为单位,以文件为单位!
* 修复相同文件同时加载造成的重载问题
* 20130410 - lazy.include - rCacheIndex bug 修复
* 20130417 - C.DIFF 增加MouseWheel兼容'DOMMouseScroll' and 'mousewheel'
*/
/**
* to do list...
* #fix - 1294 IE8出问题
* #fix - live evt.target || evt.srcElement 存在bug
* #add - include 增加对本地文件检测
*/
(function () {
var C = {},
toString = Object.prototype.toString,
Root = "http://www.jdoi.net/C/",
rTagStyle = /<style.*?>([^<]*)<\/style>/ig,
// regular of query
rQuery = /[\?|&](.*?)=([^&#\\$]*)/g,
// global unique id
Guid = 1,
// document head
dHead = document.getElementsByTagName("head")[0],
// document body
dBody = document.body,
// turn on this , all the console.log will work.
DebugMode = !!1;
/**
* NameSpace 工具集
* 20130130
* .versionComparison
* .isArray
* .each
* .copy
* .queryMap
* .isEmptyObject
*/
C.Util = {
/**
* Function 版本号对比
* @param {string} v1
* @param {string} v2
* v1 > v2 return -1
* v1 = v2 return 0
* v1 < v2 return 1
* 20130213
*/
versionComparison : function ( v1, v2 ) {
var firstArr = v1.split('.'),
lastArr = v2.split('.'),
i = 0,
len = Math.min( firstArr.length, lastArr.length ),
item1,
item2;
for ( ; i < len; i++ ) {
item1 = parseInt(firstArr[i]);
item2 = parseInt(lastArr[i]);
if ( item1 > item2 ) return -1;
if ( item1 < item2 ) return 1;
}
return 0;
},
/**
* Function isArray 是否Array Object
* @param {any type} obj 需要检测的变量
* 20130131
*/
isArray : function ( obj ) {
// 支持 isArray 属性浏览器
return Array.isArray && Array.isArray( obj ) ||
// or
obj.constructor === Array;
},
/**
* Function each : 遍历数组或者对象
* @param {object|array} obj 需要遍历的对象
* @param {function} callback 回调函数
* 20111022
*/
each : function( obj, callback ) {
var i,
len = obj.length;
// Array
if ( C.Util.isArray( obj ) )
for ( i = 0; i < len; i++ )
callback && callback( i, obj[ i ] );
// Object
else
for ( i in obj )
// 防止遍历通过new而获得的属性
if ( obj.hasOwnProperty(i) )
callback && callback( i, obj[ i ] );
},
/**
* Function 数组,对象深度拷贝
* 支持[object Object] [object Array] 两种类型数据合并
* 支持合并无限个参数
* 支持对象类型检测,类型不统一将跳过执行并输出不统一的数据类型
* 支持深度复制
* 重写 (value1, undefined, null, !1, "") | value2 -> value2
* 跳过执行 value1 | (undefined, null, !1, "", value1) -> value1
* 20130127
*/
copy : function () {
var len = arguments.length,
i = 0,
re,
item,
type;
function _ErrorType (key, val) {
console.log( 'You enter the error type of arguments['+ key +']: ' + toString.call(val) );
}
function _deep () {
var ancestor,
target;
if ( (ancestor = arguments[1]) && (target = arguments[0]) ) {
each( ancestor, function ( k, v ) {
// value1 | (undefined, null, !1, "", value1) -> value1
// window || HTMLElement will be skip
if ( !v && target[k] || target[k] === v || v.noteType || v.window == window ) return;
if ( toString.call(v)==="[object Array]" ) {
_deep( target[k] = [], v );
} else if ( toString.call(v)==="[object Object]" ) {
_deep( target[k] = {}, v );
} else target[k] = v;
});
}
}
// 遍历参数
for ( ; i < len; i++ ) {
item = arguments[i];
if ( toString.call(item)==="[object Array]" ) {
//以第一个参数的数据类型为准
if ( !i ) {
re = [];
type = "[object Array]";
}
if ( type !== "[object Array]" ) {
_ErrorType( i, item );
continue;
}
} else if ( toString.call(item)==="[object Object]" ) {
//以第一个参数的数据类型为准
if ( !i ) {
re = {};
type = "[object Object]";
}
if ( type !== "[object Object]" ) {
_ErrorType( i, item );
continue;
}
} else {
_ErrorType( i, item );
if ( !i )
return;
else
continue;
}
_deep(re, item);
}
return re;
},
/**
* Function query to map
* @param [string] url
* 20121113
*/
queryMap : function ( url ) {
var realUrl = url || document.location.href,
map = {};
realUrl.replace( rQuery, function ( a, b, c ) {
b && c && ( map[b] = c );
});
map.hash = document.location.hash.replace(/^#/, "");
return map;
},
/**
* Function isEmptyObject 检测对象是否非空
* @param {string} obj 需检测的对象
* 20120806
*/
isEmptyObject : function ( obj ) {
for ( var i in obj ) {
return !!0;
}
return !!1;
},
/**
* Function queue 简单队列机制
* .add 增加队列
* .execute 执行队列
* 20130405
*/
queue : function () {
var Events = [];
return {
add : function (evt) {
Events[Events.length] = evt;
return this
},
execute : function () {
each( Events, function (index, item) {
item();
});
// clear
Events = [];
}
}
}
}
// quote
var each = C.Util.each;
/**
* NameSpace 信息输出操作
* 20130211
* .log
* .warn
* .error
*/
C.Throw = (function () {
function _print( type, text ) {
DebugMode && !!console && !!console[type] && console[type]('C ' + Number(new Date()) + ': ' + text);
}
return {
log : function ( msg ) {
_print( 'log', msg )
},
warn : function ( msg ) {
_print( 'warn', msg )
},
error : function ( msg ) {
_print( 'error', msg )
}
}
})();
// quote
var log = C.Throw.log,
warn = C.Throw.warn,
error = C.Throw.error;
/**
* Object 浏览器属性检测
* 返回主流浏览器
* 返回渲染核心
* 返回版本号
* 201301
*/
C.Browser = (function () {
var _ua = navigator.userAgent,
_browser = {
ie : /msie\s(\d+\.\d)/gi,
firefox : /firefox\/(\d+\.\d)/gi,
safari : /version\/(\d+\.\d\.\d).*safari/gi,
opera : /opera.*version\/(\d+\.\d+)/gi,
chrome : /chrome\/([^\s]+)/gi
},
_render = {
ie : /msie/gi,
webkit : /webkit/gi,
gecko : /gecko/gi,
opera : /opera/gi
},
_checkUrl = "Browser.json",
_result = {};
for ( var i in _browser )
if ( _browser[i].test( _ua ) ) {
_result[i] = RegExp['$1'];
break;
}
for ( var j in _render )
if ( _render[j].test( _ua ) ) {
_result.render = j;
break;
}
return _result;
})();
/**
* @function 收录不同浏览器下的特殊属性,不断更新
* @create time : 20110930
* @nameSpace : C
*/
C.DIFF = (function(){
var isIe = C.Browser.ie,
isFx = C.Browser.firefox,
ver = parseInt(isIe);
return {
"class" : isIe && ver < 8 ? "className" : "class",
"doi" : isIe ? "readystatechange" : "DOMContentLoaded",
"innerText" : isIe ? "innerText" : "textContent",
"mouseenter" : isIe ? "mouseenter" : "mouseover",
"mouseleave" : isIe ? "mouseleave" : "mouseout",
"MouseWheel" : isFx ? 'DOMMouseScroll' : 'mousewheel'
}
})();
/**
* NameSpace 页面信息
* 20111130
* .info
* .box
*/
C.page = {
/**
* Function 返回页面即时的信息,包括页面高度,宽度,浏览器可见域高度,宽度,页面当前滚动高度
* param [dom] elem对象
* 20111130
* fix : 20120626 - 添加普通标签的信息返回
*/
info : function( elem ) {
var obj = !elem || elem === document ? window : elem;
if ( typeof obj !== 'object' ) return log( 'typeof elem must be object.' );
return obj === window ?
{
//页面高度
PH : window.innerHeight + window.scrollMaxY || document.body.scrollHeight,
//页面宽度
PW : window.innerWidth + window.scrollMaxX || document.body.scrollWidth,
//浏览器可见域高度
WH : document.documentElement.clientHeight,
//浏览器可见域宽度
WW : document.documentElement.clientWidth,
//页面当前滚动高度
ST : document.documentElement.scrollTop || document.body.scrollTop
}
:
{
//容器可见域高度
WH : elem.clientHeight,
//容器可见域宽度
WW : elem.clientWidth,
//容器当前滚动高度
ST : elem.scrollTop
}
},
/**
* Function 返回容器在页面的居中坐标
* param {number} height box高度
* param {number} width box宽度
* param {boolean} fix fixed定位标志
* 20111130
*/
box : function( height, width, fix ) {
var page = C.page.info(),
version = parseInt( C.Browser.ie );
return{
x : ( page.WW - width )/2,
y : ( page.WH - height )/2 + (fix && version < 7 ? 0 : page.ST)
}
}
}
/**
* NameSpace 关于数组的操作
* 20130130
*/
C.Array = {
copy : C.Util.copy,
/**
* Function 返回元素在数组中的位置
* @param {array} arr 数组
* @param {string|object|array|function} item 要判断的元素
* 20130210
*/
indexOf : function ( arr, item ) {
var len = arr.length;
if ( !C.Util.isArray(arr) || !len ) return -1;
if ( !arr.indexOf ) {
while ( len-- )
if ( arr[len] === item )
return len;
return -1;
// 支持 indexOf 属性
} else return arr.indexOf( item );
}
}
/**
* NameSpace 关于对象的操作
* 20130130
*/
C.Object = {
copy : C.Util.copy,
add : function ( obj ) {
}
}
/**
* Function 节点包装,构造函数
* @param {array} elems 节点数组
* 20130205
*/
var _domConstructor = function ( doms ) {
this.doms = doms;
}
// copy property from C._fn
_domConstructor.prototype = C._fn = {};
// 纠正构造函数
_domConstructor.prototype.constructor = _domConstructor;
this.$D = function ( elems ) {
if ( !elems || 'object' !== typeof elems ) return null;
return new _domConstructor( C.Util.isArray(elems) ? elems : [elems] );
};
/**
* Function 节点方法的扩展接口
* @param {String|Object} handle
* @param [Function|Object] fn
* 20130208
*/
C._fn._extend = C.extend = function ( handle, fn ) {
var belong,
property,
i;
// extend to handle
if ( (property = fn) !== undefined ) {
if ( (belong=this[handle]) === undefined ) belong = this[handle] = {};
}
// extent to this
else {
belong = this;
property = handle;
}
if ( toString.call( property ) === "[object Object]" ) {
for ( i in property ) {
if ( property.hasOwnProperty(i) ) {
if (belong[ i ]) log('property "'+ i +'" in "'+ belong +'" has been rewrite.');
belong[ i ] = property[ i ];
}
}
} else this[handle] = property;
}
/**
* NameSpace 关于事件的操作
* 20130130
* .on
* .un
* .stopPropagation
* .parentDefault
*/
C.Event = {
/**
* Function 给指定DOM节点绑定事件监听器
* @param {object dom} elem 节点
* @param {string} type 监听类型
* @param {function} fn 监听器
* @param [object dom] selector 代理节点(使用代理方式绑定)
* 20130210
*/
on : function ( elem, type, fn, selector ) {
var realType = C.DIFF[type] || type,
item,
// 公共的事件监听处理
sm = function( evt ) {
var obj = evt.target || evt.srcElement,
index,
livers;
if ( ( type == "mouseenter" || type == "mouseleave" ) && !C.Browser.ie ) {
var related = evt.relatedTarget,
current = evt.currentTarget;
//console.log( related, current )
// check, come from baidu's tangram
if (
// 如果current和related都是body,contains函数会返回false
related == current ||
// Firefox有时会把XUL元素作为relatedTarget
// 这些元素不能访问parentNode属性
// thanks jquery & mootools
//如果current包含related,说明没有经过current的边界
related &&
( C.Dom.contains( current, related ) || related.prefix == 'xul')
) return;
}
//console.log(Sizzle(selector, elem))
// on live
if ( selector ) {
//log( livers )
// match itself or contains the target
if ( (livers = Sizzle(selector, elem)).length && ((index=C.Array.indexOf(livers, obj))>-1 || (index=__contains(livers, obj))>-1) ) {
fn && fn.call( livers[index], evt );
}
// on bind
} else {
each( elem.Event[realType].fns, function ( key, fn ) {
fn.call( elem, evt );
});
}
};
// 返回目标节点的上级节点的key
// 不包含则返回-1
function __contains ( elems, tag ) {
each( elems, function ( key, val ) {
if ( C.Dom.contains(val, tag) ) return key;
});
return -1;
};
// deal with elem's events
if ( !elem ) return;
if ( !elem.Event ) elem.Event = {};
item = elem.Event[realType];
if ( !item ) {
item = elem.Event[realType] = {};
item.fns = [];
item.realFn = sm;
document.attachEvent ?
elem.attachEvent( 'on'+realType, sm )
:
elem.addEventListener( realType, sm, false );
}
item.fns.push( fn );
},
/**
* Function 给指定DOM节点解除事件监听
* @param {object dom} elem 节点
* @param {string} type 监听类型
* @param [function] fn 监听器
* 20130210
*/
un : function ( elem, type, fn ) {
var realType = C.DIFF[type] || type,
realFn,
Fns;
// remove the listener and distroy it
function _clear ( e ) {
// get the realy function
realFn = e.Event[realType].realFn;
document.detachEvent ?
e.detachEvent( 'on'+realType, realFn )
:
e.removeEventListener( realType, realFn, false );
delete e.Event[realType];
}
if ( elem.Event && (Fns = elem.Event[realType].fns) && !!Fns.length ) {
// no EventListener
if ( !fn ) return _clear( elem );
var index = C.Array.indexOf( Fns, fn );
// can find the listener
index > -1 &&
// remove in the queue of Fns
Fns.splice(index, 1) &&
// then, check the Fns.length
!Fns.length &&
// when Fns.length == 0, goto _clear
_clear( elem );
}
},
/**
* Function 阻止冒泡事件
* @param {event object} e 事件对象
* 20120305
*/
stopPropagation : function ( e ) {
e.stopPropagation ? e.stopPropagation() : e.cancelBubble = !1;
},
/**
* Function 阻止事件默认行为
* @param {event object} e 事件对象
* 20120305
*/
parentDefault : function ( e ) {
e.preventDefault ? e.preventDefault() : e.returnValue = !1;
},
/**
* Function key.on 支持 组合键 (ctrl,shift,alt);
* 支持 同一按键上绑定不同方法,会按照绑定先后顺序而执行;
* @param expression{string} 按键表达式
* @param callback{function} 回调
* 不允许单独绑定 组合键
* ps: 不支持 主要是由于 目前没有这变态的需求;
* 减少逻辑代码量
* use: C.Event.key.on("ctrl + delete", function (){console.log("do it")});
* date: 20130115
* design by J.do
*/
/**
* Function key.un 卸载整个键盘监听事件
* 卸载指定按键的指定事件
* 卸载指定按键的所有监听事件
* @param [string] arguments[0] 按键表达式
* @param [function]arguments[1] 监听器
* use: C.Event.key.un("a", listener);
* C.Event.key.un("a");
* C.Event.key.un();
* 20130218
* design by J.do
*/
key : (function () {
var _FnKey = {
CTRL : "ctrlKey",
ALT : "altKey",
SHIFT : "shiftKey"
},
_CodeMap = {
"ESC" : 27,
"ENTER" : 13,
"SPACE" : 32,
"DELETE" : 46,
"PAGEUP" : 33,
"PAGEDOWN" : 34,
"UP" : 38,
"DOWN" : 40,
"LEFT" : 37,
"RIGHT" : 39
},
_isPressFnKey = function (e) {
for ( var i in _FnKey )
if ( e[_FnKey[i]] ) return !0;
return !1;
},
_Condition = function ( exps ) {
// Fn + ~
if ( /\+/g.test(exps) ) {
return exps
.replace( "+", "&&")
.replace( /(\w+)/g, function ( a ) {
if ( a ) {
if ( _FnKey[a] ) return "e." + _FnKey[a];
return _CodeMap[a] ?
"e.keyCode === " + _CodeMap[a]
:
"String.fromCharCode(e.keyCode) === '" + a + "'";
}
});
} else {
return _CodeMap[exps] ?
// other word key
"e.keyCode === " + _CodeMap[exps] + " && !_isPressFnKey(e)"
:
// A-Z0-9
"String.fromCharCode(e.keyCode) === '" + exps + "'";
}
},
_listener = function ( e ) {
each( document.keyEvents, function ( k, v ) {
if ( eval(v.exp) ) {
each( v.listener, function ( m, n ) {
n.call( v, e );
})
}
});
},
// remove all the events
_unAll = function () {
C.Event.un( document, "keydown", _listener );
delete document.keyEvents;
},
// remove all the "key"'s events
_unType = function ( type ) {
delete document.keyEvents[type];
// when global key event is empty, remove all the events
C.Util.isEmptyObject(document.keyEvents) && _unAll();
};
return {
on : function ( expression, listener ) {
var exps = expression.replace(/\s/g, "").toUpperCase().split(','),
events = document.keyEvents;
if ( !events ) events = document.keyEvents = {};
// listen global keydown event
C.Util.isEmptyObject(events) && C.Event.on( document, "keydown", _listener );
each( exps, function ( k, v ) {
if ( !_FnKey[v] ) {
var item = events[v];
// create for first time
if ( !item ) {
item = events[v] = {};
item.exp = _Condition( v );
item.self = v;
item.listener = [];
}
item.listener.push( listener || function () {} );
} else console.log("You can't add event listener only on the following key : Ctrl,Shift,Alt");
});
},
un : function () {
var events = document.keyEvents;
if ( events || !C.Util.isEmptyObject(events) ) {
var len = arguments.length;
// remove all the events
if ( len == 0 ) {
_unAll();
} else {
var exps = arguments[0].replace(/\s/g, "").toUpperCase().split(','),
item,
arg1 = arguments[1],
index,
listener;
each( exps, function ( k, v ) {
if ( (item=events[v]) !== undefined ) {
// remove all the key's events
if ( len === 1 ) {
_unType( v );
} else {
//console.log( )
// when arg1 in item.listener, goto remove it
(index = C.Array.indexOf( (listener = item.listener), arg1 )) > -1 &&
// when remove success, check the listener is empty ?
!!listener.splice( index, 1 ) &&
// if empty then goto remove all the "key"'s events
!listener.length && _unType( v );
}
}
});
}
}
}
}
})()
};
/**
* 事件操作
* 20130214
* .on
* .un
* .live
* .unlive
*/
C._fn._extend({
/**
* Function 给指定DOM节点绑定事件监听器
* @param {type} 监听类型
* @param {fn} 监听器
* 20130210
*/
on : function( type, fn ) {
var on = C.Event.on;
each( this.doms, function ( key, val ) {
on( val, type, fn );
});