templates/js/library/DomDispatcher/DomDispatcher.js.twig line 1

Open in your IDE?
  1. class DomDispatcher {
  2.     static EVENTS = {
  3.         ON_DOM_NODE_ADDED: 'onDomNodeAdded'
  4.     };
  5.     static eventSubscribeOptions = {
  6.         [DomDispatcher.EVENTS.ON_DOM_NODE_ADDED]: {
  7.             groupNames: [],
  8.         }
  9.     }
  10.     options = {
  11.         someOption1: false,
  12.         someStringOption2: null,
  13.         someObjectOption3: null
  14.     };
  15.     constructor(options = null) {
  16.         this.options = { ...this.options, ...options };
  17.         this.eventDispatcher = new EventDispatcher();
  18.         this.eventDispatcher.subscribeOptions = DomDispatcher.eventSubscribeOptions;
  19.         this.eventDispatcher.onBeforeEmitCallback = (eventName, event, options) => {
  20.             for (let g of options.groupNames) {
  21.                 if (event.detail.groupNames.includes(g)) {
  22.                     return true;
  23.                 }
  24.             }
  25.             return false;
  26.         };
  27.     }
  28.     init() {
  29.         const me = this;
  30.         $(() => {
  31.             let handledElements = [];
  32.             let domListener = new DomListener(document.body, { childList: true, subtree: true },
  33.                 (mutationsList) => {
  34.                     for (let mutation of mutationsList) {
  35.                         if (mutation.addedNodes) {
  36.                             mutation.addedNodes.forEach((node) => {
  37.                                 if (node.nodeType === Node.ELEMENT_NODE) {
  38.                                     let $listenElements = $(node).find("[data-dom-listener-groups]");
  39.                                     $listenElements.each(function () {
  40.                                         let element = $(this)[0];
  41.                                         if (handledElements.includes(element)) {
  42.                                             return;
  43.                                         }
  44.                                         handledElements.push(element);
  45.                                         let listenGroups = $(this).data('dom-listener-groups');
  46.                                         if (listenGroups) {
  47.                                             me.eventDispatcher.emit(
  48.                                                 DomDispatcher.EVENTS.ON_DOM_NODE_ADDED,
  49.                                                 {
  50.                                                     element: element,
  51.                                                     groupNames: listenGroups
  52.                                                 }
  53.                                             );
  54.                                         }
  55.                                     });
  56.                                 }
  57.                             });
  58.                         }
  59.                     }
  60.                 }
  61.             );
  62.         });
  63.     }
  64. }