class DomDispatcher {
static EVENTS = {
ON_DOM_NODE_ADDED: 'onDomNodeAdded'
};
static eventSubscribeOptions = {
[DomDispatcher.EVENTS.ON_DOM_NODE_ADDED]: {
groupNames: [],
}
}
options = {
someOption1: false,
someStringOption2: null,
someObjectOption3: null
};
constructor(options = null) {
this.options = { ...this.options, ...options };
this.eventDispatcher = new EventDispatcher();
this.eventDispatcher.subscribeOptions = DomDispatcher.eventSubscribeOptions;
this.eventDispatcher.onBeforeEmitCallback = (eventName, event, options) => {
for (let g of options.groupNames) {
if (event.detail.groupNames.includes(g)) {
return true;
}
}
return false;
};
}
init() {
const me = this;
$(() => {
let handledElements = [];
let domListener = new DomListener(document.body, { childList: true, subtree: true },
(mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
let $listenElements = $(node).find("[data-dom-listener-groups]");
$listenElements.each(function () {
let element = $(this)[0];
if (handledElements.includes(element)) {
return;
}
handledElements.push(element);
let listenGroups = $(this).data('dom-listener-groups');
if (listenGroups) {
me.eventDispatcher.emit(
DomDispatcher.EVENTS.ON_DOM_NODE_ADDED,
{
element: element,
groupNames: listenGroups
}
);
}
});
}
});
}
}
}
);
});
}
}