1/** 2 * navigation.js 3 * 4 * Handles toggling the navigation menu for small screens. 5 */ 6( function () { 7 var container, button, menu, links, i, len; 8 9 container = document.getElementById( 'site-navigation' ); 10 if ( ! container ) { 11 return; 12 } 13 14 button = container.getElementsByClassName( 'menu-toggle' )[ 0 ]; 15 if ( 'undefined' === typeof button ) { 16 return; 17 } 18 19 menu = container.getElementsByTagName( 'ul' )[ 0 ]; 20 21 // Hide menu toggle button if menu is empty and return early. 22 if ( 'undefined' === typeof menu ) { 23 button.style.display = 'none'; 24 return; 25 } 26 27 menu.setAttribute( 'aria-expanded', 'false' ); 28 if ( - 1 === menu.className.indexOf( 'nav-menu' ) ) { 29 menu.className += 'nav-menu'; 30 } 31 32 button.onclick = function () { 33 if ( - 1 !== container.className.indexOf( 'main-small-navigation' ) ) { 34 container.className = container.className.replace( 'main-small-navigation', 'main-navigation' ); 35 button.setAttribute( 'aria-expanded', 'false' ); 36 menu.setAttribute( 'aria-expanded', 'false' ); 37 } else { 38 container.className = container.className.replace( 'main-navigation', 'main-small-navigation' ); 39 button.setAttribute( 'aria-expanded', 'true' ); 40 menu.setAttribute( 'aria-expanded', 'true' ); 41 } 42 }; 43 44 // Get all the link elements within the menu. 45 links = menu.getElementsByTagName( 'a' ); 46 // Each time a menu link is focused or blurred, toggle focus. 47 for ( i = 0, len = links.length; i < len; i ++ ) { 48 links[ i ].addEventListener( 'focus', toggleFocus, true ); 49 links[ i ].addEventListener( 'blur', toggleFocus, true ); 50 } 51 52 /** 53 * Sets or removes .focus class on an element. 54 */ 55 function toggleFocus() { 56 var self = this; 57 58 // Move up through the ancestors of the current link until we hit .nav-menu. 59 while ( - 1 === self.className.indexOf( 'nav-menu' ) ) { 60 61 // On li elements toggle the class .focus. 62 if ( 'li' === self.tagName.toLowerCase() ) { 63 if ( - 1 !== self.className.indexOf( 'focus' ) ) { 64 self.className = self.className.replace( ' focus', '' ); 65 } else { 66 self.className += ' focus'; 67 } 68 } 69 70 self = self.parentElement; 71 } 72 } 73 74 /** 75 * Toggles `focus` class to allow submenu access on tablets. 76 */ 77 ( function ( container ) { 78 var touchStartFn, i, 79 parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 80 81 if ( 'ontouchstart' in window && window.matchMedia( '(min-width: 768px)' ).matches ) { 82 touchStartFn = function ( e ) { 83 var menuItem = this.parentNode, i; 84 85 if ( ! menuItem.classList.contains( 'focus' ) ) { 86 e.preventDefault(); 87 for ( i = 0; i < menuItem.parentNode.children.length; ++ i ) { 88 if ( menuItem === menuItem.parentNode.children[ i ] ) { 89 continue; 90 } 91 menuItem.parentNode.children[ i ].classList.remove( 'focus' ); 92 } 93 menuItem.classList.add( 'focus' ); 94 } else { 95 menuItem.classList.remove( 'focus' ); 96 } 97 }; 98 99 for ( i = 0; i < parentLink.length; ++ i ) { 100 parentLink[ i ].addEventListener( 'touchstart', touchStartFn, false ); 101 } 102 } 103 }( container ) ); 104} )();
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.