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