1/** 2 * File primary-navigation.js. 3 * 4 * Required to open and close the mobile navigation. 5 */ 6( function() { 7 8 /** 9 * Menu Toggle Behaviors 10 * 11 * @param {Element} element 12 */ 13 var navMenu = function ( id ){ 14 var wrapper = document.body; // this is the element to which a CSS class is added when a mobile nav menu is open 15 var openButton = document.getElementById( id + '-open-menu' ); 16 var closeButton = document.getElementById( id + '-close-menu' ); 17 18 if ( openButton && closeButton ){ 19 openButton.onclick = function() { 20 wrapper.classList.add( id + '-navigation-open' ); 21 wrapper.classList.add( 'lock-scrolling' ); 22 closeButton.focus(); 23 } 24 25 closeButton.onclick = function() { 26 wrapper.classList.remove( id + '-navigation-open' ); 27 wrapper.classList.remove( 'lock-scrolling' ); 28 openButton.focus(); 29 } 30 } 31 32 /** 33 * Trap keyboard navigation in the menu modal. 34 * Adapted from TwentyTwenty 35 */ 36 document.addEventListener( 'keydown', function( event ) { 37 if ( ! wrapper.classList.contains( id + '-navigation-open' ) ){ 38 return; 39 } 40 var modal, elements, selectors, lastEl, firstEl, activeEl, tabKey, shift
40Key, escKey; 41 42 modal = document.querySelector( '.' + id + '-navigation' ); 43 selectors = "input, a, button"; 44 elements = modal.querySelectorAll( selectors ); 45 elements = Array.prototype.slice.call( elements ); 46 elements = elements.filter( function( el ) { 47 return ! el.classList.contains( 'woocommerce-cart-link' ); // ignore this element because it's hidden on mobile 48 }); 49 tabKey = event.keyCode === 9; 50 shiftKey = event.shiftKey; 51 escKey = event.keyCode === 27; 52 activeEl = document.activeElement; 53 lastEl = elements[ elements.length - 1 ]; 54 firstEl = elements[0]; 55 56 if ( escKey ) { 57 event.preventDefault(); 58 wrapper.classList.remove( id + '-navigation-open', 'lock-scrolling' ); 59 openButton.focus(); 60 } 61 62 if ( ! shiftKey && tabKey && lastEl === activeEl ) { 63 event.preventDefault(); 64 firstEl.focus(); 65 } 66 67 if ( shiftKey && tabKey && firstEl === activeEl ) { 68 event.preventDefault(); 69 lastEl.focus(); 70 } 71 72 // If there are no elements in the menu, don't move the focus 73 if ( tabKey && firstEl === lastEl ) { 74 event.preventDefault(); 75 } 76 }); 77 } 78 79 window.addEventListener( 'load', function() { 80 new navMenu( 'primary' ); 81 new navMenu( 'woo' ); 82 }); 83} )();
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.