xref: /plugin/combo/resources/snippet/js/onhover.js (revision 9337a630db122fdba0294f47d72bdf5433c2bf10)
1window.addEventListener("load", function () {
2
3    let inOutOnHover = function (event) {
4        const element = event.currentTarget;
5        let dataHoverClass = element.getAttribute("data-hover-class");
6        if (element.classList.contains(dataHoverClass)){
7            element.classList.remove(dataHoverClass);
8        } else {
9            element.classList.add(dataHoverClass);
10        }
11    };
12
13    /**
14     * Bind hover class to a toggle element
15     * @param event
16     * https://api.jquery.com/hover/
17     */
18    jQuery("[data-hover-class]").hover(inOutOnHover,inOutOnHover);
19
20
21    /**
22     * Add binding when node are added
23     * @type {MutationObserver}
24     */
25    const observer = new MutationObserver(function (mutationList) {
26        // noinspection JSUnfilteredForInLoop
27        mutationList.forEach((mutation) => {
28
29            for (let index in mutation.addedNodes) {
30                let node = mutation.addedNodes[index];
31                if (node.nodeType === Node.ELEMENT_NODE) {
32                    if (node.dataset.hasOwnProperty("hoverClass")) {
33                        jQuery(node).hover(inOutOnHover,inOutOnHover);
34                    }
35                }
36
37            }
38
39        });
40    });
41    observer.observe(document, {childList: true, subtree: true});
42
43});
44
45
46
47
48