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.
@@ -1,730 +1,3 @@
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
- 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
+ import { theshtify } from "./theshtify.js";
2
+
3
+ window.theshtify = theshtify;