Skip to content

Commit 50e9a51

Browse files
author
KHOUBZA Younes
committed
allow users to override template options and styles
1 parent 01f1d89 commit 50e9a51

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

package-lock.json

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"type": "git",
3030
"url": "https://github.com/php-flasher/flasher-js.git"
3131
},
32+
"dependencies": {
33+
"csstype": "^3.0.9",
34+
"deepmerge": "^4.2.2"
35+
},
3236
"devDependencies": {
3337
"@rollup/plugin-commonjs": "^20.0.0",
3438
"@rollup/plugin-node-resolve": "^13.0.4",

src/flasher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
QueueableInterface,
77
ResponseContext,
88
} from './interfaces';
9+
import TemplateFactory from './template';
910

1011
export default class Flasher {
1112
private static instance: Flasher;
@@ -89,7 +90,6 @@ export default class Flasher {
8990

9091
public renderEnvelopes(envelopes: Envelope[], context: ResponseContext): void {
9192
const queues = new Map<string, QueueableInterface>();
92-
9393
envelopes.forEach((envelope) => {
9494
envelope.context = context;
9595
const factory = this.create(envelope.handler);
@@ -112,8 +112,8 @@ export default class Flasher {
112112
}
113113

114114
public create(alias: string): FlasherInterface | undefined {
115-
if (0 === alias.indexOf('template.')) {
116-
return this.factories.get('template');
115+
if (0 === alias.indexOf('template.') && !this.factories.has(alias)) {
116+
this.addFactory(alias, new TemplateFactory());
117117
}
118118

119119
return this.factories.get(alias);

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import Flasher from './flasher';
22
import './template';
3+
import TemplateFactory from './template';
34

45
export * from './interfaces';
56

7+
const flasher = Flasher.getInstance();
8+
flasher.addFactory('template', new TemplateFactory());
9+
610
export default Flasher;

src/template.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Envelope, FlasherInterface, FlasherOptions } from './interfaces';
2-
import Flasher from './flasher';
2+
import { Properties } from 'csstype';
3+
import deepmerge from 'deepmerge';
34

45
interface Options {
56
timeout: number,
67
fps: number,
78
position: string,
89
direction: string,
9-
x_offset: string,
10-
y_offset: string,
10+
style: Properties<string>
1111
}
1212

1313
export default class TemplateFactory implements FlasherInterface {
@@ -16,8 +16,13 @@ export default class TemplateFactory implements FlasherInterface {
1616
fps: 30,
1717
position: 'top-right',
1818
direction: 'top',
19-
x_offset: '0.5em',
20-
y_offset: '0',
19+
style: {
20+
position: 'fixed',
21+
maxWidth: '304px',
22+
width: '100%',
23+
zIndex: 999999,
24+
transition: '0.8s',
25+
},
2126
};
2227

2328
render(envelope: Envelope): void {
@@ -28,7 +33,7 @@ export default class TemplateFactory implements FlasherInterface {
2833
return;
2934
}
3035

31-
template.style.transition = '0.8s';
36+
template.style.transition = this.options.style.transition as string;
3237

3338
if (undefined !== notification.options && undefined !== notification.options.position) {
3439
this.options.position = notification.options.position as unknown as string;
@@ -37,29 +42,32 @@ export default class TemplateFactory implements FlasherInterface {
3742
let container = document.getElementById(`flasher-container-${this.options.position}`);
3843
if (container === null) {
3944
container = document.createElement('div');
45+
4046
container.id = `flasher-container-${this.options.position}`;
41-
container.style.position = 'fixed';
42-
container.style.maxWidth = '304px';
43-
container.style.width = '100%';
44-
container.style.zIndex = '999999';
47+
48+
Object.keys(this.options.style).forEach((key: string) => {
49+
container!.style.setProperty(key, this.options.style[key as keyof Properties] as string);
50+
});
51+
52+
container.style.maxWidth = this.options.style.maxWidth as string;
4553

4654
switch (this.options.position) {
4755
case 'top-left':
48-
container.style.top = this.options.y_offset;
49-
container.style.left = this.options.x_offset;
56+
container.style.top = this.options.style.top as string || '0';
57+
container.style.left = this.options.style.left as string || '0.5em';
5058
break;
5159
case 'top-right':
52-
container.style.top = this.options.y_offset;
53-
container.style.right = this.options.x_offset;
60+
container.style.top = this.options.style.top as string || '0';
61+
container.style.right = this.options.style.right as string || '0.5em';
5462
break;
5563
case 'bottom-left':
56-
container.style.bottom = this.options.y_offset;
57-
container.style.left = this.options.x_offset;
64+
container.style.bottom = this.options.style.bottom as string || '0';
65+
container.style.left = this.options.style.left as string || '0.5em';
5866
break;
5967
case 'bottom-right':
6068
default:
61-
container.style.bottom = this.options.y_offset;
62-
container.style.right = this.options.x_offset;
69+
container.style.bottom = this.options.style.bottom as string || '0';
70+
container.style.right = this.options.style.right as string || '0.5em';
6371
break;
6472
}
6573
document.getElementsByTagName('body')[0].appendChild(container);
@@ -109,7 +117,7 @@ export default class TemplateFactory implements FlasherInterface {
109117
}
110118

111119
renderOptions(options: FlasherOptions): void {
112-
this.options = { ...this.options, ...options };
120+
this.options = deepmerge(this.options, options);
113121
}
114122

115123
private static stringToHTML(str: string) {
@@ -137,6 +145,3 @@ export default class TemplateFactory implements FlasherInterface {
137145
return dom.firstElementChild;
138146
}
139147
}
140-
141-
const flasher = Flasher.getInstance();
142-
flasher.addFactory('template', new TemplateFactory());

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"lib": [
1818
"es2017",
1919
"dom"
20-
]
20+
],
21+
"allowSyntheticDefaultImports": true
2122
}
22-
}
23+
}

0 commit comments

Comments
 (0)