-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2.toasts.js
More file actions
93 lines (89 loc) · 1.98 KB
/
2.toasts.js
File metadata and controls
93 lines (89 loc) · 1.98 KB
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
fn.toast = (arg) => {
// Why do I even check for SimpleToast? It *has* to be loaded at this point...
if (!window.SimpleToast || !arg) return false;
if (typeof arg === 'string') {
arg = {
text: arg,
};
}
const defaults = {
footer: 'via UnderScript',
css: {
'background-color': 'rgba(0,5,20,0.6)',
'text-shadow': '',
'font-family': 'monospace',
footer: {
'text-align': 'end',
},
},
};
return new SimpleToast(fn.merge(defaults, arg));
};
fn.errorToast = (error) => {
function getStack(err = {}) {
const stack = err.stack;
if (stack) {
return stack.replace('<', '<');
}
return null;
}
const toast = {
title: error.name || error.title || 'Error',
text: error.message || error.text || getStack(error.error || error) || error,
css: {
'background-color': 'rgba(200,0,0,0.6)',
},
};
if (error.footer) {
toast.footer = error.footer;
}
return fn.toast(toast);
};
fn.infoToast = (arg, key, val) => {
if (localStorage.getItem(key) === val) return null;
if (typeof arg === 'string') {
arg = {
text: arg,
};
} else if (typeof arg !== 'object') return null;
const override = {
onClose: (...args) => {
if (typeof arg.onClose === 'function') {
if (arg.onClose(...args)) {
return;
}
}
localStorage.setItem(key, val);
},
};
const defaults = {
title: 'Did you know?',
css: {
'font-family': 'inherit',
},
};
return fn.toast(fn.merge(defaults, arg, override));
};
fn.dismissable = ({ title, text, key, value = true }) => {
const buttons = {
text: 'Dismiss',
className: 'dismiss',
css: {
border: '',
height: '',
background: '',
'font-size': '',
margin: '',
'border-radius': '',
},
onclick: (e) => {
localStorage.setItem(key, value);
},
};
return fn.toast({
title,
text,
buttons,
className: 'dismissable',
});
};