-
Notifications
You must be signed in to change notification settings - Fork 35
/
dialog.js
1425 lines (1311 loc) · 44.1 KB
/
dialog.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 bh-lay
*
* @github https://github.com/bh-lay/UI
* @modified 2016-11-2 18:17
*
**/
(function( global, doc, UI_factory, BaseClass_factory, utils_factory){
//初始化工具类
var utils = utils_factory(global,doc),
BaseClass = BaseClass_factory( utils ),
//初始化UI模块
UI = UI_factory( global, doc, utils, BaseClass );
//提供window.UI的接口
global.UI = global.UI || UI;
global.UI._utils = utils;
//提供CommonJS规范的接口
global.define && define(function(){
return UI;
});
})(window,document,function( window, document, utils, BaseClass ){
/**
* 缓存utils下常用工具
* 为压缩变量名做准备
*/
var isNum = utils.isNum,
setCSS = utils.css,
getCSS = utils.getStyle,
animation = utils.animation,
outerWidth = utils.outerWidth,
outerHeight = utils.outerHeight,
findByClassName = utils.findByClassName,
bindEvent = utils.bind;
/**
* 基础模版
*/
var allCnt_tpl = '<div class="UI_lawyer"><div class="UI_mask"></div></div>',
pop_tpl = '<div class="UI_pop"><% if(title){ %><div class="UI_pop_cpt"><%=title %></div><% } %><div class="UI_cnt"></div><a href="javascript:;" class="UI_pop_close" title="\u5173\u95ED">×</a></div>',
confirm_tpl = '<div class="UI_confirm"><div class="UI_confirm_text"><%=text %></div></div>',
ask_tpl = '<div class="UI_ask"><div class="UI_ask_text"><%=text %></div><input class="UI_ask_key" type="text" name="UI_ask_key"/></div>',
confirmBar_tpl = '<div class="UI_pop_confirm"><a href="javascript:;" class="UI_pop_confirm_ok"><%=confirm %></a><a href="javascript:;" class="UI_pop_confirm_cancel"><%=cancel %></a></div>',
prompt_tpl = '<div class="UI_prompt"><div class="UI_cnt"></div></div>',
cover_tpl = '<div class="UI_cover"><div class="UI_cnt"></div><a href="javascript:;" class="UI_close UI_coverClose">×</a></div>',
plane_tpl = '<div class="UI_plane"></div>',
select_tpl = '<div class="UI_select"><div class="UI_select_body UI_cnt"><% if(title){ %><div class="UI_selectCpt"><h3><%=title %></h3><% if(intro){ %><p><%=intro %></p><% } %></div><% } %><div class="UI_selectCnt"><% for(var i=0,total=list.length;i<total;i++){ %><a class="UI_select_btn" href="javascript:;"><%=list[i] %></a><% } %></div></div><div class="UI_selectCancel"><a class="UI_select_btn" href="javascript:;">取消</a></div></div>',
popCSS = '.UI_lawyer{position:absolute;top:0;left:0;width:100%;height:0;overflow:visible;font-family:"Microsoft Yahei"}.UI_lawyer a,.UI_lawyer a:hover,.UI_lawyer a:active{outline:none;text-decoration:none;-webkit-tap-highlight-color:rgba(0,0,0,0);-webkit-tap-highlight-color:transparent}.UI_mask{position:fixed;top:0;left:0;width:100%;height:100%;background:#000;opacity:0.6;filter:alpha(opacity=60);display:none}.UI-blur{transform:translate3d(0,0,0);-webkit-filter:blur(3px)}.UI-noscroll{overflow:hidden}.UI_pop{width:200px;position:absolute;top:400px;left:300px;background:#fff;box-shadow:2px 3px 10px rgba(0,0,0,0.6)}.UI_pop_cpt{position:relative;height:36px;line-height:36px;overflow:hidden;border-bottom:1px solid #ebebeb;color:#777;font-size:16px;text-indent:15px;cursor:default}.UI_pop .UI_cnt{position:relative;min-height:100px;overflow:auto}.UI_pop_close{display:block;position:absolute;top:0;right:0;width:40px;height:36px;text-align:center;color:#ddd;font:bold 20px/36px "simsun";transition:0.1s}.UI_pop_close:hover{color:#888}.UI_pop_close:active{color:#222}.UI_confirm{width:300px;position:absolute;background:#fff;overflow:hidden;box-shadow:2px 3px 10px rgba(0,0,0,0.6)}.UI_confirm_text{padding:30px 10px 20px;line-height:26px;text-align:center;font-size:20px;color:#333}.UI_ask{width:300px;position:absolute;background:#fff;overflow:hidden;box-shadow:2px 3px 10px rgba(0,0,0,0.6)}.UI_ask_text{padding:25px 10px 15px;line-height:26px;text-align:center;font-size:18px;color:#333}.UI_ask input{display:block;margin:0 auto 15px;height:30px;padding:4px 4px;line-height:22px;box-sizing:border-box;width:90%}.UI_pop_confirm{overflow:hidden;text-align:center;border-top:1px solid #ddd;white-space:nowrap}.UI_pop_confirm a{display:inline-block;width:50%;font-size:14px;line-height:36px;color:#03f;transition:0.15s}.UI_pop_confirm a:hover{background:#eee}.UI_pop_confirm_ok{border-right:1px solid #ddd}.UI_prompt{position:absolute;width:240px;background:#fff;box-shadow:2px 2px 10px rgba(0,0,0,0.5)}.UI_prompt .UI_cnt{padding:30px 10px;font-size:18px;color:#333;text-align:center}.UI_plane{position:absolute}.UI_cover{position:absolute;left:0;width:100%;height:100%}.UI_cover .UI_cnt{position:relative;width:100%;height:100%;background:#fff;overflow:auto}.UI_coverClose{display:block;position:absolute;top:10px;right:20px;width:30px;height:30px;text-align:center;color:#aaa;font:18px/30px "simsun";background:#eee;border-radius:15px;border:1px solid #aaa}.UI_coverClose:hover{background:#333;color:#fff;transition:0.2s}.UI_select{position:absolute;width:200px;box-shadow:2px 2px 2px rgba(0,0,0,0.6)}.UI_select a{display:block;height:40px;line-height:40px;text-align:center;color:#03f;font-size:16px}.UI_select_body{overflow:hidden;background:#fff}.UI_selectCpt{padding:8px 0}.UI_selectCpt h3,.UI_selectCpt p{margin:0;font-size:15px;line-height:18px;text-align:center;color:#aaa;font-weight:normal}.UI_selectCpt p{font-size:12px}.UI_selectCnt a{height:34px;line-height:34px;font-size:14px;border-top:1px solid #ddd}.UI_selectCnt a:hover{background:#eee}.UI_selectCancel{display:none}@media(max-width:640px){.UI_select{position:fixed;bottom:0;width:100%;padding-bottom:10px}.UI_select_body, .UI_selectCancel{margin:0 10px;border-radius:8px}.UI_select_body{margin:0 10px 10px;top:initial !important}.UI_selectCancel{display:block;background:#fff}}.UI_ie678 .UI_pop,.UI_ie678 .UI_confirm,.UI_ie678 .UI_ask,.UI_ie678 .UI_prompt,.UI_ie678 .UI_select{outline:3px solid #ccc}.UI_ie67 .UI_mask{position:absolute}/** * CSS3动画 * **/@-webkit-keyframes UI-fadeInDown{0%{opacity:0;-webkit-transform:translateY(10px)}100%{opacity:1;-webkit-transform:translateY(0)}}@keyframes UI-fadeInDown{0%{opacity:0;transform:translateY(10px)}100%{opacity:1;transform:translateY(0)}}@-webkit-keyframes UI-fadeOutUp{0%{opacity:1;-webkit-transform:translateY(0)}100%{opacity:0;-webkit-transform:translateY(10px)}}@keyframes UI-fadeOutUp{0%{opacity:1;transform:translateY(0)}100%{opacity:0;transform:translateY(10px)}}.UI-fadeIn{-webkit-animation:UI-fadeInDown 0.2s ease both;animation:UI-fadeInDown 0.2s ease both}.UI-fadeOut{-webkit-animation:UI-fadeOutUp 0.2s ease both;animation:UI-fadeOutUp 0.2s ease both}';
var isIE67,
isIE678;
if(navigator.appName == "Microsoft Internet Explorer"){
var version = navigator.appVersion.split(";")[1].replace(/[ ]/g,"");
if(version == "MSIE6.0" || version == "MSIE7.0"){
isIE67 = true;
isIE678 = true;
}else if(version == "MSIE8.0"){
isIE678 = true;
}
}
/**
* 定义私有变量
*
**/
var private_allCnt = utils.createDom(allCnt_tpl)[0],
private_maskDom = findByClassName(private_allCnt,'UI_mask')[0],
private_body = document.body,
private_root_node = document.compatMode == "BackCompat" ? private_body : document.documentElement,
private_docW,
private_winH,
private_docH,
private_scrollTop,
private_config_zIndex = 499,
private_config_gap = {
top : 0,
left : 0,
bottom : 0,
right : 0
},
// 默认弹框动画
private_config_defaultAnimationClass = [ 'UI-fadeIn', 'UI-fadeOut' ];
//重新计算浏览器窗口尺寸
function refreshSize(){
private_scrollTop = private_root_node.scrollTop == 0 ? private_body.scrollTop : private_root_node.scrollTop;
private_winH = window.innerHeight || document.documentElement.clientHeight;
private_winW = window.innerWidth || document.documentElement.clientWidth;
private_docH = private_root_node.scrollHeight;
private_docW = private_root_node.clientWidth;
}
//记录当前正在显示的对象
var active_objs = [];
//从记录中移除对象
function remove_active_obj(obj){
utils.each(active_objs,function(index,item){
if(item == obj){
active_objs.splice(index,1);
return false;
}
});
}
//关闭最后一个正在显示的易于关闭的对象
function close_last_easyClose_obj(){
for(var i= active_objs.length-1;i>=0;i--){
if(active_objs[i]['_easyClose']){
active_objs[i].destroy && active_objs[i].destroy();
break;
}
}
}
//调整正在显示的对象的位置
var adapt_delay;
function adapt_active_obj(){
clearTimeout(adapt_delay);
adapt_delay = setTimeout(function(){
utils.each(active_objs,function(index,item){
item.adaption && item.adaption();
});
},150);
}
/**
* 处理对象易于关闭的扩展
* 点击自身以外 or 按下esc
*/
//检测body的mouseup事件
bindEvent(private_body,'mouseup',function checkClick(event) {
var target = event.srcElement || event.target;
setTimeout(function(){
while (!utils.hasClass(target,'UI_easyClose')) {
target = target.parentNode;
if(!target){
close_last_easyClose_obj();
break
}
}
});
});
//检测window的keydown事件(esc)
bindEvent(private_body,'keyup',function checkClick(event) {
if(event.keyCode == 27){
close_last_easyClose_obj();
}
});
/**
* 对象易于关闭方法拓展
* default_value 为默认参数
*/
function easyCloseHandle(mark,default_value){
var me = this;
if(typeof(mark) == 'boolean' ? mark : default_value){
utils.addClass( me.node, 'UI_easyClose' );
setTimeout(function(){
me._easyClose = true;
});
}
}
//初始化组件基础功能
// utils.ready(function(){
//插入css样式表
var styleSheet = utils.createStyleSheet(popCSS,{'data-module' : "UI"});
//插入基础dom
private_body.appendChild(private_allCnt);
//释放掉无用的内存
popCSS = null;
allCnt_tpl = null;
//更新窗口尺寸
refreshSize();
setTimeout(refreshSize,500);
var rebuild_fn = isIE67 ? function(){
refreshSize();
adapt_active_obj();
setCSS(private_maskDom,{
height: private_docH
});
} : function(){
refreshSize();
adapt_active_obj();
};
if( isIE67 ){
utils.addClass(private_allCnt,'UI_ie67');
}
if(isIE678){
utils.addClass(private_allCnt,'UI_ie678');
}
//监听浏览器缩放、滚屏事件
bindEvent(window,'resize',rebuild_fn);
bindEvent(window,'scroll',rebuild_fn);
// });
//限制位置区域的方法
function fix_position( top, left, width, height ){
var gap = private_config_gap;
if( top < private_scrollTop + gap.top ){
//屏幕上方
top = private_scrollTop + gap.top;
}else if( top + height - private_scrollTop > private_winH - gap.bottom ) {
//屏幕下方
if(height > private_winH - gap.top - gap.bottom){
//比屏幕高
top = private_scrollTop + gap.top;
}else{
//比屏幕矮
top = private_scrollTop + private_winH - height - gap.bottom;
}
}
if( left < gap.left ){
left = gap.left;
}else if( left + width > private_docW - gap.right ){
left = private_docW - width - gap.right;
}
return {
top : Math.ceil(top),
left : Math.ceil(left)
}
}
//为基类扩展自适应于页面位置的原型方法
BaseClass.prototype.adaption = function(){
var initTop,
initLeft,
useMethod = animation,
initPosition = this._initPosition;
if( initPosition ){
initTop = initPosition.top;
initLeft = initPosition.left;
// 初始时不使用动画
useMethod = setCSS;
// 删除初始位置
delete this._initPosition;
}
var node = this.node,
width = outerWidth( node ),
height = outerHeight( node ),
top = isNum(initTop) ? initTop : (private_winH - height)/2 + private_scrollTop,
left = isNum(initLeft) ? initLeft : (private_docW - width)/2,
newPosition = fix_position( top, left, width, height );
useMethod( node, {
top : Math.ceil(newPosition.top),
left : Math.ceil(newPosition.left)
}, 80 );
};
//增加确认方法
function add_confirm( confirmParams ){
var me = this,
callback = null,
cancel = null,
btns = ['\u786E\u8BA4','\u53D6\u6D88'],
node = me.node;
if(typeof( confirmParams ) == "function"){
callback = confirmParams;
}else if(typeof( confirmParams ) == "object"){
var paramBtns = confirmParams.btns || [];
btns[0] = paramBtns[0] || btns[0];
btns[1] = paramBtns[1] || btns[1];
if(typeof(confirmParams.callback) == "function"){
callback = confirmParams.callback;
}
if( typeof(confirmParams.cancel) == "function" ){
cancel = confirmParams.cancel;
}
}
var this_html = utils.render(confirmBar_tpl,{
confirm : btns[0],
cancel : btns[1]
});
node.appendChild( utils.createDom(this_html)[0] );
// 关闭弹窗的方法
function close(){
me.destroy();
}
//绑定事件,根据执行结果判断是否要关闭弹框
bindEvent( node, 'click','.UI_pop_confirm_ok', function(){
//点击确认按钮
callback ? ((callback() !== false) && close()) : close();
});
bindEvent( node, 'click','.UI_pop_confirm_cancel',function(){
//点击取消按钮
cancel ? ((cancel() !== false) && close()) : close();
});
}
/**
* 模糊效果
*/
function travelRootElements(callback){
var doms = private_body.childNodes;
utils.each(doms,function(i,dom){
if(dom != private_allCnt && dom.nodeType ==1 && dom.tagName != 'SCRIPT' && dom.tagName != 'LINK' && dom.tagName != 'STYLE'){
callback(dom);
}
});
}
var blur,
removeBlur;
if(utils.supports('-webkit-filter')){
blur = function (){
travelRootElements(function(dom){
utils.addClass(dom,'UI-blur');
});
};
removeBlur = function (){
travelRootElements(function(dom){
utils.removeClass(dom,'UI-blur');
});
};
}
//最后一个有蒙层的对象
function last_has_mask_item(){
//逆序遍历所有显示中的对象
for(var i= active_objs.length-1;i>=0;i--){
//判断是否含有蒙层
if(active_objs[i]._mask){
return active_objs[i];
}
}
return null;
}
//最后一个有蒙层的对象的zIndex值,
function last_has_mask_zIndex(){
var item = last_has_mask_item();
return item ? item._zIndex : private_config_zIndex; // 无则返回默认值
}
/**
* 开场动画
**/
function openAnimation(){
var me = this,
lastHasMaskZindex = last_has_mask_zIndex();
me._zIndex = lastHasMaskZindex + 2;
setCSS( me.node, {
zIndex: me._zIndex
});
// 若有蒙层则显示蒙层
if( me._mask ){
setCSS(private_maskDom,{
zIndex: lastHasMaskZindex + 1
});
//之前蒙层未显示,显示蒙层
if( lastHasMaskZindex <= private_config_zIndex ){
blur && blur();
utils.fadeIn(private_maskDom,300);
}
}
//向全局记录的对象内添加对象
active_objs.push( me );
//非ie系列 且 有动画配置,显示效果
if( !isIE678 && me.animationClass ){
utils.addClass( me.node, me.animationClass[0] );
}
}
/**
* 处理对象关闭及结束动画
*/
function closeAnimation(){
var me = this,
DOM = me.node,
animationClass = me.animationClass[1];
//从全局记录的对象内删除自己;
remove_active_obj(me);
// 若有蒙层,则关闭或移至下一个需要显示蒙层的位置
if( me._mask ){
var lastHasMaskZindex = last_has_mask_zIndex();
setCSS(private_maskDom,{
zIndex : lastHasMaskZindex - 1
});
if(lastHasMaskZindex <= private_config_zIndex){
removeBlur && removeBlur();
utils.fadeOut(private_maskDom,400);
}
}
function end(){
//删除dom
utils.removeNode(DOM);
}
//ie系列或未配置动画class,立即结束
if( isIE678 || !animationClass ){
end();
}else{
utils.addClass( DOM, animationClass );
setTimeout(end, 500);
}
}
// 过滤参数
function filterParam( param, defaults ){
param = param || {};
// 动画定义
this.animationClass = ( param.animationClass || '' ).constructor == Array ? param.animationClass : private_config_defaultAnimationClass;
// 蒙层参数
this._mask = typeof( param.mask ) == 'boolean' ? param.mask : defaults.mask;
// 初始位置
this._initPosition = {
top: param.top,
left: param.left
};
}
/**
* 弹框
* pop
*/
function POP( param ){
if( !(this instanceof POP) ){
return new POP( param );
}
param = param || {};
var me = this;
filterParam.call( me, param, {
mask: true
});
me.node = utils.createDom( utils.render( pop_tpl, {
title: param.title
}) )[0];
me.cntDom = findByClassName( me.node, 'UI_cnt' )[0];
//当有确认参数时
if(param.confirm){
add_confirm.call( me, param.confirm );
}
//处理title参数
if( param.title ){
//can drag is pop
utils.drag( findByClassName( me.node, 'UI_pop_cpt' )[0], me.node, {
move : function( mx, my, l_start, t_start, w_start, h_start ){
var left = mx + l_start,
top = my + t_start,
newSize = fix_position( top, left, w_start, h_start );
setCSS( me.node, {
left : newSize.left,
top : newSize.top
});
}
});
}
bindEvent( me.node, 'click', '.UI_pop_close', function(){
me.destroy();
});
//插入内容
me.cntDom.innerHTML = param.html || '';
//设置宽度,为计算位置尺寸做准备
setCSS( me.node, {
width: Math.min(param.width || 600,private_docW-20)
});
private_allCnt.appendChild( me.node );
//校正位置
me.adaption();
//处理是否易于关闭
easyCloseHandle.call(me,param.easyClose,true);
//开场动画
openAnimation.call( me );
}
POP.prototype = new BaseClass({
destroy: closeAnimation
});
/**
* CONFIRM
*/
function CONFIRM(param){
if(!(this instanceof CONFIRM)){
return new CONFIRM(param);
}
param = param || {};
var me = this;
filterParam.call( me, param, {
mask: true
});
me.node = utils.createDom( utils.render(confirm_tpl,{
text : param.text || 'need text in parameter!'
}) )[0];
add_confirm.call( me, param );
private_allCnt.appendChild( me.node );
me.adaption();
//处理是否易于关闭
easyCloseHandle.call(me,param.easyClose,true);
openAnimation.call( me );
}
CONFIRM.prototype = new BaseClass({
destroy: closeAnimation
});
/**
* ASK
*/
function ASK(text,callback,param){
if(!(this instanceof ASK)){
return new ASK(text,callback,param);
}
param = param || {};
var me = this;
filterParam.call( me, param, {
mask: true
});
var this_html = utils.render(ask_tpl,{
text : text || 'need text in parameter!'
});
me.node = utils.createDom(this_html)[0];
me.inputDom = findByClassName( me.node, 'UI_ask_key' )[0];
var confirm_html = utils.render(confirmBar_tpl,{
confirm : '确定',
cancel : '取消'
});
me.node.appendChild(utils.createDom(confirm_html)[0]);
//确定
bindEvent( me.node, 'click', '.UI_pop_confirm_ok', function(){
//根据执行结果判断是否要关闭弹框
callback ? ((callback(me.inputDom.value) != false) && me.destroy()) : me.destroy();
});
//取消
bindEvent( me.node, 'click', '.UI_pop_confirm_cancel', function(){
me.destroy();
});
private_allCnt.appendChild( me.node );
me.adaption();
//处理是否易于关闭
easyCloseHandle.call(me,param.easyClose,true);
openAnimation.call( me );
me.inputDom.focus();
}
ASK.prototype = new BaseClass({
destroy: closeAnimation
});
ASK.prototype.setValue = function(text){
this.inputDom.value = text.toString();
};
/**
* prompt
*
**/
function PROMPT(text,time,param){
if(!(this instanceof PROMPT)){
return new PROMPT(text,time,param);
}
param = param || {};
var me = this;
filterParam.call( me, param, {
mask: false
});
me.node = utils.createDom(prompt_tpl)[0];
me.tips(text,time);
// create pop
private_allCnt.appendChild( me.node );
me.adaption();
openAnimation.call( me );
}
PROMPT.prototype = new BaseClass({
destroy: closeAnimation
});
PROMPT.prototype.tips = function(txt,time){
var me = this;
if(txt){
findByClassName( me.node, 'UI_cnt' )[0].innerHTML = txt;
}
if(time != 0){
setTimeout(function(){
me.destroy();
},(time || 1500));
}
};
/**
* PLANE
*/
function PLANE(param){
if(!(this instanceof PLANE)){
return new PLANE(param);
}
param = param || {};
var me = this;
filterParam.call( me, param, {
mask: false
});
me.node = utils.createDom(plane_tpl)[0];
//insert html
me.node.innerHTML = param.html || '';
setCSS( me.node, {
width : param.width || 240,
height : param.height || null,
top : isNum(param.top) ? param.top : 300,
left : isNum(param.left) ? param.left : 800
});
private_allCnt.appendChild( me.node );
easyCloseHandle.call(me,true);
openAnimation.call( me );
}
PLANE.prototype = new BaseClass({
destroy: closeAnimation
});
/***
* 全屏弹框
* COVER
*/
function COVER(param){
if(!(this instanceof COVER)){
return new COVER(param);
}
param = param || {};
var me = this;
filterParam.call( me, param, {
mask: false
});
me.node = utils.createDom(cover_tpl)[0];
me.cntDom = findByClassName(me.node,'UI_cnt')[0];
//关闭事件
bindEvent(me.node,'click','.UI_close',function(){
me.destroy();
});
//记录body的scrollY设置
setCSS( me.node, {
height: private_winH,
top: private_scrollTop
});
private_allCnt.appendChild( me.node );
//处理是否易于关闭
easyCloseHandle.call(me,param.easyClose,true);
openAnimation.call( me );
utils.addClass( private_body, 'UI-noscroll' );
//insert html
me.cntDom.innerHTML = param.html || '';
me.on('destroy',function(){
utils.addClass( me.cntDom, 'UI-noscroll' );
utils.removeClass( private_body, 'UI-noscroll' );
});
}
//使用close方法
COVER.prototype = new BaseClass({
destroy: closeAnimation
});
/**
* 选择功能
*/
function SELECT(list,param){
if(!(this instanceof SELECT)){
return new SELECT(list,param);
}
param = param || {};
var me = this,
list = list || [],
fns = [],
nameList = [];
filterParam.call( me, param, {
mask: true
});
utils.each(list,function(i,item){
nameList.push(item[0]);
fns.push(item[1]);
});
var this_html = utils.render(select_tpl,{
list : nameList,
title : param.title || null,
intro : param.intro || null
});
me.node = utils.createDom(this_html)[0];
//绑定事件
var btns = findByClassName( me.node, 'UI_select_btn' );
utils.each(btns,function(index,btn){
bindEvent(btn,'click',function(){
fns[index] && fns[index]();
me.destroy();
});
});
if(private_docW < 640 && !isIE678){
//手机版
private_allCnt.appendChild( me.node );
}else{
var cssObj = {
top : param.top || 100,
left : param.left || 100,
width : param.width || 200
};
private_allCnt.appendChild( me.node );
setCSS( me.node, cssObj );
var newSize = fix_position( cssObj.top, cssObj.left, cssObj.width, outerHeight( me.node ) );
setCSS( me.node, {
left : newSize.left,
top : newSize.top
});
}
easyCloseHandle.call(me,param.easyClose,true);
openAnimation.call( me );
}
SELECT.prototype = new BaseClass({
destroy: closeAnimation
});
/**
* 抛出对外接口
*/
return {
pop : POP,
config : {
gap : function(name,value){
//name符合top/right/bottom/left,且value值为数字类型(兼容字符类型)
if(name && typeof(private_config_gap[name]) == 'number' && isNum(value)){
private_config_gap[name] = parseInt(value);
}
},
setDefaultAnimationClass: function( startClassStr, endClassStr ){
private_config_defaultAnimationClass[0] = startClassStr;
endClassStr && (private_config_defaultAnimationClass[1] = endClassStr);
},
zIndex : function(num){
var num = parseInt(num);
if(num > 0){
private_config_zIndex = num;
setCSS(private_allCnt,{
zIndex : num
});
}
}
},
confirm : CONFIRM,
ask : ASK,
prompt : PROMPT,
plane : PLANE,
cover : COVER,
select : SELECT
};
}, function(){
function isFunction( input ){
return typeof( input ) === 'function'
}
function isNotEmptyString( input ){
return typeof( input ) === 'string' && input.length > 0
}
function BaseClass( param ){
if( !param || !isFunction( param.destroy ) ){
throw new Error("use BaseClass must define param & param.destroy");
}
param = param || {};
// 切勿在此处定义事件集合
// 避免实例化在其他原型链上导致内存共享的问题
// this._events = {};
this._isDestroyed = false;
this._onDestroy = param.destroy;
}
BaseClass.prototype = {
//监听自定义事件
on: function( eventName, callback ){
this._events = this._events || {}
if( isNotEmptyString( eventName ) && isFunction( callback ) ){
//事件集合无该事件,创建一个事件集合
this._events[eventName] = this._events[eventName] || [];
// 追加至事件列表
this._events[eventName].push( callback );
}
//提供链式调用的支持
return this;
},
//解除自定义事件监听
un: function( eventName, callback ){
this._events = this._events || {}
var eventList = this._events[eventName];
//事件集合无该事件队列,或未传入事件名结束运行
if( !eventList || !isNotEmptyString( eventName ) ){
return
}
// 若未传入回调参数,则直接置空事件队列
if( !isFunction( callback ) ){
eventList = [];
}else{
// 逆序遍历事件队列
for( var i = eventList.length-1; i!=-1; i-- ){
// 回调相同,移除当前项
if( eventList[i] == callback ){
eventList.splice(i,1);
}
}
}
//提供链式调用的支持
return this;
},
// 主动触发自定义事件
emit: function( eventName ){
this._events = this._events || {}
// 获取除了事件名之外的参数
var args = Array.prototype.slice.call( arguments, 1, arguments.length );
//事件集合无该事件,结束运行
if(!this._events[eventName]){
return
}
for(var i=0,total=this._events[eventName].length;i<total;i++){
this._events[eventName][i].apply( this, args );
}
},
// 保证只有一遍有效执行
destroy: function(){
if( this._isDestroyed ){
return;
}
this._isDestroyed = true;
this._onDestroy.call( this );
this.emit( 'destroy' );
}
};
return BaseClass;
}, function (window,document) {
/**
* 判断对象类型
* string number array
* object function
* htmldocument
* undefined null
*/
function TypeOf(obj) {
return Object.prototype.toString.call(obj).match(/\s(\w+)/)[1].toLowerCase();
}
/**
* 检测是否为数字
* 兼容字符类数字 '23'
*/
function isNum(ipt){
return (ipt !== '') && (ipt == +ipt) ? true : false;
}
/**
* 遍历数组或对象
*
*/
function each(arr,fn){
//检测输入的值
if(typeof(arr) != 'object' || typeof(fn) != 'function'){
return;
}
var Length = arr.length;
if( isNum(Length) ){
for(var i=0;i<Length;i++){
if(fn.call(this,i,arr[i]) === false){
break
}
}
}else{
for(var i in arr){
if (!arr.hasOwnProperty(i)){
continue;
}
if(fn.call(this,i,arr[i]) === false){
break
}
}
}
}
/**
* 对象拷贝
*
*/
function clone(fromObj,toObj){
each(fromObj,function(i,item){
if(typeof item == "object"){
toObj[i] = item.constructor==Array ? [] : {};
clone(item,toObj[i]);
}else{
toObj[i] = item;
}
});
return toObj;
}
/**
* 判断是否支持css属性
* 兼容css3
*/
var supports = (function() {
var styles = document.createElement('div').style,
vendors = 'Webkit Khtml Ms O Moz'.split(/\s/);
return function(prop) {
var returns = false;
if ( prop in styles ){
returns = prop;
}else{
prop = prop.replace(/^[a-z]/, function(val) {
return val.toUpperCase();
});
each(vendors,function(i,value){
if ( value + prop in styles ) {
returns = ('-' + value + '-' + prop).toLowerCase();
}
});
}
return returns;
};
})();
/**
* class 操作
*/
var private_css3 = !!(supports('transition') && supports('transform')),
supports_classList = !!document.createElement('div').classList,
// 是否含有某个 class
hasClass = supports_classList ? function( node, classSingle ){
return node && node.classList && node.classList.contains( classSingle );
} : function ( node, classSingle ){
if( !node || typeof( node.className ) !== 'string' ){
return false;
}
return !! node.className.match(new RegExp('(\\s|^)' + classSingle + '(\\s|$)'));
},
// 增加一个 class
addClass = supports_classList ? function( node, classSingle ){
node && node.classList && node.classList.add( classSingle );
} : function ( node, cls) {
!hasClass(node, cls) && ( node.className += " " + cls );
},
// 移除一个 class
removeClass = supports_classList ? function ( node, classSingle ) {
node && node.classList && node.classList.remove( classSingle );
} : function ( node, classSingle ) {
if ( hasClass( node, classSingle ) ) {
node.className = node.className.replace( new RegExp('(\\s+|^)' + classSingle + '(\\s+|$)'), '' );
}
};
//获取样式
function getStyle(elem, prop) {
var value;
prop == "borderWidth" ? prop = "borderLeftWidth" : prop;
if (elem.style[prop]){
value = elem.style[prop];
} else if(document.defaultView) {
var style = document.defaultView.getComputedStyle(elem, null);
value = prop in style ? style[prop] : style.getPropertyValue(prop);
} else if (elem.currentStyle) {
value = elem.currentStyle[prop];
}
if (/\px$/.test(value)){
value = parseInt(value);
}else if (isNum(value) ){
value = Number(value);