-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToasts.js
225 lines (205 loc) · 6.18 KB
/
Toasts.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
/**
* Creates simple non-intrusive pop-up notifications.
* Last modified: 1691250473268
* @author Arashiryuu0
* @module Toasts
* @version 1.1.0
*/
/*
jshint
undef: true,
noarg: true,
devel: true,
jquery: true,
strict: true,
eqeqeq: true,
freeze: true,
newcap: true,
esnext: true,
browser: true,
latedef: true,
shadow: outer,
varstmt: false,
laxbreak: true,
quotmark: single,
singleGroups: true,
futurehostile: true
*/
;(function (mw) {
'use strict';
var toString = Object.prototype.toString;
if (window.dev && window.dev.toasts) return;
window.importArticles({
type: 'style',
articles: [
'u:dev:MediaWiki:Toasts.css'
]
});
function log (level) {
var parts = [
'%c[Toasts] %o',
'color: #C9F',
new Date().toUTCString()
];
var method = level in console
? level
: 'log';
return function () {
console.groupCollapsed.apply(null, parts);
console[method].apply(null, arguments);
console.groupEnd();
};
}
function isObject (item) {
return toString.call(item) === '[object Object]';
}
function deepFreeze (object, exclude) {
if (exclude && exclude(object)) return;
if (typeof object === 'object' && object !== null) {
var props = Object.getOwnPropertyNames(object);
var len = props.length;
for (var i = 0; i < len; i++) {
var key = props[i];
deepFreeze(object[key], exclude);
}
}
Object.freeze(object);
return object;
}
function create (tag, props, children) {
var el = document.createElement(tag);
Object.assign(el, props);
if (Array.isArray(children)) el.append.apply(el, children);
return el;
}
function createNS (tag, props, children) {
var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var key in props) {
el.setAttribute(key, props[key]);
}
if (Array.isArray(children)) el.append.apply(el, children);
return el;
}
function createIcon (d) {
return createNS('svg', {
width: 20,
height: 20,
viewBox: '0 0 24 24'
}, [
createNS('path', { d: 'M0 0h24v24H0z', fill: 'none' }),
createNS('path', {
d: d
})
]);
}
var onError = log('error');
var helpers = {
ensureContainer: function () {
if (document.querySelector('.toasts')) return;
var wrapper = create('div', { className: 'toasts' });
wrapper.style.setProperty('width', document.documentElement.offsetWidth + 'px');
wrapper.style.setProperty('bottom', '80px');
document.body.appendChild(wrapper);
},
buildToast: function (message, type, icon) {
var name = ['toast'];
if (type || icon) name.push('toast-has-icon');
if (type !== '') name.push('toast-' + type);
if (!icon && type) icon = type;
var html = create('div', { className: name.join(' ') }, [
this.icons[icon] && create('div', { className: 'toast-icon' }, [
this.icons[icon].cloneNode(true)
]),
create('div', { className: 'toast-text', textContent: message })
].filter(Boolean));
return html;
},
parseType: function (type, types) {
return types[type] || types['default'];
},
icons: {
warning: createIcon('M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z'),
success: 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'
),
info: 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'
),
error: 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'
)
}
};
function defaultToast (content, options) {
content = typeof content === 'string'
? content
: '';
options = isObject(options)
? options
: {};
helpers.ensureContainer();
var toast = helpers.buildToast(
content,
helpers.parseType(arguments[2], defaultToast.types),
helpers.parseType(options.icon, defaultToast.types)
);
document.querySelector('.toasts').appendChild(toast);
new Promise(function (resolve) {
var timeout = options.timeout
? options.timeout
: 3000;
setTimeout(resolve, timeout);
})
.then(function () {
toast.classList.add('closing');
return new Promise(function (resolve) {
setTimeout(resolve, 300);
});
}, onError)
.then(function () {
toast.parentElement.removeChild(toast);
if (document.querySelectorAll('.toasts .toast').length) return;
var toasts = document.querySelector('.toasts');
toasts.parentElement.removeChild(toasts);
}, onError);
}
Object.assign(defaultToast, {
show: function (content, options) {
return defaultToast(content, options, options.type);
},
info: function (content, options) {
return defaultToast(content, options, 'info');
},
error: function (content, options) {
return defaultToast(content, options, 'error');
},
success: function (content, options) {
return defaultToast(content, options, 'success');
},
warning: function (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);
})(window.mediaWiki);
/*@end@*/