notification.js

  1. import { queryOne } from '@ecl/dom-utils';
  2. import EventManager from '@ecl/event-manager';
  3. /**
  4. * @param {HTMLElement} element DOM element for component instantiation and scope
  5. * @param {Object} options
  6. * @param {String} options.closeSelector Selector for closing the notification
  7. * @param {Boolean} options.attachClickListener Whether or not to bind click events
  8. */
  9. export class Notification {
  10. /**
  11. * @static
  12. * Shorthand for instance creation and initialisation.
  13. *
  14. * @param {HTMLElement} root DOM element for component instantiation and scope
  15. *
  16. * @return {Notification} An instance of Notification.
  17. */
  18. static autoInit(root, { NOTIFICATION: defaultOptions = {} } = {}) {
  19. const notification = new Notification(root, defaultOptions);
  20. notification.init();
  21. root.ECLNotification = notification;
  22. return notification;
  23. }
  24. /**
  25. * An array of supported events for this component.
  26. *
  27. * @type {Array<string>}
  28. * @event Notification#onClose
  29. * @memberof Notification
  30. */
  31. supportedEvents = ['onClose'];
  32. constructor(
  33. element,
  34. {
  35. closeSelector = '[data-ecl-notification-close]',
  36. attachClickListener = true,
  37. } = {},
  38. ) {
  39. // Check element
  40. if (!element || element.nodeType !== Node.ELEMENT_NODE) {
  41. throw new TypeError(
  42. 'DOM element should be given to initialize this widget.',
  43. );
  44. }
  45. this.element = element;
  46. this.eventManager = new EventManager();
  47. // Options
  48. this.closeSelector = closeSelector;
  49. this.attachClickListener = attachClickListener;
  50. // Private variables
  51. this.close = null;
  52. // Bind `this` for use in callbacks
  53. this.handleClickOnClose = this.handleClickOnClose.bind(this);
  54. }
  55. /**
  56. * Initialise component.
  57. */
  58. init() {
  59. if (!ECL) {
  60. throw new TypeError('Called init but ECL is not present');
  61. }
  62. ECL.components = ECL.components || new Map();
  63. this.close = queryOne(this.closeSelector, this.element);
  64. // Bind click event on close
  65. if (this.attachClickListener && this.close) {
  66. this.close.addEventListener('click', this.handleClickOnClose);
  67. }
  68. // Set ecl initialized attribute
  69. this.element.setAttribute('data-ecl-auto-initialized', 'true');
  70. ECL.components.set(this.element, this);
  71. }
  72. /**
  73. * Register a callback function for a specific event.
  74. *
  75. * @param {string} eventName - The name of the event to listen for.
  76. * @param {Function} callback - The callback function to be invoked when the event occurs.
  77. * @returns {void}
  78. * @memberof Notification
  79. * @instance
  80. *
  81. * @example
  82. * // Registering a callback for the 'close' event
  83. * notification.on('onClose', (event) => {
  84. * console.log('Close event occurred!', event);
  85. * });
  86. */
  87. on(eventName, callback) {
  88. this.eventManager.on(eventName, callback);
  89. }
  90. /**
  91. * Trigger a component event.
  92. *
  93. * @param {string} eventName - The name of the event to trigger.
  94. * @param {any} eventData - Data associated with the event.
  95. *
  96. * @memberof Notification
  97. */
  98. trigger(eventName, eventData) {
  99. this.eventManager.trigger(eventName, eventData);
  100. }
  101. /**
  102. * Destroy component.
  103. */
  104. destroy() {
  105. if (this.attachClickListener && this.close) {
  106. this.close.removeEventListener('click', this.handleClickOnClose);
  107. }
  108. if (this.element) {
  109. this.element.removeAttribute('data-ecl-auto-initialized');
  110. ECL.components.delete(this.element);
  111. }
  112. }
  113. /**
  114. * Remove the notification component.
  115. *
  116. * @param {Event} e
  117. *
  118. * @fires Notification#onClose
  119. */
  120. handleClickOnClose(e) {
  121. // IE way to remove a node...
  122. if (this.element.parentNode) {
  123. this.element.parentNode.removeChild(this.element);
  124. }
  125. this.trigger('onClose', e);
  126. return this;
  127. }
  128. }
  129. export default Notification;