MDL-64331 modals: Be careful closing modals

Don't close a modal when the user clicks outside of it and the modal contains a form.
This commit is contained in:
Damyon Wiese
2019-03-28 09:14:10 +08:00
parent 0920f35ed9
commit cdc7390446
7 changed files with 78 additions and 21 deletions

View File

@@ -35,6 +35,7 @@ define(['jquery', 'core/templates', 'core/notification', 'core/key_codes',
FOOTER: '[data-region="footer"]',
HIDE: '[data-action="hide"]',
DIALOG: '[role=dialog]',
FORM: 'form',
MENU_BAR: '[role=menubar]',
HAS_Z_INDEX: '.moodle-has-zindex',
CAN_RECEIVE_FOCUS: 'input:not([type="hidden"]), a[href], button, textarea, select, [tabindex]',
@@ -571,6 +572,18 @@ define(['jquery', 'core/templates', 'core/notification', 'core/key_codes',
}.bind(this));
};
/**
* Hide this modal if it does not contain a form.
*
* @method hideIfNotForm
*/
Modal.prototype.hideIfNotForm = function() {
var formElement = this.modal.find(SELECTORS.FORM);
if (formElement.length == 0) {
this.hide();
}
};
/**
* Hide this modal.
*
@@ -727,7 +740,7 @@ define(['jquery', 'core/templates', 'core/notification', 'core/key_codes',
// So, we check if we can still find the container element or not. If not, then the DOM tree is changed.
// It's best not to hide the modal in that case.
if ($(e.target).closest(SELECTORS.CONTAINER).length) {
this.hide();
this.hideIfNotForm();
}
}
}.bind(this));