forked from aui/artDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.js
502 lines (371 loc) · 10.9 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
/*!
* artDialog
* Date: 2014-11-09
* https://github.com/aui/artDialog
* (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 Popup = require('./popup');
var defaults = require('./dialog-config');
var css = defaults.cssUri;
// css loader: RequireJS & SeaJS
if (css) {
var fn = require[require.toUrl ? 'toUrl' : 'resolve'];
if (fn) {
css = fn(css);
css = '<link rel="stylesheet" href="' + css + '" />';
if ($('base')[0]) {
$('base').before(css);
} else {
$('head').append(css);
}
}
}
var _count = 0;
var _expando = new Date() - 0; // Date.now()
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);
options.original = originalOptions;
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;
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,
display: options.cancelDisplay
});
}
// 确定按钮
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.create = function (options) {
var that = this;
$.extend(this, new Popup());
var originalOptions = options.original;
var $popup = $(this.node).html(options.innerHTML);
var $backdrop = $(this.backdrop);
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);
// 点击任意空白处关闭对话框
if (options.quickClose) {
$backdrop
.on(
'onmousedown' in document ? 'mousedown' : 'click',
function () {
that._trigger('cancel');
return false;// 阻止抢夺焦点
});
}
// 遮罩设置
this.addEventListener('show', function () {
$backdrop.css({
opacity: 0,
background: options.backdropBackground
}).animate(
{opacity: options.backdropOpacity}
, 150);
});
// 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 ++;
artDialog.oncreate(this);
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.onbeforeremove
* @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) {
var $content = this._$('content');
// HTMLElement
if (typeof html === 'object') {
html = $(html);
$content.empty('').append(html.show());
this.addEventListener('beforeremove', function () {
$('body').append(html.hide());
});
// String
} else {
$content.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}
* Options: value, callback, autofocus, disabled
*/
button: function (args) {
args = args || [];
var that = this;
var html = '';
var number = 0;
this.callbacks = {};
if (typeof args === 'string') {
html = args;
number ++;
} else {
$.each(args, function (i, val) {
var id = val.id = val.id || val.value;
var style = '';
that.callbacks[id] = val.callback;
if (val.display === false) {
style = ' style="display:none"';
} else {
number ++;
}
html +=
'<button'
+ ' type="button"'
+ ' i-id="' + id + '"'
+ style
+ (val.disabled ? ' disabled' : '')
+ (val.autofocus ? ' autofocus class="ui-dialog-autofocus"' : '')
+ '>'
+ val.value
+ '</button>';
that._$('button')
.on('click', '[i-id=' + id +']', function (event) {
var $this = $(this);
if (!$this.attr('disabled')) {// IE BUG
that._trigger(id);
}
event.preventDefault();
});
});
}
this._$('button').html(html);
this._$('footer')[number ? 'show' : 'hide']();
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;
}
});
artDialog.oncreate = $.noop;
/** 获取最顶层的对话框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 = defaults;
return artDialog;
});