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