1
- window . modals = [ ] ;
1
+ window . modals = { } ;
2
2
3
3
class Modal {
4
4
constructor ( options , onclose ) {
5
5
if ( ! options || ! options . type ) throw "Missing parameters" ;
6
6
7
7
this . type = options . type ;
8
- this . id = window . modals . length ;
8
+ this . id = require ( "nanoid" ) ( ) ;
9
+ while ( typeof window . modals [ this . id ] !== "undefined" ) {
10
+ this . id = require ( "nanoid" ) ( ) ;
11
+ }
9
12
this . title = options . title || options . type || "Modal window" ;
10
13
this . message = options . message || "Lorem ipsum dolor sit amet." ;
11
14
this . onclose = onclose ;
12
- let classes = "modal_popup" ;
15
+ this . classes = "modal_popup" ;
13
16
let buttons = [ ] ;
14
17
let zindex = 0 ;
15
18
@@ -18,29 +21,29 @@ class Modal {
18
21
19
22
switch ( this . type ) {
20
23
case "error" :
21
- classes += " error" ;
24
+ this . classes += " error" ;
22
25
zindex = 1500 ;
23
- buttons . push ( { label :"PANIC" , action :"window.modals[" + this . id + "].close();" } , { label :"RELOAD" , action :"window.location.reload(true);" } ) ;
26
+ buttons . push ( { label :"PANIC" , action :"window.modals[' " + this . id + "' ].close();" } , { label :"RELOAD" , action :"window.location.reload(true);" } ) ;
24
27
break ;
25
28
case "warning" :
26
- classes += " warning" ;
29
+ this . classes += " warning" ;
27
30
zindex = 1000 ;
28
- buttons . push ( { label :"OK" , action :"window.modals[" + this . id + "].close();" } ) ;
31
+ buttons . push ( { label :"OK" , action :"window.modals[' " + this . id + "' ].close();" } ) ;
29
32
break ;
30
33
case "custom" :
31
- classes += " info custom" ;
34
+ this . classes += " info custom" ;
32
35
zindex = 500 ;
33
36
buttons = options . buttons || [ ] ;
34
- buttons . push ( { label :"Close" , action :"window.modals[" + this . id + "].close();" } ) ;
37
+ buttons . push ( { label :"Close" , action :"window.modals[' " + this . id + "' ].close();" } ) ;
35
38
break ;
36
39
default :
37
- classes += " info" ;
40
+ this . classes += " info" ;
38
41
zindex = 500 ;
39
- buttons . push ( { label :"OK" , action :"window.modals[" + this . id + "].close();" } ) ;
42
+ buttons . push ( { label :"OK" , action :"window.modals[' " + this . id + "' ].close();" } ) ;
40
43
break ;
41
44
}
42
45
43
- let DOMstring = `<div id="modal_${ this . id } " class="${ classes } " style="z-index:${ zindex + this . id } ;">
46
+ let DOMstring = `<div id="modal_${ this . id } " class="${ this . classes } " style="z-index:${ zindex + Object . keys ( window . modals ) . length } ;">
44
47
<h1>${ this . title } </h1>
45
48
${ this . type === "custom" ? options . html : "<h5>" + this . message + "</h5>" }
46
49
<div>` ;
@@ -56,17 +59,39 @@ class Modal {
56
59
window . audioManager . denied . play ( ) ;
57
60
setTimeout ( ( ) => {
58
61
modalElement . remove ( ) ;
62
+ delete window . modals [ this . id ] ;
59
63
} , 100 ) ;
60
64
61
65
if ( typeof this . onclose === "function" ) {
62
66
this . onclose ( ) ;
63
67
}
64
68
} ;
65
69
70
+ this . focus = ( ) => {
71
+ let modalElement = document . getElementById ( "modal_" + this . id ) ;
72
+ modalElement . setAttribute ( "class" , this . classes + " focus" ) ;
73
+ Object . keys ( window . modals ) . forEach ( id => {
74
+ if ( id === this . id ) return ;
75
+ window . modals [ id ] . unfocus ( ) ;
76
+ } ) ;
77
+ } ;
78
+
79
+ this . unfocus = ( ) => {
80
+ let modalElement = document . getElementById ( "modal_" + this . id ) ;
81
+ modalElement . setAttribute ( "class" , this . classes ) ;
82
+ } ;
83
+
66
84
let tmp = document . createElement ( "div" ) ;
67
85
tmp . innerHTML = DOMstring ;
68
86
let element = tmp . firstChild ;
69
87
88
+ element . addEventListener ( "mousedown" , ( ) => {
89
+ this . focus ( ) ;
90
+ } ) ;
91
+ element . addEventListener ( "touchstart" , ( ) => {
92
+ this . focus ( ) ;
93
+ } ) ;
94
+
70
95
switch ( this . type ) {
71
96
case "error" :
72
97
window . audioManager . error . play ( ) ;
0 commit comments