HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/resources/js/site/components/toggleHandler.js
/* ==========================================================================
   Accordion handler
   - Handles the accordion component which has the proper classes.
 ========================================================================== */

const ToggleHandler = {

    toggleClass : 'js-toggle',
    toggleSwitchClass : 'js-toggle-switch',
    toggleContentClass : 'js-toggle-content',


    init: function () {

        const toggleList = document.querySelectorAll('.'+ToggleHandler.toggleClass);
        const toggleListCount = toggleList.length;

        if(isset(toggleList) && toggleListCount !== 0){
            for(let i = 0; i < toggleListCount; i++){
                const toggle = toggleList[i];
                ToggleHandler.initToggle(toggle);
            }
        }
    },

    initToggle: function (toggle) {

        /*
        * Set "pointer events: none" on all direct children of the toggle
        * Because we don't want clicks on them to register, only on the parent toggle element
        */
        const toggleList = toggle.querySelectorAll('.'+ToggleHandler.toggleSwitchClass);
        for (var i = 0; i < toggleList.length; i++) {
            var toggleItem = toggleList[i];

            for (var j = 0; j < toggleItem.children.length; j++) {
                var toggleChild = toggleItem.children[j];

                toggleChild.style.pointerEvents = "none";
            }

        }
        toggle.addEventListener('click', ToggleHandler.activateToggle, false);
    },

    activateToggle: function (event) {
        const item = event.target.parentNode;
        const itemList = item.parentNode.children;

        // Bail if we didn't click on the toggle element
        if (!event.target.classList.contains(ToggleHandler.toggleSwitchClass)) return;

        // Check if content element exists
        if (!item.querySelector('.'+ToggleHandler.toggleContentClass)) return;

        // Prevent default link behavior
        event.preventDefault();

        // Toggle the active class
        item.classList.toggle('is-active');
    }
};

ToggleHandler.init();