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