@@ -2,10 +2,15 @@ import "regenerator-runtime/runtime"; // needed for ``await`` support
22import $ from "jquery" ;
33import Base from "../../core/base" ;
44import Parser from "../../core/parser" ;
5+ import dom from "../../core/dom" ;
56import events from "../../core/events" ;
67import inject from "../inject/inject" ;
8+ import registry from "../../core/registry" ;
79import utils from "../../core/utils" ;
810
11+ // Initialize close-panel functionality.
12+ import "../close-panel/close-panel" ;
13+
914export const parser = new Parser ( "modal" ) ;
1015parser . addArgument ( "class" ) ;
1116parser . addArgument ( "closing" , [ "close-button" ] , [ "close-button" , "outside" ] , true ) ;
@@ -18,8 +23,6 @@ export default Base.extend({
1823 // div's are turned into modals
1924 // links, forms and subforms inject modals
2025 trigger : "div.pat-modal, a.pat-modal, form.pat-modal, .pat-modal.pat-subform" ,
21- els_close_panel : [ ] ,
22- els_close_panel_submit : [ ] ,
2326
2427 init ( $el , opts , trigger ) {
2528 if ( window . __patternslib_import_styles ) {
@@ -69,13 +72,12 @@ export default Base.extend({
6972 } ,
7073
7174 _init_div1 ( ) {
72- const $header = $ ( "<div class='header' />" ) ;
75+ const header = document . createElement ( "div" ) ;
76+ header . setAttribute ( "class" , "header" ) ;
77+
7378 if ( this . options . closing . indexOf ( "close-button" ) !== - 1 ) {
74- $ (
75- "<button type='button' class='close-panel'>" +
76- this . options . closeText +
77- "</button>"
78- ) . appendTo ( $header ) ;
79+ header . innerHTML = `<button type="button" class="close-panel">${ this . options . closeText } </button>` ;
80+ registry . scan ( header , [ "close-panel" ] ) ; // initialize close-panel
7981 }
8082
8183 // We cannot handle text nodes here
@@ -93,8 +95,8 @@ export default Base.extend({
9395 } else {
9496 this . $el . append ( "<div class='panel-content' />" ) ;
9597 }
96- this . $el . children ( ".panel-content" ) . before ( $ header) ;
97- this . $el . children ( this . options . panelHeaderContent ) . prependTo ( $ header) ;
98+ this . $el . children ( ".panel-content" ) . before ( header ) ;
99+ this . $el . children ( this . options . panelHeaderContent ) . prependTo ( header ) ;
98100
99101 // Restore focus in case the active element was a child of $el and
100102 // the focus was lost during the wrapping.
@@ -114,35 +116,8 @@ export default Base.extend({
114116 } ,
115117
116118 _init_handlers ( ) {
117- // All .close-panel buttons which are not submit buttons.
118- this . els_close_panel = this . el . querySelectorAll (
119- ".close-panel:not([type=submit])"
120- ) ;
121-
122- // All .close-panel buttons which are submit buttons.
123- this . els_close_panel_submit = this . el . querySelectorAll (
124- ".close-panel[type=submit]"
125- ) ;
126-
127- for ( const _el of this . els_close_panel ) {
128- events . add_event_listener (
129- _el ,
130- "click" ,
131- "pat-modal--destroy--trigger" ,
132- this . destroy . bind ( this ) ,
133- { once : true }
134- ) ;
135- }
136-
137- for ( const _el of this . els_close_panel_submit ) {
138- events . add_event_listener (
139- _el ,
140- "click" ,
141- "pat-modal--destroy-inject--trigger" ,
142- this . destroy_inject . bind ( this ) ,
143- { once : true }
144- ) ;
145- }
119+ this . el . classList . add ( "has-close-panel" ) ;
120+ dom . set_data ( this . el , "close_panel" , this . _close_handler . bind ( this ) ) ;
146121
147122 $ ( document ) . on ( "keyup.pat-modal" , this . _onKeyUp . bind ( this ) ) ;
148123 if ( this . options . closing . indexOf ( "outside" ) !== - 1 ) {
@@ -181,6 +156,16 @@ export default Base.extend({
181156 }
182157 } ,
183158
159+ _close_handler ( e ) {
160+ if ( e . target . matches ( "[type=submit], button:not([type=button])" ) ) {
161+ // submit + close
162+ this . destroy_inject ( e ) ;
163+ } else {
164+ // close only
165+ this . destroy ( ) ;
166+ }
167+ } ,
168+
184169 getTallestChild ( ) {
185170 let $tallest_child ;
186171 for ( const child of $ ( "*" , this . $el ) ) {
@@ -214,14 +199,6 @@ export default Base.extend({
214199
215200 async destroy ( ) {
216201 await utils . timeout ( 1 ) ; // wait a tick for event handlers (e.g. form submit) have a chance to kick in first.
217-
218- for ( const _el of this . els_close_panel ) {
219- events . remove_event_listener ( _el , "pat-modal--destroy--trigger" ) ;
220- }
221- for ( const _el of this . els_close_panel_submit ) {
222- events . remove_event_listener ( _el , "pat-modal--destroy-inject--trigger" ) ;
223- }
224-
225202 $ ( document ) . off ( ".pat-modal" ) ;
226203 this . $el . remove ( ) ;
227204 $ ( "body" ) . removeClass ( "modal-active" ) ;
@@ -231,8 +208,9 @@ export default Base.extend({
231208 destroy_inject ( e ) {
232209 const button = e . target ;
233210 const form = button . form ;
211+
234212 if ( form && form . classList . contains ( "pat-inject" ) ) {
235- // if the modal contains a for mwith pat-inject, wait for injection
213+ // if the modal contains a form with pat-inject, wait for injection
236214 // to be finished and then destroy the modal.
237215 const destroy_handler = ( ) => {
238216 this . destroy ( ) ;
0 commit comments