-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToasts6.js
235 lines (214 loc) · 6.18 KB
/
Toasts6.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
/**
* Creates simple non-intrusive pop-up notifications.
* Last modified: 1741578597967
* @author Arashiryuu0
* @module Toasts
* @version 1.2.0
*/
/*
jshint
undef: true,
noarg: true,
devel: true,
typed: true,
jquery: true,
strict: true,
eqeqeq: true,
freeze: true,
newcap: true,
browser: true,
latedef: true,
shadow: outer,
varstmt: true,
quotmark: single,
laxbreak: true,
esversion: 11,
singleGroups: true,
futurehostile: true
*/
/*
globals
mw
*/
'use strict';
__main__: {
if (window.dev && window.dev.toasts) break __main__;
const toString = Object.prototype.toString;
window.importArticles({
type: 'style',
articles: [
'u:dev:MediaWiki:Toasts.css'
]
});
const log = (level) => {
const parts = [
'%c[Toasts] %o',
'color: #C9F',
new Date().toUTCString()
];
const method = level in console
? level
: 'log';
return (...args) => {
console.groupCollapsed(...parts);
console[method](...args);
console.groupEnd();
};
};
const isObject = (item) => toString.call(item) === '[object Object]';
const deepFreeze = (object, exclude) => {
if (exclude && exclude(object)) return;
if (typeof object === 'object' && object !== null) {
const props = Object.getOwnPropertyNames(object);
const len = props.length;
for (let i = 0; i < len; i++) {
const key = props[i];
deepFreeze(object[key], exclude);
}
}
Object.freeze(object);
return object;
};
const create = (tag, props, children = []) => {
const el = document.createElement(tag);
Object.assign(el, props);
if (Array.isArray(children)) el.append(...children);
return el;
};
const createNS = (tag, props, children = []) => {
const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (const key in props) {
el.setAttribute(key, props[key]);
}
if (Array.isArray(children)) el.append(...children);
return el;
};
const createIcon = (d) => createNS('svg', {
width: 20,
height: 20,
viewBox: '0 0 24 24'
}, [
createNS('path', { d: 'M0 0h24v24H0z', fill: 'none' }),
createNS('path', {
d: d
})
]);
const onError = log('error');
const helpers = {
ensureContainer () {
if (document.querySelector('.toasts')) return;
const wrapper = create('div', { className: 'toasts' });
wrapper.style.setProperty('width', document.documentElement.offsetWidth + 'px');
wrapper.style.setProperty('bottom', '80px');
document.body.appendChild(wrapper);
},
buildToast (message, type, icon) {
const name = ['toast'];
if (type || icon) name.push('toast-has-icon');
if (type !== '') name.push('toast-' + type);
if (!icon && type) icon = type;
const html = create('div', { className: name.join(' ') }, [
Object.hasOwn(this.icons, icon) && create('div', { className: 'toast-icon' }, [
this.icons[icon]
]),
create('div', { className: 'toast-text', textContent: message })
].filter(Boolean));
return html;
},
parseType (type, types) {
return types[type] || types['default'];
},
icons: {
get warning () {
return createIcon('M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z');
},
get success () {
return createIcon(
'M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 '
+ '10-10S17.52 2 12 2zm-2 '
+ '15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z'
);
},
get info () {
return createIcon(
'M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 '
+ '10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z'
);
},
get error () {
return createIcon(
'M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 '
+ '10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z'
);
}
}
};
const defaultToast = function (content, options) {
content = typeof content === 'string'
? content
: '';
options = isObject(options)
? options
: {};
helpers.ensureContainer();
const toast = helpers.buildToast(
content,
helpers.parseType(arguments[2], defaultToast.types),
helpers.parseType(options.icon, defaultToast.types)
);
document.querySelector('.toasts').appendChild(toast);
new Promise((resolve) => {
const timeout = options.timeout
? options.timeout
: 3000;
setTimeout(resolve, timeout);
})
.then(() => {
toast.classList.add('closing');
return new Promise((resolve) => {
setTimeout(resolve, 300);
});
}, onError)
.then(() => {
toast.parentElement.removeChild(toast);
if (document.querySelectorAll('.toasts .toast').length) return;
const toasts = document.querySelector('.toasts');
toasts.parentElement.removeChild(toasts);
}, onError);
};
Object.assign(defaultToast, {
show (content, options) {
return defaultToast(content, options, options.type);
},
info (content, options) {
return defaultToast(content, options, 'info');
},
error (content, options) {
return defaultToast(content, options, 'error');
},
success (content, options) {
return defaultToast(content, options, 'success');
},
warning (content, options) {
return defaultToast(content, options, 'warning');
},
types: {
'default': '',
'warning': 'warning',
'success': 'success',
'error': 'error',
'info': 'info'
}
});
Object.defineProperty(defaultToast, Symbol.toStringTag, {
configurable: false,
enumerable: false,
writable: false,
value: 'Toasts'
});
deepFreeze(defaultToast);
window.dev = window.dev || {};
window.dev.toasts = defaultToast;
mw.hook('dev.toasts').fire(window.dev.toasts);
};
/*@end@*/