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/slenders/slenders.nl/resources/js/site/components/navigationHandler.js
/* ==========================================================================
   Navigation handler
   - Handles the navigation component which has the proper classes.
 ========================================================================== */

const NavigationHandler = {

    navigationNode: null,
    scrolledLog: 0,
    isOpen: false,

    init: function () {

        const navTriggers = document.querySelectorAll('.js-nav-trigger');
        const closeButton = document.querySelector('.js-close-overlay-nav');
        NavigationHandler.navigationNode = document.querySelector('.js-overlay-nav');

        if(isset(NavigationHandler.navigationNode)){

            document.addEventListener('keydown', function(event) {
                if(event.code === 'Escape') NavigationHandler.closeNavigation();
            });

            for(let i = 0; i < navTriggers.length; i++) {
                NavigationHandler.initNavigation(navTriggers[i])
            }

            closeButton.addEventListener('click', function () {
                NavigationHandler.closeNavigation();
            });

        }
    },

    initNavigation: function (navTrigger) {
        navTrigger.addEventListener('click', NavigationHandler.toggleNavigation, false);
    },

    toggleNavigation: function (event) {

        // Prevent default link behavior
        event.preventDefault();
        NavigationHandler.scrolled = window.pageYOffset;

        // If the nav is already active, collapse it and quit
        if (NavigationHandler.isOpen) NavigationHandler.closeNavigation();
        else NavigationHandler.openNavigation();

    },

    openNavigation: function() {
        NavigationHandler.isOpen = true;
        NavigationHandler.scrolled = window.pageYOffset;

        document.body.classList.add('nav-is-active');

        setTimeout(function () {
            document.body.classList.add('scroll-lock');
        }, 400);
    },

    closeNavigation: function () {
        document.body.classList.remove('scroll-lock');
        window.pageYOffset = NavigationHandler.scrolled;

        document.body.classList.remove('nav-is-active');
        NavigationHandler.isOpen = false;
    }
};

NavigationHandler.init();