@@ -336,8 +336,21 @@ export default class GridCommons {
336336 }
337337 }
338338
339+ // get the configuration for the provided modal
340+ getModalConfig ( name ) {
341+ let modalConfig = this . modals [ name || this . activeModal ] ;
342+ // sometimes the modalConfig is a function that returns a modalConfig
343+ // for example when the configuration dependes on the selected row in the grid
344+ if ( modalConfig && typeof modalConfig === "function" ) {
345+ modalConfig = modalConfig ( this . context ) ;
346+ }
347+ return modalConfig ;
348+ }
349+
339350 // change the current active modal
340351 changeActiveModal ( name ) {
352+ const prevModal = this . activeModal ;
353+
341354 // 1. check if there is a modal rendered
342355 if ( this . activeModal ) {
343356 ModalUtils . close ( `GridModal${ this . activeModal } ` ) ;
@@ -351,6 +364,19 @@ export default class GridCommons {
351364 this . context . updateComplete . then ( ( ) => {
352365 if ( this . activeModal ) {
353366 ModalUtils . show ( `GridModal${ this . activeModal } ` ) ;
367+
368+ // 4. if the active modal has changed, we can perform some action
369+ if ( prevModal !== this . activeModal ) {
370+ const modalConfig = this . getModalConfig ( ) ;
371+
372+ // if the clearAfterClosing flag is set, we need to reset the active modal once it is closed
373+ if ( modalConfig . clearAfterClosing === true ) {
374+ this . registerModalEventListener ( this . activeModal , "hidden.bs.modal" , ( ) => {
375+ // console.log(`${this.activeModal} modal closed, clearing active modal`);
376+ this . clearActiveModal ( ) ;
377+ } ) ;
378+ }
379+ }
354380 }
355381 } ) ;
356382 }
@@ -365,15 +391,20 @@ export default class GridCommons {
365391 this . modals = modals ;
366392 }
367393
394+ // method to register an event listener on the specified modal
395+ registerModalEventListener ( modalName , eventName , callback ) {
396+ if ( this . activeModal === modalName ) {
397+ const modalElement = this . context . querySelector ( `#GridModal${ modalName } ` ) ;
398+ if ( modalElement ) {
399+ modalElement . addEventListener ( eventName , event => callback ( event ) ) ;
400+ }
401+ }
402+ }
403+
368404 // render the active modal
369405 renderModals ( ) {
370406 if ( this . activeModal ) {
371- let modalConfig = this . modals [ this . activeModal ] ;
372- // sometimes the modalConfig is a function that returns a modalConfig
373- // for example when the configuration dependes on the selected row in the grid
374- if ( modalConfig && typeof modalConfig === "function" ) {
375- modalConfig = modalConfig ( this . context ) ;
376- }
407+ const modalConfig = this . getModalConfig ( ) ;
377408 // check if the modalConfig is a valid object
378409 if ( modalConfig && typeof modalConfig . render === "function" ) {
379410 return ModalUtils . create ( this . context , `GridModal${ this . activeModal } ` , modalConfig ) ;
0 commit comments