Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"events.min.js","sources":["../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 <http://www.gnu.org/licenses/>.\n\n/**\n * Javascript events for the `core_form` subsystem.\n *\n * @module core_form/events\n * @copyright 2021 Huong Nguyen <huongnv13@gmail.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.10\n *\n * @example <caption>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 {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 * @property {String} fieldStructureChanged See {@link event:core_form/fieldStructureChanged}\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     * An event triggered when a form field structure has changed.\n     *\n     * @event core_form/fieldStructureChanged\n     * @type {CustomEvent}\n     * @property {HTMLElement} target The form field that has changed\n     */\n    fieldStructureChanged: 'core_form/fieldStructureChanged',\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\n/**\n * Trigger an event to notify the field structure has changed.\n *\n * @method\n * @param {string} elementId The element which was changed\n * @returns {CustomEvent}\n * @fires fieldStructureChanged\n */\nexport const notifyFieldStructureChanged = elementId => dispatchEvent(\n    eventTypes.fieldStructureChanged,\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","fieldStructureChanged","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,weAMEC,iBAAmBC,IACjBA,IACAA,EAAEC,YAAcH,oBAcXI,WAAa,CAQtBC,UAAW,kBAYXC,0BAA2B,kCAW3BC,0BA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