159 lines
5 KiB
JavaScript
159 lines
5 KiB
JavaScript
/* global shoreditchScreenReaderText */
|
|
( function( $ ) {
|
|
|
|
var body, masthead, menuToggle, siteMenu, siteNavigation;
|
|
|
|
function initMainNavigation( container ) {
|
|
|
|
// Add dropdown toggle that displays child menu items.
|
|
var dropdownToggle = $( '<button />', {
|
|
'class': 'dropdown-toggle',
|
|
'aria-expanded': false
|
|
} ).append( $( '<span />', {
|
|
'class': 'screen-reader-text',
|
|
text: shoreditchScreenReaderText.expand
|
|
} ) );
|
|
|
|
container.find( '.menu-item-has-children > a' ).after( dropdownToggle );
|
|
|
|
// Toggle buttons and submenu items with active children menu items.
|
|
container.find( '.current-menu-ancestor > button' ).addClass( 'toggled-on' );
|
|
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
|
|
|
|
// Add menu items with submenus to aria-haspopup="true".
|
|
container.find( '.menu-item-has-children' ).attr( 'aria-haspopup', 'true' );
|
|
|
|
container.find( '.dropdown-toggle' ).click( function( e ) {
|
|
var _this = $( this ),
|
|
screenReaderSpan = _this.find( '.screen-reader-text' );
|
|
|
|
e.preventDefault();
|
|
_this.toggleClass( 'toggled-on' );
|
|
_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
|
|
|
|
// jscs:disable
|
|
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
|
|
// jscs:enable
|
|
screenReaderSpan.text( screenReaderSpan.text() === shoreditchScreenReaderText.expand ? shoreditchScreenReaderText.collapse : shoreditchScreenReaderText.expand );
|
|
} );
|
|
|
|
//Ensure submenus do not fall outside the left side of the window, rendering them unusable.
|
|
var submenus = container.find( '.children, .sub-menu' );
|
|
|
|
//Return early if no submenus exist
|
|
if ( ! submenus.length ) {
|
|
return;
|
|
} else {
|
|
submenus.each( function() {
|
|
var parent = $( this ).parent( 'li' );
|
|
|
|
parent.hover(
|
|
function() {
|
|
var submenu = $( this ).find( '> .sub-menu, > .children' );
|
|
|
|
if ( $( submenu ) ) {
|
|
if ( $( submenu ).offset().left < 0 ) {
|
|
$( submenu ).addClass( 'offscreen' );
|
|
}
|
|
}
|
|
},
|
|
function() {
|
|
$( this ).find( '> .sub-menu, > .children' ).removeClass( 'offscreen' );
|
|
}
|
|
);
|
|
} );
|
|
}
|
|
}
|
|
initMainNavigation( $( '.main-navigation' ) );
|
|
|
|
masthead = $( '#masthead' );
|
|
menuToggle = masthead.find( '#menu-toggle' );
|
|
siteMenu = masthead.find( '#site-menu' );
|
|
siteNavigation = masthead.find( '#site-navigation' );
|
|
|
|
// Enable menuToggle.
|
|
( function() {
|
|
|
|
// Return early if menuToggle is missing.
|
|
if ( ! menuToggle.length ) {
|
|
return;
|
|
}
|
|
|
|
// Add an initial values for the attribute.
|
|
menuToggle.add( siteNavigation ).attr( 'aria-expanded', 'false' );
|
|
|
|
menuToggle.on( 'click.shoreditch', function() {
|
|
$( this ).add( siteMenu ).add( siteNavigation ).toggleClass( 'toggled-on' );
|
|
|
|
// jscs:disable
|
|
$( this ).add( siteMenu ).add( siteNavigation ).attr( 'aria-expanded', $( this ).add( siteNavigation ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
|
|
// jscs:enable
|
|
} );
|
|
} )();
|
|
|
|
// 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 ( window.innerWidth >= 896 ) {
|
|
$( document.body ).on( 'touchstart.shoreditch', function( e ) {
|
|
if ( ! $( e.target ).closest( '.main-navigation li' ).length ) {
|
|
$( '.main-navigation li' ).removeClass( 'focus' );
|
|
}
|
|
} );
|
|
siteNavigation.find( '.menu-item-has-children > a' ).on( 'touchstart.shoreditch', 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' ).unbind( 'touchstart.shoreditch' );
|
|
}
|
|
}
|
|
|
|
if ( 'ontouchstart' in window ) {
|
|
$( window ).on( 'resize.shoreditch', toggleFocusClassTouchScreen );
|
|
toggleFocusClassTouchScreen();
|
|
}
|
|
|
|
siteNavigation.find( 'a' ).on( 'focus.shoreditch blur.shoreditch', function() {
|
|
$( this ).parents( '.menu-item' ).toggleClass( 'focus' );
|
|
} );
|
|
} )();
|
|
|
|
// Add the default ARIA attributes for the menu toggle and the navigations.
|
|
function onResizeARIA() {
|
|
if ( window.innerWidth < 896 ) {
|
|
if ( menuToggle.hasClass( 'toggled-on' ) ) {
|
|
menuToggle.attr( 'aria-expanded', 'true' );
|
|
siteMenu.attr( 'aria-expanded', 'true' );
|
|
siteNavigation.attr( 'aria-expanded', 'true' );
|
|
} else {
|
|
menuToggle.attr( 'aria-expanded', 'false' );
|
|
siteMenu.attr( 'aria-expanded', 'false' );
|
|
siteNavigation.attr( 'aria-expanded', 'false' );
|
|
}
|
|
} else {
|
|
menuToggle.removeAttr( 'aria-expanded' );
|
|
siteMenu.removeAttr( 'aria-expanded' );
|
|
siteNavigation.removeAttr( 'aria-expanded' );
|
|
}
|
|
}
|
|
|
|
$( document ).ready( function() {
|
|
body = $( document.body );
|
|
|
|
$( window )
|
|
.on( 'load.shoreditch', onResizeARIA )
|
|
.on( 'resize.shoreditch', onResizeARIA );
|
|
} );
|
|
|
|
} )( jQuery );
|