theshtify 1.0.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/theshtify.js CHANGED
@@ -1,730 +1,937 @@
1
- let theshtify_infos = {
2
- message: '',
3
- type: '',
4
- lang: 'en',
5
- config: {
6
- x_pos: 'right',
7
- y_pos: 'top',
8
- font: {
9
- family: 'arial',
10
- size: 14,
11
- weight: ''
12
- },
13
- min_duration: 1000,
14
- max_duration: 5000,
15
- duration: 5000,
16
- borderWidth: 2,
17
- bordered: false,
18
- progress: false,
19
- progressHeight: 2,
20
- closer: false,
21
- trans: false,
22
- display: {
23
- width: 300,
24
- colors: {
25
- custom: {
26
- color: 'white',
27
- bg: '#000',
28
- border: {
29
- type: 'solid',
30
- color: 'transparent',
31
- },
32
- progress: {
33
- bg: '#fff'
34
- }
35
- },
36
- success: {
37
- color: 'white',
38
- bg: 'rgba(1, 191, 102, 0.822)',
39
- border: {
40
- type: 'solid',
41
- color: 'transparent',
42
- },
43
- progress: {
44
- bg: '#fff'
45
- }
46
- },
47
- danger: {
48
- bg: 'red',
49
- color: 'white',
50
- border: {
51
- type: 'solid',
52
- color: 'transparent',
53
- },
54
- progress: {
55
- bg: '#fff'
56
- }
57
- },
58
- info: {
59
- bg: 'rgba(7, 133, 250, 0.822)',
60
- color: 'white',
61
- border: {
62
- type: 'solid',
63
- color: 'transparent',
64
- },
65
- progress: {
66
- bg: '#fff'
67
- }
68
- },
69
- warning: {
70
- bg: '#f89406',
71
- color: 'black',
72
- border: {
73
- type: 'solid',
74
- color: 'transparent',
75
- },
76
- progress: {
77
- bg: '#fff'
78
- }
79
- },
80
- },
81
- padding: {
82
- left: 10,
83
- right: 10,
84
- top: 10,
85
- bottom: 10
86
- },
87
- margin: {
88
- left: 20,
89
- right: 20,
90
- top: 20,
91
- bottom: 20
92
- },
93
- radius: 5,
94
- }
95
- }
96
- };
97
- const types = ['success', 'info', 'danger', 'warning', 'custom'];
98
- const positions = {
99
- x: ["middle", "left", "right"],
100
- y: ["middle", "top", "bottom"],
101
- }
102
- const messages = {
103
- en: {
104
- errors: {
105
- type_error: 'the {name} have to be {type} ',
106
- type: '{name} is {type}',
107
- duration: "the {type} duration is {ms}MS ({sec}s)",
108
- position: "the {axis}_pos property have to be one the listed values {values}"
109
- },
110
- labels: {
111
- empty: "empty",
112
- undefined: "undefined",
113
- min: "min",
114
- max: "max",
115
- }
116
- }
117
- }
118
- /**
119
- *
120
- * @param {*} infos
121
- */
122
- export function theshtify(infos) {
123
- let notif = buildBox(infos);
124
- if (notif != false) {
125
- addAndPositionNotif(notif);
126
- animProgress(notif);
127
- moveDisplayedNotifications(notif)
128
- fadeShow(notif);
129
- fadeHide(notif);
130
- }
131
- }
132
- /**
133
- * Allow to set all the notification box configuration
134
- * @param {*} config
135
- * @returns
136
- */
137
- function configure(datas) {
138
- let config = datas.config??{};
139
- // trying to set message
140
- try {
141
- datas.message ? message(datas.message) : '';
142
- config.duration ? duration(config.duration) : '';
143
- config.custom_colors ? setCustomColors(config.custom_colors) : '';
144
- datas.type ? type(datas.type) : '';
145
- config.bordered ? bordered(config.bordered) : '';
146
- config.bordered && config.border_width ? borderWidth(config.border_width) : '';
147
- config.radius ? radius(config.radius) : '';
148
- config.colors ? customColors(config.colors) : '';
149
- config.x_align ? setBoxPosition('x', config.x_align) : '';
150
- config.y_align ? setBoxPosition('y', config.y_align) : '';
151
- config.font ? font(config.font) : '';
152
- config.closer ? withCloser(config.closer) : '';
153
- config.progress ? haveProgress(config.progress) : '';
154
- config.progress && config.progress_height ? progressHeight(config.progress_height) : '';
155
- } catch (error) {
156
- console.error(`theshtify error \n ${error}`);
157
- return false
158
- }
159
- return true;
160
- }
161
- /**
162
- *
163
- */
164
- function setCustomColors(customColors) {
165
- for (const key in customColors) {
166
- if (Object.prototype.hasOwnProperty.call(customColors, key)) {
167
- if (getColor(key)) {
168
- theshtify_infos.type = key;
169
- configureColor(customColors[key]);
170
- }
171
-
172
- }
173
- }
174
- }
175
- /**
176
- *
177
- * @param {*} notif
178
- */
179
- function addAndPositionNotif(notif) {
180
- document.querySelector("body").before(notif);
181
-
182
- let pos = getPosition({ width: notif.offsetWidth, height: notif.offsetHeight });
183
- notif.style.left = `${pos.x}px`;
184
- notif.style.top = `${pos.y}px`;
185
- }
186
-
187
- /**setters */
188
- /**
189
- * set or return the message
190
- * @param {*} message
191
- * @returns
192
- */
193
- function message(message = null) {
194
- if (message != null) {
195
- switch (true) {
196
- case message == '':
197
- throw (cMessages().errors.type, { name: "message", type: cMessages().labels.empty });
198
- case checkType(message, "undefined"):
199
- throw (cMessages().errors.type, { name: "message", type: cMessages().labels.undefined });
200
- case !checkType(message, "string"):
201
- throw (getMessage(cMessages().errors.type_error, { name: "message", type: "string" }));
202
-
203
- default:
204
- theshtify_infos.message = message;
205
- break;
206
- }
207
- } else {
208
- return message
209
- }
210
- }
211
- /**
212
- *
213
- */
214
- function setBoxPosition(axis, value) {
215
- if (checkType(value, "string")) {
216
- if (positions[axis].includes(value)) {
217
- theshtify_infos.config[`${axis}_pos`] = value
218
- } else {
219
- throw (getMessage(cMessages().errors.position, { axis: axis, values: positions[axis].join(',') }));
220
-
221
- }
222
- } else {
223
- throw (getMessage(cMessages().errors.type_error, { name: "colors.color", type: "string" }));
224
- }
225
- }
226
- /**
227
- *
228
- * @param {*} colors
229
- */
230
- function customColors(colors) {
231
- if (checkType(colors, "object")) {
232
- if (theshtify_infos.type == "custom") {
233
- // box background
234
- configureColor(colors)
235
- }
236
- } else {
237
- throw (getMessage(cMessages().errors.type_error, { name: "colors", type: "object" }));
238
- }
239
- }
240
- function configureColor(colors) {
241
- colors.bg ? background(colors.bg) : '';
242
- // box text colors
243
- colors.color ? textColor(colors.color) : '';
244
- // borders
245
- colors.border.type ? borderType(colors.border.type) : '';
246
- colors.border.color ? borderColor(colors.border.color) : '';
247
- colors.progress.bg ? progressColor(colors.progress.bg) : '';
248
- }
249
- function getColor(name) {
250
- return theshtify_infos.config.display.colors[name] ?? false;
251
- }
252
- /**
253
- *
254
- * @param {*} bg
255
- */
256
- function background(bg) {
257
- if (checkType(bg, "string")) {
258
- currentColor().bg = bg
259
- } else {
260
- throw (getMessage(cMessages().errors.type_error, { name: "colors.bg", type: "string" }));
261
- }
262
- }
263
- /**
264
- *
265
- * @param {*} color
266
- */
267
- function textColor(color) {
268
- if (checkType(color, "string")) {
269
- currentColor().color = color
270
- } else {
271
- throw (getMessage(cMessages().errors.type_error, { name: "colors.color", type: "string" }));
272
- }
273
- }
274
- /**
275
- *
276
- * @param {*} borders
277
- */
278
- function borderColor(color) {
279
- //borders colors
280
- if (checkType(color, "string")) {
281
- currentColor().border.color = color
282
- } else {
283
- throw (getMessage(cMessages().errors.type_error, { name: "colors.border.color", type: "string" }));
284
- }
285
- }
286
- /**
287
- *
288
- * @param {*} type
289
- */
290
- function borderType(type) {
291
- // border type
292
- if (checkType(type, "string")) {
293
- currentColor().border.type = type
294
- } else {
295
- throw (getMessage(cMessages().errors.type_error, { name: "colors.border.type", type: "string" }));
296
- }
297
- }
298
- /**
299
- *
300
- * @param {*} width
301
- */
302
- function borderWidth(width) {
303
- if (checkType(width, "number")) {
304
- theshtify_infos.config.borderWidth = width
305
- } else {
306
- throw (getMessage(cMessages().errors.type_error, { name: "borderWidth", type: "number" }));
307
- }
308
- }
309
- /**
310
- *
311
- * @param {*} width
312
- */
313
- function radius(radius) {
314
- if (checkType(radius, "number")) {
315
- theshtify_infos.config.display.radius = radius
316
- } else {
317
- throw (getMessage(cMessages().errors.type_error, { name: "radius", type: "number" }));
318
- }
319
- }
320
- /**
321
- *
322
- * @param {*} bordered
323
- */
324
- function bordered(bordered = false) {
325
- if (checkType(bordered, "boolean")) {
326
- theshtify_infos.config.bordered = bordered
327
- } else {
328
- throw (getMessage(cMessages().errors.type_error, { name: "bordered", type: "boolean" }));
329
- }
330
- }
331
- /**
332
- *
333
- * @param {*} progress
334
- */
335
- function haveProgress(progress = false) {
336
- if (checkType(progress, "boolean")) {
337
- theshtify_infos.config.progress = progress
338
- } else {
339
- throw (getMessage(cMessages().errors.type_error, { name: "progress", type: "boolean" }));
340
- }
341
- }
342
- /**
343
- *
344
- * @param {*} closer
345
- */
346
- function withCloser(closer = false) {
347
- if (checkType(closer, "boolean")) {
348
- theshtify_infos.config.closer = closer
349
- } else {
350
- throw (getMessage(cMessages().errors.type_error, { name: "closer", type: "boolean" }));
351
- }
352
- }
353
- /**
354
- *
355
- * @param {*} width
356
- */
357
- function progressHeight(height) {
358
- if (checkType(height, "number")) {
359
- theshtify_infos.config.progressHeight = height
360
- } else {
361
- throw (getMessage(cMessages().errors.type_error, { name: "progressHeight", type: "number" }));
362
- }
363
- }
364
- /**
365
- *
366
- * @param {*} color
367
- */
368
- function progressColor(color) {
369
- if (checkType(color, "string")) {
370
- currentColor().progress.bg = color
371
- } else {
372
- throw (getMessage(cMessages().errors.type_error, { name: "progress.bg", type: "string" }));
373
- }
374
- }
375
-
376
- /**
377
- * set the display duration of the notification box
378
- * @param {*} duration
379
- */
380
- function duration(duration) {
381
- if (checkType(duration, "number")) {
382
- if (duration <= theshtify_infos.config.max_duration) {
383
- theshtify_infos.config.duration = duration;
384
- } else {
385
- throw (getMessage(cMessages().errors.duration, { type: cMessages().labels.max, ms: `${theshtify_infos.config.max_duration}`, sec: `${theshtify_infos.config.max_duration / 1000}` }));
386
- }
387
- if (duration >= theshtify_infos.config.min_duration) {
388
- theshtify_infos.config.duration = duration;
389
- } else {
390
- throw (getMessage(cMessages().errors.duration, { type: cMessages().labels.min, ms: `${theshtify_infos.config.min_duration}`, sec: `${theshtify_infos.config.min_duration / 1000}` }));
391
- }
392
- } else {
393
- throw (getMessage(cMessages().errors.type_error, { name: "duration", type: "number" }));
394
-
395
- }
396
- }
397
- /**
398
- * set or return the type of the notification type
399
- * @param {*} type
400
- * @returns
401
- */
402
- function type(type) {
403
- if (type != null) {
404
- if (types.includes(type)) {
405
- theshtify_infos.type = type;
406
- } else {
407
- throw ('the given type is not supported')
408
- }
409
- } else {
410
- return type
411
- }
412
- }
413
- /**
414
- * allow you to configure the font properties
415
- * @param {*} font
416
- * @returns
417
- */
418
- function font(font) {
419
- if (font) {
420
- //
421
- if (font.size) {
422
- if (checkType(font.size, 'number')) {
423
- theshtify_infos.config.font.size = font.size;
424
- } else {
425
- throw (getMessage(cMessages().errors.type_error, { name: 'font.size', type: 'number' }))
426
- }
427
- }
428
- if (font.weight) {
429
- if (checkType(font.weight, 'number')) {
430
- theshtify_infos.config.font.weight = font.weight;
431
- } else {
432
- throw (getMessage(cMessages().errors.type_error, { name: 'font.weight', type: 'number' }))
433
- }
434
- }
435
- if (font.family) {
436
- if (checkType(font.family, 'string')) {
437
- theshtify_infos.config.font.family = font.family;
438
- } else {
439
- throw (getMessage(cMessages().errors.type_error, { name: 'font.family', type: 'string' }))
440
- }
441
- }
442
- } else {
443
- return theshtify_infos.config.font;
444
- }
445
- }
446
- /**
447
- * allow to get the messages of the current lang
448
- * @returns
449
- */
450
- function cMessages() {
451
- return messages[theshtify_infos.lang];
452
- }
453
- /**
454
- * allow to get the message
455
- * @param {*} message
456
- * @param {*} params
457
- * @returns
458
- */
459
- function getMessage(message = '', params = {}) {
460
- let mes = message;
461
- for (const key in params) {
462
- if (Object.prototype.hasOwnProperty.call(params, key)) {
463
- mes = mes.replace(`{${key}}`, params[key])
464
- }
465
- }
466
- return mes;
467
- }
468
- /**
469
- * return the current colors that matches the selected type
470
- * @returns
471
- */
472
- function currentColor() {
473
- return theshtify_infos.config.display.colors[theshtify_infos.type];
474
- }
475
- /**
476
- * return the position of the notigications boxes according with the current position
477
- * @param {*} boxSize
478
- * @returns
479
- */
480
- function getPosition(boxSize) {
481
- let retPos = {
482
- x: 0,
483
- y: 0
484
- }
485
- theshtify_infos.config.x_pos == 'left' ? retPos.x = theshtify_infos.config.display.margin.left : 0;
486
- theshtify_infos.config.x_pos == 'right' ? retPos.x = window.innerWidth - (boxSize.width + theshtify_infos.config.display.margin.right) : 0;
487
- theshtify_infos.config.x_pos == 'middle' ? retPos.x = (window.innerWidth - boxSize.width) / 2 : 0;
488
- theshtify_infos.config.y_pos == 'top' ? retPos.y = 0 + theshtify_infos.config.display.margin.top : 0;
489
- theshtify_infos.config.y_pos == 'bottom' ? retPos.y = window.innerHeight - (boxSize.height + theshtify_infos.config.display.margin.bottom) : 0;
490
- theshtify_infos.config.y_pos == 'middle' ? retPos.y = (window.innerHeight - boxSize.height) / 2 : 0;
491
- return retPos;
492
- }
493
- /**controllers */
494
- /**
495
- * allow to verify element type (is mostly used to check configuration property type)
496
- * @param {*} value
497
- * @param {*} neededType
498
- * @returns
499
- */
500
- function checkType(value, neededType) {
501
- return typeof (value) === neededType;
502
- }
503
- /** builders */
504
- /**
505
- *
506
- * @param {*} config
507
- * @returns
508
- */
509
- function buildBox(config) {
510
- if (configure(config)) {
511
- let box = mainContainer();
512
- theshtify_infos.config.closer ? box.append(dismiss()) : '';
513
- box.append(body());
514
- theshtify_infos.config.progress ? box.append(progress()) : '';
515
- return box;
516
-
517
- } else {
518
- return false;
519
- }
520
- }
521
- /**
522
- *
523
- * @returns
524
- */
525
- function mainContainer() {
526
- var notif = document.createElement("div");
527
-
528
- notif.setAttribute("class", "Thesharsol-notifyer");
529
- notif.style.width = `${theshtify_infos.config.display.width}px`;
530
- notif.style.position = 'fixed';
531
- notif.style.zIndex = 999, 999;
532
- notif.style.opacity = 0;
533
-
534
- notif.style.fontFamily = `${theshtify_infos.config.font.family}`;
535
- notif.style.fontSize = `${theshtify_infos.config.font.size}px`;
536
- notif.style.fontWeight = `${theshtify_infos.config.font.weight}`;
537
-
538
- notif.style.borderRadius = `${theshtify_infos.config.display.radius}px`;
539
- notif.style.backgroundColor = theshtify_infos.config.display.colors[theshtify_infos.type].bg;
540
- notif.setAttribute('index', document.querySelectorAll('.Thesharsol-notifyer').length)
541
- if (theshtify_infos.config.bordered) {
542
- notif.style.border = `${theshtify_infos.config.borderWidth}px ${currentColor().border.type} ${currentColor().border.color}`
543
- }
544
-
545
- return notif;
546
- }
547
-
548
- /**
549
- * Build the boy of the notification
550
- * @returns
551
- */
552
- function body() {
553
- let body = document.createElement('div');
554
- let bodyContent = document.createElement('div');
555
- body.setAttribute('style', 'width:100%')
556
- // bodyContent.setAttribute('style','width:100%')
557
-
558
- bodyContent.style.color = theshtify_infos.config.display.colors[theshtify_infos.type].color;
559
- bodyContent.style.paddingLeft = `${theshtify_infos.config.display.padding.left}px`;
560
- bodyContent.style.paddingRight = `${theshtify_infos.config.display.padding.right}px`;
561
- bodyContent.style.paddingTop = `${theshtify_infos.config.display.padding.top}px`;
562
- bodyContent.style.paddingBottom = `${theshtify_infos.config.display.padding.bottom}px`;
563
- bodyContent.innerText = theshtify_infos.message;
564
- body.append(bodyContent);
565
- return body;
566
- }
567
- /**
568
- * Buildand return the dismiss notification part
569
- * @returns
570
- */
571
- function dismiss() {
572
- let dismiss = document.createElement('div');
573
- let dismissContent = document.createElement('button');
574
- // style
575
- dismissContent.classList.add('close-icon');
576
- dismissContent.fontSize = `22px`
577
- dismiss.setAttribute('style', `display:flex;padding:${theshtify_infos.config.display.padding.top}px ${theshtify_infos.config.display.padding.top}px 0px ${theshtify_infos.config.display.padding.top}px;`);
578
- dismissContent.setAttribute('style', `font-weight:bold;margin-left:auto;background:transparent;border:0px;color:${currentColor().color}`);
579
-
580
- dismiss.append(dismissContent);
581
-
582
- dismissContent.addEventListener('click', () => {
583
- let boxToRemove = dismissContent.parentNode.parentNode;
584
- moveDisplayedNotifications(boxToRemove, "remove", parseInt(boxToRemove.getAttribute("index")));
585
- boxToRemove.remove()
586
- })
587
- return dismiss;
588
- }
589
-
590
- /**
591
- * Build and return the pargress bar of the notification box
592
- * @returns
593
- */
594
- function progress() {
595
- let progress = document.createElement('div');
596
- let progressContent = document.createElement('div');
597
- progress.style.width = '100%';
598
- progress.style.borderRadius = `${theshtify_infos.config.display.radius}px`;
599
- progress.style.overflow = 'hidden'
600
- progress.classList.add('progress');
601
- progressContent.setAttribute('style', 'height:2px;width:0px');
602
-
603
- progressContent.style.minHeight = `${theshtify_infos.config.progressHeight}px`;
604
- progressContent.style.width = '0px';
605
- progressContent.classList.add('content');
606
- progressContent.style.background = `${currentColor().progress ? currentColor().progress.bg : 'white'}`;
607
- progress.append(progressContent);
608
- return progress;
609
- }
610
- /**
611
- * animate the progress bar
612
- * @param {*} box
613
- */
614
- function animProgress(box) {
615
- let progressBar = box.querySelector('.progress');
616
- if (progressBar != undefined) {
617
- let progressBoxContent = box.querySelector('.content');
618
-
619
- //
620
- let maxProgressContentWidth = progressBar.offsetWidth;
621
- let increment = maxProgressContentWidth / (theshtify_infos.config.duration / 30);
622
- let width = 0;
623
- let incInt = setInterval(() => {
624
- width += increment;
625
- progressBoxContent.style.width = `${width}px`;
626
-
627
- if (maxProgressContentWidth <= width) {
628
- clearInterval(incInt);
629
- }
630
- }, 30);
631
- }
632
-
633
-
634
- }
635
- /** animations */
636
- /**
637
- * Move the displayed notification afer creating or destroying one
638
- * @param {*} createdBox
639
- * @param {*} moveType
640
- * @param {*} startIndex
641
- */
642
- function moveDisplayedNotifications(createdBox, moveType = "add", startIndex = 0) {
643
- let notifBoxes = document.querySelectorAll('.Thesharsol-notifyer');
644
- let offset = createdBox.offsetHeight + 20;
645
- //
646
- let boxesCumulatedHeihgt = offset;
647
-
648
- switch (moveType) {
649
- case "add":
650
- for (let i = startIndex; i < notifBoxes.length; i++) {
651
- boxesCumulatedHeihgt = changeBoxPosition(notifBoxes, i, moveType, boxesCumulatedHeihgt, createdBox, offset);
652
- }
653
- break;
654
- case "remove":
655
- console.log('remove')
656
- let si = Array.prototype.indexOf.call(notifBoxes, createdBox);
657
- for (let i = si; i >= 0; i--) {
658
- boxesCumulatedHeihgt = changeBoxPosition(notifBoxes, i, moveType, boxesCumulatedHeihgt, createdBox, offset);
659
- }
660
- for (let j = (si + 1); j < notifBoxes.length; j++) {
661
- notifBoxes[j].setAttribute('index', j - 1);
662
- console.log((si))
663
- }
664
- break;
665
- default:
666
- break;
667
- }
668
-
669
- if (boxesCumulatedHeihgt > ((window.innerHeight * 50) / 100)) {
670
- notifBoxes[0].remove();
671
- }
672
- }
673
- /**
674
- *
675
- * @param {*} notifBoxes
676
- * @param {*} i
677
- * @param {*} moveType
678
- * @param {*} boxesCumulatedHeihgt
679
- * @param {*} createdBox
680
- * @param {*} offset
681
- * @returns
682
- */
683
- function changeBoxPosition(notifBoxes, i, moveType, boxesCumulatedHeihgt, createdBox, offset) {
684
- let box = notifBoxes[i];
685
-
686
- if (createdBox != box && box != undefined) {
687
-
688
- if (theshtify_infos.config.y_pos == 'bottom') {
689
- box.style.top = moveType == "add" ? `${box.offsetTop - offset}px` : `${box.offsetTop + offset}px`;
690
- }
691
- if (theshtify_infos.config.y_pos == 'top') {
692
- box.style.top = moveType == "add" ? `${box.offsetTop + offset}px` : `${box.offsetTop - offset}px`;
693
- }
694
- boxesCumulatedHeihgt += box.offsetHeight;
695
- }
696
- return boxesCumulatedHeihgt;
697
- }
698
- /**
699
- *
700
- * @param {*} notif
701
- */
702
- function fadeShow(notif) {
703
- var op = 0;
704
- var it = setInterval(function () {
705
- op = op + 0.01;
706
- notif.style.opacity = op;
707
- // console.log(notif)
708
- if (op > 0.9) {
709
- clearInterval(it);
710
- }
711
- }, 1);
712
- }
713
- /**
714
- *
715
- * @param {*} notif
716
- */
717
- function fadeHide(notif) {
718
- setTimeout(function () {
719
- var op = 1;
720
- var it = setInterval(function () {
721
- op = op - 0.01;
722
- notif.style.opacity = op;
723
- // console.log(notif)
724
- if (op < 0.01) {
725
- notif.remove();
726
- clearInterval(it);
727
- }
728
- }, 1);
729
- }, theshtify_infos.config.duration);
730
- }
1
+ let theshtify_infos = {
2
+ title: "",
3
+ message: "",
4
+ type: "",
5
+ lang: "en",
6
+ config: {
7
+ main_box_classes: "",
8
+ x_pos: "right",
9
+ y_pos: "top",
10
+ font: {
11
+ family: "arial",
12
+ size: 14,
13
+ weight: "",
14
+ },
15
+ min_duration: 1000,
16
+ max_duration: 5000,
17
+ duration: 5000,
18
+ borderWidth: 2,
19
+ bordered: false,
20
+ progress: false,
21
+ progressHeight: 2,
22
+ closer: false,
23
+ trans: false,
24
+ display: {
25
+ width: 300,
26
+ colors: {
27
+ custom: {
28
+ color: "white",
29
+ bg: "#000",
30
+ border: {
31
+ type: "solid",
32
+ color: "transparent",
33
+ },
34
+ progress: {
35
+ bg: "#fff",
36
+ },
37
+ },
38
+ success: {
39
+ color: "white",
40
+ bg: "rgba(1, 191, 102, 0.822)",
41
+ border: {
42
+ type: "solid",
43
+ color: "transparent",
44
+ },
45
+ progress: {
46
+ bg: "#fff",
47
+ },
48
+ },
49
+ danger: {
50
+ bg: "red",
51
+ color: "white",
52
+ border: {
53
+ type: "solid",
54
+ color: "transparent",
55
+ },
56
+ progress: {
57
+ bg: "#fff",
58
+ },
59
+ },
60
+ info: {
61
+ bg: "rgba(7, 133, 250, 0.822)",
62
+ color: "white",
63
+ border: {
64
+ type: "solid",
65
+ color: "transparent",
66
+ },
67
+ progress: {
68
+ bg: "#fff",
69
+ },
70
+ },
71
+ warning: {
72
+ bg: "#f89406",
73
+ color: "black",
74
+ border: {
75
+ type: "solid",
76
+ color: "transparent",
77
+ },
78
+ progress: {
79
+ bg: "#fff",
80
+ },
81
+ },
82
+ },
83
+ padding: {
84
+ left: 10,
85
+ right: 10,
86
+ top: 10,
87
+ bottom: 10,
88
+ },
89
+ margin: {
90
+ left: 20,
91
+ right: 20,
92
+ top: 20,
93
+ bottom: 20,
94
+ },
95
+ radius: 5,
96
+ },
97
+ },
98
+ callback: () => {
99
+ // alert("i am the callback");
100
+ },
101
+ };
102
+ const types = ["success", "info", "danger", "warning", "custom"];
103
+ const positions = {
104
+ x: ["middle", "left", "right"],
105
+ y: ["middle", "top", "bottom"],
106
+ };
107
+ const messages = {
108
+ en: {
109
+ errors: {
110
+ type_error: "the {name} have to be {type} ",
111
+ padding_error:
112
+ "the padding must be a number or an object that include keys left, right, top, bottom with number values",
113
+ type: "{name} is {type}",
114
+ duration: "the {type} duration is {ms}MS ({sec}s)",
115
+ position:
116
+ "the {axis}_pos property have to be one the listed values {values}",
117
+ },
118
+ labels: {
119
+ empty: "empty",
120
+ undefined: "undefined",
121
+ min: "min",
122
+ max: "max",
123
+ },
124
+ },
125
+ };
126
+ /**
127
+ *
128
+ * @param {*} infos
129
+ */
130
+ export function theshtify(infos) {
131
+ let notif = buildBox(infos);
132
+ if (notif != false) {
133
+ addAndPositionNotif(notif);
134
+ animProgress(notif);
135
+ moveDisplayedNotifications(notif);
136
+ fadeShow(notif);
137
+ fadeHide(notif);
138
+ addCallback(notif);
139
+ }
140
+ }
141
+ /**
142
+ * Allow to set all the notification box configuration
143
+ * @param {*} config
144
+ * @returns
145
+ */
146
+ function configure(datas) {
147
+ let config = datas.config ?? {};
148
+ // trying to set message
149
+ try {
150
+ datas.message ? message(datas.message) : "";
151
+ datas.title ? (theshtify_infos.title = datas.title) : "";
152
+ config.main_box_classes && config.main_box_classes != ""
153
+ ? (theshtify_infos.config.main_box_classes = config.main_box_classes)
154
+ : "";
155
+ config.duration ? duration(config.duration) : "";
156
+ config.custom_colors ? setCustomColors(config.custom_colors) : "";
157
+ datas.type ? type(datas.type) : "";
158
+ config.bordered ? bordered(config.bordered) : "";
159
+ config.bordered && config.border_width
160
+ ? borderWidth(config.border_width)
161
+ : "";
162
+ config.radius ? radius(config.radius) : "";
163
+ config.colors ? customColors(config.colors) : "";
164
+ config.x_align ? setBoxPosition("x", config.x_align) : "";
165
+ config.y_align ? setBoxPosition("y", config.y_align) : "";
166
+ config.font ? font(config.font) : "";
167
+ config.closer ? withCloser(config.closer) : "";
168
+ config.progress ? haveProgress(config.progress) : "";
169
+ config.padding ? setPadding(config.padding) : "";
170
+ datas.callback ? setCallback(datas.callback) : "";
171
+ config.progress && config.progress_height
172
+ ? progressHeight(config.progress_height)
173
+ : "";
174
+ } catch (error) {
175
+ console.error(`theshtify error \n ${error}`);
176
+ return false;
177
+ }
178
+ return true;
179
+ }
180
+
181
+ function setPadding(padding) {
182
+ if (checkType(padding, "number")) {
183
+ theshtify_infos.config.display.padding.left = padding;
184
+ theshtify_infos.config.display.padding.right = padding;
185
+ theshtify_infos.config.display.padding.top = padding;
186
+ theshtify_infos.config.display.padding.bottom = padding;
187
+ return;
188
+ } else {
189
+ if (checkType(padding, "object")) {
190
+ if (
191
+ Object.keys(padding).includes("left") &&
192
+ checkType(padding.left, "number") &&
193
+ Object.keys(padding).includes("right") &&
194
+ checkType(padding.right, "number") &&
195
+ Object.keys(padding).includes("top") &&
196
+ checkType(padding.top, "number") &&
197
+ Object.keys(padding).includes("bottom") &&
198
+ checkType(padding.bottom, "number")
199
+ ) {
200
+ theshtify_infos.config.display.padding = padding;
201
+ return;
202
+ }
203
+ }
204
+ }
205
+
206
+ throw getMessage(cMessages().errors.padding_error);
207
+ }
208
+ /**
209
+ *
210
+ * @param {*} callback
211
+ */
212
+ function setCallback(callback) {
213
+ if (checkType(callback, "function")) {
214
+ theshtify_infos.callback = callback;
215
+ } else {
216
+ throw getMessage(cMessages().errors.type_error, {
217
+ name: "callback",
218
+ type: "function",
219
+ });
220
+ }
221
+ }
222
+ /**
223
+ *
224
+ */
225
+ function setCustomColors(customColors) {
226
+ for (const key in customColors) {
227
+ if (Object.prototype.hasOwnProperty.call(customColors, key)) {
228
+ if (getColor(key)) {
229
+ theshtify_infos.type = key;
230
+ configureColor(customColors[key]);
231
+ }
232
+ }
233
+ }
234
+ }
235
+ /**
236
+ *
237
+ * @param {*} notif
238
+ */
239
+ function addAndPositionNotif(notif) {
240
+ document.querySelector("body").before(notif);
241
+
242
+ let pos = getPosition({
243
+ width: notif.offsetWidth,
244
+ height: notif.offsetHeight,
245
+ });
246
+ notif.style.left = `${pos.x}px`;
247
+ notif.style.top = `${pos.y}px`;
248
+ }
249
+
250
+ /**setters */
251
+ /**
252
+ * set or return the message
253
+ * @param {*} message
254
+ * @returns
255
+ */
256
+ function message(message = null) {
257
+ if (message != null) {
258
+ switch (true) {
259
+ case message == "":
260
+ throw (
261
+ (cMessages().errors.type,
262
+ { name: "message", type: cMessages().labels.empty })
263
+ );
264
+ case checkType(message, "undefined"):
265
+ throw (
266
+ (cMessages().errors.type,
267
+ { name: "message", type: cMessages().labels.undefined })
268
+ );
269
+ case !checkType(message, "string"):
270
+ throw getMessage(cMessages().errors.type_error, {
271
+ name: "message",
272
+ type: "string",
273
+ });
274
+
275
+ default:
276
+ theshtify_infos.message = message;
277
+ break;
278
+ }
279
+ } else {
280
+ return message;
281
+ }
282
+ }
283
+ /**
284
+ *
285
+ */
286
+ function setBoxPosition(axis, value) {
287
+ if (checkType(value, "string")) {
288
+ if (positions[axis].includes(value)) {
289
+ theshtify_infos.config[`${axis}_pos`] = value;
290
+ } else {
291
+ throw getMessage(cMessages().errors.position, {
292
+ axis: axis,
293
+ values: positions[axis].join(","),
294
+ });
295
+ }
296
+ } else {
297
+ throw getMessage(cMessages().errors.type_error, {
298
+ name: "colors.color",
299
+ type: "string",
300
+ });
301
+ }
302
+ }
303
+ /**
304
+ *
305
+ * @param {*} colors
306
+ */
307
+ function customColors(colors) {
308
+ if (checkType(colors, "object")) {
309
+ if (theshtify_infos.type == "custom") {
310
+ // box background
311
+ configureColor(colors);
312
+ }
313
+ } else {
314
+ throw getMessage(cMessages().errors.type_error, {
315
+ name: "colors",
316
+ type: "object",
317
+ });
318
+ }
319
+ }
320
+ function configureColor(colors) {
321
+ colors.bg ? background(colors.bg) : "";
322
+ // box text colors
323
+ colors.color ? textColor(colors.color) : "";
324
+ // borders
325
+ colors.border.type ? borderType(colors.border.type) : "";
326
+ colors.border.color ? borderColor(colors.border.color) : "";
327
+ colors.progress.bg ? progressColor(colors.progress.bg) : "";
328
+ }
329
+ function getColor(name) {
330
+ return theshtify_infos.config.display.colors[name] ?? false;
331
+ }
332
+ /**
333
+ *
334
+ * @param {*} bg
335
+ */
336
+ function background(bg) {
337
+ if (checkType(bg, "string")) {
338
+ currentColor().bg = bg;
339
+ } else {
340
+ throw getMessage(cMessages().errors.type_error, {
341
+ name: "colors.bg",
342
+ type: "string",
343
+ });
344
+ }
345
+ }
346
+ /**
347
+ *
348
+ * @param {*} color
349
+ */
350
+ function textColor(color) {
351
+ if (checkType(color, "string")) {
352
+ currentColor().color = color;
353
+ } else {
354
+ throw getMessage(cMessages().errors.type_error, {
355
+ name: "colors.color",
356
+ type: "string",
357
+ });
358
+ }
359
+ }
360
+ /**
361
+ *
362
+ * @param {*} borders
363
+ */
364
+ function borderColor(color) {
365
+ //borders colors
366
+ if (checkType(color, "string")) {
367
+ currentColor().border.color = color;
368
+ } else {
369
+ throw getMessage(cMessages().errors.type_error, {
370
+ name: "colors.border.color",
371
+ type: "string",
372
+ });
373
+ }
374
+ }
375
+ /**
376
+ *
377
+ * @param {*} type
378
+ */
379
+ function borderType(type) {
380
+ // border type
381
+ if (checkType(type, "string")) {
382
+ currentColor().border.type = type;
383
+ } else {
384
+ throw getMessage(cMessages().errors.type_error, {
385
+ name: "colors.border.type",
386
+ type: "string",
387
+ });
388
+ }
389
+ }
390
+ /**
391
+ *
392
+ * @param {*} width
393
+ */
394
+ function borderWidth(width) {
395
+ if (checkType(width, "number")) {
396
+ theshtify_infos.config.borderWidth = width;
397
+ } else {
398
+ throw getMessage(cMessages().errors.type_error, {
399
+ name: "borderWidth",
400
+ type: "number",
401
+ });
402
+ }
403
+ }
404
+ /**
405
+ *
406
+ * @param {*} width
407
+ */
408
+ function radius(radius) {
409
+ if (checkType(radius, "number")) {
410
+ theshtify_infos.config.display.radius = radius;
411
+ } else {
412
+ throw getMessage(cMessages().errors.type_error, {
413
+ name: "radius",
414
+ type: "number",
415
+ });
416
+ }
417
+ }
418
+ /**
419
+ *
420
+ * @param {*} bordered
421
+ */
422
+ function bordered(bordered = false) {
423
+ if (checkType(bordered, "boolean")) {
424
+ theshtify_infos.config.bordered = bordered;
425
+ } else {
426
+ throw getMessage(cMessages().errors.type_error, {
427
+ name: "bordered",
428
+ type: "boolean",
429
+ });
430
+ }
431
+ }
432
+ /**
433
+ *
434
+ * @param {*} progress
435
+ */
436
+ function haveProgress(progress = false) {
437
+ if (checkType(progress, "boolean")) {
438
+ theshtify_infos.config.progress = progress;
439
+ } else {
440
+ throw getMessage(cMessages().errors.type_error, {
441
+ name: "progress",
442
+ type: "boolean",
443
+ });
444
+ }
445
+ }
446
+ /**
447
+ *
448
+ * @param {*} closer
449
+ */
450
+ function withCloser(closer = false) {
451
+ if (checkType(closer, "boolean")) {
452
+ theshtify_infos.config.closer = closer;
453
+ } else {
454
+ throw getMessage(cMessages().errors.type_error, {
455
+ name: "closer",
456
+ type: "boolean",
457
+ });
458
+ }
459
+ }
460
+ /**
461
+ *
462
+ * @param {*} width
463
+ */
464
+ function progressHeight(height) {
465
+ if (checkType(height, "number")) {
466
+ theshtify_infos.config.progressHeight = height;
467
+ } else {
468
+ throw getMessage(cMessages().errors.type_error, {
469
+ name: "progressHeight",
470
+ type: "number",
471
+ });
472
+ }
473
+ }
474
+ /**
475
+ *
476
+ * @param {*} color
477
+ */
478
+ function progressColor(color) {
479
+ if (checkType(color, "string")) {
480
+ currentColor().progress.bg = color;
481
+ } else {
482
+ throw getMessage(cMessages().errors.type_error, {
483
+ name: "progress.bg",
484
+ type: "string",
485
+ });
486
+ }
487
+ }
488
+
489
+ /**
490
+ * set the display duration of the notification box
491
+ * @param {*} duration
492
+ */
493
+ function duration(duration) {
494
+ if (checkType(duration, "number")) {
495
+ if (duration <= theshtify_infos.config.max_duration) {
496
+ theshtify_infos.config.duration = duration;
497
+ } else {
498
+ throw getMessage(cMessages().errors.duration, {
499
+ type: cMessages().labels.max,
500
+ ms: `${theshtify_infos.config.max_duration}`,
501
+ sec: `${theshtify_infos.config.max_duration / 1000}`,
502
+ });
503
+ }
504
+ if (duration >= theshtify_infos.config.min_duration) {
505
+ theshtify_infos.config.duration = duration;
506
+ } else {
507
+ throw getMessage(cMessages().errors.duration, {
508
+ type: cMessages().labels.min,
509
+ ms: `${theshtify_infos.config.min_duration}`,
510
+ sec: `${theshtify_infos.config.min_duration / 1000}`,
511
+ });
512
+ }
513
+ } else {
514
+ throw getMessage(cMessages().errors.type_error, {
515
+ name: "duration",
516
+ type: "number",
517
+ });
518
+ }
519
+ }
520
+ /**
521
+ * set or return the type of the notification type
522
+ * @param {*} type
523
+ * @returns
524
+ */
525
+ function type(type) {
526
+ if (type != null) {
527
+ if (types.includes(type)) {
528
+ theshtify_infos.type = type;
529
+ } else {
530
+ throw "the given type is not supported";
531
+ }
532
+ } else {
533
+ return type;
534
+ }
535
+ }
536
+ /**
537
+ * allow you to configure the font properties
538
+ * @param {*} font
539
+ * @returns
540
+ */
541
+ function font(font) {
542
+ if (font) {
543
+ //
544
+ if (font.size) {
545
+ if (checkType(font.size, "number")) {
546
+ theshtify_infos.config.font.size = font.size;
547
+ } else {
548
+ throw getMessage(cMessages().errors.type_error, {
549
+ name: "font.size",
550
+ type: "number",
551
+ });
552
+ }
553
+ }
554
+ if (font.weight) {
555
+ if (checkType(font.weight, "number")) {
556
+ theshtify_infos.config.font.weight = font.weight;
557
+ } else {
558
+ throw getMessage(cMessages().errors.type_error, {
559
+ name: "font.weight",
560
+ type: "number",
561
+ });
562
+ }
563
+ }
564
+ if (font.family) {
565
+ if (checkType(font.family, "string")) {
566
+ theshtify_infos.config.font.family = font.family;
567
+ } else {
568
+ throw getMessage(cMessages().errors.type_error, {
569
+ name: "font.family",
570
+ type: "string",
571
+ });
572
+ }
573
+ }
574
+ } else {
575
+ return theshtify_infos.config.font;
576
+ }
577
+ }
578
+ /**
579
+ * allow to get the messages of the current lang
580
+ * @returns
581
+ */
582
+ function cMessages() {
583
+ return messages[theshtify_infos.lang];
584
+ }
585
+ /**
586
+ * allow to get the message
587
+ * @param {*} message
588
+ * @param {*} params
589
+ * @returns
590
+ */
591
+ function getMessage(message = "", params = {}) {
592
+ let mes = message;
593
+ for (const key in params) {
594
+ if (Object.prototype.hasOwnProperty.call(params, key)) {
595
+ mes = mes.replace(`{${key}}`, params[key]);
596
+ }
597
+ }
598
+ return mes;
599
+ }
600
+ /**
601
+ * return the current colors that matches the selected type
602
+ * @returns
603
+ */
604
+ function currentColor() {
605
+ return theshtify_infos.config.display.colors[theshtify_infos.type];
606
+ }
607
+ /**
608
+ * return the position of the notigications boxes according with the current position
609
+ * @param {*} boxSize
610
+ * @returns
611
+ */
612
+ function getPosition(boxSize) {
613
+ let retPos = {
614
+ x: 0,
615
+ y: 0,
616
+ };
617
+ theshtify_infos.config.x_pos == "left"
618
+ ? (retPos.x = theshtify_infos.config.display.margin.left)
619
+ : 0;
620
+ theshtify_infos.config.x_pos == "right"
621
+ ? (retPos.x =
622
+ window.innerWidth -
623
+ (boxSize.width + theshtify_infos.config.display.margin.right))
624
+ : 0;
625
+ theshtify_infos.config.x_pos == "middle"
626
+ ? (retPos.x = (window.innerWidth - boxSize.width) / 2)
627
+ : 0;
628
+ theshtify_infos.config.y_pos == "top"
629
+ ? (retPos.y = 0 + theshtify_infos.config.display.margin.top)
630
+ : 0;
631
+ theshtify_infos.config.y_pos == "bottom"
632
+ ? (retPos.y =
633
+ window.innerHeight -
634
+ (boxSize.height + theshtify_infos.config.display.margin.bottom))
635
+ : 0;
636
+ theshtify_infos.config.y_pos == "middle"
637
+ ? (retPos.y = (window.innerHeight - boxSize.height) / 2)
638
+ : 0;
639
+ return retPos;
640
+ }
641
+ /**controllers */
642
+ /**
643
+ * allow to verify element type (is mostly used to check configuration property type)
644
+ * @param {*} value
645
+ * @param {*} neededType
646
+ * @returns
647
+ */
648
+ function checkType(value, neededType) {
649
+ return typeof value === neededType;
650
+ }
651
+ /** builders */
652
+ /**
653
+ *
654
+ * @param {*} config
655
+ * @returns
656
+ */
657
+ function buildBox(config) {
658
+ if (configure(config)) {
659
+ let box = mainContainer();
660
+ theshtify_infos.config.closer ? box.append(dismiss()) : "";
661
+ box.append(body());
662
+ theshtify_infos.config.progress ? box.append(progress()) : "";
663
+ return box;
664
+ } else {
665
+ return false;
666
+ }
667
+ }
668
+ /**
669
+ *
670
+ * @returns
671
+ */
672
+ function mainContainer() {
673
+ var notif = document.createElement("div");
674
+
675
+ notif.setAttribute("class", "Thesharsol-notifyer");
676
+ theshtify_infos.config.main_box_classes
677
+ ? notif.classList.add(theshtify_infos.config.main_box_classes)
678
+ : "";
679
+ notif.style.width = "90%";
680
+ notif.style.maxWidth = `${theshtify_infos.config.display.width}px`;
681
+ notif.style.position = "fixed";
682
+ (notif.style.zIndex = 999), 999;
683
+ notif.style.opacity = 0;
684
+
685
+ notif.style.fontFamily = `${theshtify_infos.config.font.family}`;
686
+ notif.style.fontSize = `${theshtify_infos.config.font.size}px`;
687
+ notif.style.fontWeight = `${theshtify_infos.config.font.weight}`;
688
+
689
+ notif.style.borderRadius = `${theshtify_infos.config.display.radius}px`;
690
+ notif.style.backgroundColor =
691
+ theshtify_infos.config.display.colors[theshtify_infos.type].bg;
692
+ notif.setAttribute(
693
+ "index",
694
+ document.querySelectorAll(".Thesharsol-notifyer").length
695
+ );
696
+ if (theshtify_infos.config.bordered) {
697
+ notif.style.border = `${theshtify_infos.config.borderWidth}px ${
698
+ currentColor().border.type
699
+ } ${currentColor().border.color}`;
700
+ }
701
+
702
+ return notif;
703
+ }
704
+
705
+ /**
706
+ * Build the boy of the notification
707
+ * @returns
708
+ */
709
+ function body() {
710
+ let body = document.createElement("div");
711
+ let bodyContent = document.createElement("div");
712
+ body.setAttribute("style", "width:100%");
713
+ // bodyContent.setAttribute('style','width:100%')
714
+
715
+ bodyContent.style.color =
716
+ theshtify_infos.config.display.colors[theshtify_infos.type].color;
717
+ bodyContent.style.paddingLeft = `${theshtify_infos.config.display.padding.left}px`;
718
+ bodyContent.style.paddingRight = `${theshtify_infos.config.display.padding.right}px`;
719
+ bodyContent.style.paddingTop = `${theshtify_infos.config.display.padding.top}px`;
720
+ bodyContent.style.paddingBottom = `${theshtify_infos.config.display.padding.bottom}px`;
721
+ const title = span(theshtify_infos.title, true);
722
+ title.style.marginBottom = `${theshtify_infos.message != "" ? 4 : 0}px`;
723
+ theshtify_infos.title ? bodyContent.append(title) : "";
724
+ theshtify_infos.message
725
+ ? bodyContent.append(span(theshtify_infos.message))
726
+ : "";
727
+ body.append(bodyContent);
728
+ return body;
729
+ }
730
+
731
+ function span(text = "", bold = false) {
732
+ let span = document.createElement("span");
733
+ span.setAttribute(
734
+ "style",
735
+ `display:block;font-weight:${bold ? "bold" : "light"};`
736
+ );
737
+ if (text && text != "") {
738
+ span.textContent = text;
739
+ }
740
+ return span;
741
+ }
742
+ /**
743
+ * Buildand return the dismiss notification part
744
+ * @returns
745
+ */
746
+ function dismiss() {
747
+ let dismiss = document.createElement("div");
748
+ let dismissContent = document.createElement("button");
749
+ // style
750
+ dismissContent.classList.add("close-icon");
751
+ dismissContent.fontSize = `22px`;
752
+ dismiss.setAttribute(
753
+ "style",
754
+ `display:flex;padding:${theshtify_infos.config.display.padding.top}px ${theshtify_infos.config.display.padding.top}px 0px ${theshtify_infos.config.display.padding.top}px;`
755
+ );
756
+ dismissContent.setAttribute(
757
+ "style",
758
+ `font-weight:bold;margin-left:auto;background:transparent;border:0px;color:${
759
+ currentColor().color
760
+ }`
761
+ );
762
+
763
+ dismiss.append(dismissContent);
764
+
765
+ dismissContent.addEventListener("click", () => {
766
+ let boxToRemove = dismissContent.parentNode.parentNode;
767
+ moveDisplayedNotifications(
768
+ boxToRemove,
769
+ "remove",
770
+ parseInt(boxToRemove.getAttribute("index"))
771
+ );
772
+ boxToRemove.remove();
773
+ });
774
+ return dismiss;
775
+ }
776
+
777
+ /**
778
+ * Build and return the progress bar of the notification box
779
+ * @returns
780
+ */
781
+ function progress() {
782
+ let progress = document.createElement("div");
783
+ let progressContent = document.createElement("div");
784
+ progress.style.width = "100%";
785
+ progress.style.borderRadius = `${theshtify_infos.config.display.radius}px`;
786
+ progress.style.overflow = "hidden";
787
+ progress.classList.add("progress");
788
+ progressContent.setAttribute("style", "height:2px;width:0px");
789
+
790
+ progressContent.style.minHeight = `${theshtify_infos.config.progressHeight}px`;
791
+ progressContent.style.width = "0px";
792
+ progressContent.classList.add("content");
793
+ progressContent.style.background = `${
794
+ currentColor().progress ? currentColor().progress.bg : "white"
795
+ }`;
796
+ progress.append(progressContent);
797
+ return progress;
798
+ }
799
+ /**
800
+ * animate the progress bar
801
+ * @param {*} box
802
+ */
803
+ function animProgress(box) {
804
+ let progressBar = box.querySelector(".progress");
805
+ if (progressBar != undefined) {
806
+ let progressBoxContent = box.querySelector(".content");
807
+ let maxProgressContentWidth = progressBar.offsetWidth;
808
+
809
+ progressBoxContent.style.width = "0px";
810
+ progressBoxContent.style.transitionDuration = `${theshtify_infos.config.duration}ms`;
811
+
812
+ requestAnimationFrame(() => {
813
+ progressBoxContent.style.width = `${maxProgressContentWidth}px`;
814
+ });
815
+ }
816
+ }
817
+ /** animations */
818
+ /**
819
+ * Move the displayed notification after creating or destroying one
820
+ * @param {*} createdBox
821
+ * @param {*} moveType
822
+ * @param {*} startIndex
823
+ */
824
+ function moveDisplayedNotifications(
825
+ createdBox,
826
+ moveType = "add",
827
+ startIndex = 0
828
+ ) {
829
+ let notifBoxes = document.querySelectorAll(".Thesharsol-notifyer");
830
+ let offset = createdBox.offsetHeight + 20;
831
+ //
832
+ let boxesCumulatedHeihgt = offset;
833
+
834
+ switch (moveType) {
835
+ case "add":
836
+ for (let i = startIndex; i < notifBoxes.length; i++) {
837
+ boxesCumulatedHeihgt = changeBoxPosition(
838
+ notifBoxes,
839
+ i,
840
+ moveType,
841
+ boxesCumulatedHeihgt,
842
+ createdBox,
843
+ offset
844
+ );
845
+ }
846
+ break;
847
+ case "remove":
848
+ console.log("remove");
849
+ let si = Array.prototype.indexOf.call(notifBoxes, createdBox);
850
+ for (let i = si; i >= 0; i--) {
851
+ boxesCumulatedHeihgt = changeBoxPosition(
852
+ notifBoxes,
853
+ i,
854
+ moveType,
855
+ boxesCumulatedHeihgt,
856
+ createdBox,
857
+ offset
858
+ );
859
+ }
860
+ for (let j = si + 1; j < notifBoxes.length; j++) {
861
+ notifBoxes[j].setAttribute("index", j - 1);
862
+ console.log(si);
863
+ }
864
+ break;
865
+ default:
866
+ break;
867
+ }
868
+
869
+ if (boxesCumulatedHeihgt > (window.innerHeight * 50) / 100) {
870
+ notifBoxes[0].remove();
871
+ }
872
+ }
873
+ /**
874
+ *
875
+ * @param {*} notifBoxes
876
+ * @param {*} i
877
+ * @param {*} moveType
878
+ * @param {*} boxesCumulatedHeihgt
879
+ * @param {*} createdBox
880
+ * @param {*} offset
881
+ * @returns
882
+ */
883
+ function changeBoxPosition(
884
+ notifBoxes,
885
+ i,
886
+ moveType,
887
+ boxesCumulatedHeihgt,
888
+ createdBox,
889
+ offset
890
+ ) {
891
+ let box = notifBoxes[i];
892
+
893
+ if (createdBox != box && box != undefined) {
894
+ if (theshtify_infos.config.y_pos == "bottom") {
895
+ box.style.top =
896
+ moveType == "add"
897
+ ? `${box.offsetTop - offset}px`
898
+ : `${box.offsetTop + offset}px`;
899
+ }
900
+ if (theshtify_infos.config.y_pos == "top") {
901
+ box.style.top =
902
+ moveType == "add"
903
+ ? `${box.offsetTop + offset}px`
904
+ : `${box.offsetTop - offset}px`;
905
+ }
906
+ boxesCumulatedHeihgt += box.offsetHeight;
907
+ }
908
+ return boxesCumulatedHeihgt;
909
+ }
910
+ /**
911
+ *
912
+ * @param {*} notif
913
+ */
914
+ function fadeShow(notif) {
915
+ requestAnimationFrame(() => {
916
+ notif.style.opacity = 0.9;
917
+ });
918
+ }
919
+ /**
920
+ *
921
+ * @param {*} notif
922
+ */
923
+ function fadeHide(notif) {
924
+ setTimeout(function () {
925
+ notif.classList.add("fade-out");
926
+ notif.addEventListener("transitionend", function handler() {
927
+ notif.removeEventListener("transitionend", handler);
928
+ notif.remove();
929
+ });
930
+ }, theshtify_infos.config.duration);
931
+ }
932
+
933
+ function addCallback(box = HTMLDivElement) {
934
+ box.addEventListener("click", () => {
935
+ theshtify_infos.callback();
936
+ });
937
+ }