-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
311 lines (270 loc) · 9.4 KB
/
server.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const e = require("express")
const bodyparser = require("body-parser")
const app = e()
app.use(e.static("public"))
app.use(bodyparser.text({type: "*/*"}))
app.post("/minify", async (req, res) => {
res.send(`
/*
* Notification js
* https://amaterasusan.github.io/notification
* @license MIT licensed
*
* Copyright (C) 2024 Helen Nikitina
*/
function Notification(options = {}) {
let opts = {};
let timeouts = {};
const defNumberOpened = 5;
const defMaxNumberOpened = 10;
const defDuration = 5000;
const allowedPosition = ['top-right', 'bottom-right', 'top-left', 'bottom-left', 'center'];
const defaultOpts = {
position: 'top-right',
duration: defDuration,
isHidePrev: false,
isHideTitle: false,
maxOpened: defNumberOpened,
};
const setProperty = (obj = {}) => {
const defOpts = Object.keys(opts).length ? opts : defaultOpts;
opts = !!obj && obj.constructor.name === 'Object' ? Object.assign({}, defOpts, obj) : defOpts;
// check position
if (!allowedPosition.includes(opts.position)) {
opts.position = allowedPosition[0];
}
// check duration
opts.duration = parseInt(opts.duration);
if (isNaN(opts.duration) || (opts.duration < 1000 && opts.duration !== 0)) {
opts.duration = defDuration;
}
// check maxOpened
opts.maxOpened = parseInt(opts.maxOpened);
if (isNaN(opts.maxOpened) || opts.maxOpened < 1 || opts.maxOpened > defMaxNumberOpened) {
opts.maxOpened = defNumberOpened;
}
};
setProperty(options);
// selectors
const classContainer = 'notification-container';
const classPopup = 'notification';
const animationInClass = 'animation-slide-in';
const animationOutClass = 'animation-slide-out';
const animationFadeInClass = 'animation-fade-in';
const animationFadeOutClass = 'animation-fade-out';
const titleTextSel = '.notification-title .title';
const descSel = '.notification-desc';
const closeSel = '.notification-close';
const actionButSel = '.notification-action';
const cancelButSel = '.notification-cancel';
const overlayClass = 'overlay';
// class, defaultTitle and defaultMessage
const dataByType = {
dialog: {
classType: 'notification-dialog',
defaultTitle: 'Confirm',
defaultMessage: 'Default Confirm message',
},
info: {
classType: 'notification-info',
defaultTitle: 'Info',
defaultMessage: 'default Info',
},
success: {
classType: 'notification-success',
defaultTitle: 'Success',
defaultMessage: 'default Success',
},
warning: {
classType: 'notification-warning',
defaultTitle: 'Warning',
defaultMessage: 'default Warning',
},
error: {
classType: 'notification-error',
defaultTitle: 'Error',
defaultMessage: 'An error has occurred',
},
};
const dialogButtons = () => {
return \`<div class="notification-buttons">
<span class="notification-button notification-cancel"></span>
<span class="notification-button notification-action"></span>
</div>\`;
};
const createOverlay = () => {
if (!document.querySelector(\`.\${overlayClass}\`)) {
const overlayEl = document.createElement('div');
overlayEl.classList.add(overlayClass);
document.body.appendChild(overlayEl);
}
document.querySelector(\`.\${overlayClass}\`).classList.add('active');
};
const tempatePopup = (type) => {
const closeEl = \`<a class="notification-close"><svg viewbox="0 0 50 50"><path class="close-x" d="M 10,10 L 30,30 M 30,10 L 10,30" /></svg></a>\`;
const titleBlock = \`<div class="notification-title"><div class="title"></div>\${closeEl}</div>\`;
return \`\${opts.isHideTitle ? '' : titleBlock}
<div class="notification-body">
<div class="notification-desc"></div>
\${opts.isHideTitle && opts.duration === 0 && type !== 'dialog' ? closeEl : ''}
</div>
\${type === 'dialog' ? dialogButtons() : ''}\`;
};
const createMainContainer = () => {
let container = document.querySelector(\`.\${classContainer}.\${opts.position}\`);
if (!container) {
container = document.createElement('div');
container.classList = \`\${classContainer} \${opts.position}\`;
document.body.appendChild(container);
}
return container;
};
const createPopup = (type) => {
const container = createMainContainer();
if (container.childElementCount >= opts.maxOpened) {
if (opts.position.includes('bottom')) {
hidePopUp(container.lastChild);
} else {
hidePopUp(container.firstChild);
}
}
const elPopup = document.createElement('div');
elPopup.classList.add(
classPopup,
opts.position === 'center' ? animationFadeInClass : animationInClass,
dataByType[type].classType
);
elPopup.dataset.type = type;
elPopup.dataset.position = opts.position;
elPopup.dataset.id = new Date().getTime();
// insert template in element
elPopup.insertAdjacentHTML('beforeend', tempatePopup(type));
// create overlay
if (type === 'dialog') {
createOverlay();
}
// add element to container in the required sequence
if (opts.position.includes('bottom')) {
container.prepend(elPopup);
} else {
container.appendChild(elPopup);
}
return elPopup;
};
const showPopup = ({ type, title, message, callback = null, validFunc = null } = {}) => {
if (opts.isHidePrev) {
hide();
}
const elPopup = createPopup(type);
// set title and message
const elTitle = elPopup.querySelector(titleTextSel);
const elText = elPopup.querySelector(descSel);
if (elTitle) {
elTitle.innerHTML = title || dataByType[type].defaultTitle;
}
elText.innerHTML = message || dataByType[type].defaultMessage;
if (type === 'dialog') {
// set buttons click event
setButtonsEvent(elPopup, callback, validFunc);
} else if (opts.duration) {
// store new timeout to timeouts obj if type is not dialog
const timeout = setTimeout(() => hidePopUp(elPopup), opts.duration);
timeouts[elPopup.dataset.id] = timeout;
}
// add click event to close element
setCloseEvent(elPopup);
};
const setButtonsEvent = (elPopup, callback = null, validFunc = null) => {
const elAction = elPopup.querySelector(actionButSel);
elAction?.addEventListener(
'click',
function handlerAction(event) {
event.stopPropagation();
event.preventDefault();
let valid = true;
if (validFunc) {
valid = validFunc();
}
if (!valid) {
return false;
}
hidePopUp(elPopup);
elAction.removeEventListener('click', handlerAction, false);
if (callback) {
return callback('ok');
}
return false;
},
false
);
const elCancel = elPopup.querySelector(cancelButSel);
elCancel?.addEventListener(
'click',
function handlerCancel(event) {
event.stopPropagation();
event.preventDefault();
hidePopUp(elPopup);
elCancel.removeEventListener('click', handlerCancel, false);
if (callback) {
return callback('cancel');
}
return false;
},
false
);
};
const setCloseEvent = (elPopup) => {
const elClose = elPopup.querySelector(closeSel);
elClose?.addEventListener(
'click',
function handlerClose() {
hidePopUp(elPopup);
elClose.removeEventListener('click', handlerClose, false);
},
false
);
};
const hidePopUp = (elPopup) => {
const container = document.querySelector(\`.\${classContainer}.\${elPopup.dataset.position}\`);
clearTimeout(timeouts[elPopup.dataset.id]);
delete timeouts[elPopup.dataset.id];
// change animation class
elPopup.classList.remove(elPopup.dataset.position === 'center' ? animationFadeInClass : animationInClass);
elPopup.classList.add(elPopup.dataset.position === 'center' ? animationFadeOutClass : animationOutClass);
if (elPopup.dataset.type === 'dialog') {
document.querySelector(\`.\${overlayClass}\`)?.classList.remove('active');
}
setTimeout(function () {
if (elPopup.parentNode === container) {
container.removeChild(elPopup);
}
// Remove container if it empty
if (!container?.hasChildNodes() && container?.parentElement === document.body) {
document.body.removeChild(container);
}
}, 400);
};
const hide = () => {
const containers = document.querySelectorAll(\`.\${classContainer}\`);
document.querySelector(\`.\${overlayClass}\`)?.classList.remove('active');
for (const key in timeouts) {
clearTimeout(timeouts[key]);
}
timeouts = {};
containers.forEach((container) => {
if (container && container.parentElement === document.body) {
document.body.removeChild(container);
}
});
};
const dialog = ({ title, message, callback = null, validFunc = null }) =>
showPopup({ type: 'dialog', title, message, callback, validFunc });
const info = ({ title, message }) => showPopup({ type: 'info', title, message });
const success = ({ title, message }) => showPopup({ type: 'success', title, message });
const warning = ({ title, message }) => showPopup({ type: 'warning', title, message });
const error = ({ title, message }) => showPopup({ type: 'error', title, message });
return { dialog, info, success, warning, error, setProperty, hide };
}
`)
})
app.listen(3000)