Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions public/js/icinga/behavior/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
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);
this.on('change', '#modal form input.autosubmit', this.onFormAutoSubmit, this);
this.on('click', '[data-icinga-modal]', this.onModalToggleClick, this);
this.on('mousedown', '#layout > #modal', this.onModalLeave, this);
this.on('click', '.modal-header > button', this.onModalClose, this);
this.on('keydown', this.onKeyDown, 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
Loading