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, dropdown, icon, screenreadertext, parentLink, menu, submenu, links, i, len; 9 10 container = document.getElementById( 'header' ); 11 if ( ! container ) { 12 return; 13 } 14 15 button = container.getElementsByClassName( 'toggle' )[0]; 16 if ( 'undefined' === typeof button ) { 17 return; 18 } 19 20 menu = container.getElementsByTagName( 'nav' )[0]; 21 22 screenreadertext = document.createElement( 'span' ); 23 screenreadertext.classList.add( 'screen-reader-text' ); 24 screenreadertext.textContent = NavigationScreenReaderText.expandMain; 25 button.appendChild( screenreadertext ); 26 27 parentLink = container.querySelectorAll( '.menu-item-has-children, .page_item_has_children' ); 28 29 for ( i = 0, len = parentLink.length; i < len; i++ ) { 30 var dropdown = document.createElement( 'button' ), 31 submenu = parentLink[i].querySelector( '.sub-menu' ), 32 icon = document.createElement( 'span' ), 33 screenreadertext = document.createElement( 'span' ); 34 35 icon.classList.add( 'dashicons' ); 36 icon.setAttribute( 'aria-hidden', 'true' ); 37 38 screenreadertext.classList.add( 'screen-reader-text' ); 39 screenreadertext.textContent = NavigationScreenReaderText.expandChild; 40 41 parentLink[i].insertBefore( dropdown, submenu ); 42 dropdown.classList.add( 'dropdown-toggle' ); 43 dropdown.setAttribute( 'aria-expanded', 'false' ); 44 dropdown.appendChild( icon ); 45 dropdown.appendChild( screenreadertext ); 46 47 dropdown.onclick = function() { 48 var parentLink = this.parentElement, 49 submenu = parentLink.querySelector( '.sub-menu' ), 50 screenreadertext = this.querySelector( '.screen-reader-text' ); 51 52 if ( -1 !== parentLink.className.indexOf( 'toggled-on' ) ) { 53 parentLink.className = parentLink.className.replace( ' toggled-on', '' ); 54 this.setAttribute( 'aria-expanded', 'false' ); 55 submenu.setAttribute ( 'aria-expanded', 'false'); 56 screenreadertext.textContent = NavigationScreenReaderText.expandChild; 57 } else { 58 parentLink.className += ' toggled-on'; 59 this.setAttribute( 'aria-expanded', 'true' ); 60 submenu.setAttribute ( 'aria-expanded', 'true'); 61 screenreadertext.textContent = NavigationScreenReaderText.collapseChild; 62 } 63 }; 64 } 65 66 // Hide menu toggle button if menu is empty and return early. 67 if ( 'undefined' === typeof menu ) { 68 button.style.display = 'none'; 69 return; 70 } 71 72 menu.setAttribute( 'aria-expanded', 'false' ); 73 if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { 74 menu.className += ' nav-menu'; 75 } 76 77 button.onclick = function() { 78 screenreadertext = this.querySelector( '.screen-reader-text' ); 79 if ( -1 !== container.className.indexOf( 'toggled' ) ) { 80 container.className = container.className.replace( ' toggled', '' ); 81 button.setAttribute( 'aria-expanded', 'false' ); 82 screenreadertext.textContent = NavigationScreenReaderText.expandMain; 83 menu.setAttribute( 'aria-expanded', 'false' ); 84 } else { 85 container.className += ' toggled'; 86 button.setAttribute( 'aria-expanded', 'true' ); 87 screenreadertext.textContent = NavigationScreenReaderText.collapseMain; 88 menu.setAttribute( 'aria-expanded', 'true' ); 89 } 90 }; 91 92 // Get all the link elements within the primary menu. 93 94 links = menu.getElementsByTagName( 'a' ); 95 96 // Each time a menu link is focused or blurred, toggle focus. 97 for ( i = 0, len = links.length; i < len; i++ ) { 98 links[i].addEventListener( 'focus', toggleFocus, true ); 99 links[i].addEventListener( 'blur', toggleFocus, true ); 100 } 101 102 /** 103 * Sets or removes .focus class on an element. 104 */ 105 function toggleFocus() { 106 var self = this; 107 108 // Move up through the ancestors of the current link until we hit .nav-menu. 109 while ( -1 === self.className.indexOf( 'nav-menu' ) ) { 110 111 // On li elements toggle the class .focus. 112 if ( 'li' === self.tagName.toLowerCase() ) { 113 114 if ( -1 !== self.className.indexOf( 'focus' ) ) { 115 self.className = self.className.replace( ' focus', '' ); 116 } else { 117 self.className += ' focus'; 118 } 119 } 120 self = self.parentElement; 121 } 122 } 123 124 /** 125 * Toggles `focus` class to allow submenu access on tablets. 126 */ 127 ( function( container ) { 128 var touchStartFn, i, 129 parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 130 131 if ( 'ontouchstart' in window ) { 132 touchStartFn = function( e ) { 133 var menuItem = this.parentNode, i; 134 135 if ( ! menuItem.classList.contains( 'focus' ) ) { 136 e.preventDefault(); 137 for ( i = 0; i < menuItem.parentNode.children.length; ++i ) { 138 if ( menuItem === menuItem.parentNode.children[i] ) { 139 continue; 140 } 141 menuItem.parentNode.children[i].classList.remove( 'focus' ); 142 } 143 menuItem.classList.add( 'focus' ); 144 } else { 145 menuItem.classList.remove( 'focus' ); 146 } 147 }; 148 149 for ( i = 0; i < parentLink.length; ++i ) { 150 parentLink[i].addEventListener( 'touchstart', touchStartFn, false ); 151 } 152 } 153 }( container ) ); 154} )();
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.