-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathalerty.js
357 lines (312 loc) · 11.4 KB
/
alerty.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
/*!
* alerty v0.0.1 (https://github.com/undead25/alerty#readme)
* Copyright 2022 undead25
* Licensed under the MIT license
*/
(function () {
'use strict';
// common function which is often using
var commonUse = {
/**
* [Add class to element]
*
* @param el {Object} -- element.
* @param cls {String} -- classes.
*/
addClass: function(el, cls) {
var elClass = el.className;
var blank = (elClass !== '') ? ' ' : '';
var added = elClass + blank + cls;
el.className = added;
},
/**
* [Remove class from element]
*
* @param el {Object} -- element.
* @param cls {String} -- classes.
*/
removeClass: function(el, cls) {
var elClass = ' '+el.className+' ';
elClass = elClass.replace(/(\s+)/gi, ' ');
var removed = elClass.replace(' '+cls+' ', ' ');
removed = removed.replace(/(^\s+)|(\s+$)/g, '');
el.className = removed;
},
/**
* [if element has some class]
*
* @param el {Object} -- element.
* @param cls {String} -- classes.
*
* @return {Boolean} -- true or false.
*/
hasClass: function(el, cls) {
var elClass = el.className;
var elClassList = elClass.split(/\s+/);
var x = 0;
for(x in elClassList) {
if(elClassList[x] == cls) {
return true;
}
}
return false;
},
/**
* [add event to some element, dom0, dom1, supports fuck ie]
*
* @param el {Object} -- element.
* @param type {String} -- event type, such as 'click', 'mouseover'.
* @param func {Function} -- function.
*
*/
addEvent: function(el, type, func) {
if(el.addEventListener) {
el.addEventListener(type, func, false);
} else if(el.attachEvent){
el.attachEvent('on' + type, func);
} else{
el['on' + type] = func;
}
},
/**
* [remove event to some element, dom0, dom1, supports fuck ie]
*
* @param el {Object} -- element.
* @param type {String} -- event type, such as 'click', 'mouseover'.
* @param func {Function} -- function.
*
*/
removeEvent: function(el, type, func) {
if (el.removeEventListener){
el.removeEventListener(type, func, false);
} else if (el.detachEvent){
el.detachEvent('on' + type, func);
} else {
delete el['on' + type];
}
},
/**
* [Remove element node]
*
* @param el {Object} -- element.
*
*/
removeElement: function(el) {
(el && el.parentNode) && el.parentNode.removeChild(el);
},
/**
* [Set unique id]
*
* @param prefix {String} -- id prefix name.
*
* @return {String}
*/
setUid: function(prefix) {
do prefix += Math.floor(Math.random() * 1000000);
while (document.getElementById(prefix));
return prefix;
}
};
/**
* [Alertiy public API]
*
* @return {Object}
*/
var Alerty = function() {
// private object for Alerty object inherit
var Dialog = {
// static defaults params
defaults: {
okLabel: '\u786e\u5b9a',
cancelLabel: '\u53d6\u6d88',
time: 2000
},
previousCallback: null, // for cache previous toasts callbak, to handle if call more than 1 alerty
// html templates
template: '<div class="alerty-overlay" tabindex="-1"></div>'+
'<div class="alerty">'+
'<div class="alerty-title"></div>'+
'<div class="alerty-content">'+
'<p class="alerty-message"></p>'+
'<div class="alerty-prompt">'+
'<input type="text" placeholder="" value="">'+
'<div class="input-line"></div>'+
'</div>'+
'</div>'+
'<div class="alerty-action">'+
'<a class="btn-cancel"></a>'+
'<a class="btn-ok"></a>'+
'</div>'+
'</div>',
/**
* [Build the HTML contents]
*
* @param type {String} -- get the dialog type to arrange the correspondent html content.
* @param content {String} -- the text contents dialog to users.
* @param opts {Object} -- options.
* @param onOk {Function} -- custom callback function after click ok button.
* @param onCancel {Function} -- custom callback function after click cancel button.
*/
setup: function(type, content, opts, onOk, onCancel) {
// for if argument opts is not given.
var detect = typeof opts === 'function';
if (detect) {
onCancel = onOk;
onOk = opts;
}
var $oldModal = document.querySelector('.alerty');
// if previous modal is open, remove it and immediately callback
if ($oldModal) {
commonUse.removeElement($oldModal);
commonUse.removeElement(document.querySelector('.alerty-overlay'));
var _callback = this.previousCallback;
if (_callback) _callback();
}
var $wrapper = document.createElement('div');
$wrapper.innerHTML = this.template;
// append alerty to body
while ($wrapper.firstChild) {
document.body.appendChild($wrapper.firstChild);
}
// cache alerty dom for next use
var $modal = document.querySelector('.alerty');
var $overlay = document.querySelector('.alerty-overlay');
var $title = $modal.querySelector('.alerty-title');
var $message = $modal.querySelector('.alerty-message');
var $btnArea = $modal.querySelector('.alerty-action');
var $btnOk = $modal.querySelector('.btn-ok');
var $btnCancel = $modal.querySelector('.btn-cancel');
var $prompt = $modal.querySelector('.alerty-prompt');
var $input = $prompt.querySelector('input');
// set uid
$modal.id = commonUse.setUid('alerty');
$overlay.id = 'overlay-'+$modal.id;
// animation show alerty
commonUse.addClass($overlay, 'active');
commonUse.addClass($modal, 'alerty-show');
$message.innerHTML = content; // set msg
if (opts && opts.time) this.defaults.time = opts.time; // handle time if set
if (type !== 'prompt') {
commonUse.removeElement($prompt); // other type do not need
} else {
$input.focus(); // auto focus input if type prompt
if(opts && opts.inputType) $input.setAttribute('type', opts.inputType); // handle input type, such as 'password'
if(opts && opts.inputPlaceholder) $input.setAttribute('placeholder', opts.inputPlaceholder); // handle input placeholder
if(opts && opts.inputValue) $input.setAttribute('value', opts.inputValue); // handle input default value
}
if (type === 'toasts') {
this.previousCallback = onOk; // cache callback
// rearrange template
commonUse.removeElement($title);
commonUse.removeElement($btnArea);
commonUse.removeElement($overlay);
commonUse.addClass($modal, 'toasts');
if (opts && opts.place === 'top') commonUse.addClass($modal, 'place-top'); // handle toasts top place
if (opts && opts.bgColor) $modal.style.backgroundColor = opts.bgColor;
if (opts && opts.fontColor) $message.style.color =opts.fontColor;
} else {
commonUse.addClass(document.body, 'no-scrolling'); // body no scorll
(opts && opts.title) ? $title.innerHTML = opts.title : commonUse.removeElement($title); // handle title if set
(opts && opts.okLabel) ? $btnOk.innerHTML = opts.okLabel : $btnOk.innerHTML = this.defaults.okLabel; // handle ok text if set
$modal.style.marginTop = - $modal.offsetHeight / 2 + 'px'; // set the place to center using margin-top;
if (type === 'confirm' || type === 'prompt') {
(opts && opts.cancelLabel) ? $btnCancel.innerHTML = opts.cancelLabel : $btnCancel.innerHTML = this.defaults.cancelLabel; // handle cancel text if set
} else {
commonUse.removeElement($btnCancel); // toasts and alery type do not need cancel btn
}
}
this.bindEvent($modal, onOk, onCancel); // see next
},
/**
* [Bind event to dialog]
*
* @param $modal {Object} -- modal node.
* @param: onOk {Function} -- ok callback.
* @param: onCancel {Function} -- cancel callback.
*/
bindEvent: function($modal, onOk, onCancel) {
var that = this;
var $btnOk = $modal.querySelector('.btn-ok');
var $btnCancel = $modal.querySelector('.btn-cancel');
// toasts delay hide
if (commonUse.hasClass($modal, 'toasts')) {
setTimeout(function() {
// if toasts has been removed
if (document.getElementById($modal.id) === null) return;
that.close($modal, onOk);
}, that.defaults.time);
}
// click ok button
if ($btnOk) {
commonUse.addEvent($btnOk, 'click', function() {
that.close($modal, onOk);
});
}
// click cancel button
if ($btnCancel) {
commonUse.addEvent($btnCancel, 'click', function() {
that.close($modal, onCancel);
});
}
},
/**
* [Close the actived modal and remove it]
*
* @param: $modal {Obejct} -- modal element to remove.
* @param: callback {Function} -- callback function.
*/
close: function($modal, callback) {
var $input = $modal.querySelector('input');
var $overlay = document.getElementById('overlay-'+$modal.id);
// hide alerty with animation
commonUse.removeClass($modal, 'alerty-show');
commonUse.addClass($modal, 'alerty-hide');
// remove alerty and other added elements
setTimeout(function(){
$overlay && commonUse.removeClass($overlay, 'active'), commonUse.removeClass(document.body, 'no-scrolling');
commonUse.removeElement($modal);
commonUse.removeElement($overlay);
if (callback) {
setTimeout(function(){
!$input ? callback() : callback($input.value); // handle prompt type, callback the input value
}, 100);
}
},100);
}
};
return {
// return alerty.toasts();
toasts: function(content, opts, callback) {
Dialog.setup('toasts', content, opts, callback);
},
// return alerty.alert();
alert: function(content, opts, onOk) {
Dialog.setup('alert', content, opts, onOk);
},
// return alerty.confirm();
confirm: function(content, opts, onOk, onCancel) {
Dialog.setup('confirm', content, opts, onOk, onCancel);
},
// return alerty.prompt();
prompt: function(content, opts, onOk, oncancel) {
Dialog.setup('prompt', content, opts, onOk, oncancel);
}
};
};
// NPM, AMD, and wndow support
if ('undefined' !== typeof module && !! module && !! module.exports) {
module.exports = function() {
return new Alerty();
};
var obj = new Alerty();
for (var key in obj) {
module.exports[key] = obj[key];
}
} else if (typeof define === 'function' && define.amd) {
define(function() {
return new Alerty();
});
} else {
window.alerty = new Alerty();
}
}());