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 "use strict"; 9 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 if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { 25 menu.className += ' nav-menu'; 26 } 27 28 function setARIA() { 29 if ( 783 > window.innerWidth ) { 30 button.setAttribute( 'aria-controls', 'primary-menu' ); 31 button.setAttribute( 'aria-expanded', 'false' ); 32 menu.setAttribute( 'aria-expanded', 'false' ); 33 } else { 34 button.removeAttribute( 'aria-controls' ); 35 button.removeAttribute( 'aria-expanded' ); 36 menu.removeAttribute( 'aria-expanded' ); 37 } 38 } 39 40 button.onclick = function() { 41 if ( -1 !== container.className.indexOf( 'toggled' ) ) { 42 container.className = container.className.replace( ' toggled', '' ); 43 button.setAttribute( 'aria-expanded', 'false' ); 44 menu.setAttribute( 'aria-expanded', 'false' ); 45 } else { 46 container.className += ' toggled'; 47 button.setAttribute( 'aria-expanded', 'true' ); 48 menu.setAttribute( 'aria-expanded', 'true' ); 49 } 50 }; 51 52 // Get all the link elements within the menu. 53 links = menu.getElementsByTagName( 'a' ); 54 subMenus = menu.getElementsByTagName( 'ul' ); 55 56 // Set ARIA attributes properly. 57 window.addEventListener( 'load', setARIA, false ); 58 window.addEventListener( 'resize', setARIA, true ); 59 60 // Set menu items with submenus to aria-haspopup="true". 61 for ( var i = 0, len = subMenus.length; i < len; i++ ) { 62 subMenus[i].parentNode.setAttribute( 'aria-haspopup', 'true' ); 63 } 64 65 // Each time a menu link is focused or blurred, toggle focus. 66 for ( i = 0, len = links.length; i < len; i++ ) { 67 links[i].addEventListener( 'focus', toggleFocus, true ); 68 links[i].addEventListener( 'blur', toggleFocus, true ); 69 } 70 71 /** 72 * Sets or removes .focus class on an element. 73 */ 74 function toggleFocus() { 75 var self = this; 76 77 // Move up through the ancestors of the current link until we hit .nav-menu. 78 while ( -1 === self.className.indexOf( 'nav-menu' ) ) { 79 80 // On li elements toggle the class .focus. 81 if ( 'li' === self.tagName.toLowerCase() ) { 82 if ( -1 !== self.className.indexOf( 'focus' ) ) { 83 self.className = self.className.replace( ' focus', '' ); 84 } else { 85 self.className += ' focus'; 86 } 87 } 88 89 self = self.parentElement; 90 } 91 } 92} )();
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.