forked from aui/artDialog
/
dialog.js
527 lines (383 loc) · 11.6 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
/*!
* artDialog v6.0.0
* Date: 2013-12-06
* https://github.com/aui/artDialog
* (c) 2009-2013 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 Popup = require('./popup');
var css = '../css/ui-dialog.css';
// css loader: RequireJS & SeaJS
if (!$('ui-dialog-style')[0]) {
css = require[require.toUrl ? 'toUrl' : 'resolve'](css);
css = '<link id="ui-dialog-style" rel="stylesheet" href="' + css + '" />';
$('base')[0] ? $('base').before(css) : $('head').append(css);
}
var _version = '6.0.0';
var _count = 0;
var _expando = + new Date + '';
var _isIE6 = !('minWidth' in $('html')[0].style);
var _isMobile = 'createTouch' in document && !('onmousemove' in document)
|| /(iPhone|iPad|iPod)/i.test(navigator.userAgent);
var _isFixed = !_isIE6 && !_isMobile;
var artDialog = function (options, ok, cancel) {
var originalOptions = options = options || {};
if (typeof options === 'string' || options.nodeType === 1) {
options = {content: options, fixed: !_isMobile};
}
options = $.extend(true, {}, artDialog.defaults, options);
var id = options.id = options.id || _expando + _count;
var api = artDialog.get(id);
// 如果存在同名的对话框对象,则直接返回
if (api) {
return api.focus();
}
// 目前主流移动设备对fixed支持不好,禁用此特性
if (!_isFixed) {
options.fixed = false;
}
// 快捷关闭支持:点击对话框外快速关闭对话框
if (options.quickClose) {
options.modal = true;
if (!originalOptions.backdropOpacity) {
options.backdropOpacity = 0;
}
}
// 按钮组
if (!$.isArray(options.button)) {
options.button = [];
}
// 取消按钮
if (cancel !== undefined) {
options.cancel = cancel;
}
if (options.cancel) {
options.button.push({
id: 'cancel',
value: options.cancelValue,
callback: options.cancel
});
}
// 确定按钮
if (ok !== undefined) {
options.ok = ok;
}
if (options.ok) {
options.button.push({
id: 'ok',
value: options.okValue,
callback: options.ok,
autofocus: true
});
}
return artDialog.list[id] = new artDialog.create(options);
};
var popup = function () {};
popup.prototype = Popup.prototype;
var prototype = artDialog.prototype = new popup;
artDialog.version = _version;
artDialog.create = function (options) {
var that = this;
$.extend(this, new Popup);
var $popup = $(this.node).html(options.innerHTML);
this.options = options;
this._popup = $popup;
$.each(options, function (name, value) {
if (typeof that[name] === 'function') {
that[name](value);
} else {
that[name] = value;
}
});
// 更新 zIndex 全局配置
if (options.zIndex) {
Popup.zIndex = options.zIndex;
}
// 设置 ARIA 信息
$popup.attr({
'aria-labelledby': this._$('title')
.attr('id', 'title:' + this.id).attr('id'),
'aria-describedby': this._$('content')
.attr('id', 'content:' + this.id).attr('id')
});
// 关闭按钮
this._$('close')
.css('display', this.cancel === false ? 'none' : '')
.attr('title', this.cancelValue)
.on('click', function (event) {
that._trigger('cancel');
event.preventDefault();
});
// 添加视觉参数
this._$('dialog').addClass(this.skin);
this._$('body').css('padding', this.padding);
// 按钮组点击
$popup.on('click', '[data-id]', function (event) {
var $this = $(this);
if (!$this.attr('disabled')) {// IE BUG
that._trigger($this.data('id'));
}
event.preventDefault();
});
// 点击遮罩自动关闭对话框
if (options.quickClose) {
$(this.backdrop).on('click', function () {
that._trigger('cancel');
});
}
// ESC 快捷键关闭对话框
this._esc = function (event) {
var target = event.target;
var nodeName = target.nodeName;
var rinput = /^input|textarea$/i;
var isTop = Popup.current === that;
var keyCode = event.keyCode;
// 避免输入状态中 ESC 误操作关闭
if (!isTop || rinput.test(nodeName) && target.type !== 'button') {
return;
}
if (keyCode === 27) {
that._trigger('cancel');
}
};
$(document).on('keydown', this._esc);
this.addEventListener('remove', function () {
$(document).off('keydown', this._esc);
delete artDialog.list[this.id];
});
_count ++;
return this;
};
artDialog.create.prototype = prototype;
$.extend(prototype, {
/**
* 显示对话框
* @name artDialog.prototype.show
* @param {HTMLElement Object, Event Object} 指定位置(可选)
*/
/**
* 显示对话框(模态)
* @name artDialog.prototype.showModal
* @param {HTMLElement Object, Event Object} 指定位置(可选)
*/
/**
* 关闭对话框
* @name artDialog.prototype.close
* @param {String, Number} 返回值,可被 onclose 事件收取(可选)
*/
/**
* 销毁对话框
* @name artDialog.prototype.remove
*/
/**
* 重置对话框位置
* @name artDialog.prototype.reset
*/
/**
* 让对话框聚焦(同时置顶)
* @name artDialog.prototype.focus
*/
/**
* 让对话框失焦(同时置顶)
* @name artDialog.prototype.blur
*/
/**
* 添加事件
* @param {String} 事件类型
* @param {Function} 监听函数
* @name artDialog.prototype.addEventListener
*/
/**
* 删除事件
* @param {String} 事件类型
* @param {Function} 监听函数
* @name artDialog.prototype.removeEventListener
*/
/**
* 对话框显示事件,在 show()、showModal() 执行
* @name artDialog.prototype.onshow
* @event
*/
/**
* 关闭事件,在 close() 执行
* @name artDialog.prototype.onclose
* @event
*/
/**
* 关闭事件,在 remove() 执行
* @name artDialog.prototype.onremove
* @event
*/
/**
* 关闭事件,在 reset() 执行
* @name artDialog.prototype.onreset
* @event
*/
/**
* 焦点事件,在 foucs() 执行
* @name artDialog.prototype.onfocus
* @event
*/
/**
* 失焦事件,在 blur() 执行
* @name artDialog.prototype.onblur
* @event
*/
/**
* 设置内容
* @param {String, HTMLElement} 内容
*/
content: function (html) {
this._$('content').empty('')
[typeof html === 'object' ? 'append' : 'html'](html);
return this.reset();
},
/**
* 设置标题
* @param {String} 标题内容
*/
title: function (text) {
this._$('title').text(text);
this._$('header')[text ? 'show' : 'hide']();
return this;
},
/** 设置宽度 */
width: function (value) {
this._$('content').css('width', value);
return this.reset();
},
/** 设置高度 */
height: function (value) {
this._$('content').css('height', value);
return this.reset();
},
/**
* 设置按钮组
* @param {Array, String}
*/
button: function (args) {
args = args || [];
var that = this;
var html = '';
this.callbacks = {};
this._$('footer')[args.length ? 'show' : 'hide']();
if (typeof args === 'string') {
html = args;
} else {
$.each(args, function (i, val) {
val.id = val.id || val.value;
that.callbacks[val.id] = val.callback;
html +=
'<button'
+ ' type="button"'
+ ' data-id="' + val.id + '"'
+ (val.disabled ? ' disabled' : '')
+ (val.autofocus ? ' autofocus class="ui-dialog-autofocus"' : '')
+ '>'
+ val.value
+ '</button>';
});
}
this._$('button').html(html);
return this;
},
statusbar: function (html) {
this._$('statusbar')
.html(html)[html ? 'show' : 'hide']();
return this;
},
_$: function (i) {
return this._popup.find('[i=' + i + ']');
},
// 触发按钮回调函数
_trigger: function (id) {
var fn = this.callbacks[id];
return typeof fn !== 'function' || fn.call(this) !== false ?
this.close().remove() : this;
}
});
/** 最顶层的对话框API */
artDialog.getCurrent = function () {
return Popup.current;
};
/**
* 根据 ID 获取某对话框 API
* @param {String} 对话框 ID
* @return {Object} 对话框 API (实例)
*/
artDialog.get = function (id) {
return id === undefined
? artDialog.list
: artDialog.list[id];
};
artDialog.list = {};
/**
* 默认配置
*/
artDialog.defaults = {
// 消息内容
content: '<span class="ui-dialog-loading">Loading..</span>',
// 标题
title: '',
// 对话框状态栏区域 HTML 代码
statusbar: '',
// 自定义按钮
button: null,
// 确定按钮回调函数
ok: null,
// 取消按钮回调函数
cancel: null,
// 确定按钮文本
okValue: 'ok',
// 取消按钮文本
cancelValue: 'cancel',
// 内容宽度
width: '',
// 内容高度
height: '',
// 内容与边界填充距离
padding: '',
// 对话框自定义 className
skin: '',
/** 是否支持快捷关闭(点击遮罩层自动关闭) */
quickClose: false,
// 是否固定定位
//fixed: false,
// 对话框叠加高度值(重要:此值不能超过浏览器最大限制)
//zIndex: 1987,
// 设置遮罩背景颜色
//backdropBackground: '#000',
// 设置遮罩透明度
//backdropOpacity: 0.7,
// 模板(使用 table 解决 IE7 宽度自适应的 BUG)
innerHTML:
'<div i="dialog" class="ui-dialog">'
+ '<div class="ui-dialog-arrow-a"></div>'
+ '<div class="ui-dialog-arrow-b"></div>'
+ '<table class="ui-dialog-grid">'
+ '<tr>'
+ '<td i="header" class="ui-dialog-header">'
+ '<button i="close" class="ui-dialog-close">×</button>'
+ '<div i="title" class="ui-dialog-title"></div>'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td i="body" class="ui-dialog-body">'
+ '<div i="content" class="ui-dialog-content"></div>'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td i="footer" class="ui-dialog-footer">'
+ '<div i="statusbar" class="ui-dialog-statusbar"></div>'
+ '<div i="button" class="ui-dialog-button"></div>'
+ '</td>'
+ '</tr>'
+ '</table>'
+'</div>'
};
return $.dialog = artDialog;
});