1 /* global twentyseventeenScreenReaderText */
3 * Theme functions file.
5 * Contains handlers for navigation and widget area.
9 var masthead, menuToggle, siteNavContain, siteNavigation;
11 function initMainNavigation( container ) {
13 // Add dropdown toggle that displays child menu items.
14 var dropdownToggle = $( '<button />', { 'class': 'dropdown-toggle', 'aria-expanded': false })
15 .append( twentyseventeenScreenReaderText.icon )
16 .append( $( '<span />', { 'class': 'screen-reader-text', text: twentyseventeenScreenReaderText.expand }) );
18 container.find( '.menu-item-has-children > a, .page_item_has_children > a' ).after( dropdownToggle );
20 // Set the active submenu dropdown toggle button initial state.
21 container.find( '.current-menu-ancestor > button' )
22 .addClass( 'toggled-on' )
23 .attr( 'aria-expanded', 'true' )
24 .find( '.screen-reader-text' )
25 .text( twentyseventeenScreenReaderText.collapse );
26 // Set the active submenu initial state.
27 container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
29 container.find( '.dropdown-toggle' ).click( function( e ) {
30 var _this = $( this ),
31 screenReaderSpan = _this.find( '.screen-reader-text' );
34 _this.toggleClass( 'toggled-on' );
35 _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
37 _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
39 screenReaderSpan.text( screenReaderSpan.text() === twentyseventeenScreenReaderText.expand ? twentyseventeenScreenReaderText.collapse : twentyseventeenScreenReaderText.expand );
43 initMainNavigation( $( '.main-navigation' ) );
45 masthead = $( '#masthead' );
46 menuToggle = masthead.find( '.menu-toggle' );
47 siteNavContain = masthead.find( '.main-navigation' );
48 siteNavigation = masthead.find( '.main-navigation > div > ul' );
53 // Return early if menuToggle is missing.
54 if ( ! menuToggle.length ) {
58 // Add an initial value for the attribute.
59 menuToggle.attr( 'aria-expanded', 'false' );
61 menuToggle.on( 'click.twentyseventeen', function() {
62 siteNavContain.toggleClass( 'toggled-on' );
64 $( this ).attr( 'aria-expanded', siteNavContain.hasClass( 'toggled-on' ) );
68 // Fix sub-menus for touch devices and better focus for hidden submenu items for accessibility.
70 if ( ! siteNavigation.length || ! siteNavigation.children().length ) {
74 // Toggle `focus` class to allow submenu access on tablets.
75 function toggleFocusClassTouchScreen() {
76 if ( 'none' === $( '.menu-toggle' ).css( 'display' ) ) {
78 $( document.body ).on( 'touchstart.twentyseventeen', function( e ) {
79 if ( ! $( e.target ).closest( '.main-navigation li' ).length ) {
80 $( '.main-navigation li' ).removeClass( 'focus' );
84 siteNavigation.find( '.menu-item-has-children > a, .page_item_has_children > a' )
85 .on( 'touchstart.twentyseventeen', function( e ) {
86 var el = $( this ).parent( 'li' );
88 if ( ! el.hasClass( 'focus' ) ) {
90 el.toggleClass( 'focus' );
91 el.siblings( '.focus' ).removeClass( 'focus' );
96 siteNavigation.find( '.menu-item-has-children > a, .page_item_has_children > a' ).unbind( 'touchstart.twentyseventeen' );
100 if ( 'ontouchstart' in window ) {
101 $( window ).on( 'resize.twentyseventeen', toggleFocusClassTouchScreen );
102 toggleFocusClassTouchScreen();
105 siteNavigation.find( 'a' ).on( 'focus.twentyseventeen blur.twentyseventeen', function() {
106 $( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' );