-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.js
89 lines (78 loc) · 2.31 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
define(['overlay',
'view',
'render',
'class'],
function(Overlay, View, render, clazz) {
// TODO: Implement support for effects.
function Dialog(el, options) {
options = options || {};
Dialog.super_.call(this, el, options);
this._bodySel = options.bodySelector || options.contentSelector || '.body';
this._autoRemove = options.autoRemove !== undefined ? options.autoRemove : true;
var self = this
, el = this.el;
el.find('.close').on('click', function(){
self.emit('close');
self.hide();
return false;
});
this.on('escape', this.hide.bind(this, true));
}
clazz.inherits(Dialog, View);
Dialog.prototype.body =
Dialog.prototype.content = function(el) {
this.el.find(this._bodySel).empty().append(el);
return this;
};
Dialog.prototype.overlay = function(options) {
options = options || {};
options.autoRemove = this._autoRemove;
var self = this
, template = options.template || 'overlay';
this.el.addClass('modal');
this._overlay = new Overlay(template, options);
this._overlay.on('hide', function(){
if (self._autoRemove) self._overlay = null;
self.hide();
});
return this;
};
Dialog.prototype.escapable = function() {
this._onkeydown = keydown.bind(this);
function keydown(e) {
if (27 != e.which) return true;
this.emit('escape');
return false;
}
};
Dialog.prototype.show = function() {
var el = this.el
, overlay = this._overlay;
this.emit('show');
if (this._onkeydown) render.$(document).on('keydown', this._onkeydown);
if (overlay) overlay.show();
el.appendTo(document.body);
el.removeClass('hide');
return this;
}
Dialog.prototype.hide = function(esc) {
var overlay = this._overlay;
this.emit('hide');
if (this._onkeydown) render.$(document).off('keydown', this._onkeydown);
this.el.addClass('hide');
if (esc && overlay) overlay.hide();
if (this._autoRemove) {
var self = this;
setTimeout(function() {
self.remove();
self.dispose();
}, 10);
}
return this;
}
Dialog.prototype.remove = function() {
if (this._overlay) this._overlay.remove();
return Dialog.super_.prototype.remove.call(this);
};
return Dialog;
});