From ce0ac9ce7597cfa13f205f5bb86c2f32d703cb11 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 9 Dec 2021 15:03:43 +0800 Subject: [PATCH] MDL-73314 core: Accept a Promise which resolves to string for toasts --- lib/amd/build/toast.min.js.map | 2 +- lib/amd/src/toast.js | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/amd/build/toast.min.js.map b/lib/amd/build/toast.min.js.map index 65c03cce498..b3ef073c823 100644 --- a/lib/amd/build/toast.min.js.map +++ b/lib/amd/build/toast.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/toast.js"],"names":["addToastRegion","parent","pendingPromise","Pending","Templates","renderForPromise","html","js","prependNodeContents","Notification","exception","resolve","add","message","configuration","type","closeButton","autohide","delay","templateName","getTargetNode","targetNode","regions","document","querySelectorAll","length","body"],"mappings":"mMAsBA,OACA,OACA,O,6jCAQO,GAAMA,CAAAA,CAAc,4CAAG,WAAMC,CAAN,+FACpBC,CADoB,CACH,GAAIC,UAAJ,CAAY,gBAAZ,CADG,yBAIGC,WAAUC,gBAAV,CAA2B,0BAA3B,CAAuD,EAAvD,CAJH,iBAIfC,CAJe,GAIfA,IAJe,CAITC,CAJS,GAITA,EAJS,CAKtBH,UAAUI,mBAAV,CAA8BP,CAA9B,CAAsCK,CAAtC,CAA4CC,CAA5C,EALsB,qDAOtBE,UAAaC,SAAb,OAPsB,QAU1BR,CAAc,CAACS,OAAf,GAV0B,uDAAH,uDAApB,C,mBAyCA,GAAMC,CAAAA,CAAG,4CAAG,WAAMC,CAAN,CAAeC,CAAf,mGACTZ,CADS,CACQ,GAAIC,UAAJ,CAAY,gBAAZ,CADR,CAEfW,CAAa,IACTC,IAAI,CAAE,MADG,CAETC,WAAW,GAFF,CAGTC,QAAQ,GAHC,CAITC,KAAK,CAAE,GAJE,EAKNJ,CALM,CAAb,CAQMK,CAVS,oDAYcC,CAAAA,CAAa,EAZ3B,QAYLC,CAZK,aAacjB,SAbd,MAayCe,CAbzC,wBAcQN,CAAAA,CAdR,2BAcPA,OAdO,YAeJC,CAfI,gDAawBT,gBAbxB,uCAaJC,CAbI,GAaJA,IAbI,CAaEC,CAbF,GAaEA,EAbF,CAiBXH,UAAUI,mBAAV,CAA8Ba,CAA9B,CAA0Cf,CAA1C,CAAgDC,CAAhD,EAjBW,qDAmBXE,UAAaC,SAAb,OAnBW,QAsBfR,CAAc,CAACS,OAAf,GAtBe,uDAAH,uDAAT,C,QAyBP,GAAMS,CAAAA,CAAa,4CAAG,oGACZE,CADY,CACFC,QAAQ,CAACC,gBAAT,CAA0B,gBAA1B,CADE,KAGdF,CAAO,CAACG,MAHM,0CAIPH,CAAO,CAACA,CAAO,CAACG,MAAR,CAAiB,CAAlB,CAJA,wBAOZzB,CAAAA,CAAc,CAACuB,QAAQ,CAACG,IAAV,CAAgB,cAAhB,CAPF,iCAQXN,CAAa,EARF,0CAAH,uD","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 * 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 \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\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} 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 {get_string as getString} from 'core/str';\n *\n * getString('example', 'mod_myexample')\n * .then(str => {\n * addToast(str, {\n * type: 'warning',\n * autohide: false,\n * closeButton: true,\n * });\n * return;\n * })\n * .catch();\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 targetNode = await getTargetNode();\n const {html, js} = await Templates.renderForPromise(templateName, {\n message: await message,\n ...configuration\n });\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('.toast-wrapper');\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"],"file":"toast.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/toast.js"],"names":["addToastRegion","parent","pendingPromise","Pending","Templates","renderForPromise","html","js","prependNodeContents","Notification","exception","resolve","add","message","configuration","type","closeButton","autohide","delay","templateName","getTargetNode","targetNode","regions","document","querySelectorAll","length","body"],"mappings":"mMAsBA,OACA,OACA,O,6jCAQO,GAAMA,CAAAA,CAAc,4CAAG,WAAMC,CAAN,+FACpBC,CADoB,CACH,GAAIC,UAAJ,CAAY,gBAAZ,CADG,yBAIGC,WAAUC,gBAAV,CAA2B,0BAA3B,CAAuD,EAAvD,CAJH,iBAIfC,CAJe,GAIfA,IAJe,CAITC,CAJS,GAITA,EAJS,CAKtBH,UAAUI,mBAAV,CAA8BP,CAA9B,CAAsCK,CAAtC,CAA4CC,CAA5C,EALsB,qDAOtBE,UAAaC,SAAb,OAPsB,QAU1BR,CAAc,CAACS,OAAf,GAV0B,uDAAH,uDAApB,C,mBA0CA,GAAMC,CAAAA,CAAG,4CAAG,WAAMC,CAAN,CAAeC,CAAf,mGACTZ,CADS,CACQ,GAAIC,UAAJ,CAAY,gBAAZ,CADR,CAEfW,CAAa,IACTC,IAAI,CAAE,MADG,CAETC,WAAW,GAFF,CAGTC,QAAQ,GAHC,CAITC,KAAK,CAAE,GAJE,EAKNJ,CALM,CAAb,CAQMK,CAVS,oDAYcC,CAAAA,CAAa,EAZ3B,QAYLC,CAZK,aAacjB,SAbd,MAayCe,CAbzC,wBAcQN,CAAAA,CAdR,2BAcPA,OAdO,YAeJC,CAfI,gDAawBT,gBAbxB,uCAaJC,CAbI,GAaJA,IAbI,CAaEC,CAbF,GAaEA,EAbF,CAiBXH,UAAUI,mBAAV,CAA8Ba,CAA9B,CAA0Cf,CAA1C,CAAgDC,CAAhD,EAjBW,qDAmBXE,UAAaC,SAAb,OAnBW,QAsBfR,CAAc,CAACS,OAAf,GAtBe,uDAAH,uDAAT,C,QAyBP,GAAMS,CAAAA,CAAa,4CAAG,oGACZE,CADY,CACFC,QAAQ,CAACC,gBAAT,CAA0B,gBAA1B,CADE,KAGdF,CAAO,CAACG,MAHM,0CAIPH,CAAO,CAACA,CAAO,CAACG,MAAR,CAAiB,CAAlB,CAJA,wBAOZzB,CAAAA,CAAc,CAACuB,QAAQ,CAACG,IAAV,CAAgB,cAAhB,CAPF,iCAQXN,CAAa,EARF,0CAAH,uD","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 * 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 \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\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} 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 {get_string as 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 targetNode = await getTargetNode();\n const {html, js} = await Templates.renderForPromise(templateName, {\n message: await message,\n ...configuration\n });\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('.toast-wrapper');\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"],"file":"toast.min.js"} \ No newline at end of file diff --git a/lib/amd/src/toast.js b/lib/amd/src/toast.js index 3349a7febf5..ecb27b1dd68 100644 --- a/lib/amd/src/toast.js +++ b/lib/amd/src/toast.js @@ -47,7 +47,7 @@ export const addToastRegion = async(parent) => { * Add a new toast or snackbar notification to the page. * * @method - * @param {String} message + * @param {String|Promise} message * @param {Object} configuration * @param {String} [configuration.title] * @param {String} [configuration.subtitle] @@ -60,16 +60,17 @@ export const addToastRegion = async(parent) => { * import {add as addToast} from 'core/toast'; * import {get_string as getString} from 'core/str'; * - * getString('example', 'mod_myexample') - * .then(str => { - * addToast(str, { - * type: 'warning', - * autohide: false, - * closeButton: true, - * }); - * return; - * }) - * .catch(); + * addToast('Example string', { + * type: 'warning', + * autohide: false, + * closeButton: true, + * }); + * + * addToast(getString('example', 'mod_myexample'), { + * type: 'warning', + * autohide: false, + * closeButton: true, + * }); */ export const add = async(message, configuration) => { const pendingPromise = new Pending('addToastRegion');