-
Notifications
You must be signed in to change notification settings - Fork 12
/
vanilla-notify.js
232 lines (193 loc) · 6.03 KB
/
vanilla-notify.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
var vNotify = (function() {
var positionOption = {
topLeft: 'topLeft',
topRight: 'topRight',
bottomLeft: 'bottomLeft',
bottomRight: 'bottomRight',
center: 'center'
};
var options = {
fadeInDuration: 1000,
fadeOutDuration: 1000,
fadeInterval: 50,
visibleDuration: 5000,
postHoverVisibleDuration: 500,
position: positionOption.topRight,
sticky: false,
showClose: true
};
var info = function(params) {
params.notifyClass = 'vnotify-info';
return addNotify(params);
};
var success = function(params) {
params.notifyClass = 'vnotify-success';
return addNotify(params);
};
var error = function(params) {
params.notifyClass = 'vnotify-error';
return addNotify(params);
};
var warning = function(params) {
params.notifyClass = 'vnotify-warning';
return addNotify(params);
};
var notify = function(params) {
params.notifyClass = 'vnotify-notify';
return addNotify(params);
};
var custom = function(params) {
return addNotify(params);
};
var addNotify = function(params) {
if (!params.title && !params.text) {
return null;
}
var frag = document.createDocumentFragment();
var item = document.createElement('div');
item.classList.add('vnotify-item');
item.classList.add(params.notifyClass);
item.style.opacity = 0;
item.options = getOptions(params);
if (params.title) {
item.appendChild(addTitle(params.title));
}
if (params.text) {
item.appendChild(addText(params.text));
}
if (item.options.showClose) {
item.appendChild(addClose(item));
}
item.visibleDuration = item.options.visibleDuration; //option
var hideNotify = function() {
item.fadeInterval = fade('out', item.options.fadeOutDuration, item);
};
var resetInterval = function() {
clearTimeout(item.interval);
clearTimeout(item.fadeInterval);
item.style.opacity = null;
item.visibleDuration = item.options.postHoverVisibleDuration;
};
var hideTimeout = function () {
item.interval = setTimeout(hideNotify, item.visibleDuration);
};
frag.appendChild(item);
var container = getNotifyContainer(item.options.position);
container.appendChild(frag);
item.addEventListener("mouseover", resetInterval);
fade('in', item.options.fadeInDuration, item);
if (!item.options.sticky){
item.addEventListener("mouseout", hideTimeout);
hideTimeout();
}
return item;
};
var addText = function(text) {
var item = document.createElement('div');
item.classList.add('vnotify-text');
item.innerHTML = text;
return item;
};
var addTitle = function(title) {
var item = document.createElement('div');
item.classList.add('vnotify-title');
item.innerHTML = title;
return item;
};
var addClose = function(parent) {
var item = document.createElement('span');
item.classList.add('vn-close');
item.addEventListener('click', function(){remove(parent);});
return item;
};
var getNotifyContainer = function(position) {
var positionClass = getPositionClass(position);
var container = document.querySelector('.' + positionClass);
return container ? container : createNotifyContainer(positionClass);
};
var createNotifyContainer = function(positionClass) {
var frag = document.createDocumentFragment();
container = document.createElement('div');
container.classList.add('vnotify-container');
container.classList.add(positionClass);
container.setAttribute('role', 'alert');
frag.appendChild(container);
document.body.appendChild(frag);
return container;
};
var getPositionClass = function(option) {
switch (option) {
case positionOption.topLeft:
return 'vn-top-left';
case positionOption.bottomRight:
return 'vn-bottom-right';
case positionOption.bottomLeft:
return 'vn-bottom-left';
case positionOption.center:
return 'vn-center';
default:
return 'vn-top-right';
}
};
var getOptions = function(opts) {
return {
fadeInDuration: opts.fadeInDuration || options.fadeInDuration,
fadeOutDuration: opts.fadeOutDuration || options.fadeOutDuration,
fadeInterval: opts.fadeInterval || options.fadeInterval,
visibleDuration: opts.visibleDuration || options.visibleDuration,
postHoverVisibleDuration: opts.postHoverVisibleDuration || options.postHoverVisibleDuration,
position: opts.position || options.position,
sticky: opts.sticky != null ? opts.sticky : options.sticky,
showClose: opts.showClose != null ? opts.showClose : options.showClose
};
};
var remove = function(item) {
item.style.display = 'none';
item.outerHTML = '';
item = null;
};
//New fade - based on http://toddmotto.com/raw-javascript-jquery-style-fadein-fadeout-functions-hugo-giraudel/
var fade = function(type, ms, el) {
var isIn = type === 'in',
opacity = isIn ? 0 : el.style.opacity || 1,
goal = isIn ? 0.8 : 0,
gap = options.fadeInterval / ms;
if(isIn) {
el.style.display = 'block';
el.style.opacity = opacity;
}
function func() {
opacity = isIn ? opacity + gap : opacity - gap;
el.style.opacity = opacity;
if(opacity <= 0) {
remove(el);
checkRemoveContainer();
}
if((!isIn && opacity <= goal) || (isIn && opacity >= goal)) {
window.clearInterval(fading);
}
}
var fading = window.setInterval(func, options.fadeInterval);
return fading;
};
var checkRemoveContainer = function() {
var item = document.querySelector('.vnotify-item');
if (!item) {
var container = document.querySelectorAll('.vnotify-container');
for (var i=0; i< container.length; i++) {
container[i].outerHTML = '';
container[i] = null;
}
}
};
return {
info: info,
success: success,
error: error,
warning: warning,
notify: notify,
custom: custom,
options: options,
positionOption: positionOption
};
})();