/* global twentyseventeenscreenreadertext */
/**
* theme functions file.
*
* contains handlers for navigation and widget area.
*/
(function( $ ) {
var masthead, menutoggle, sitenavcontain, sitenavigation;
function initmainnavigation( container ) {
// add dropdown toggle that displays child menu items.
var dropdowntoggle = $( '', { 'class': 'dropdown-toggle', 'aria-expanded': false })
.append( twentyseventeenscreenreadertext.icon )
.append( $( '', { 'class': 'screen-reader-text', text: twentyseventeenscreenreadertext.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-on' )
.attr( 'aria-expanded', 'true' )
.find( '.screen-reader-text' )
.text( twentyseventeenscreenreadertext.collapse );
// set the active submenu initial state.
container.find( '.current-menu-ancestor > .sub-menu' ).addclass( 'toggled-on' );
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' );
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
screenreaderspan.text( screenreaderspan.text() === twentyseventeenscreenreadertext.expand ? twentyseventeenscreenreadertext.collapse : twentyseventeenscreenreadertext.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.twentyseventeen', function() {
sitenavcontain.toggleclass( 'toggled-on' );
$( this ).attr( 'aria-expanded', sitenavcontain.hasclass( 'toggled-on' ) );
});
})();
// 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.twentyseventeen', 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.twentyseventeen', 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.twentyseventeen' );
}
}
if ( 'ontouchstart' in window ) {
$( window ).on( 'resize.twentyseventeen', togglefocusclasstouchscreen );
togglefocusclasstouchscreen();
}
sitenavigation.find( 'a' ).on( 'focus.twentyseventeen blur.twentyseventeen', function() {
$( this ).parents( '.menu-item, .page_item' ).toggleclass( 'focus' );
});
})();
})( jquery );