diff --git a/src/wp-content/themes/twentytwelve/functions.php b/src/wp-content/themes/twentytwelve/functions.php index a86de032a57b3..61276aa4ee5a8 100644 --- a/src/wp-content/themes/twentytwelve/functions.php +++ b/src/wp-content/themes/twentytwelve/functions.php @@ -199,7 +199,7 @@ function twentytwelve_scripts_styles() { } // Adds JavaScript for handling the navigation menu hide-and-show behavior. - wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), '20141205', true ); + wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20220217', true ); $font_url = twentytwelve_get_font_url(); if ( ! empty( $font_url ) ) { diff --git a/src/wp-content/themes/twentytwelve/js/navigation.js b/src/wp-content/themes/twentytwelve/js/navigation.js index f7141bff7eefa..181084765a874 100644 --- a/src/wp-content/themes/twentytwelve/js/navigation.js +++ b/src/wp-content/themes/twentytwelve/js/navigation.js @@ -3,6 +3,32 @@ * accessibility for submenu items. */ ( function() { + function getParentElements( element, selector ) { + var parentElements = []; + + while ( element.parentElement !== null ) { + if ( ! selector || element.parentElement.matches( selector ) ) { + parentElements.push( element.parentElement ); + } + + element = element.parentElement; + } + + return parentElements; + } + + function getSiblingElements( element, selector ) { + var siblingElements = []; + + element.parentElement.children.forEach( function( sibling ) { + if ( sibling !== element && ( ! selector || sibling.matches( selector ) ) ) { + siblingElements.push( sibling ); + } + } ); + + return siblingElements; + } + var nav = document.getElementById( 'site-navigation' ), button, menu; if ( ! nav ) { return; @@ -21,35 +47,53 @@ } button.onclick = function() { - if ( -1 === menu.className.indexOf( 'nav-menu' ) ) { + if ( ! menu.classList.contains( 'nav-menu' ) ) { menu.className = 'nav-menu'; } - if ( -1 !== button.className.indexOf( 'toggled-on' ) ) { - button.className = button.className.replace( ' toggled-on', '' ); - menu.className = menu.className.replace( ' toggled-on', '' ); - } else { - button.className += ' toggled-on'; - menu.className += ' toggled-on'; - } + button.classList.toggle( 'toggled-on' ); + menu.classList.toggle( 'toggled-on' ); }; -} )(); -// Better focus for hidden submenu items for accessibility. -( function( $ ) { - $( '.main-navigation' ).find( 'a' ).on( 'focus.twentytwelve blur.twentytwelve', function() { - $( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' ); + // Better focus for hidden submenu items for accessibility. + function toggleParentsFocusClass() { + getParentElements( this, '.menu-item, .page_item' ).forEach( function( parentElement ) { + parentElement.classList.toggle( 'focus' ); + } ); + } + document.querySelector( '.main-navigation' ).getElementsByTagName( 'a' ).forEach( function( menuLink ) { + menuLink.addEventListener( 'focus', toggleParentsFocusClass ); + menuLink.addEventListener( 'blur', toggleParentsFocusClass ); } ); - if ( 'ontouchstart' in window ) { - $('body').on( 'touchstart.twentytwelve', '.menu-item-has-children > a, .page_item_has_children > a', function( e ) { - var el = $( this ).parent( 'li' ); - - if ( ! el.hasClass( 'focus' ) ) { - e.preventDefault(); - el.toggleClass( 'focus' ); - el.siblings( '.focus').removeClass( 'focus' ); - } - } ); - } -} )( jQuery ); + if ( 'ontouchstart' in window ) { + document.body.addEventListener( 'touchstart', function( e ) { + for ( var target = e.target; target && target != this; target = target.parentNode ) { + if ( target.matches( '.menu-item-has-children > a, .page_item_has_children > a' ) ) { + var el = getParentElements( target, 'li' )[0]; + if ( el.classList.contains( 'focus' ) ) { + e.preventDefault(); + el.classList.add( 'focus' ); + getSiblingElements( el, '.focus' ).forEach( function( siblingElement ) { + siblingElement.classList.remove( 'focus' ); + } ); + } + break; + } + } + } ); + document.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ).forEach( function( menuLink ) { + menuLink.addEventListener( 'touchstart', function( e ) { + var el = getParentElements( this, 'li' )[0]; + + if ( el.classList.contains( 'focus' ) ) { + e.preventDefault(); + el.classList.add( 'focus' ); + getSiblingElements( el, '.focus' ).forEach( function( siblingElement ) { + siblingElement.classList.remove( 'focus' ); + } ); + } + } ); + } ); + } +} )();