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( 36 'click', 37 function() { 38 siteNavigation.classList.toggle( 'toggled' ); 39 40 if ( button.getAttribute( 'aria-expanded' ) === 'true' ) { 41 button.setAttribute( 'aria-expanded', 'false' ); 42 } else { 43 button.setAttribute( 'aria-expanded', 'true' ); 44 } 45 } 46 ); 47 48 // Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation. 49 document.addEventListener( 50 'click', 51 function( event ) { 52 const isClickInside = siteNavigation.contains( event.target ); 53 54 if ( ! isClickInside ) { 55 siteNavigation.classList.remove( 'toggled' ); 56 button.setAttribute( 'aria-expanded', 'false' ); 57 } 58 } 59 ); 60 61 // Get all the link elements within the menu. 62 const links = menu.getElementsByTagName( 'a' ); 63 64 // Get all the link elements with children within the menu. 65 const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 66 67 // Toggle focus each time a menu link is focused or blurred. 68 for ( const link of links ) { 69 link.addEventListener( 'focus', toggleFocus, true ); 70 link.addEventListener( 'blur', toggleFocus, true ); 71 } 72 73 // Toggle focus each time a menu link with children receive a touch event. 74 for ( const link of linksWithChildren ) { 75 link.addEventListener( 'touchstart', toggleFocus, false ); 76 } 77 78 /** 79 * Sets or removes .focus class on an element. 80 */ 81 function toggleFocus() { 82 if ( event.type === 'focus' || event.type === 'blur' ) { 83 let self = this; 84 // Move up through the ancestors of the current link until we hit .nav-menu. 85 while ( ! self.classList.contains( 'nav-menu' ) ) { 86 // On li elements toggle the class .focus. 87 if ( 'li' === self.tagName.toLowerCase() ) { 88 self.classList.toggle( 'focus' ); 89 } 90 self = self.parentNode; 91 } 92 } 93 94 if ( event.type === 'touchstart' ) { 95 const menuItem = this.parentNode; 96 event.preventDefault(); 97 for ( const link of menuItem.parentNode.children ) { 98 if ( menuItem !== link ) { 99 link.classList.remove( 'focus' ); 100 } 101 } 102 menuItem.classList.toggle( 'focus' ); 103 } 104 } 105}() );
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.