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 don't exist. 11 if ( ! siteNavigation ) { 12 return; 13 } 14 15 const menu = siteNavigation.getElementsByTagName( 'ul' )[ 0 ]; 16 17 if ( ! menu.classList.contains( 'nav-menu' ) ) { 18 menu.classList.add( 'nav-menu' ); 19 } 20 21 // Get all the link elements within the menu. 22 const links = menu.getElementsByTagName( 'a' ); 23 24 // Get all the link elements with children within the menu. 25 const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 26 27 // Toggle focus each time a menu link is focused or blurred. 28 for ( const link of links ) { 29 link.addEventListener( 'focus', toggleFocus, true ); 30 link.addEventListener( 'blur', toggleFocus, true ); 31 } 32 33 // Toggle focus each time a menu link with children receive a touch event. 34 for ( const link of linksWithChildren ) { 35 link.addEventListener( 'touchstart', toggleFocus, false ); 36 } 37 38 /** 39 * Sets or removes .focus class on an element. 40 */ 41 function toggleFocus() { 42 if ( event.type === 'focus' || event.type === 'blur' ) { 43 let self = this; 44 // Move up through the ancestors of the current link until we hit .nav-menu. 45 while ( ! self.classList.contains( 'nav-menu' ) ) { 46 // On li elements toggle the class .focus. 47 if ( 'li' === self.tagName.toLowerCase() ) { 48 self.classList.toggle( 'focus' ); 49 } 50 self = self.parentNode; 51 } 52 } 53 54 if ( event.type === 'touchstart' ) { 55 const menuItem = this.parentNode; 56 event.preventDefault(); 57 for ( const link of menuItem.parentNode.children ) { 58 if ( menuItem !== link ) { 59 link.classList.remove( 'focus' ); 60 } 61 } 62 menuItem.classList.toggle( 'focus' ); 63 } 64 } 65}() );
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.