Skip to content

Commit bad5229

Browse files
Update app.js
Previous Commit didn't correct issue with nested modals. This commit changes how modals are created to put each one in it's own unique container so that closing a nested modal does not close the parent container
1 parent 5dc7619 commit bad5229

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

js/app.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -404,30 +404,54 @@ $(document).ready(function() {
404404

405405
/*
406406
|---------------------------------------------------------------
407-
| Nested Modal Fix for Bootstrap/AdminLTE
408-
| Prevents parent modals from closing when a child modal closes
407+
| Nested Modal Fix for ITFlow AJAX Modals
408+
| Each modal gets a unique container, parent modals remain open
409409
|---------------------------------------------------------------
410410
*/
411-
$(document).on('show.bs.modal', '.modal', function () {
412-
// How many modals are currently visible?
413-
const visibleModals = $('.modal:visible').length;
414-
415-
// Increase z-index for each new modal
416-
const zIndex = 1040 + (10 * visibleModals);
417-
$(this).css('z-index', zIndex);
418-
419-
// Delay required because Bootstrap inserts backdrop asynchronously
420-
setTimeout(() => {
421-
$('.modal-backdrop').not('.modal-stack')
422-
.css('z-index', zIndex - 1)
423-
.addClass('modal-stack'); // mark backdrops so they aren't reset
424-
}, 0);
425-
});
426411

427-
// Restore modal-open class when closing a child modal
428-
$(document).on('hidden.bs.modal', '.modal', function () {
429-
if ($('.modal:visible').length > 0) {
430-
// Ensure background scroll remains locked
431-
$('body').addClass('modal-open');
432-
}
433-
});
412+
(function() {
413+
if (!window.createAjaxModal) return;
414+
415+
// Keep original function just in case
416+
const originalCreateAjaxModal = window.createAjaxModal;
417+
418+
window.createAjaxModal = function(contentHtml) {
419+
// Generate a unique ID for this modal
420+
const modalId = 'ajaxModal_' + Date.now();
421+
422+
// Create a new modal container
423+
const $modal = $('<div class="modal fade" tabindex="-1" aria-hidden="true">')
424+
.attr('id', modalId)
425+
.html(contentHtml)
426+
.appendTo('body');
427+
428+
// Initialize Bootstrap modal
429+
$modal.modal('show');
430+
431+
// Ensure only this modal is removed when hidden
432+
$modal.on('hidden.bs.modal', function () {
433+
$(this).remove();
434+
});
435+
436+
return $modal;
437+
};
438+
439+
// Optional: Keep proper z-index stacking for multiple nested modals
440+
$(document).on('show.bs.modal', '.modal', function () {
441+
const zIndex = 1040 + (10 * $('.modal:visible').length);
442+
$(this).css('z-index', zIndex);
443+
setTimeout(() => {
444+
$('.modal-backdrop').not('.modal-stack')
445+
.css('z-index', zIndex - 1)
446+
.addClass('modal-stack');
447+
}, 0);
448+
});
449+
450+
$(document).on('hidden.bs.modal', '.modal', function () {
451+
if ($('.modal:visible').length > 0) {
452+
$('body').addClass('modal-open');
453+
}
454+
});
455+
456+
})();
457+

0 commit comments

Comments
 (0)