1/** 2 * Handles toggling the navigation menu for small screens and enables TAB key 3 * navigation support for dropdown menus. 4 * 5 * @package scaffold 6 * @copyright Copyright (c) 2020, Danny Cooper 7 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License 8 */ 9 10( function() { 11 var container, button, menu, links, i, len; 12 13 container = document.getElementById( 'site-navigation' ); 14 if ( ! container ) { 15 return; 16 } 17 18 button = container.getElementsByTagName( 'button' )[0]; 19 if ( 'undefined' === typeof button ) { 20 return; 21 } 22 23 menu = container.getElementsByTagName( 'ul' )[0]; 24 25 // Hide menu toggle button if menu is empty and return early. 26 if ( 'undefined' === typeof menu ) { 27 button.style.display = 'none'; 28 return; 29 } 30 31 menu.setAttribute( 'aria-expanded', 'false' ); 32 if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { 33 menu.className += ' nav-menu'; 34 } 35 36 button.onclick = function() { 37 if ( -1 !== container.className.indexOf( 'toggled' ) ) { 38 container.className = container.className.replace( ' toggled', '' ); 39 button.setAttribute( 'aria-expanded', 'false' ); 40 menu.setAttribute( 'aria-expanded', 'false' ); 41 } else { 42 container.className += ' toggled'; 43 button.setAttribute( 'aria-expanded', 'true' ); 44 menu.setAttribute( 'aria-expanded', 'true' ); 45 } 46 }; 47 48 // Get all the link elements within the menu. 49 links = menu.getElementsByTagName( 'a' ); 50 51 // Each time a menu link is focused or blurred, toggle focus. 52 for ( i = 0, len = links.length; i < len; i++ ) { 53 links[i].addEventListener( 'focus', toggleFocus, true ); 54 links[i].addEventListener( 'blur', toggleFocus, true ); 55 } 56 57 /** 58 * Sets or removes .focus class on an element. 59 */ 60 function toggleFocus() { 61 var self = this; 62 63 // Move up through the ancestors of the current link until we hit .nav-menu. 64 while ( -1 === self.className.indexOf( 'nav-menu' ) ) { 65 66 // On li elements toggle the class .focus. 67 if ( 'li' === self.tagName.toLowerCase() ) { 68 if ( -1 !== self.className.indexOf( 'focus' ) ) { 69 self.className = self.className.replace( ' focus', '' ); 70 } else { 71 self.className += ' focus'; 72 } 73 } 74 75 self = self.parentElement; 76 } 77 } 78 79 /** 80 * Toggles `focus` class to allow submenu access on tablets. 81 */ 82 ( function( container ) { 83 var touchStartFn, i, 84 parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 85 86 if ( 'ontouchstart' in window ) { 87 touchStartFn = function( e ) { 88 var menuItem = this.parentNode, i; 89 90 if ( ! menuItem.classList.contains( 'focus' ) ) { 91 e.preventDefault(); 92 for ( var i = 0, len = menuItem.parentNode.children.length; i < len; ++i ) { 93 if ( menuItem === menuItem.parentNode.children[i] ) { 94 continue; 95 } 96 menuItem.parentNode.children[i].classList.remove( 'focus' ); 97 } 98 menuItem.classList.add( 'focus' ); 99 } else { 100 menuItem.classList.remove( 'focus' ); 101 } 102 }; 103 104 for ( var i = 0, len = parentLink.length; i < len; ++i ) { 105 parentLink[i].addEventListener( 'touchstart', touchStartFn, false ); 106 } 107 } 108 }( container ) ); 109} )();
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.