123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /**
- * File navigation.js.
- *
- * Handles toggling the navigation menu for small screens and enables TAB key
- * navigation support for dropdown menus.
- */
- ( function( $ ) {
- var masthead, menuToggle, siteNavContain, siteNavigation;
- function initMainNavigation( container ) {
- // Add dropdown toggle that displays child menu items.
- var dropdownToggle = $( '<button />', { 'class': 'dropdown-toggle', 'aria-expanded': false } )
- .append( screenReaderText.icon_expand )
- .append( $( '<span />', { 'class': 'screen-reader-text', text: screenReaderText.expand } ) );
- container.find( '.menu-item-has-children > a, .page_item_has_children > a' ).after( dropdownToggle );
- // Set the active submenu dropdown toggle button initial state.
- container.find( '.current-menu-ancestor > button' )
- .addClass( 'toggled' )
- .attr( 'aria-expanded', 'true' )
- .find( '.screen-reader-text' )
- .text( screenReaderText.collapse );
- // Set the active submenu initial state.
- container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled' );
- container.find( '.dropdown-toggle' ).click( function( e ) {
- var _this = $( this ),
- screenReaderSpan = _this.find( '.screen-reader-text' );
- e.preventDefault();
- _this.toggleClass( 'toggled' );
- _this.next( '.children, .sub-menu' ).toggleClass( 'toggled' );
- _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
- screenReaderSpan.text( screenReaderSpan.text() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
- });
- }
- initMainNavigation( $( '.main-navigation' ) );
- masthead = $( '#masthead' );
- menuToggle = masthead.find( '.menu-toggle' );
- siteNavContain = masthead.find( '.main-navigation' );
- siteNavigation = masthead.find( '.main-navigation > div > ul' );
- // Enable menuToggle.
- ( function() {
- // Return early if menuToggle is missing.
- if ( ! menuToggle.length ) {
- return;
- }
- // Add an initial value for the attribute.
- menuToggle.attr( 'aria-expanded', 'false' );
- menuToggle.on( 'click.radcliffe_2', function() {
- siteNavContain.toggleClass( 'toggled' );
- $( this ).attr( 'aria-expanded', siteNavContain.hasClass( 'toggled' ) );
- } );
- } )();
- // Fix sub-menus for touch devices and better focus for hidden submenu items for accessibility.
- ( function() {
- if ( ! siteNavigation.length || ! siteNavigation.children().length ) {
- return;
- }
- // Toggle `focus` class to allow submenu access on tablets.
- function toggleFocusClassTouchScreen() {
- if ( 'none' === $( '.menu-toggle' ).css( 'display' ) ) {
- $( document.body ).on( 'touchstart.radcliffe_2', function( e ) {
- if ( ! $( e.target ).closest( '.main-navigation li' ).length ) {
- $( '.main-navigation li' ).removeClass( 'focus' );
- }
- } );
- siteNavigation.find( '.menu-item-has-children > a, .page_item_has_children > a' )
- .on( 'touchstart.radcliffe_2', function( e ) {
- var el = $( this ).parent( 'li' );
- if ( ! el.hasClass( 'focus' ) ) {
- e.preventDefault();
- el.toggleClass( 'focus' );
- el.siblings( '.focus' ).removeClass( 'focus' );
- }
- } );
- } else {
- siteNavigation.find( '.menu-item-has-children > a, .page_item_has_children > a' ).unbind( 'touchstart.radcliffe_2' );
- }
- }
- if ( 'ontouchstart' in window ) {
- $( window ).on( 'resize.radcliffe_2', toggleFocusClassTouchScreen );
- toggleFocusClassTouchScreen();
- }
- siteNavigation.find( 'a' ).on( 'focus.radcliffe_2 blur.radcliffe_2', function() {
- $( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' );
- } );
- } )();
- } )( jQuery );
|