diff --git a/public/lib/amd/build/edit_switch.min.js b/public/lib/amd/build/edit_switch.min.js index 908684047c1..903b0d5b686 100644 --- a/public/lib/amd/build/edit_switch.min.js +++ b/public/lib/amd/build/edit_switch.min.js @@ -1,11 +1,10 @@ -define("core/edit_switch",["exports","core/ajax","core/event_dispatcher","core/notification"],(function(_exports,_ajax,_event_dispatcher,_notification){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=_exports.eventTypes=void 0; +define("core/edit_switch",["exports","core/ajax","core/event_dispatcher","core/notification","core/pending"],(function(_exports,_ajax,_event_dispatcher,_notification,_pending){var obj; /** * Controls the edit switch. * * @module core/edit_switch * @copyright 2021 Bas Brands * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -const eventTypes={editModeSet:"core/edit_switch/editModeSet"};_exports.eventTypes=eventTypes;const notifyEditModeSet=(container,editMode)=>(0,_event_dispatcher.dispatchEvent)(eventTypes.editModeSet,{editMode:editMode},container,{cancelable:!0});_exports.init=editingSwitchId=>{const editSwitch=document.getElementById(editingSwitchId);editSwitch.addEventListener("change",(()=>{var context,setmode;(context=editSwitch.dataset.context,setmode=editSwitch.checked,(0,_ajax.call)([{methodname:"core_change_editmode",args:{context:context,setmode:setmode}}])[0]).then((result=>{result.success?(editSwitch=>{editSwitch.checked?editSwitch.setAttribute("aria-checked",!0):editSwitch.setAttribute("aria-checked",!1),notifyEditModeSet(editSwitch,editSwitch.checked).defaultPrevented||(editSwitch.setAttribute("disabled",!0),window.location=editSwitch.dataset.pageurl)})(editSwitch):editSwitch.checked=!1})).catch(_notification.exception)}))}})); + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=_exports.eventTypes=void 0,_pending=(obj=_pending)&&obj.__esModule?obj:{default:obj};const eventTypes={editModeSet:"core/edit_switch/editModeSet"};_exports.eventTypes=eventTypes;const notifyEditModeSet=(container,editMode)=>(0,_event_dispatcher.dispatchEvent)(eventTypes.editModeSet,{editMode:editMode},container,{cancelable:!0});_exports.init=editingSwitchId=>{const editSwitch=document.getElementById(editingSwitchId);editSwitch.addEventListener("change",(()=>{const pendingPromise=new _pending.default("core/edit_switch:toggle");var context,setmode;(context=editSwitch.dataset.context,setmode=editSwitch.checked,(0,_ajax.call)([{methodname:"core_change_editmode",args:{context:context,setmode:setmode}}])[0]).then((result=>{if(result.success){const redirected=(editSwitch=>(editSwitch.checked?editSwitch.setAttribute("aria-checked",!0):editSwitch.setAttribute("aria-checked",!1),!notifyEditModeSet(editSwitch,editSwitch.checked).defaultPrevented&&(editSwitch.setAttribute("disabled",!0),window.location=editSwitch.dataset.pageurl,!0)))(editSwitch);redirected||pendingPromise.resolve()}else editSwitch.checked=!1,pendingPromise.resolve()})).catch((error=>{pendingPromise.resolve(),(0,_notification.exception)(error)}))}))}})); //# sourceMappingURL=edit_switch.min.js.map \ No newline at end of file diff --git a/public/lib/amd/build/edit_switch.min.js.map b/public/lib/amd/build/edit_switch.min.js.map index ac217b88911..c5b0c449002 100644 --- a/public/lib/amd/build/edit_switch.min.js.map +++ b/public/lib/amd/build/edit_switch.min.js.map @@ -1 +1 @@ -{"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 .\n\n/**\n * Controls the edit switch.\n *\n * @module core/edit_switch\n * @copyright 2021 Bas Brands \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"} \ No newline at end of file +{"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 .\n\n/**\n * Controls the edit switch.\n *\n * @module core/edit_switch\n * @copyright 2021 Bas Brands \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';\nimport Pending from \"core/pending\";\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 return true;\n }\n return false;\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 const pendingPromise = new Pending(\"core/edit_switch:toggle\");\n setEditMode(editSwitch.dataset.context, editSwitch.checked)\n .then((result) => {\n if (result.success) {\n const redirected = toggleEditSwitch(editSwitch);\n if (!redirected) {\n pendingPromise.resolve();\n }\n } else {\n editSwitch.checked = false;\n pendingPromise.resolve();\n }\n return;\n })\n .catch((error) => {\n pendingPromise.resolve();\n displayException(error);\n });\n });\n};\n"],"names":["eventTypes","editModeSet","notifyEditModeSet","container","editMode","cancelable","editingSwitchId","editSwitch","document","getElementById","addEventListener","pendingPromise","Pending","context","setmode","dataset","checked","methodname","args","then","result","success","redirected","setAttribute","defaultPrevented","window","location","pageurl","toggleEditSwitch","resolve","catch","error"],"mappings":";;;;;;;sKAwEaA,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,WAC5BC,eAAiB,IAAIC,iBAAQ,2BA9EvB,IAACC,QAASC,SAATD,QA+EDN,WAAWQ,QAAQF,QA/ETC,QA+EkBP,WAAWS,SA/EjB,cAAU,CAAC,CACjDC,WAAY,uBACZC,KAAM,CACFL,QAAAA,QACAC,QAAAA,YAEJ,IA0EKK,MAAMC,YACCA,OAAOC,QAAS,OACVC,WAnEGf,CAAAA,aACjBA,WAAWS,QACXT,WAAWgB,aAAa,gBAAgB,GAExChB,WAAWgB,aAAa,gBAAgB,IAG9BrB,kBAAkBK,WAAYA,WAAWS,SAC5CQ,mBACPjB,WAAWgB,aAAa,YAAY,GACpCE,OAAOC,SAAWnB,WAAWQ,QAAQY,SAC9B,IAwDoBC,CAAiBrB,YAC/Be,YACDX,eAAekB,eAGnBtB,WAAWS,SAAU,EACrBL,eAAekB,aAItBC,OAAOC,QACJpB,eAAekB,sCACEE"} \ No newline at end of file diff --git a/public/lib/amd/src/edit_switch.js b/public/lib/amd/src/edit_switch.js index 0c545e053a0..746be157ba2 100644 --- a/public/lib/amd/src/edit_switch.js +++ b/public/lib/amd/src/edit_switch.js @@ -24,6 +24,7 @@ import {call as fetchMany} from 'core/ajax'; import {dispatchEvent} from 'core/event_dispatcher'; import {exception as displayException} from 'core/notification'; +import Pending from "core/pending"; /** * Change the Edit mode. @@ -58,7 +59,9 @@ const toggleEditSwitch = editSwitch => { if (!event.defaultPrevented) { editSwitch.setAttribute('disabled', true); window.location = editSwitch.dataset.pageurl; + return true; } + return false; }; /** @@ -108,15 +111,23 @@ const notifyEditModeSet = (container, editMode) => dispatchEvent( export const init = editingSwitchId => { const editSwitch = document.getElementById(editingSwitchId); editSwitch.addEventListener('change', () => { + const pendingPromise = new Pending("core/edit_switch:toggle"); setEditMode(editSwitch.dataset.context, editSwitch.checked) - .then(result => { + .then((result) => { if (result.success) { - toggleEditSwitch(editSwitch); + const redirected = toggleEditSwitch(editSwitch); + if (!redirected) { + pendingPromise.resolve(); + } } else { editSwitch.checked = false; + pendingPromise.resolve(); } return; }) - .catch(displayException); + .catch((error) => { + pendingPromise.resolve(); + displayException(error); + }); }); };