@@ -106,7 +106,6 @@ export default class Dialog extends WithTemplate {
106
106
}
107
107
108
108
open ( settings = { } ) {
109
- this . dialog . returnValue = undefined
110
109
const dialog = Object . assign ( { } , this . settings , settings )
111
110
this . dialog . className = 'umap-dialog window'
112
111
if ( dialog . className ) {
@@ -118,7 +117,6 @@ export default class Dialog extends WithTemplate {
118
117
this . elements . cancel . hidden = ! dialog . cancel
119
118
this . elements . message . textContent = dialog . message
120
119
this . elements . message . hidden = ! dialog . message
121
- this . elements . target = dialog . target || ''
122
120
this . elements . template . innerHTML = ''
123
121
if ( dialog . template ?. nodeType === 1 ) {
124
122
this . elements . template . appendChild ( dialog . template )
@@ -137,12 +135,13 @@ export default class Dialog extends WithTemplate {
137
135
if ( currentZIndex ) {
138
136
this . dialog . style . zIndex = currentZIndex + 1
139
137
}
140
-
141
- this . toggle ( true )
142
-
138
+ if ( this . dialogSupported ) {
139
+ this . dialog . show ( )
140
+ } else {
141
+ this . dialog . hidden = false
142
+ }
143
143
if ( this . hasFormData ) this . focusable [ 0 ] . focus ( )
144
144
else this . elements . accept . focus ( )
145
-
146
145
return this . waitForUser ( )
147
146
}
148
147
@@ -151,44 +150,40 @@ export default class Dialog extends WithTemplate {
151
150
}
152
151
153
152
close ( ) {
154
- this . toggle ( false )
153
+ this . _closing = true
154
+ if ( this . dialogSupported ) {
155
+ this . dialog . close ( )
156
+ } else {
157
+ this . dialog . hidden = true
158
+ this . dialog . dispatchEvent ( new CustomEvent ( 'close' ) )
159
+ }
155
160
}
156
161
157
162
accept ( ) {
158
163
this . dialog . returnValue = 'accept'
159
164
this . close ( )
160
165
}
161
166
162
- toggle ( open = false ) {
163
- if ( this . dialogSupported ) {
164
- if ( open ) {
165
- this . dialog . show ( )
166
- } else {
167
- this . dialog . close ( )
167
+ waitForUser ( ) {
168
+ return new Promise ( ( resolve ) => {
169
+ const onClose = ( ) => {
170
+ this . _closing = false
171
+ if ( this . dialog . returnValue === 'accept' ) {
172
+ const value = this . hasFormData ? this . collectFormData ( ) : true
173
+ resolve ( value )
174
+ }
168
175
}
169
- } else {
170
- this . dialog . hidden = ! open
171
- if ( this . elements . target && ! open ) {
172
- this . elements . target . focus ( )
176
+ const waitForClose = ( ) => {
177
+ this . dialog . returnValue = undefined
178
+ this . dialog . addEventListener ( 'close' , ( ) => onClose ( ) , { once : true } )
173
179
}
174
- if ( ! open ) {
175
- this . dialog . dispatchEvent ( new CustomEvent ( 'close' ) )
180
+ if ( this . _closing ) {
181
+ // We are opening a new dialog while another is not fully closed,
182
+ // so let's first wait for that one to be fully closed
183
+ this . dialog . addEventListener ( 'close' , ( ) => waitForClose ( ) , { once : true } )
184
+ } else {
185
+ waitForClose ( )
176
186
}
177
- }
178
- }
179
-
180
- waitForUser ( ) {
181
- return new Promise ( ( resolve ) => {
182
- this . dialog . addEventListener (
183
- 'close' ,
184
- ( event ) => {
185
- if ( this . dialog . returnValue === 'accept' ) {
186
- const value = this . hasFormData ? this . collectFormData ( ) : true
187
- resolve ( value )
188
- }
189
- } ,
190
- { once : true }
191
- )
192
187
} )
193
188
}
194
189
0 commit comments