Skip to content

Commit

Permalink
MDL-64573 output: ajax form event
Browse files Browse the repository at this point in the history
Add an event that can be fired when an mform is about to be submitted via ajax.
This allows custom field types to perform an action when the form is submitted.

The atto text editor will reset any autosaves when the form is submitted.
  • Loading branch information
Damyon Wiese committed Apr 24, 2019
1 parent 9d4f4f0 commit 260565e
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 18 deletions.
2 changes: 1 addition & 1 deletion calendar/amd/build/modal_event_form.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion calendar/amd/src/modal_event_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,17 @@ define([
* @return {object} A promise
*/
ModalEventForm.prototype.save = function() {
var loadingContainer = this.saveButton.find(SELECTORS.LOADING_ICON_CONTAINER);
var invalid,
loadingContainer = this.saveButton.find(SELECTORS.LOADING_ICON_CONTAINER);

// Now the change events have run, see if there are any "invalid" form fields.
invalid = this.getForm().find('[aria-invalid="true"]');

// If we found invalid fields, focus on the first one and do not submit via ajax.
if (invalid.length) {
invalid.first().focus();
return;
}

loadingContainer.removeClass('hidden');
this.disableButtons();
Expand Down Expand Up @@ -472,6 +482,8 @@ define([
// Catch the submit event before it is actually processed by the browser and
// prevent the submission. We'll take it from here.
this.getModal().on('submit', function(e) {
Event.notifyFormSubmitAjax(this.getForm()[0]);

this.save();

// Stop the form from actually submitting and prevent it's
Expand Down
2 changes: 1 addition & 1 deletion lib/amd/build/event.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions lib/amd/src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@ define(['jquery', 'core/yui'],
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: yuiNodes});
});
},

/**
* Trigger an event using both JQuery and YUI
*
* @method notifyFormSubmittedAjax
* @param {DOMElement} form
* @param {boolean} skipValidation Submit the form without validation. E.g. "Cancel".
*/
notifyFormSubmitAjax: function(form, skipValidation) {

// Argument is optional.
skipValidation = skipValidation || false;

Y.use('event', 'moodle-core-event', function(Y) {
if (skipValidation) {
window.skipClientValidation = true;
}
// Trigger it the JQuery way.
$(form).trigger(M.core.event.FORM_SUBMIT_AJAX);

// And again for YUI.
Y.one(form).fire(M.core.event.FORM_SUBMIT_AJAX, {currentTarget: Y.one(form)});

if (skipValidation) {
window.skipClientValidation = false;
}
});
},

/**
* Trigger an event using both JQuery and YUI
* This event alerts the world that the editor has restored some content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ Y.extend(EditorAutosaveIoDispatcher, Y.Base, {
if (typeof this._submitEvents[node.generateID()] === 'undefined') {
this._submitEvents[node.generateID()] = {
event: node.on('submit', this._onSubmit, this),
ajaxEvent: node.on(M.core.event.FORM_SUBMIT_AJAX, this._onSubmit, this),
ios: []
};
}
Expand Down
Loading

0 comments on commit 260565e

Please sign in to comment.