/** * File scripts.js. * * Handles toggling the navigation menu for small screens and enables TAB key * navigation support for dropdown menus. */ jQuery(document).ready(function($){ const siteNavigation = document.getElementById( 'site-navigation' ); const siteBody = document.body; // Return early if the navigation don't exist. if ( ! siteNavigation ) { return; } const button = siteNavigation.getElementsByTagName( 'button' )[ 0 ]; // Return early if the button don't exist. if ( 'undefined' === typeof button ) { return; } const menu = siteNavigation.getElementsByTagName( 'ul' )[ 0 ]; // Hide menu toggle button if menu is empty and return early. if ( 'undefined' === typeof menu ) { button.style.display = 'none'; return; } if ( ! menu.classList.contains( 'nav-menu' ) ) { menu.classList.add( 'nav-menu' ); } // Toggle the .toggled class and the aria-expanded value each time the button is clicked. button.addEventListener( 'click', function() { siteNavigation.classList.toggle( 'toggled' ); siteBody.classList.toggle( 'overflow-hidden' ); if ( button.getAttribute( 'aria-expanded' ) === 'true' ) { button.setAttribute( 'aria-expanded', 'false' ); } else { button.setAttribute( 'aria-expanded', 'true' ); } } ); // tab only inside mobile menu var mainNavigation = $( ".main-navigation" ) .attr( "tabindex", "-1" ) // Inpsect any keydown events that come from within the capture container. .keydown( function handleKeydown( event ) { if ( event.key.toLowerCase() !== "tab" ) { return; } var tabbable = $() // All form elements can receive focus. .add( mainNavigation.find( "button, input, select, textarea" ) ) // Any element that has an HREF can receive focus. .add( mainNavigation.find( "[href]" ) ) // Any element that has a non-(-1) tabindex can receive focus. .add( mainNavigation.find( "[tabindex]:not([tabindex='-1'])" ) ) ; var target = $( event.target ); // Reverse tabbing (Key: Shift+Tab). if ( event.shiftKey ) { if ( target.is( mainNavigation ) || target.is( tabbable.first() ) ) { // Force focus to last element in container. event.preventDefault(); tabbable.last().focus(); } // Forward tabbing (Key: Tab). } else { if ( target.is( tabbable.last() ) ) { // Force focus to first element in container. event.preventDefault(); tabbable.first().focus(); } } } ) ; // Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation. document.addEventListener( 'click', function( event ) { const isClickInside = siteNavigation.contains( event.target ); if ( ! isClickInside ) { siteNavigation.classList.remove( 'toggled' ); siteBody.classList.remove( 'overflow-hidden' ); button.setAttribute( 'aria-expanded', 'false' ); } } ); // Get all the link elements within the menu. const links = menu.getElementsByTagName( 'a' ); // Get all the link elements with children within the menu. const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); // Toggle focus each time a menu link is focused or blurred. for ( const link of links ) { link.addEventListener( 'focus', toggleFocus, true ); link.addEventListener( 'blur', toggleFocus, true ); } // Toggle focus each time a menu link with children receive a touch event. for ( const link of linksWithChildren ) { link.addEventListener( 'touchstart', toggleFocus, false ); } /** * Sets or removes .focus class on an element. */ function toggleFocus() { if ( event.type === 'focus' || event.type === 'blur' ) { let self = this; const parentSelf = self.parentNode; const mainParent = parentSelf.parentNode; // Move up through the ancestors of the current link until we hit .nav-menu. while ( ! self.classList.contains( 'nav-menu' ) ) { // On li elements toggle the class .focus. if ( 'li' === self.tagName.toLowerCase() ) { self.classList.toggle( 'focus' ); } self = self.parentNode; } mainParent.classList.toggle( 'open' ); } if ( event.type === 'touchstart' ) { const menuItem = this.parentNode; event.preventDefault(); for ( const link of menuItem.parentNode.children ) { if ( menuItem !== link ) { link.classList.remove( 'focus' ); } } menuItem.classList.toggle( 'focus' ); } } // Get all the + of parent menu item var linksWithPlus = document.querySelectorAll('.page_item_has_children'); [...linksWithPlus].forEach((parent, i) => { var el = document.createElement('span'); el.innerHTML = "+"; var menuLink = parent.children[0]; var href = menuLink.href; menuLink.after(el); el.addEventListener( 'click', function() { var menuChildren = parent.children[2]; menuChildren.classList.toggle('open'); }); menuLink.addEventListener( 'click', function() { window.location = href; }); }); }); (function() { var elements; var windowHeight; function init() { elements = document.querySelectorAll('.hidden'); windowHeight = window.innerHeight; } function checkPosition() { for (var i = 0; i < elements.length; i++) { var element = elements[i]; var positionFromTop = elements[i].getBoundingClientRect().top; if (positionFromTop - windowHeight <= 0) { element.classList.add('fadeInBottom'); element.classList.remove('hidden'); } } } window.addEventListener('scroll', checkPosition); window.addEventListener('resize', init); init(); checkPosition(); })();