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' ), KEYCODE_TAB = 9;; 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 // focus trap for menu 45 var menuElement = document.querySelector( '#site-navigation.toggled' ); 46 if( menuElement ) { 47 document.addEventListener('keydown', function(e) { 48 if( document.querySelector( '#site-navigation.toggled' ) ) { 49 var focusable = document.querySelector( '#site-navigation.toggled' ).querySelectorAll( 'input, a, button, ul.sub-menu.isShow a' ); 50 focusable = Array.prototype.slice.call( focusable ); 51 focusable = focusable.filter( function( focusableelement ) { 52 return null !== focusableelement.offsetParent; 53 } ); 54 var firstFocusable = document.querySelector('.menu-toggle'); 55 var lastFocusable = focusable[focusable.length - 1]; 56 pubnews_focus_trap( firstFocusable, lastFocusable, e ); 57 } 58 }) 59 } 60 } ); 61 62 // focus trap for search 63 const searchElement = document.getElementsByClassName( 'search-wrap' ) 64 if( searchElement.length > 0 ) { 65 const searchButton = searchElement[0].getElementsByTagName( 'button' )[ 0 ]; 66 searchButton.addEventListener( 'click', function() { 67 var searchElementToggled = document.querySelector( '.search-wrap' ); 68 if( searchElementToggled ) { 69 document.addEventListener('keydown', function(e) { 70 if( document.querySelector( '.search-wrap' ) ) { 71 var focusable = document.querySelector( '.search-wrap' ).querySelectorAll( 'input.search-field, input.search-submit, .search_close_btn' ); 72 focusable = Array.prototype.slice.call( focusable ); 73 focusable = focusable.filter( function( focusableelement ) { 74 return null !== focusableelement.offsetParent; 75 } ); 76 var firstFocusable = document.querySelector('input.search-field'); 77 var lastFocusable = focusable[focusable.length - 1]; 78 pubnews_focus_trap( firstFocusable, lastFocusable, e ); 79 if( e.which == 13 && e.target == document.querySelector('button.search-form-close') ) { 80 document.querySelector('button.search-form-close').click() 81 document.querySelector('button.search-trigger').focus() 82 e.preventDefault() 83 } 84 } 85 }) 86 } 87 }); 88 } 89 90 // Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation. 91 document.addEventListener( 'click', function( event ) { 92 const isClickInside = siteNavigation.contains( event.target ); 93 94 if ( ! isClickInside ) { 95 siteNavigation.classList.remove( 'toggled' ); 96 button.setAttribute( 'aria-expanded', 'false' ); 97 } 98 } ); 99 100 // Get all the link elements within the menu. 101 const links = menu.getElementsByTagName( 'a' ); 102 103 // Get all the link elements with children within the menu. 104 const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 105 106 // Toggle focus each time a menu link is focused or blurred. 107 for ( const link of links ) { 108 link.addEventListener( 'focus', toggleFocus, true ); 109 link.addEventListener( 'blur', toggleFocus, true ); 110 } 111 112 // Toggle focus each time a menu link with children receive a touch event. 113 for ( const link of linksWithChildren ) { 114 link.addEventListener( 'touchstart', toggleFocus, false ); 115 } 116 117 /**
118 * Sets or removes .focus class on an element. 119 */ 120 function toggleFocus() { 121 if ( event.type === 'focus' || event.type === 'blur' ) { 122 let self = this; 123 // Move up through the ancestors of the current link until we hit .nav-menu. 124 while ( ! self.classList.contains( 'nav-menu' ) ) { 125 // On li elements toggle the class .focus. 126 if ( 'li' === self.tagName.toLowerCase() ) { 127 self.classList.toggle( 'focus' ); 128 } 129 self = self.parentNode; 130 } 131 } 132 133 if ( event.type === 'touchstart' ) { 134 const menuItem = this.parentNode; 135 for ( const link of menuItem.parentNode.children ) { 136 if ( menuItem !== link ) { 137 link.classList.remove( 'focus' ); 138 } 139 } 140 menuItem.classList.toggle( 'focus' ); 141 } 142 } 143 144 /** 145 * Responsive header menu modal 146 * 147 * @returns void 148 * @since 1.0.0 149 */ 150 function pubnews_focus_trap( firstFocusable, lastFocusable, e ) { 151 if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) { 152 if ( e.shiftKey ) /* shift + tab */ { 153 if (document.activeElement === firstFocusable) { 154 lastFocusable.focus(); 155 e.preventDefault(); 156 } 157 } else /* tab */ { 158 if ( document.activeElement === lastFocusable ) { 159 firstFocusable.focus(); 160 e.preventDefault(); 161 } 162 } 163 } 164 } 165 166 /** 167 * Responsive header sub menu toggler 168 * 169 * @returns void 170 * @since 1.0.0 171 */ 172 function dropdownMenuMobileHandle() { 173 if ( ! $( "#site-navigation .menu-item.menu-item-has-children" ).length > 0 && ! $( "#site-navigation .page_item.page_item_has_children" ).length > 0 ) { 174 return; 175 } 176 $( "#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>' ); 177 $(document).on( "click", "#site-navigation .toggle-sub-menu", function (e) { 178 e.preventDefault(); 179 var _this = $(this) 180 _this.parent().siblings().children( ".sub-menu, .children" ).removeClass( "isShow" ); 181 _this.siblings( ".sub-menu, .children" ).toggleClass( "isShow" ); 182 _this.parent().siblings().find( "> .toggle-sub-menu i" ).removeClass("fa-minus").addClass("fa-plus"); 183 if( _this.siblings( ".sub-menu, .children" ).hasClass( "isShow" ) ) { 184 _this.children().removeClass( "fa-plus" ).addClass( "fa-minus" ); 185 } else { 186 _this.children().removeClass( "fa-minus" ).addClass( "fa-plus" ); 187 } 188 }); 189 } 190 dropdownMenuMobileHandle(); 191 192}(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.