Skip to content

Commit 70cbc05

Browse files
author
KHOUBZA Younes
committed
fix user cannot override template options
1 parent d0050d5 commit 70cbc05

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/template.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,52 @@ export default class TemplateFactory implements FlasherInterface {
3333
return;
3434
}
3535

36-
template.style.transition = this.options.style.transition as string;
36+
let options = JSON.parse(JSON.stringify(this.options));
37+
if (!Array.isArray(notification.options)) {
38+
options = deepmerge(options, notification.options);
39+
}
40+
41+
template.style.transition = options.style.transition as string;
3742

3843
if (undefined !== notification.options && undefined !== notification.options.position) {
39-
this.options.position = notification.options.position as unknown as string;
44+
options.position = notification.options.position as unknown as string;
4045
}
4146

42-
let container = document.getElementById(`flasher-container-${this.options.position}`);
47+
let container = document.getElementById(`flasher-container-${options.position}`);
4348
if (container === null) {
4449
container = document.createElement('div');
4550

46-
container.id = `flasher-container-${this.options.position}`;
51+
container.id = `flasher-container-${options.position}`;
4752

48-
Object.keys(this.options.style).forEach((key: string) => {
49-
container!.style.setProperty(key, this.options.style[key as keyof Properties] as string);
53+
Object.keys(options.style).forEach((key: string) => {
54+
container!.style.setProperty(key, options.style[key as keyof Properties] as string);
5055
});
5156

52-
container.style.maxWidth = this.options.style.maxWidth as string;
57+
container.style.maxWidth = options.style.maxWidth as string;
5358

54-
switch (this.options.position) {
59+
switch (options.position) {
5560
case 'top-left':
56-
container.style.top = this.options.style.top as string || '0';
57-
container.style.left = this.options.style.left as string || '0.5em';
61+
container.style.top = options.style.top as string || '0';
62+
container.style.left = options.style.left as string || '0.5em';
5863
break;
5964
case 'top-right':
60-
container.style.top = this.options.style.top as string || '0';
61-
container.style.right = this.options.style.right as string || '0.5em';
65+
container.style.top = options.style.top as string || '0';
66+
container.style.right = options.style.right as string || '0.5em';
6267
break;
6368
case 'bottom-left':
64-
container.style.bottom = this.options.style.bottom as string || '0';
65-
container.style.left = this.options.style.left as string || '0.5em';
69+
container.style.bottom = options.style.bottom as string || '0';
70+
container.style.left = options.style.left as string || '0.5em';
6671
break;
6772
case 'bottom-right':
6873
default:
69-
container.style.bottom = this.options.style.bottom as string || '0';
70-
container.style.right = this.options.style.right as string || '0.5em';
74+
container.style.bottom = options.style.bottom as string || '0';
75+
container.style.right = options.style.right as string || '0.5em';
7176
break;
7277
}
7378
document.getElementsByTagName('body')[0].appendChild(container);
7479
}
7580

76-
switch (this.options.direction) {
81+
switch (options.direction) {
7782
case 'top':
7883
container.insertBefore(template, container.firstChild);
7984
break;
@@ -91,14 +96,14 @@ export default class TemplateFactory implements FlasherInterface {
9196
});
9297

9398
const progressBar = template.querySelector('.flasher-progress');
94-
if (progressBar instanceof HTMLElement && this.options.timeout > 0) {
99+
if (progressBar instanceof HTMLElement && options.timeout > 0) {
95100
let width = 0;
96101
let progress: NodeJS.Timeout;
97-
const lapse = 1000 / this.options.fps;
102+
const lapse = 1000 / options.fps;
98103

99104
const showProgress = () => {
100105
width += 1;
101-
const percent = (1 - lapse * (width / this.options.timeout)) * 100;
106+
const percent = (1 - lapse * (width / options.timeout)) * 100;
102107
progressBar.style.width = `${percent}%`;
103108

104109
if (percent <= 0) {

0 commit comments

Comments
 (0)