lib/amd/src/toast.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * A system for displaying small snackbar notifications to users which disappear shortly after they are shown.
  17. *
  18. * @module core/toast
  19. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Templates from 'core/templates';
  23. import Notification from 'core/notification';
  24. import Pending from 'core/pending';
  25. const regionSelector = '.toast-wrapper';
  26. /**
  27. * Add a new region to place toasts in, taking in a parent element.
  28. *
  29. * @method
  30. * @param {HTMLElement} parent
  31. */
  32. export const addToastRegion = async(parent) => {
  33. const pendingPromise = new Pending('addToastRegion');
  34. try {
  35. const {html, js} = await Templates.renderForPromise('core/local/toast/wrapper', {});
  36. Templates.prependNodeContents(parent, html, js);
  37. } catch (e) {
  38. Notification.exception(e);
  39. }
  40. pendingPromise.resolve();
  41. };
  42. /**
  43. * Add a new toast or snackbar notification to the page.
  44. *
  45. * @method
  46. * @param {String|Promise<string>} message
  47. * @param {Object} configuration
  48. * @param {String} [configuration.title]
  49. * @param {String} [configuration.subtitle]
  50. * @param {String} [configuration.type=info] Optional type of the toast notification ('success', 'info', 'warning' or 'danger')
  51. * @param {Boolean} [configuration.autohide=true]
  52. * @param {Boolean} [configuration.closeButton=false]
  53. * @param {Number} [configuration.delay=4000]
  54. *
  55. * @example
  56. * import {add as addToast} from 'core/toast';
  57. * import {getString} from 'core/str';
  58. *
  59. * addToast('Example string', {
  60. * type: 'warning',
  61. * autohide: false,
  62. * closeButton: true,
  63. * });
  64. *
  65. * addToast(getString('example', 'mod_myexample'), {
  66. * type: 'warning',
  67. * autohide: false,
  68. * closeButton: true,
  69. * });
  70. */
  71. export const add = async(message, configuration) => {
  72. const pendingPromise = new Pending('addToastRegion');
  73. configuration = {
  74. type: 'info',
  75. closeButton: false,
  76. autohide: true,
  77. delay: 4000,
  78. ...configuration,
  79. };
  80. const templateName = `core/local/toast/message`;
  81. try {
  82. const {html, js} = await Templates.renderForPromise(templateName, {
  83. message: await message,
  84. ...configuration
  85. });
  86. const targetNode = await getTargetNode();
  87. Templates.prependNodeContents(targetNode, html, js);
  88. } catch (e) {
  89. Notification.exception(e);
  90. }
  91. pendingPromise.resolve();
  92. };
  93. const getTargetNode = async() => {
  94. const regions = document.querySelectorAll(regionSelector);
  95. if (regions.length) {
  96. return regions[regions.length - 1];
  97. }
  98. await addToastRegion(document.body, 'fixed-bottom');
  99. return getTargetNode();
  100. };
  101. /**
  102. * Remove a parent region.
  103. *
  104. * 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.
  105. *
  106. * @param {HTMLElement} parent The region that the toast region is currently a child of.
  107. * @param {HTMLElement} newParent The parent element to move the toast region content to.
  108. */
  109. export const removeToastRegion = async(parent, newParent = document) => {
  110. const pendingPromise = new Pending('core/toast:removeToastRegion');
  111. const getRegionFromParent = (thisParent) => thisParent.querySelector(regionSelector);
  112. const regionToRemove = getRegionFromParent(parent);
  113. if (regionToRemove) {
  114. const targetRegion = getRegionFromParent(newParent);
  115. regionToRemove.children.forEach((node) => {
  116. targetRegion.insertBefore(node, targetRegion.firstChild);
  117. });
  118. regionToRemove.remove();
  119. }
  120. pendingPromise.resolve();
  121. };