lib/amd/src/event_dispatcher.js

  1. // This file is part of Moodle - http://moodle.org/ //
  2. // Moodle is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. //
  7. // Moodle is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU General Public License
  13. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  14. /**
  15. * An Event dispatcher used to dispatch Native JS CustomEvent objects with custom default properties.
  16. *
  17. * @module core/event_dispatcher
  18. * @copyright 2021 Andrew Nicols <andrew@nicols.co.uk>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. * @since 4.0
  21. */
  22. /**
  23. * Dispatch an event as a CustomEvent on the specified container.
  24. * By default events are bubbled, and cancelable.
  25. *
  26. * The eventName should typically by sourced using a constant. See the supplied examples.
  27. *
  28. * Note: This function uses native events. Any additional details are passed to the function in event.detail.
  29. *
  30. * This function mimics the behaviour of EventTarget.dispatchEvent but bubbles by default.
  31. *
  32. * @method dispatchEvent
  33. * @param {String} eventName The name of the event
  34. * @param {Object} detail Any additional details to pass into the eveent
  35. * @param {HTMLElement} container The point at which to dispatch the event
  36. * @param {Object} options
  37. * @param {Boolean} options.bubbles Whether to bubble up the DOM
  38. * @param {Boolean} options.cancelable Whether preventDefault() can be called
  39. * @param {Boolean} options.composed Whether the event can bubble across the ShadowDOM bounadry
  40. * @returns {CustomEvent}
  41. *
  42. * @example <caption>Using a native CustomEvent to indicate that some example data was displayed.</caption>
  43. * // mod/example/amd/src/events.js
  44. *
  45. * import {dispatchEvent} from 'core/event_dispatcher';
  46. *
  47. * export const eventTypes = {
  48. * exampleDataDisplayed: 'mod_example/exampleDataDisplayed',
  49. * };
  50. *
  51. * export const notifyExampleDisplayed = someArgument => dispatchEvent(eventTypes.exampleDataDisplayed, {
  52. * someArgument,
  53. * }, document, {
  54. * cancelable: false,
  55. * });
  56. */
  57. export const dispatchEvent = (
  58. eventName,
  59. detail = {},
  60. container = document,
  61. {
  62. bubbles = true,
  63. cancelable = false,
  64. composed = false,
  65. } = {}
  66. ) => {
  67. const customEvent = new CustomEvent(
  68. eventName,
  69. {
  70. bubbles,
  71. cancelable,
  72. composed,
  73. detail,
  74. }
  75. );
  76. container.dispatchEvent(customEvent);
  77. return customEvent;
  78. };