forked from aui/artDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
639 lines (464 loc) · 14.9 KB
/
popup.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
/*!
* PopupJS
* Date: 2014-11-09
* https://github.com/aui/popupjs
* (c) 2009-2014 TangBin, http://www.planeArt.cn
*
* This is licensed under the GNU LGPL, version 2.1 or later.
* For details, see: http://www.gnu.org/licenses/lgpl-2.1.html
*/
define(function (require) {
var $ = require('jquery');
var _count = 0;
var _isIE6 = !('minWidth' in $('html')[0].style);
var _isFixed = !_isIE6;
function Popup () {
this.destroyed = false;
this.__popup = $('<div />')
/*使用 <dialog /> 元素可能导致 z-index 永远置顶的问题(chrome)*/
.css({
display: 'none',
position: 'absolute',
/*
left: 0,
top: 0,
bottom: 'auto',
right: 'auto',
margin: 0,
padding: 0,
border: '0 none',
background: 'transparent'
*/
outline: 0
})
.attr('tabindex', '-1')
.html(this.innerHTML)
.appendTo('body');
this.__backdrop = this.__mask = $('<div />')
.css({
opacity: .7,
background: '#000'
});
// 使用 HTMLElement 作为外部接口使用,而不是 jquery 对象
// 统一的接口利于未来 Popup 移植到其他 DOM 库中
this.node = this.__popup[0];
this.backdrop = this.__backdrop[0];
_count ++;
}
$.extend(Popup.prototype, {
/**
* 初始化完毕事件,在 show()、showModal() 执行
* @name Popup.prototype.onshow
* @event
*/
/**
* 关闭事件,在 close() 执行
* @name Popup.prototype.onclose
* @event
*/
/**
* 销毁前事件,在 remove() 前执行
* @name Popup.prototype.onbeforeremove
* @event
*/
/**
* 销毁事件,在 remove() 执行
* @name Popup.prototype.onremove
* @event
*/
/**
* 重置事件,在 reset() 执行
* @name Popup.prototype.onreset
* @event
*/
/**
* 焦点事件,在 foucs() 执行
* @name Popup.prototype.onfocus
* @event
*/
/**
* 失焦事件,在 blur() 执行
* @name Popup.prototype.onblur
* @event
*/
/** 浮层 DOM 素节点[*] */
node: null,
/** 遮罩 DOM 节点[*] */
backdrop: null,
/** 是否开启固定定位[*] */
fixed: false,
/** 判断对话框是否删除[*] */
destroyed: true,
/** 判断对话框是否显示 */
open: false,
/** close 返回值 */
returnValue: '',
/** 是否自动聚焦 */
autofocus: true,
/** 对齐方式[*] */
align: 'bottom left',
/** 内部的 HTML 字符串 */
innerHTML: '',
/** CSS 类名 */
className: 'ui-popup',
/**
* 显示浮层
* @param {HTMLElement, Event} 指定位置(可选)
*/
show: function (anchor) {
if (this.destroyed) {
return this;
}
var that = this;
var popup = this.__popup;
var backdrop = this.__backdrop;
this.__activeElement = this.__getActive();
this.open = true;
this.follow = anchor || this.follow;
// 初始化 show 方法
if (!this.__ready) {
popup
.addClass(this.className)
.attr('role', this.modal ? 'alertdialog' : 'dialog')
.css('position', this.fixed ? 'fixed' : 'absolute');
if (!_isIE6) {
$(window).on('resize', $.proxy(this.reset, this));
}
// 模态浮层的遮罩
if (this.modal) {
var backdropCss = {
position: 'fixed',
left: 0,
top: 0,
width: '100%',
height: '100%',
overflow: 'hidden',
userSelect: 'none',
zIndex: this.zIndex || Popup.zIndex
};
popup.addClass(this.className + '-modal');
if (!_isFixed) {
$.extend(backdropCss, {
position: 'absolute',
width: $(window).width() + 'px',
height: $(document).height() + 'px'
});
}
backdrop
.css(backdropCss)
.attr({tabindex: '0'})
.on('focus', $.proxy(this.focus, this));
// 锁定 tab 的焦点操作
this.__mask = backdrop
.clone(true)
.attr('style', '')
.insertAfter(popup);
backdrop
.addClass(this.className + '-backdrop')
.insertBefore(popup);
this.__ready = true;
}
if (!popup.html()) {
popup.html(this.innerHTML);
}
}
popup
.addClass(this.className + '-show')
.show();
backdrop.show();
this.reset().focus();
this.__dispatchEvent('show');
return this;
},
/** 显示模态浮层。参数参见 show() */
showModal: function () {
this.modal = true;
return this.show.apply(this, arguments);
},
/** 关闭浮层 */
close: function (result) {
if (!this.destroyed && this.open) {
if (result !== undefined) {
this.returnValue = result;
}
this.__popup.hide().removeClass(this.className + '-show');
this.__backdrop.hide();
this.open = false;
this.blur();// 恢复焦点,照顾键盘操作的用户
this.__dispatchEvent('close');
}
return this;
},
/** 销毁浮层 */
remove: function () {
if (this.destroyed) {
return this;
}
this.__dispatchEvent('beforeremove');
if (Popup.current === this) {
Popup.current = null;
}
// 从 DOM 中移除节点
this.__popup.remove();
this.__backdrop.remove();
this.__mask.remove();
if (!_isIE6) {
$(window).off('resize', this.reset);
}
this.__dispatchEvent('remove');
for (var i in this) {
delete this[i];
}
return this;
},
/** 重置位置 */
reset: function () {
var elem = this.follow;
if (elem) {
this.__follow(elem);
} else {
this.__center();
}
this.__dispatchEvent('reset');
return this;
},
/** 让浮层获取焦点 */
focus: function () {
var node = this.node;
var popup = this.__popup;
var current = Popup.current;
var index = this.zIndex = Popup.zIndex ++;
if (current && current !== this) {
current.blur(false);
}
// 检查焦点是否在浮层里面
if (!$.contains(node, this.__getActive())) {
var autofocus = popup.find('[autofocus]')[0];
if (!this._autofocus && autofocus) {
this._autofocus = true;
} else {
autofocus = node;
}
this.__focus(autofocus);
}
// 设置叠加高度
popup.css('zIndex', index);
//this.__backdrop.css('zIndex', index);
Popup.current = this;
popup.addClass(this.className + '-focus');
this.__dispatchEvent('focus');
return this;
},
/** 让浮层失去焦点。将焦点退还给之前的元素,照顾视力障碍用户 */
blur: function () {
var activeElement = this.__activeElement;
var isBlur = arguments[0];
if (isBlur !== false) {
this.__focus(activeElement);
}
this._autofocus = false;
this.__popup.removeClass(this.className + '-focus');
this.__dispatchEvent('blur');
return this;
},
/**
* 添加事件
* @param {String} 事件类型
* @param {Function} 监听函数
*/
addEventListener: function (type, callback) {
this.__getEventListener(type).push(callback);
return this;
},
/**
* 删除事件
* @param {String} 事件类型
* @param {Function} 监听函数
*/
removeEventListener: function (type, callback) {
var listeners = this.__getEventListener(type);
for (var i = 0; i < listeners.length; i ++) {
if (callback === listeners[i]) {
listeners.splice(i--, 1);
}
}
return this;
},
// 获取事件缓存
__getEventListener: function (type) {
var listener = this.__listener;
if (!listener) {
listener = this.__listener = {};
}
if (!listener[type]) {
listener[type] = [];
}
return listener[type];
},
// 派发事件
__dispatchEvent: function (type) {
var listeners = this.__getEventListener(type);
if (this['on' + type]) {
this['on' + type]();
}
for (var i = 0; i < listeners.length; i ++) {
listeners[i].call(this);
}
},
// 对元素安全聚焦
__focus: function (elem) {
// 防止 iframe 跨域无权限报错
// 防止 IE 不可见元素报错
try {
// ie11 bug: iframe 页面点击会跳到顶部
if (this.autofocus && !/^iframe$/i.test(elem.nodeName)) {
elem.focus();
}
} catch (e) {}
},
// 获取当前焦点的元素
__getActive: function () {
try {// try: ie8~9, iframe #26
var activeElement = document.activeElement;
var contentDocument = activeElement.contentDocument;
var elem = contentDocument && contentDocument.activeElement || activeElement;
return elem;
} catch (e) {}
},
// 居中浮层
__center: function () {
var popup = this.__popup;
var $window = $(window);
var $document = $(document);
var fixed = this.fixed;
var dl = fixed ? 0 : $document.scrollLeft();
var dt = fixed ? 0 : $document.scrollTop();
var ww = $window.width();
var wh = $window.height();
var ow = popup.width();
var oh = popup.height();
var left = (ww - ow) / 2 + dl;
var top = (wh - oh) * 382 / 1000 + dt;// 黄金比例
var style = popup[0].style;
style.left = Math.max(parseInt(left), dl) + 'px';
style.top = Math.max(parseInt(top), dt) + 'px';
},
// 指定位置 @param {HTMLElement, Event} anchor
__follow: function (anchor) {
var $elem = anchor.parentNode && $(anchor);
var popup = this.__popup;
if (this.__followSkin) {
popup.removeClass(this.__followSkin);
}
// 隐藏元素不可用
if ($elem) {
var o = $elem.offset();
if (o.left * o.top < 0) {
return this.__center();
}
}
var that = this;
var fixed = this.fixed;
var $window = $(window);
var $document = $(document);
var winWidth = $window.width();
var winHeight = $window.height();
var docLeft = $document.scrollLeft();
var docTop = $document.scrollTop();
var popupWidth = popup.width();
var popupHeight = popup.height();
var width = $elem ? $elem.outerWidth() : 0;
var height = $elem ? $elem.outerHeight() : 0;
var offset = this.__offset(anchor);
var x = offset.left;
var y = offset.top;
var left = fixed ? x - docLeft : x;
var top = fixed ? y - docTop : y;
var minLeft = fixed ? 0 : docLeft;
var minTop = fixed ? 0 : docTop;
var maxLeft = minLeft + winWidth - popupWidth;
var maxTop = minTop + winHeight - popupHeight;
var css = {};
var align = this.align.split(' ');
var className = this.className + '-';
var reverse = {top: 'bottom', bottom: 'top', left: 'right', right: 'left'};
var name = {top: 'top', bottom: 'top', left: 'left', right: 'left'};
var temp = [{
top: top - popupHeight,
bottom: top + height,
left: left - popupWidth,
right: left + width
}, {
top: top,
bottom: top - popupHeight + height,
left: left,
right: left - popupWidth + width
}];
var center = {
left: left + width / 2 - popupWidth / 2,
top: top + height / 2 - popupHeight / 2
};
var range = {
left: [minLeft, maxLeft],
top: [minTop, maxTop]
};
// 超出可视区域重新适应位置
$.each(align, function (i, val) {
// 超出右或下边界:使用左或者上边对齐
if (temp[i][val] > range[name[val]][1]) {
val = align[i] = reverse[val];
}
// 超出左或右边界:使用右或者下边对齐
if (temp[i][val] < range[name[val]][0]) {
align[i] = reverse[val];
}
});
// 一个参数的情况
if (!align[1]) {
name[align[1]] = name[align[0]] === 'left' ? 'top' : 'left';
temp[1][align[1]] = center[name[align[1]]];
}
//添加follow的css, 为了给css使用
className += align.join('-') + ' '+ this.className+ '-follow';
that.__followSkin = className;
if ($elem) {
popup.addClass(className);
}
css[name[align[0]]] = parseInt(temp[0][align[0]]);
css[name[align[1]]] = parseInt(temp[1][align[1]]);
popup.css(css);
},
// 获取元素相对于页面的位置(包括iframe内的元素)
// 暂时不支持两层以上的 iframe 套嵌
__offset: function (anchor) {
var isNode = anchor.parentNode;
var offset = isNode ? $(anchor).offset() : {
left: anchor.pageX,
top: anchor.pageY
};
anchor = isNode ? anchor : anchor.target;
var ownerDocument = anchor.ownerDocument;
var defaultView = ownerDocument.defaultView || ownerDocument.parentWindow;
if (defaultView == window) {// IE <= 8 只能使用两个等于号
return offset;
}
// {Element: Ifarme}
var frameElement = defaultView.frameElement;
var $ownerDocument = $(ownerDocument);
var docLeft = $ownerDocument.scrollLeft();
var docTop = $ownerDocument.scrollTop();
var frameOffset = $(frameElement).offset();
var frameLeft = frameOffset.left;
var frameTop = frameOffset.top;
return {
left: offset.left + frameLeft - docLeft,
top: offset.top + frameTop - docTop
};
}
});
/** 当前叠加高度 */
Popup.zIndex = 1024;
/** 顶层浮层的实例 */
Popup.current = null;
return Popup;
});