-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpletoast.js
244 lines (234 loc) · 6.98 KB
/
simpletoast.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
/*
* SimpleToast - A small library for toasts
*/
((root, factory) => {
// Do we care about frames? Until I get some tests in... no
if (window !== window.top) return;
const boundToast = window.SimpleToast;
const localToast = factory();
root.SimpleToast = localToast;
console.log(`SimpleToast(v${localToast.versionString}): Loaded`);
// Apply to window if SimpleToast doesn't exist or is outdated
if (root !== window && (!boundToast?.version || boundToast.version < localToast.version)) {
window.SimpleToast = localToast;
console.log(`SimpleToast(v${localToast.versionString}): Publicized`);
}
})(this, () => {
const version = buildVersion(2, 0, 1);
const style = {
root: {
display: 'flex',
'flex-direction': 'column-reverse',
'align-items': 'flex-end',
position: 'fixed',
'white-space': 'pre-wrap',
bottom: 0,
right: 0,
zIndex: 1000,
},
title: {
display: 'block',
fontSize: '15px',
'font-style': 'italic',
},
toast: {
maxWidth: '320px',
padding: '5px 8px',
borderRadius: '3px',
fontFamily: 'cursive, sans-serif',
fontSize: '13px',
cursor: 'pointer',
color: '#fafeff',
margin: '4px',
textShadow: '#3498db 1px 2px 1px',
background: '#2980b9',
},
footer: {
display: 'block',
fontSize: '10px',
},
button: {
height: '20px',
margin: '-3px 0 0 3px',
padding: '0 5px',
verticalAlign: 'middle',
whiteSpace: 'nowrap',
border: '1px solid rgba(27,31,35,0.2)',
borderRadius: '10px',
fontSize: '11px',
textShadow: '#173646 0px 0px 3px',
background: '#2c9fea',
mouseOver: {
'border-color': 'rgba(27,31,35,0.35)',
background: '#149FFF',
},
},
};
function applyCSS(element, css = {}) {
const old = {};
Object.keys(css).forEach((key) => {
const val = css[key];
if (typeof val === 'object') return;
old[key] = element.style[key];
element.style[key] = css[key];
});
return old;
}
const toasts = new Map();
let root = (() => {
function create() {
const el = document.createElement('div');
el.setAttribute('id', 'AlertToast');
applyCSS(el, style.root);
const body = document.getElementsByTagName('body')[0];
if (body) { // Depending on when the script is loaded... this might be null
body.appendChild(el);
} else {
window.addEventListener('load', () => {
const exists = document.getElementById(el.id);
if (exists) { // Another script may have created it already
if (el.hasChildNodes()) { // Transfer existing nodes to new root
const nodes = el.childNodes;
for (let i = 0, l = nodes.length; i < l; i++) {
exists.appendChild(nodes[i]);
}
}
root = exists; // Set this incase anyone still has a reference to this toast
return;
}
document.getElementsByTagName('body')[0].appendChild(el);
});
}
return el;
}
return document.getElementById('AlertToast') || create();
})();
let count = 0;
let timeout = null;
function startTimeout() {
if (timeout) return;
timeout = setTimeout(() => {
timeout = null;
const now = Date.now();
let pending = 0;
toasts.forEach((toast) => {
if (!toast.timeout) return;
if (now < toast.timeout) {
pending += 1;
return;
}
toast.close('timeout');
});
if (pending) {
startTimeout();
}
}, 1000);
}
function noop() {}
const blankToast = Object.freeze({
setText: noop,
exists: () => false,
close: noop,
});
function Toast({title, text, footer, className, css = {}, buttons, timeout, onClose} = {}) {
if (typeof arguments[0] === 'string') {
text = arguments[0];
}
if (!text) return blankToast;
const id = count++;
const el = document.createElement('div');
const tel = el.appendChild(document.createElement('span'));
const body = el.appendChild(document.createElement('span'));
const fel = el.appendChild(document.createElement('span'));
if (className) {
const clazz = className.toast || className;
el.className = Array.isArray(clazz) ? clazz.join(' ') : (typeof clazz === 'string' ? clazz : undefined);
}
applyCSS(el, style.toast);
applyCSS(el, css.toast || css);
// Add title, body
if (title) {
applyCSS(tel, style.title);
applyCSS(tel, css.title);
tel.innerHTML = title;
}
body.innerHTML = text;
if (footer) {
applyCSS(fel, style.footer);
applyCSS(fel, css.footer);
fel.innerHTML = footer;
}
const safeToast = {};
const toast = {
setText: (newText) => {
if (!newText || !toast.exists()) return;
body.innerHTML = newText;
},
exists: () => toasts.has(id),
close: (closeType) => {
if (!toast.exists()) return;
root.removeChild(el);
toasts.delete(id);
if (typeof onClose === 'function') {
onClose.call(safeToast, closeType || 'unknown', safeToast);
}
},
};
if (timeout) {
toast.timeout = Date.now() + timeout;
}
if (typeof buttons === 'object') {
if (!Array.isArray(buttons)) {
buttons = [buttons];
}
buttons.forEach((button) => {
if (!button.text) return;
const elb = document.createElement('button');
if (button.className || className && className.button) {
const clazz = button.className || className.button;
elb.className = Array.isArray(clazz) ? clazz.join(' ') : clazz;
}
elb.innerHTML = button.text;
applyCSS(elb, style.button);
applyCSS(elb, css.button);
applyCSS(elb, button.css);
if (typeof button.onclick === 'function') {
elb.onclick = button.onclick;
}
let prev = {};
elb.onmouseover = () => {
const hoverStyle = Object.assign(
{},
style.button.mouseOver,
css.button && css.button.mouseOver,
button.css && button.css.mouseOver
);
prev = applyCSS(hoverStyle);
};
elb.onmouseout = () => {
applyCSS(elb, prev);
prev = {};
};
el.insertBefore(elb, fel);
});
}
el.addEventListener('click', toast.close.bind(null, 'dismissed'));
root.appendChild(el);
toasts.set(id, toast);
if (timeout) {
startTimeout();
}
Object.keys(blankToast).forEach((key) => safeToast[key] = toast[key]);
return safeToast;
}
Toast.version = version.number;
Toast.versionString = version.string;
Toast.count = () => toasts.size;
function buildVersion(major, minor = 0, patch = 0) {
return {
string: `${major}.${minor}${patch ? `.${patch}` : ''}`,
number: major * 1000000000 + minor * 1000 + patch,
};
}
return Object.freeze(Toast);
});