Skip to content

Commit

Permalink
modal.js: Prevent unintentional closing of a modal
Browse files Browse the repository at this point in the history
Don't close modal if it contains changes and:
- `ESCAPE` is pressed
- User click on the overlay
  • Loading branch information
sukhwinder33445 committed Feb 3, 2025
1 parent 74369b7 commit dd1f8e3
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions public/js/icinga/behavior/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
this.icinga = icinga;
this.$layout = $('#layout');
this.$ghost = $('#modal-ghost');
this.hasChanges = false;

this.on('submit', '#modal form', this.onFormSubmit, this);
this.on('change', '#modal form select.autosubmit', this.onFormAutoSubmit, this);
Expand All @@ -25,6 +26,7 @@
this.on('mousedown', '#layout > #modal', this.onModalLeave, this);
this.on('click', '.modal-header > button', this.onModalClose, this);
this.on('keydown', '#layout > #modal.active', this.onKeyDown, this);
this.on('change', '#modal form', this.OnChange, this);
};

Modal.prototype = new Icinga.EventListener();
Expand Down Expand Up @@ -179,7 +181,7 @@
var _this = event.data.self;
var $target = $(event.target);

if ($target.is('#modal')) {
if (! _this.hasChanges && $target.is('#modal')) {
_this.hide($target);
}
};
Expand All @@ -201,16 +203,39 @@
* @param event {Event} The `keydown` event triggered by pushing a key
*/
Modal.prototype.onKeyDown = function(event) {
if (event.key !== 'Escape') {
return;
}

var _this = event.data.self;

if (! event.isDefaultPrevented() && event.key === 'Escape') {
if (_this.hasChanges || document.activeElement.matches('form :scope')) {
if (! _this.hasChanges) {
document.activeElement.blur();
}

return;
}

if (! event.isDefaultPrevented()) {
let $modal = _this.$layout.children('#modal');
if ($modal.length) {
_this.hide($modal);
}
}
};

/**
* Event handler to register whether the modal form has new changes after opening.
*
* This prevents the modal from being unintentionally closed
*
* @param event {Event} The `OnChange` event
*/
Modal.prototype.OnChange = function(event) {
event.data.self.hasChanges = true;
}

/**
* Make final preparations and add the modal to the DOM
*
Expand Down Expand Up @@ -248,6 +273,7 @@
// Remove pointerEvent none style to make the button clickable again
this.modalOpener.style.pointerEvents = '';
this.modalOpener = null;
this.hasChanges = false;

$modal.removeClass('active');
// Using `setTimeout` here to let the transition finish
Expand Down

0 comments on commit dd1f8e3

Please sign in to comment.