{"version":3,"file":"events.min.js","sources":["https:\/\/ead.cbm.al.gov.br\/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":["_interopRequireDefault","obj","__esModule","default","changesMadeString","_jquery","_yui","changesMadeCheck","e","returnValue","eventTypes","formError","formSubmittedByJavascript","formFieldValidationFailed","uploadStarted","uploadCompleted","uploadChanged","_exports","notifyFormError","field","dispatchEvent","notifyFormSubmittedByJavascript","form","skipValidation","arguments","length","undefined","fallbackHandled","window","skipClientValidation","customEvent","notifyFieldValidationFailure","message","cancelable","notifyUploadStarted","async","getString","addEventListener","document","getElementById","elementId","bubbles","cancellable","notifyUploadCompleted","removeEventListener","triggerUploadStarted","triggerUploadCompleted","types","legacyEventsRegistered","Y","use","element","one","target","formElement","closest","Global","fire","M","core","globalEvents","FORM_ERROR","formid","generateID","elementid","detail","event","FORM_SUBMIT_AJAX","currentTarget","legacyEvent","jQuery","Event","trigger","notifyUploadChanged"],"mappings":"qJAuHyB,SAAAA,uBAAAC,KAAAA,OAAAA,KAAAA,IAAAC,WAAAD,IAAAE,CAAAA,QAAAF,IAAA;;;;;;;;;;;;;;;;KApFzB,IAAIG,mXAmFJC,QAAAL,uBAAAK,SACAC,KAAAN,uBAAAM,MA9EA,MAAMC,iBAAmBC,IACjBA,IACAA,EAAEC,YAAcL,kBACpB,EAYSM,WAAa,CAQtBC,UAAW,kBAYXC,0BAA2B,kCAW3BC,0BAA2B,kCAS3BC,cAAe,0BASfC,gBAAiB,4BASjBC,cAAe,2BACjBC,SAAAP,WAAAA,WAcqFO,SAAAC,gBAAxDC,QAAS,EAAAC,kBAAaA,eAACV,WAAW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