-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2.toasts.js
More file actions
133 lines (123 loc) · 3.35 KB
/
2.toasts.js
File metadata and controls
133 lines (123 loc) · 3.35 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
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
import Translation from 'src/structures/constants/translation.js';
import { buttonCSS } from './1.variables.js';
import merge from './merge.js';
import eventManager from './eventManager.js';
import toArray from './toArray.js';
let ready = false;
eventManager.on('underscript:ready', () => ready = true);
export function blankToast() {
return new SimpleToast();
}
export function toast(arg) {
if (!arg) return false;
if (typeof arg === 'string' || arg instanceof Translation) {
arg = {
text: arg,
};
}
const isTranslation = [
arg.text,
arg.title,
...toArray(arg.buttons).map(({ text }) => text),
].some((obj) => obj instanceof Translation);
const defaults = {
footer: 'via UnderScript',
css: {
'background-color': 'rgba(0,5,20,0.6)',
'text-shadow': '',
'font-family': 'monospace',
footer: {
'text-align': 'end',
},
},
};
if (ready && isTranslation) preprocess(arg);
const slice = new SimpleToast(merge(defaults, arg));
if (!ready && isTranslation && slice.exists()) {
const el = $('#AlertToast > div:last');
eventManager.on('underscript:ready', () => {
process(slice, el, arg);
});
}
return slice;
}
export function errorToast(error) {
function getStack(err = {}) {
const stack = err.stack;
if (stack) {
return stack.replace('<', '<');
}
return null;
}
const lToast = {
title: error.name || error.title || Translation.ERROR,
text: error.message || error.text || getStack(error.error || error) || error,
css: {
'background-color': 'rgba(200,0,0,0.6)',
},
className: error.className,
onClose: error.onClose,
footer: error.footer,
buttons: error.buttons,
};
return toast(lToast);
}
export function infoToast(arg, key, val = '1') {
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: Translation.INFO,
css: {
'font-family': 'inherit',
},
};
return toast(merge(defaults, arg, override));
}
export function dismissable({ title, text, key, value = 'true', css = {} }) {
if (localStorage.getItem(key) === value) return undefined;
const buttons = {
text: Translation.DISMISS,
className: 'dismiss',
css: buttonCSS,
onclick: (e) => {
localStorage.setItem(key, value);
},
};
return toast({
title,
text,
buttons,
className: 'dismissable',
css,
});
}
function preprocess(arg) {
['text', 'title'].forEach((prop) => {
if (arg[prop] instanceof Translation) arg[prop] = `${arg[prop]}`;
});
toArray(arg.buttons).forEach((button) => {
if (button.text instanceof Translation) button.text = `${button.text}`;
});
}
function process(instance, el, { text, title, buttons }) {
if (text instanceof Translation) instance.setText(`${text}`);
if (title instanceof Translation) el.find('> span:first').text(title);
const $buttons = el.find('> button');
toArray(buttons).forEach((button, i) => {
if (button.text instanceof Translation) $buttons[i].textContent = button.text;
});
}