-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (81 loc) · 2.05 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
/**
* Notice
*
* A notice message at the top of a webpage.
*
* Copyright (c) 2014 by Hsiaoming Yang.
*/
var query = require('query');
var events = require('event');
var COUNT = 0;
function Notice(options) {
var el = createElement(options);
el.id = 'notice-' + (COUNT++);
this.el = el;
}
Notice.prototype.show = function() {
if (document.getElementById(this.el.id)) return;
var container = query('.notice-container');
if (!container) {
container = document.createElement('div');
container.className = 'notice-container';
document.body.appendChild(container);
}
container.appendChild(this.el);
};
Notice.prototype.hide = Notice.prototype.clear = function() {
dismiss(this.el);
};
function createElement(options) {
// div.notice-item
// span.notice-close
// div.notice-content
var container = document.createElement('div');
container.className = 'notice-item';
if (options.type) {
container.className += ' ' + options.type;
}
var content;
if (options.url) {
content = document.createElement('a');
content.href = options.url;
content.target = '_blank';
} else {
content = document.createElement('div');
}
content.className = 'notice-content';
content.innerHTML = options.message;
var close = document.createElement('span');
close.className = 'notice-close';
close.innerHTML = '×';
container.appendChild(close);
container.appendChild(content);
var eventType = options.closeEvent || 'click';
events.bind(close, eventType, function(e) {
dismiss(container);
});
return container;
}
function dismiss(el) {
el.className += ' notice-dismiss';
setTimeout(function() {
if (el && el.parentNode) {
el.parentNode.removeChild(el);
}
}, 200);
}
function notify(options, cb) {
if (!options) return;
if (!options.message) {
options = {message: options};
}
var time = options.duration || 4000;
var item = new Notice(options);
item.show();
setTimeout(function() {
item.clear();
cb && cb();
}, time);
}
notify.Notice = Notice;
module.exports = notify;