diff options
author | Michael <michaelvickers.uk@gmail.com> | 2018-10-16 01:47:38 +0100 |
---|---|---|
committer | fguillot <fred@kanboard.net> | 2018-10-15 17:47:38 -0700 |
commit | 71630aaa77b518d3d0165f772251f09cc249f015 (patch) | |
tree | 76f0fcb273d610a87b138481ea54981cb7394e4f /assets/js/core | |
parent | cc81f9d4f5a9857e024829613ad670bc51056be5 (diff) |
Easier closing of dialogs
Allow closing of dialogs by clicking on the background area around it.
When the dialog is closed in this manner, if it contains a form with changed data present a warning that there are unsaved changes. Likewise if the user attempts to navigate away from the page, eg page reload, bookmark click, address bar entry.
If the dialog does not contain a form or the data remains unchanged no warning messages are shown.
Diffstat (limited to 'assets/js/core')
-rw-r--r-- | assets/js/core/modal.js | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/assets/js/core/modal.js b/assets/js/core/modal.js index e7f2c2ff..dc656efa 100644 --- a/assets/js/core/modal.js +++ b/assets/js/core/modal.js @@ -1,18 +1,42 @@ (function () { var isOpen = false; + var isFormDirty = false; + + function closeIfDirty() { + if (isFormDirty == false) { + return true; + } + + return window.confirm($("body").data("js-modal-close-msg").replace(/\\n/g,"\n")); + } function onOverlayClick(e) { if (e.target.matches('#modal-overlay')) { - e.stopPropagation(); - e.preventDefault(); - destroy(); + if (closeIfDirty()) { + e.stopPropagation(); + e.preventDefault(); + destroy(); + } } } + function onBeforeUnload(e) { + // Cancel the event as stated by the standard. + e.preventDefault(); + + // Chrome requires returnValue to be set. + e.returnValue = ''; + } + function onCloseButtonClick() { KB.trigger('modal.close'); } + function onFormChange() { + isFormDirty = true; + window.addEventListener('beforeunload', onBeforeUnload, false); + } + function onFormSubmit() { KB.trigger('modal.loading'); submitForm(); @@ -51,6 +75,7 @@ function afterRendering() { var formElement = KB.find('#modal-content form'); if (formElement) { + formElement.on('change', onFormChange, false); formElement.on('submit', onFormSubmit, false); } @@ -65,6 +90,13 @@ _KB.tagAutoComplete(); _KB.get('Task').onPopoverOpened(); + if (formElement) { + $('.form-date').datepicker('option', 'onSelect', onFormChange); + $('.form-datetime').datepicker('option', 'onSelect', onFormChange); + $(".color-picker").on('change', onFormChange); + $(".tag-autocomplete").on('change', onFormChange); + } + KB.trigger('modal.afterRender'); } @@ -122,6 +154,8 @@ function destroy() { isOpen = false; + isFormDirty = false; + window.removeEventListener('beforeunload', onBeforeUnload, false); var overlayElement = KB.find('#modal-overlay'); if (overlayElement) { |