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, i, len; 9 10 // Add svg icon to submenu item. 11 var svg = '<svg class="icon-down" viewBox="0 0 129 129"><path d="m121.3,34.6c-1.6-1.6-4.2-1.6-5.8,0l-51,51.1-51.1-51.1c-1.6-1.6-4.2-1.6-5.8,0-1.6,1.6-1.6,4.2 0,5.8l53.9,53.9c0.8,0.8 1.8,1.2 2.9,1.2 1,0 2.1-0.4 2.9-1.2l53.9-53.9c1.7-1.6 1.7-4.2 0.1-5.8z"/></svg>'; 12 var dropdownToggle = $( '<button />', { 'class': 'dropdown-toggle', 'aria-expanded': false }); 13 dropdownToggle.append( svg ); 14 $( '.main-navigation' ).find( '.menu-item-has-children > a, .page_item_has_children > a' ).after( dropdownToggle ); 15 16 17 container = document.getElementById( 'site-navigation' ); 18 if ( ! container ) { 19 return; 20 } 21 22 button = container.getElementsByTagName( 'button' )[0]; 23 if ( 'undefined' === typeof button ) { 24 return; 25 } 26 27 menu = container.getElementsByTagName( 'ul' )[0]; 28 29 // Hide menu toggle button if menu is empty and return early. 30 if ( 'undefined' === typeof menu ) { 31 button.style.display = 'none'; 32 return; 33 } 34 35 menu.setAttribute( 'aria-expanded', 'false' ); 36 if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { 37 menu.className += ' nav-menu'; 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 55 // Each time a menu link is focused or blurred, toggle focus. 56 for ( i = 0, len = links.length; i < len; i++ ) { 57 links[i].addEventListener( 'focus', toggleFocus, true ); 58 links[i].addEventListener( 'blur', toggleFocus, true ); 59 } 60 61 /** 62 * Sets or removes .focus class on an element. 63 */ 64 function toggleFocus() { 65 var self = this; 66 67 // Move up through the ancestors of the current link until we hit .nav-menu. 68 while ( -1 === self.className.indexOf( 'nav-menu' ) ) { 69 70 // On li elements toggle the class .focus. 71 if ( 'li' === self.tagName.toLowerCase() ) { 72 if ( -1 !== self.className.indexOf( 'focus' ) ) { 73 self.className = self.className.replace( ' focus', '' ); 74 } else { 75 self.className += ' focus'; 76 } 77 } 78 79 self = self.parentElement; 80 } 81 } 82 83 /** 84 * Toggles `focus` class to allow submenu access on tablets. 85 */ 86 ( function( container ) { 87 var touchStartFn, i, 88 parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 89 90 if ( 'ontouchstart' in window ) { 91 touchStartFn = function( e ) { 92 var menuItem = this.parentNode, i; 93 94 if ( ! menuItem.classList.contains( 'focus' ) ) { 95 e.preventDefault(); 96 for ( i = 0; i < menuItem.parentNode.children.length; ++i ) { 97 if ( menuItem === menuItem.parentNode.children[i] ) { 98 continue; 99 } 100 menuItem.parentNode.children[i].classList.remove( 'focus' ); 101 } 102 menuItem.classList.add( 'focus' ); 103 } else { 104 menuItem.classList.remove( 'focus' ); 105 } 106 }; 107 108 for ( i = 0; i < parentLink.length; ++i ) { 109 parentLink[i].addEventListener( 'touchstart', touchStartFn, false ); 110 } 111 } 112 }( container ) ); 113} )( jQuery );
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.