1/** 2 * navigation.js 3 * 4 * Handles toggling the navigation menu for small screens. 5 */ 6( function() { 7 "use strict"; 8 var container, button, menu, links, subMenus; 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', 'true' ); 29 if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { 30 menu.className += ' nav-menu'; 31 } 32 33 button.onclick = function() { 34 if ( -1 == menu.className.indexOf( 'nav-menu' ) ) 35 menu.className = 'nav-menu'; 36 37 if ( -1 != button.className.indexOf( 'toggled-on' ) ) { 38 button.className = button.className.replace( ' toggled-on', '' ); 39 menu.className = menu.className.replace( ' toggled-on', '' ); 40 container.className = container.className.replace( 'main-small-navigation', 'navigation-main' ); 41 button.setAttribute( 'aria-expanded', 'false' ); 42 menu.setAttribute( 'aria-expanded', 'false' ); 43 } else { 44 button.className += ' toggled-on'; 45 menu.className += ' toggled-on'; 46 container.className = container.className.replace( 'navigation-main', 'main-small-navigation' ); 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 menu items with submenus to aria-haspopup="true". 57 for ( var i = 0, len = subMenus.length; i < len; i++ ) { 58 subMenus[i].parentNode.setAttribute( 'aria-haspopup', 'true' ); 59 } 60 61 // Each time a menu link is focused or blurred, toggle focus. 62 for ( i = 0, len = links.length; i < len; i++ ) { 63 links[i].addEventListener( 'focus', toggleFocus, true ); 64 links[i].addEventListener( 'blur', toggleFocus, true ); 65 } 66 67 /** 68 * Sets or removes .focus class on an element. 69 */ 70 function toggleFocus() { 71 var self = this; 72 73 // Move up through the ancestors of the current link until we hit .nav-menu. 74 while ( -1 === self.className.indexOf( 'nav-menu' ) ) { 75 76 // On li elements toggle the class .focus. 77 if ( 'li' === self.tagName.toLowerCase() ) { 78 if ( -1 !== self.className.indexOf( 'focus' ) ) { 79 self.className = self.className.replace( ' focus', '' ); 80 } else { 81 self.className += ' focus'; 82 } 83 } 84 85 self = self.parentElement; 86 } 87 } 88} )();
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.