1 {"version":3,"sources":["../src/confirm.js"],"names":["registered","confirmedPromise","title","question","saveLabel","Promise","resolve","reject","getConfirmationString","dataset","field","Str","get_string","apply","JSON","parse","registerConfirmationListeners","document","addEventListener","e","confirmRequest","target","closest","preventDefault","then","stringForToast","str","catch","window","location","href","confirmationDestination"],"mappings":"qYA6CA,O,yiBAIIA,CAAAA,CAAU,G,CAYRC,CAAgB,CAAG,SAACC,CAAD,CAAQC,CAAR,CAAkBC,CAAlB,QAAgC,IAAIC,CAAAA,OAAJ,CAAY,SAACC,CAAD,CAAUC,CAAV,CAAqB,CACtF,iBAAWL,CAAX,CAAkBC,CAAlB,CAA4BC,CAA5B,CAAuCE,CAAvC,CAAgDC,CAAhD,CACH,CAFwD,CAAhC,C,CAcnBC,CAAqB,CAAG,SAACC,CAAD,CAAUC,CAAV,CAAoB,CAC9C,GAAID,CAAO,uBAAgBC,CAAhB,QAAX,CAAwC,CACpC,MAAOC,CAAAA,CAAG,CAACC,UAAJ,CAAeC,KAAf,CAAqB,IAArB,CAA2BC,IAAI,CAACC,KAAL,CAAWN,CAAO,uBAAgBC,CAAhB,QAAlB,CAA3B,CACV,CACD,MAAOD,CAAAA,CAAO,uBAAgBC,CAAhB,EACjB,C,CAQKM,CAA6B,CAAG,UAAM,CACxCC,QAAQ,CAACC,gBAAT,CAA0B,OAA1B,CAAmC,SAAAC,CAAC,CAAI,CACpC,GAAMC,CAAAA,CAAc,CAAGD,CAAC,CAACE,MAAF,CAASC,OAAT,CAAiB,+BAAjB,CAAvB,CACA,GAAIF,CAAJ,CAAoB,CAChBD,CAAC,CAACI,cAAF,GACAtB,CAAgB,CACZO,CAAqB,CAACY,CAAc,CAACX,OAAhB,CAAyB,OAAzB,CADT,CAEZD,CAAqB,CAACY,CAAc,CAACX,OAAhB,CAAyB,UAAzB,CAFT,CAGZD,CAAqB,CAACY,CAAc,CAACX,OAAhB,CAAyB,WAAzB,CAHT,CAAhB,CAKCe,IALD,CAKM,UAAM,CACR,GAAoD,MAAhD,GAAAJ,CAAc,CAACX,OAAf,kBAAJ,CAA4D,CACxD,GAAMgB,CAAAA,CAAc,CAAGjB,CAAqB,CAACY,CAAc,CAACX,OAAhB,CAAyB,mBAAzB,CAA5C,CACA,GAA8B,QAA1B,QAAOgB,CAAAA,CAAX,CAAwC,CACpC,UAASA,CAAT,CACH,CAFD,IAEO,CACHA,CAAc,CAACD,IAAf,CAAoB,SAAAE,CAAG,QAAI,UAASA,CAAT,CAAJ,CAAvB,EAA0CC,KAA1C,CAAgD,SAAAR,CAAC,QAAI,gBAAUA,CAAV,CAAJ,CAAjD,CACH,CACJ,CACDS,MAAM,CAACC,QAAP,CAAgBC,IAAhB,CAAuBV,CAAc,CAACX,OAAf,CAAuBsB,uBAEjD,CAhBD,EAgBGJ,KAhBH,CAgBS,UAAM,CAAE,CAhBjB,CAiBH,CACJ,CAtBD,CAuBH,C,CAED,GAAI,CAAC3B,CAAL,CAAiB,CACbgB,CAA6B,GAC7BhB,CAAU,GACb,C","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_confirm` modal.\n *\n * @module core/confirm\n * @copyright 2021 Andrew Nicols <andrew@nicols.co.uk>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 4.0\n *\n * @example <caption>Calling the confirmation modal to delete a block</caption>\n *\n * // The following is an example of how to use this module via an indirect PHP call with a button.\n *\n * $controls[] = new action_menu_link_secondary(\n * $deleteactionurl,\n * new pix_icon('t/delete', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')),\n * $str,\n * [\n * 'class' => 'editing_delete',\n * 'data-confirmation' => 'modal', // Needed so this module will pick it up in the click handler.\n * 'data-confirmation-title-str' => json_encode(['deletecheck_modal', 'block']),\n * 'data-confirmation-question-str' => json_encode(['deleteblockcheck', 'block', $blocktitle]),\n * 'data-confirmation-yes-button-str' => json_encode(['delete', 'core']),\n * 'data-confirmation-toast' => 'true', // Can be set to inform the user that their action was a success.\n * 'data-confirmation-toast-confirmation-str' => json_encode(['deleteblockinprogress', 'block', $blocktitle]),\n * 'data-confirmation-destination' => $deleteconfirmationurl->out(false), // Where do you want to direct the user?\n * ]\n * );\n */\n\nimport {saveCancel, exception} from 'core/notification';\nimport * as Str from 'core/str';\nimport {add as addToast} from 'core/toast';\n\n// We want to ensure that we only initialize the listeners only once.\nlet registered = false;\n\n/**\n * Add all of the supplied notifications.\n *\n * @method confirmedPromise\n * @protected\n * @param {Promise|String} title The header of the modal\n * @param {Promise|String} question What do we want the user to confirm\n * @param {Promise|String} saveLabel The modal action link text\n * @return {Promise}\n */\nconst confirmedPromise = (title, question, saveLabel) => new Promise((resolve, reject) => {\n saveCancel(title, question, saveLabel, resolve, reject);\n});\n\n/**\n * Either fetch the string or return it from the dom node.\n *\n * @method getConfirmationString\n * @private\n * @param {HTMLElement} dataset The page element to fetch dataset items in\n * @param {String} field The dataset field name to fetch the contents of\n * @return {Promise|String}\n *\n */\nconst getConfirmationString = (dataset, field) => {\n if (dataset[`confirmation${field}Str`]) {\n return Str.get_string.apply(null, JSON.parse(dataset[`confirmation${field}Str`]));\n }\n return dataset[`confirmation${field}`];\n};\n\n/**\n * Set up the listeners for the confirmation modal widget within the page.\n *\n * @method registerConfirmationListeners\n * @private\n */\nconst registerConfirmationListeners = () => {\n document.addEventListener('click', e => {\n const confirmRequest = e.target.closest('[data-confirmation=\"modal\"]');\n if (confirmRequest) {\n e.preventDefault();\n confirmedPromise(\n getConfirmationString(confirmRequest.dataset, 'Title'),\n getConfirmationString(confirmRequest.dataset, 'Question'),\n getConfirmationString(confirmRequest.dataset, 'YesButton'),\n )\n .then(() => {\n if (confirmRequest.dataset[`confirmationToast`] === 'true') {\n const stringForToast = getConfirmationString(confirmRequest.dataset, 'ToastConfirmation');\n if (typeof stringForToast === \"string\") {\n addToast(stringForToast);\n } else {\n stringForToast.then(str => addToast(str)).catch(e => exception(e));\n }\n }\n window.location.href = confirmRequest.dataset.confirmationDestination;\n return;\n }).catch(() => {}); // Catch an empty function as the user cancelled rather than an actual error.\n }\n });\n};\n\nif (!registered) {\n registerConfirmationListeners();\n registered = true;\n}\n"],"file":"confirm.min.js"}