-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
333 lines (280 loc) · 5.56 KB
/
index.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
/**
* Module dependencies.
*/
var Emitter = require('emitter')
, overlay = require('overlay')
, domify = require('domify')
, events = require('event')
, classes = require('classes')
, query = require('query');
/**
* Active dialog.
*/
var active;
/**
* Expose `dialog()`.
*/
exports = module.exports = dialog;
/**
* Expose `Dialog`.
*/
exports.Dialog = Dialog;
/**
* Return a new `Dialog` with the given
* (optional) `title` and `msg`.
*
* @param {String} title or msg
* @param {String} msg
* @return {Dialog}
* @api public
*/
function dialog(title, msg){
switch (arguments.length) {
case 2:
return new Dialog({ title: title, message: msg });
case 1:
return new Dialog({ message: title });
}
};
/**
* Initialize a new `Dialog`.
*
* Options:
*
* - `title` dialog title
* - `message` a message to display
*
* Emits:
*
* - `show` when visible
* - `hide` when hidden
*
* @param {Object} options
* @api public
*/
function Dialog(options) {
Emitter.call(this);
options = options || {};
this.template = require('./template.html');
this.el = domify(this.template);
this._classes = classes(this.el);
this.render(options);
if (active && !active.hiding) active.hide();
if (exports.effect) this.effect(exports.effect);
active = this;
this.on('escape', function(){
active.hide();
});
};
/**
* Inherit from `Emitter.prototype`.
*/
Dialog.prototype = new Emitter;
/**
* Render with the given `options`.
*
* @param {Object} options
* @api public
*/
Dialog.prototype.render = function(options){
var self = this
, el = self.el
, title = options.title
, titleEl = query('.title', el)
, pEl = query('p', el)
, msg = options.message;
events.bind(query('.close', el), 'click', function (ev) {
ev.preventDefault();
self.emit('close');
self.hide();
});
if (titleEl) {
if (!title) {
titleEl.parentNode.removeChild(titleEl);
} else {
titleEl.textContent = title;
}
}
// message
if ('string' == typeof msg) {
pEl.textContent = msg;
} else if (msg) {
pEl.parentNode.insertBefore(msg.el || msg, pEl);
pEl.parentNode.removeChild(pEl);
}
};
/**
* Enable the dialog close link.
*
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.closable = function(){
return this.addClass('closable');
};
/**
* Add class `name`.
*
* @param {String} name
* @return {Dialog}
* @api public
*/
Dialog.prototype.addClass = function(name){
this._classes.add(name);
return this;
};
/**
* Set the effect to `type`.
*
* @param {String} type
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.effect = function(type){
this._effect = type;
this.addClass(type);
return this;
};
/**
* Make it modal!
*
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.modal = function(){
this.overlay();
return this;
};
/**
* Add an overlay.
*
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.overlay = function(opts){
this._overlayOptions = opts || { closable: true };
return this;
};
/**
* Close the dialog when the escape key is pressed.
*
* @api public
*/
Dialog.prototype.escapable = function(){
var self = this;
// Save reference to remove listener later
self._escKeyCallback = self._escKeyCallback || function (e) {
e.which = e.which || e.keyCode;
if (27 !== e.which) return;
self.emit('escape');
};
events.bind(document, 'keydown', self._escKeyCallback);
return this;
};
/**
* Fixed dialogs position can be manipulated through CSS.
*
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.fixed = function(){
this._fixed = true;
return this;
}
/**
* Show the dialog.
*
* Emits "show" event.
*
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.show = function(){
var overlay = this._overlay;
// overlay
this.showOverlay();
// escape
if (!overlay || overlay.closable) this.escapable();
// position
document.body.appendChild(this.el);
this._classes.remove('hide');
this.emit('show');
return this;
};
/**
* Hide the overlay.
*
* @api private
*/
Dialog.prototype.hideOverlay = function(){
if (!this._overlay) return;
this._overlay.off('hide');
this._overlay.hide();
this._overlay = null;
};
/**
* Show the overlay.
*
* @api private
*/
Dialog.prototype.showOverlay = function(){
var self = this;
if (!self._overlayOptions) return;
self._overlay = overlay(self._overlayOptions)
.once('hide', function(){
self._overlay = null;
self.hide();
})
.show();
self._classes.add('modal');
};
/**
* Hide the dialog with optional delay of `ms`,
* otherwise the dialog is removed immediately.
*
* Emits "hide" event.
*
* @return {Number} ms
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.hide = function(ms){
var self = this;
if (self._escKeyCallback) {
events.unbind(document, 'keydown', self._escKeyCallback);
}
// prevent thrashing
self.hiding = true;
// duration
if (ms) {
setTimeout(function(){
self.hide();
}, ms);
return self;
}
// hide / remove
self._classes.add('hide');
if (self._effect) {
setTimeout(function(){
self.remove();
}, 500);
} else {
self.remove();
}
// overlay
self.hideOverlay();
return self;
};
/**
* Hide the dialog without potential animation.
*
* @return {Dialog} for chaining
* @api public
*/
Dialog.prototype.remove = function(){
if (this.el.parentNode) {
this.emit('hide');
this.el.parentNode.removeChild(this.el);
}
return this;
};