{"version":3,"file":"events.min.js","sources":["https:\/\/lms.innovativelearning.eu\/lib\/form\/amd\/src\/events.js"],"sourcesContent":["\/\/ This file is part of Moodle - http:\/\/moodle.org\/\n\/\/\n\/\/ Moodle is free software: you can redistribute it and\/or modify\n\/\/ it under the terms of the GNU General Public License as published by\n\/\/ the Free Software Foundation, either version 3 of the License, or\n\/\/ (at your option) any later version.\n\/\/\n\/\/ Moodle is distributed in the hope that it will be useful,\n\/\/ but WITHOUT ANY WARRANTY; without even the implied warranty of\n\/\/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\/\/ GNU General Public License for more details.\n\/\/\n\/\/ You should have received a copy of the GNU General Public License\n\/\/ along with Moodle. If not, see .\n\n\/**\n * Javascript events for the `core_form` subsystem.\n *\n * @module core_form\/events\n * @copyright 2021 Huong Nguyen \n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n * @since 3.10\n *\n * @example Example of listening to a form event.<\/caption>\n * import {eventTypes as formEventTypes} from 'core_form\/events';\n *\n * document.addEventListener(formEventTypes.formSubmittedByJavascript, e => {\n * window.console.log(e.target); \/\/ The form that was submitted.\n * window.console.log(e.detail.skipValidation); \/\/ Whether form validation was skipped.\n * });\n *\/\n\nimport {get_string as getString} from 'core\/str';\nimport {dispatchEvent} from 'core\/event_dispatcher';\n\nlet changesMadeString;\n\n\/**\n * Prevent user navigate away when upload progress still running.\n * @param {Event} e The event\n *\/\nconst changesMadeCheck = e => {\n if (e) {\n e.returnValue = changesMadeString;\n }\n};\n\n\/**\n * Events for `core_form`.\n *\n * @constant\n * @property {String} formError See {@link event:core_form\/error}\n * @property {String} formFieldValidationFailed See {@link event:core_form\/fieldValidationFailed}\n * @property {String} formSubmittedByJavascript See {@link event:core_form\/submittedByJavascript}\n * @property {String} uploadChanged See {@link event:core_form\/uploadChanged}\n *\/\nexport const eventTypes = {\n \/**\n * An event triggered when a form contains an error\n *\n * @event formError\n * @type {CustomEvent}\n * @property {HTMLElement} target The form field which errored\n *\/\n formError: 'core_form\/error',\n\n \/**\n * An event triggered when an mform is about to be submitted via javascript.\n *\n * @event core_form\/submittedByJavascript\n * @type {CustomEvent}\n * @property {HTMLElement} target The form that was submitted\n * @property {object} detail\n * @property {boolean} detail.skipValidation Whether the form was submitted without validation (i.e. via a Cancel button)\n * @property {boolean} detail.fallbackHandled Whether the legacy YUI event has been handled\n *\/\n formSubmittedByJavascript: 'core_form\/submittedByJavascript',\n\n \/**\n * An event triggered upon form field validation failure.\n *\n * @event core_form\/fieldValidationFailed\n * @type {CustomEvent}\n * @property {HTMLElement} target The field that failed validation\n * @property {object} detail\n * @property {String} detail.message The message displayed upon failure\n *\/\n formFieldValidationFailed: 'core_form\/fieldValidationFailed',\n\n \/**\n * An event triggered when an upload is started\n *\n * @event core_form\/uploadStarted\n * @type {CustomEvent}\n * @property {HTMLElement} target The location where the upload began\n *\/\n uploadStarted: 'core_form\/uploadStarted',\n\n \/**\n * An event triggered when an upload completes\n *\n * @event core_form\/uploadCompleted\n * @type {CustomEvent}\n * @property {HTMLElement} target The location where the upload completed\n *\/\n uploadCompleted: 'core_form\/uploadCompleted',\n\n \/**\n * An event triggered when a file upload field has been changed.\n *\n * @event core_form\/uploadChanged\n * @type {CustomEvent}\n * @property {HTMLElement} target The form field which was changed\n *\/\n uploadChanged: 'core_form\/uploadChanged',\n};\n\n\/\/ These are only imported for legacy.\nimport jQuery from 'jquery';\nimport Y from 'core\/yui';\n\n\/**\n * Trigger an event to indicate that a form field contained an error.\n *\n * @method notifyFormError\n * @param {HTMLElement} field The form field causing the error\n * @returns {CustomEvent}\n * @fires formError\n *\/\nexport const notifyFormError = field => dispatchEvent(eventTypes.formError, {}, field);\n\n\/**\n * Trigger an event to indiciate that a form was submitted by Javascript.\n *\n * @method\n * @param {HTMLElement} form The form that was submitted\n * @param {Boolean} skipValidation Submit the form without validation. E.g. \"Cancel\".\n * @param {Boolean} fallbackHandled The legacy YUI event has been handled\n * @returns {CustomEvent}\n * @fires formSubmittedByJavascript\n *\/\nexport const notifyFormSubmittedByJavascript = (form, skipValidation = false, fallbackHandled = false) => {\n if (skipValidation) {\n window.skipClientValidation = true;\n }\n\n const customEvent = dispatchEvent(\n eventTypes.formSubmittedByJavascript,\n {\n skipValidation,\n fallbackHandled,\n },\n form\n );\n\n if (skipValidation) {\n window.skipClientValidation = false;\n }\n\n return customEvent;\n};\n\n\/**\n * Trigger an event to indicate that a form field contained an error.\n *\n * @method notifyFieldValidationFailure\n * @param {HTMLElement} field The field which failed validation\n * @param {String} message The message displayed\n * @returns {CustomEvent}\n * @fires formFieldValidationFailed\n *\/\nexport const notifyFieldValidationFailure = (field, message) => dispatchEvent(\n eventTypes.formFieldValidationFailed,\n {\n message,\n },\n field,\n {\n cancelable: true\n }\n);\n\n\/**\n * Trigger an event to indicate that an upload was started.\n *\n * @method\n * @param {String} elementId The element which was uploaded to\n * @returns {CustomEvent}\n * @fires uploadStarted\n *\/\nexport const notifyUploadStarted = async elementId => {\n \/\/ Add an additional check for changes made.\n changesMadeString = await getString('changesmadereallygoaway', 'moodle');\n window.addEventListener('beforeunload', changesMadeCheck);\n\n return dispatchEvent(\n eventTypes.uploadStarted,\n {},\n document.getElementById(elementId),\n {\n bubbles: true,\n cancellable: false,\n }\n );\n};\n\n\/**\n * Trigger an event to indicate that an upload was completed.\n *\n * @method\n * @param {String} elementId The element which was uploaded to\n * @returns {CustomEvent}\n * @fires uploadCompleted\n *\/\nexport const notifyUploadCompleted = elementId => {\n \/\/ Remove the additional check for changes made.\n window.removeEventListener('beforeunload', changesMadeCheck);\n\n return dispatchEvent(\n eventTypes.uploadCompleted,\n {},\n document.getElementById(elementId),\n {\n bubbles: true,\n cancellable: false,\n }\n );\n};\n\n\/**\n * Trigger upload start event.\n *\n * @method\n * @param {String} elementId\n * @returns {CustomEvent}\n * @fires uploadStarted\n * @deprecated Since Moodle 4.0 See {@link module:core_form\/events.notifyUploadStarted notifyUploadStarted}\n *\/\nexport const triggerUploadStarted = notifyUploadStarted;\n\n\/**\n * Trigger upload complete event.\n *\n * @method\n * @param {String} elementId\n * @returns {CustomEvent}\n * @fires uploadCompleted\n * @deprecated Since Moodle 4.0 See {@link module:core_form\/events.notifyUploadCompleted notifyUploadCompleted}\n *\/\nexport const triggerUploadCompleted = notifyUploadCompleted;\n\n\/**\n * List of the events.\n *\n * @deprecated since Moodle 4.0. See {@link module:core_form\/events.eventTypes eventTypes} instead.\n **\/\nexport const types = {\n uploadStarted: 'core_form\/uploadStarted',\n uploadCompleted: 'core_form\/uploadCompleted',\n};\n\nlet legacyEventsRegistered = false;\nif (!legacyEventsRegistered) {\n \/\/ The following event triggers are legacy and will be removed in the future.\n \/\/ The following approach provides a backwards-compatability layer for the new events.\n \/\/ Code should be updated to make use of native events.\n Y.use('event', 'moodle-core-event', () => {\n\n \/\/ Watch for the new native formError event, and trigger the legacy YUI event.\n document.addEventListener(eventTypes.formError, e => {\n const element = Y.one(e.target);\n const formElement = Y.one(e.target.closest('form'));\n\n Y.Global.fire(\n M.core.globalEvents.FORM_ERROR,\n {\n formid: formElement.generateID(),\n elementid: element.generateID(),\n }\n );\n });\n\n \/\/ Watch for the new native formSubmittedByJavascript event, and trigger the legacy YUI event.\n document.addEventListener(eventTypes.formSubmittedByJavascript, e => {\n if (e.detail.fallbackHandled) {\n \/\/ This event was originally generated by a YUI event.\n \/\/ Do not generate another as this will recurse.\n return;\n }\n\n if (e.skipValidation) {\n window.skipClientValidation = true;\n }\n\n \/\/ Trigger the legacy YUI event.\n const form = Y.one(e.target);\n form.fire(\n M.core.event.FORM_SUBMIT_AJAX,\n {\n currentTarget: form,\n fallbackHandled: true,\n }\n );\n\n if (e.skipValidation) {\n window.skipClientValidation = false;\n }\n });\n });\n\n \/\/ Watch for the new native formFieldValidationFailed event, and trigger the legacy jQuery event.\n document.addEventListener(eventTypes.formFieldValidationFailed, e => {\n \/\/ Note: The \"core_form-field-validation\" event is hard-coded in core\/event.\n \/\/ This is not included to prevent cyclic module dependencies.\n const legacyEvent = jQuery.Event(\"core_form-field-validation\");\n\n jQuery(e.target).trigger(legacyEvent, e.detail.message);\n });\n\n legacyEventsRegistered = true;\n}\n\n\/**\n * Trigger an event to notify the file upload field has been changed.\n *\n * @method\n * @param {string} elementId The element which was changed\n * @returns {CustomEvent}\n * @fires uploadChanged\n *\/\nexport const notifyUploadChanged = elementId => dispatchEvent(\n eventTypes.uploadChanged,\n {},\n document.getElementById(elementId),\n {\n bubbles: true,\n cancellable: false,\n }\n);\n"],"names":["changesMadeString","changesMadeCheck","e","returnValue","eventTypes","formError","formSubmittedByJavascript","formFieldValidationFailed","uploadStarted","uploadCompleted","uploadChanged","field","form","skipValidation","fallbackHandled","window","skipClientValidation","customEvent","message","cancelable","notifyUploadStarted","async","addEventListener","document","getElementById","elementId","bubbles","cancellable","notifyUploadCompleted","removeEventListener","triggerUploadStarted","triggerUploadCompleted","legacyEventsRegistered","use","element","Y","one","target","formElement","closest","Global","fire","M","core","globalEvents","FORM_ERROR","formid","generateID","elementid","detail","event","FORM_SUBMIT_AJAX","currentTarget","legacyEvent","jQuery","Event","trigger"],"mappings":";;;;;;;;;;;;;;;;SAmCIA,mcAMEC,iBAAmBC,IACjBA,IACAA,EAAEC,YAAcH,oBAaXI,WAAa,CAQtBC,UAAW,kBAYXC,0BAA2B,kCAW3BC,0BAA2B,kCAS3BC,cAAe,0BASfC,gBAAiB,4BASjBC,cAAe,mFAeYC,QAAS,mCAAcP,WAAWC,UAAW,GAAIM,gDAYjC,SAACC,UAAMC,uEAAwBC,wEACtED,iBACAE,OAAOC,sBAAuB,SAG5BC,aAAc,mCAChBb,WAAWE,0BACX,CACIO,eAAAA,eACAC,gBAAAA,iBAEJF,aAGAC,iBACAE,OAAOC,sBAAuB,GAG3BC,mDAYiC,CAACN,MAAOO,WAAY,mCAC5Dd,WAAWG,0BACX,CACIW,QAAAA,SAEJP,MACA,CACIQ,YAAY,UAYPC,oBAAsBC,MAAAA,YAE\/BrB,wBAA0B,mBAAU,0B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