1/** 2 * File navigation.js. 3 * 4 * Handles toggling the navigation menu for small screens and enables TAB key 5 * navigation support for dropdown menus. 6 */ 7( function() { 8 const siteNavigation = document.getElementById( 'site-navigation' ); 9 10 // Return early if the navigation doesn't exist. 11 if ( ! siteNavigation ) { 12 return; 13 } 14 15 const button = siteNavigation.getElementsByTagName( 'button' )[ 0 ]; 16 17 // Return early if the button doesn't exist. 18 if ( 'undefined' === typeof button ) { 19 return; 20 } 21 22 const menu = siteNavigation.getElementsByTagName( 'ul' )[ 0 ]; 23 24 // Hide menu toggle button if menu is empty and return early. 25 if ( 'undefined' === typeof menu ) { 26 button.style.display = 'none'; 27 return; 28 } 29 30 if ( ! menu.classList.contains( 'nav-menu' ) ) { 31 menu.classList.add( 'nav-menu' ); 32 } 33 34 // Toggle the .toggled class and the aria-expanded value each time the button is clicked. 35 button.addEventListener( 'click', function() { 36 siteNavigation.classList.toggle( 'toggled' ); 37 document.body.classList.toggle( 'no-scroll' ); 38 39 if ( button.getAttribute( 'aria-expanded' ) === 'true' ) { 40 button.setAttribute( 'aria-expanded', 'false' ); 41 } else { 42 button.setAttribute( 'aria-expanded', 'true' ); 43 } 44 } ); 45 46 // Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation. 47 document.addEventListener( 'click', function( event ) { 48 const isClickInside = siteNavigation.contains( event.target ); 49 50 if ( ! isClickInside ) { 51 siteNavigation.classList.remove( 'toggled' ); 52 button.setAttribute( 'aria-expanded', 'false' ); 53 } 54 } ); 55 56 // Get all the link elements within the menu. 57 const links = menu.getElementsByTagName( 'a' ); 58 59 // Get all the link elements with children within the menu. 60 const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 61 62 // Toggle focus each time a menu link is focused or blurred. 63 for ( const link of links ) { 64 link.addEventListener( 'focus', toggleFocus, true ); 65 link.addEventListener( 'blur', toggleFocus, true ); 66 } 67 68 // Toggle focus each time a menu link with children receive a touch event. 69 for ( const link of linksWithChildren ) { 70 link.addEventListener( 'touchstart', toggleFocus, false ); 71 } 72 73 /** 74 * Sets or removes .focus class on an element. 75 */ 76 function toggleFocus() { 77 if ( event.type === 'focus' || event.type === 'blur' ) { 78 let self = this; 79 // Move up through the ancestors of the current link until we hit .nav-menu. 80 while ( ! self.classList.contains( 'nav-menu' ) ) { 81 // On li elements toggle the class .focus. 82 if ( 'li' === self.tagName.toLowerCase() ) { 83 self.classList.toggle( 'focus' ); 84 } 85 self = self.parentNode; 86 } 87 } 88 89 if ( event.type === 'touchstart' ) { 90 const menuItem = this.parentNode; 91 //event.preventDefault(); 92 for ( const link of menuItem.parentNode.children ) { 93 if ( menuItem !== link ) { 94 link.classList.remove( 'focus' ); 95 } 96 } 97 menuItem.classList.toggle( 'focus' ); 98 } 99 } 100}() );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.