-
Notifications
You must be signed in to change notification settings - Fork 41
/
vanillatoasts.js
175 lines (150 loc) · 4.72 KB
/
vanillatoasts.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
(function (root, factory) {
try {
// commonjs
if (typeof exports === 'object') {
module.exports = factory();
// global
} else {
root.VanillaToasts = factory();
}
} catch (error) {
console.log('Isomorphic compatibility is not supported at this time for VanillaToasts.')
}
})(this, function () {
// We need DOM to be ready
if (document.readyState === 'complete') {
init();
} else {
window.addEventListener('DOMContentLoaded', init);
}
// Create VanillaToasts object
VanillaToasts = {
// In case toast creation is attempted before dom has finished loading!
create: function () {
console.error([
'DOM has not finished loading.',
'\tInvoke create method when DOM\s readyState is complete'
].join('\n'))
},
//function to manually set timeout after create
setTimeout: function () {
console.error([
'DOM has not finished loading.',
'\tInvoke create method when DOM\s readyState is complete'
].join('\n'))
},
toasts: {} //store toasts to modify later
};
var autoincrement = 0;
// Initialize library
function init() {
// Toast container
var container = document.createElement('div');
container.id = 'vanillatoasts-container';
document.body.appendChild(container);
// @Override
// Replace create method when DOM has finished loading
VanillaToasts.create = function (options) {
var toast = document.createElement('div');
toast.id = ++autoincrement;
toast.id = 'toast-' + toast.id;
toast.className = 'vanillatoasts-toast';
// title
if (options.title) {
var h4 = document.createElement('h4');
h4.className = 'vanillatoasts-title';
h4.innerHTML = options.title;
toast.appendChild(h4);
}
// text
if (options.text) {
var p = document.createElement('p');
p.className = 'vanillatoasts-text';
p.innerHTML = options.text;
toast.appendChild(p);
}
// icon
if (options.icon) {
var img = document.createElement('img');
img.src = options.icon;
img.className = 'vanillatoasts-icon';
toast.appendChild(img);
}
if (options.onHide) {
// do something
}
// position
var position = options.positionClass
switch (position) {
case 'topLeft':
container.classList.add('toasts-top-left');
break;
case 'bottomLeft':
container.classList.add('toasts-bottom-left');
break;
case 'bottomRight':
container.classList.add('toasts-bottom-right');
break;
case 'topRight':
container.classList.add('toasts-top-right');
break;
case 'topCenter':
container.classList.add('toasts-top-center');
break;
case 'bottomCenter':
container.classList.add('toasts-bottom-center');
break;
default:
container.classList.add('toasts-top-right');
break;
}
// click callback
if (typeof options.callback === 'function') {
toast.addEventListener('click', options.callback);
}
// toast api
toast.hide = function () {
toast.className += ' vanillatoasts-fadeOut';
toast.addEventListener('animationend', removeToast, false);
if (options.onHide) {
options.onHide();
}
}
// single
if (options.single === true) {
var elements = document.getElementsByClassName('vanillatoasts-toast');
while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]);
}
}
// autohide
if (options.timeout) {
setTimeout(toast.hide, options.timeout);
}
if (options.type) {
toast.className += ' vanillatoasts-' + options.type;
}
toast.addEventListener('click', toast.hide);
function removeToast() {
document.getElementById('vanillatoasts-container').removeChild(toast);
delete VanillaToasts.toasts[toast.id]; //remove toast from object
}
document.getElementById('vanillatoasts-container').appendChild(toast);
//add toast to object so its easily gettable by its id
VanillaToasts.toasts[toast.id] = toast;
return toast;
}
/*
custom function to manually initiate timeout of
the toast. Useful if toast is created as persistant
because we don't want it to start to timeout until
we tell it to
*/
VanillaToasts.setTimeout = function (toastid, val) {
if (VanillaToasts.toasts[toastid]) {
setTimeout(VanillaToasts.toasts[toastid].hide, val);
}
}
}
return VanillaToasts;
});