Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"edit_switch.min.js","sources":["../src/edit_switch.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 * Controls the edit switch.\n *\n * @module     core/edit_switch\n * @copyright  2021 Bas Brands <bas@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport {call as fetchMany} from 'core/ajax';\nimport {dispatchEvent} from 'core/event_dispatcher';\nimport {exception as displayException} from 'core/notification';\n\n/**\n * Change the Edit mode.\n *\n * @param {number} context The contextid that editing is being set for\n * @param {bool} setmode Whether editing is set or not\n * @return {Promise} Resolved with an array file the stored file url.\n */\nconst setEditMode = (context, setmode) => fetchMany([{\n    methodname: 'core_change_editmode',\n    args: {\n        context,\n        setmode,\n    },\n}])[0];\n\n/**\n * Toggle the edit switch\n *\n * @method\n * @protected\n * @param {HTMLElement} editSwitch\n */\nconst toggleEditSwitch = editSwitch => {\n    if (editSwitch.checked) {\n        editSwitch.setAttribute('aria-checked', true);\n    } else {\n        editSwitch.setAttribute('aria-checked', false);\n    }\n\n    const event = notifyEditModeSet(editSwitch, editSwitch.checked);\n    if (!event.defaultPrevented) {\n        editSwitch.setAttribute('disabled', true);\n        window.location = editSwitch.dataset.pageurl;\n    }\n};\n\n/**\n * Names of events for core/edit_switch.\n *\n * @static\n * @property {String} editModeSet See {@link event:core/edit_switch/editModeSet}\n */\nexport const eventTypes = {\n    /**\n     * An event triggered when the edit mode toggled.\n     *\n     * @event core/edit_switch/editModeSet\n     * @type {CustomEvent}\n     * @property {HTMLElement} target The switch used to toggle the edit mode\n     * @property {object} detail\n     * @property {bool} detail.editMode\n     */\n    editModeSet: 'core/edit_switch/editModeSet',\n};\n\n/**\n * Dispatch the editModeSet event after changing the edit mode.\n *\n * This event is cancelable.\n *\n * The default action is to reload the page after toggling the edit mode.\n *\n * @method\n * @protected\n * @param {HTMLElement} container\n * @param {bool} editMode\n * @returns {CustomEvent}\n */\nconst notifyEditModeSet = (container, editMode) => dispatchEvent(\n    eventTypes.editModeSet,\n    {editMode},\n    container,\n    {cancelable: true}\n);\n\n/**\n * Add the eventlistener for the editswitch.\n *\n * @param {string} editingSwitchId The id of the editing switch to listen for\n */\nexport const init = editingSwitchId => {\n    const editSwitch = document.getElementById(editingSwitchId);\n    editSwitch.addEventListener('change', () => {\n        setEditMode(editSwitch.dataset.context, editSwitch.checked)\n        .then(result => {\n            if (result.success) {\n                toggleEditSwitch(editSwitch);\n            } else {\n                editSwitch.checked = false;\n            }\n            return;\n        })\n        .catch(displayException);\n    });\n};\n"],"names":["eventTypes","editModeSet","notifyEditModeSet","container","editMode","cancelable","editingSwitchId","editSwitch","document","getElementById","addEventListener","context","setmode","dataset","checked","methodname","args","then","result","success","setAttribute","defaultPrevented","window","location","pageurl","toggleEditSwitch","catch","displayException"],"mappings":";;;;;;;;MAqEaA,WAAa,CAUtBC,YAAa,qEAgBXC,kBAAoB,CAACC,UAAWC,YAAa,mCAC/CJ,WAAWC,YACX,CAACG,SAAAA,UACDD,UACA,CAACE,YAAY,kBAQGC,wBACVC,WAAaC,SAASC,eAAeH,iBAC3CC,WAAWG,iBAAiB,UAAU,KA3EtB,IAACC,QAASC,SAATD,QA4EDJ,WAAWM,QAAQF,QA5ETC,QA4EkBL,WAAWO,SA5EjB,cAAU,CAAC,CACjDC,WAAY,uBACZC,KAAM,CACFL,QAAAA,QACAC,QAAAA,YAEJ,IAuEKK,MAAKC,SACEA,OAAOC,QA/DEZ,CAAAA,aACjBA,WAAWO,QACXP,WAAWa,aAAa,gBAAgB,GAExCb,WAAWa,aAAa,gBAAgB,GAG9BlB,kBAAkBK,WAAYA,WAAWO,SAC5CO,mBACPd,WAAWa,aAAa,YAAY,GACpCE,OAAOC,SAAWhB,WAAWM,QAAQW,UAsD7BC,CAAiBlB,YAEjBA,WAAWO,SAAU,KAI5BY,MAAMC"}