Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"toast.min.js","sources":["../src/toast.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 * A system for displaying small snackbar notifications to users which disappear shortly after they are shown.\n *\n * @module     core/toast\n * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport Templates from 'core/templates';\nimport Notification from 'core/notification';\nimport Pending from 'core/pending';\n\nconst regionSelector = '.toast-wrapper';\n\n/**\n * Add a new region to place toasts in, taking in a parent element.\n *\n * @method\n * @param {HTMLElement} parent\n */\nexport const addToastRegion = async(parent) => {\n    const pendingPromise = new Pending('addToastRegion');\n\n    try {\n        const {html, js} = await Templates.renderForPromise('core/local/toast/wrapper', {});\n        Templates.prependNodeContents(parent, html, js);\n    } catch (e) {\n        Notification.exception(e);\n    }\n\n    pendingPromise.resolve();\n};\n\n/**\n * Add a new toast or snackbar notification to the page.\n *\n * @method\n * @param {String|Promise<string>} message\n * @param {Object} configuration\n * @param {String} [configuration.title]\n * @param {String} [configuration.subtitle]\n * @param {String} [configuration.type=info] Optional type of the toast notification ('success', 'info', 'warning' or 'danger')\n * @param {Boolean} [configuration.autohide=true]\n * @param {Boolean} [configuration.closeButton=false]\n * @param {Number} [configuration.delay=4000]\n *\n * @example\n * import {add as addToast} from 'core/toast';\n * import {getString} from 'core/str';\n *\n * addToast('Example string', {\n *     type: 'warning',\n *     autohide: false,\n *     closeButton: true,\n * });\n *\n * addToast(getString('example', 'mod_myexample'), {\n *     type: 'warning',\n *     autohide: false,\n *     closeButton: true,\n * });\n */\nexport const add = async(message, configuration) => {\n    const pendingPromise = new Pending('addToastRegion');\n    configuration = {\n        type: 'info',\n        closeButton: false,\n        autohide: true,\n        delay: 4000,\n        ...configuration,\n    };\n\n    const templateName = `core/local/toast/message`;\n    try {\n        const {html, js} = await Templates.renderForPromise(templateName, {\n            message: await message,\n            ...configuration\n        });\n        const targetNode = await getTargetNode();\n        Templates.prependNodeContents(targetNode, html, js);\n    } catch (e) {\n        Notification.exception(e);\n    }\n\n    pendingPromise.resolve();\n};\n\nconst getTargetNode = async() => {\n    const regions = document.querySelectorAll(regionSelector);\n\n    if (regions.length) {\n        return regions[regions.length - 1];\n    }\n\n    await addToastRegion(document.body, 'fixed-bottom');\n    return getTargetNode();\n};\n\n/**\n * Remove a parent region.\n *\n * This is useful in cases such as where a dialog is to be removed and the toast region should be moved back to the body.\n *\n * @param {HTMLElement} parent The region that the toast region is currently a child of.\n * @param {HTMLElement} newParent The parent element to move the toast region content to.\n */\nexport const removeToastRegion = async(parent, newParent = document) => {\n    const pendingPromise = new Pending('core/toast:removeToastRegion');\n    const getRegionFromParent = (thisParent) => thisParent.querySelector(regionSelector);\n\n    const regionToRemove = getRegionFromParent(parent);\n    if (regionToRemove) {\n        const targetRegion = getRegionFromParent(newParent);\n\n        regionToRemove.children.forEach((node) => {\n            targetRegion.insertBefore(node, targetRegion.firstChild);\n        });\n\n        regionToRemove.remove();\n    }\n    pendingPromise.resolve();\n};\n"],"names":["addToastRegion","async","pendingPromise","Pending","html","js","Templates","renderForPromise","prependNodeContents","parent","e","exception","resolve","message","configuration","type","closeButton","autohide","delay","targetNode","getTargetNode","regions","document","querySelectorAll","length","body","newParent","getRegionFromParent","thisParent","querySelector","regionToRemove","targetRegion","children","forEach","node","insertBefore","firstChild","remove"],"mappings":";;;;;;;sRAkCaA,eAAiBC,MAAAA,eACpBC,eAAiB,IAAIC,iBAAQ,4BAGzBC,KAACA,KAADC,GAAOA,UAAYC,mBAAUC,iBAAiB,2BAA4B,uBACtEC,oBAAoBC,OAAQL,KAAMC,IAC9C,MAAOK,yBACQC,UAAUD,GAG3BR,eAAeU,+DAgCAX,MAAMY,QAASC,uBACxBZ,eAAiB,IAAIC,iBAAQ,kBACnCW,cAAgB,CACZC,KAAM,OACNC,aAAa,EACbC,UAAU,EACVC,MAAO,OACJJ,yBAKGV,KAACA,KAADC,GAAOA,UAAYC,mBAAUC,4CAA+B,CAC9DM,cAAeA,WACZC,gBAEDK,iBAAmBC,mCACfZ,oBAAoBW,WAAYf,KAAMC,IAClD,MAAOK,yBACQC,UAAUD,GAG3BR,eAAeU,iBAGbQ,cAAgBnB,gBACZoB,QAAUC,SAASC,iBA5EN,yBA8EfF,QAAQG,OACDH,QAAQA,QAAQG,OAAS,UAG9BxB,eAAesB,SAASG,MACvBL,6CAWsBnB,eAAMQ,YAAQiB,iEAAYJ,eACjDpB,eAAiB,IAAIC,iBAAQ,gCAC7BwB,oBAAuBC,YAAeA,WAAWC,cAhGpC,kBAkGbC,eAAiBH,oBAAoBlB,WACvCqB,eAAgB,OACVC,aAAeJ,oBAAoBD,WAEzCI,eAAeE,SAASC,SAASC,OAC7BH,aAAaI,aAAaD,KAAMH,aAAaK,eAGjDN,eAAeO,SAEnBnC,eAAeU"}