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