{"version":3,"file":"utility.min.js","sources":["../src/utility.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 handling for HTML attributes. This module gets autoloaded on page load.\n *\n * With the appropriate HTML attributes, various functionalities defined in this module can be used such as a displaying\n * an alert or a confirmation modal, etc.\n *\n * @module core/utility\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-modal' => 'confirmation', // Needed so this module will pick it up in the click handler.\n * 'data-modal-title-str' => json_encode(['deletecheck_modal', 'block']),\n * 'data-modal-content-str' => json_encode(['deleteblockcheck', 'block', $blocktitle]),\n * 'data-modal-yes-button-str' => json_encode(['delete', 'core']),\n * 'data-modal-toast' => 'true', // Can be set to inform the user that their action was a success.\n * 'data-modal-toast-confirmation-str' => json_encode(['deleteblockinprogress', 'block', $blocktitle]),\n * 'data-modal-destination' => $deleteconfirmationurl->out(false), // Where do you want to direct the user?\n * ]\n * );\n */\n\nimport * as Str from 'core/str';\nimport Pending from 'core/pending';\nimport {add as addToast} from 'core/toast';\nimport {saveCancelPromise, deleteCancelPromise, exception} from 'core/notification';\n\n// We want to ensure that we only initialize the listeners only once.\nlet registered = false;\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} type The type of string to fetch\n * @param {String} field The dataset field name to fetch the contents of\n * @param {Array|null} [defaultValue=null] The default params to pass to get_string if no value is found in a dataset\n * @return {Promise}\n *\n */\nconst getModalString = (dataset, type, field, defaultValue = null) => {\n if (dataset[`${type}${field}Str`]) {\n return Str.get_string.apply(null, JSON.parse(dataset[`${type}${field}Str`]));\n }\n if (dataset[`${type}${field}`]) {\n return Promise.resolve(dataset[`${type}${field}`]);\n }\n\n if (defaultValue) {\n return Str.get_string.apply(null, defaultValue);\n }\n\n return null;\n};\n\n/**\n * Display a save/cancel confirmation.\n *\n * @private\n * @param {HTMLElement} source The title of the confirmation\n * @param {String} type The content of the confirmation\n * @returns {Promise}\n */\nconst displayConfirmation = (source, type) => {\n let confirmationPromise = null;\n if (`${type}Type` in source.dataset && source.dataset[`${type}Type`] === 'delete') {\n confirmationPromise = deleteCancelPromise(\n getModalString(source.dataset, type, 'Title', ['confir