1
1
import { Envelope , FlasherInterface , FlasherOptions } from './interfaces' ;
2
- import Flasher from './flasher' ;
2
+ import { Properties } from 'csstype' ;
3
+ import deepmerge from 'deepmerge' ;
3
4
4
5
interface Options {
5
6
timeout : number ,
6
7
fps : number ,
7
8
position : string ,
8
9
direction : string ,
9
- x_offset : string ,
10
- y_offset : string ,
10
+ style : Properties < string >
11
11
}
12
12
13
13
export default class TemplateFactory implements FlasherInterface {
@@ -16,8 +16,13 @@ export default class TemplateFactory implements FlasherInterface {
16
16
fps : 30 ,
17
17
position : 'top-right' ,
18
18
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
+ } ,
21
26
} ;
22
27
23
28
render ( envelope : Envelope ) : void {
@@ -28,7 +33,7 @@ export default class TemplateFactory implements FlasherInterface {
28
33
return ;
29
34
}
30
35
31
- template . style . transition = '0.8s' ;
36
+ template . style . transition = this . options . style . transition as string ;
32
37
33
38
if ( undefined !== notification . options && undefined !== notification . options . position ) {
34
39
this . options . position = notification . options . position as unknown as string ;
@@ -37,29 +42,32 @@ export default class TemplateFactory implements FlasherInterface {
37
42
let container = document . getElementById ( `flasher-container-${ this . options . position } ` ) ;
38
43
if ( container === null ) {
39
44
container = document . createElement ( 'div' ) ;
45
+
40
46
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 ;
45
53
46
54
switch ( this . options . position ) {
47
55
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' ;
50
58
break ;
51
59
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' ;
54
62
break ;
55
63
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' ;
58
66
break ;
59
67
case 'bottom-right' :
60
68
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' ;
63
71
break ;
64
72
}
65
73
document . getElementsByTagName ( 'body' ) [ 0 ] . appendChild ( container ) ;
@@ -109,7 +117,7 @@ export default class TemplateFactory implements FlasherInterface {
109
117
}
110
118
111
119
renderOptions ( options : FlasherOptions ) : void {
112
- this . options = { ... this . options , ... options } ;
120
+ this . options = deepmerge ( this . options , options ) ;
113
121
}
114
122
115
123
private static stringToHTML ( str : string ) {
@@ -137,6 +145,3 @@ export default class TemplateFactory implements FlasherInterface {
137
145
return dom . firstElementChild ;
138
146
}
139
147
}
140
-
141
- const flasher = Flasher . getInstance ( ) ;
142
- flasher . addFactory ( 'template' , new TemplateFactory ( ) ) ;
0 commit comments