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 const siteNavigation = document.getElementById( 'site-navigation' ); 9 10 // Return early if the navigation doesn't exist. 11 if ( ! siteNavigation ) { 12 return; 13 } 14 15 const button = siteNavigation.getElementsByTagName( 'button' )[ 0 ]; 16 17 // Return early if the button doesn't exist. 18 if ( 'undefined' === typeof button ) { 19 return; 20 } 21 22 const menu = siteNavigation.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 if ( ! menu.classList.contains( 'nav-menu' ) ) { 31 menu.classList.add( 'nav-menu' ); 32 } 33 34 // Toggle the .toggled class and the aria-expanded value each time the button is clicked. 35 button.addEventListener( 'click', function() { 36 siteNavigation.classList.toggle( 'toggled' ); 37 38 if ( button.getAttribute( 'aria-expanded' ) === 'true' ) { 39 button.setAttribute( 'aria-expanded', 'false' ); 40 } else { 41 button.setAttribute( 'aria-expanded', 'true' ); 42 } 43 } ); 44 45 // Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation. 46 document.addEventListener( 'click', function( event ) { 47 const isClickInside = siteNavigation.contains( event.target ); 48 49 if ( ! isClickInside ) { 50 siteNavigation.classList.remove( 'toggled' ); 51 button.setAttribute( 'aria-expanded', 'false' ); 52 } 53 } ); 54 55 // Get all the link elements within the menu. 56 const links = menu.getElementsByTagName( 'a' ); 57 58 // Get all the link elements with children within the menu. 59 const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 60 61 // Toggle focus each time a menu link is focused or blurred. 62 for ( const link of links ) { 63 link.addEventListener( 'focus', toggleFocus, true ); 64 link.addEventListener( 'blur', toggleFocus, true ); 65 } 66 67 // Toggle focus each time a menu link with children receive a touch event. 68 for ( const link of linksWithChildren ) { 69 link.addEventListener( 'touchstart', toggleFocus, false ); 70 } 71 72 /** 73 * Sets or removes .focus class on an element. 74 */ 75 function toggleFocus() { 76 if ( event.type === 'focus' || event.type === 'blur' ) { 77 let self = this; 78 // Move up through the ancestors of the current link until we hit .nav-menu. 79 while ( ! self.classList.contains( 'nav-menu' ) ) { 80 // On li elements toggle the class .focus. 81 if ( 'li' === self.tagName.toLowerCase() ) { 82 self.classList.toggle( 'focus' ); 83 } 84 self = self.parentNode; 85 } 86 } 87 88 if ( event.type === 'touchstart' ) { 89 const menuItem = this.parentNode; 90 event.preventDefault(); 91 for ( const link of menuItem.parentNode.children ) { 92 if ( menuItem !== link ) { 93 link.classList.remove( 'focus' ); 94 } 95 } 96 menuItem.classList.toggle( 'focus' ); 97 } 98 } 99 100 /** 101 * Responsive header sub menu toggler 102 * 103 * @returns void 104 * @since 1.0.0 105 */ 106 function dropdownMenuMobileHandle() { 107 if ( ! $( "#site-navigation .menu-item.menu-item-has-children" ).length > 0 && ! $( "#site-navigation .page_item.page_item_has_children" ).length > 0 ) { 108 return; 109 } 110 $( "#site-navigation .menu-item.menu-item-has-children .sub-menu, #site-navigation .page_item.page_item_has_children .children" ).before( '<a href="#" class="toggle-sub-menu"><i class="fas fa-plus"></i></a>' ); 111 $(document).on( "click", "#site-navigation .toggle-sub-menu", function (e) { 112 e.preventDefault(); 113 var _this = $(this) 114 _this.parent().siblings().children( ".sub-menu, .children" ).removeClass( "isShow" ); 115 _this.siblings( ".sub-menu, .children" ).toggleClass( "isShow" ); 116 _this.parent().siblings().find( "> .toggle-sub-menu i" ).removeClass("fa-minus").addClass("fa-plus"); 117 if( _this.siblings( ".sub-menu, .children" ).hasClass( "isShow" ) ) { 118 _this.children().removeClass( "fa-plus" ).addClass( "fa-minus" ); 119 } else { 120 _this.children().removeClass( "fa-minus" ).addClass( "fa-plus" ); 121 } 122 }); 123 } 124 dropdownMenuMobileHandle(); 125 126}(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.