1 /* global screenReaderText */
3 * Theme functions file.
5 * Contains handlers for navigation and widget area.
9 var $body, $window, $sidebar, adminbarOffset, top = false,
10 bottom = false, windowWidth, windowHeight, lastWindowPos = 0,
11 topOffset = 0, bodyHeight, sidebarHeight, resizeTimer,
14 function initMainNavigation( container ) {
15 // Add dropdown toggle that display child menu items.
16 container.find( '.menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
18 // Toggle buttons and submenu items with active children menu items.
19 container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' );
20 container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
22 container.find( '.dropdown-toggle' ).click( function( e ) {
23 var _this = $( this );
25 _this.toggleClass( 'toggle-on' );
26 _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
27 _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
28 _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
31 initMainNavigation( $( '.main-navigation' ) );
33 // Re-initialize the main navigation when it is updated, persisting any existing submenu expanded states.
34 $( document ).on( 'customize-preview-menu-refreshed', function( e, params ) {
35 if ( 'primary' === params.wpNavMenuArgs.theme_location ) {
36 initMainNavigation( params.newContainer );
38 // Re-sync expanded states from oldContainer.
39 params.oldContainer.find( '.dropdown-toggle.toggle-on' ).each(function() {
40 var containerId = $( this ).parent().prop( 'id' );
41 $( params.newContainer ).find( '#' + containerId + ' > .dropdown-toggle' ).triggerHandler( 'click' );
46 secondary = $( '#secondary' );
47 button = $( '.site-branding' ).find( '.secondary-toggle' );
49 // Enable menu toggle for small screens.
51 var menu, widgets, social;
52 if ( ! secondary || ! button ) {
56 // Hide button if there are no widgets and the menus are missing or empty.
57 menu = secondary.find( '.nav-menu' );
58 widgets = secondary.find( '#widget-area' );
59 social = secondary.find( '#social-navigation' );
60 if ( ! widgets.length && ! social.length && ( ! menu || ! menu.children().length ) ) {
65 button.on( 'click.twentyfifteen', function() {
66 secondary.toggleClass( 'toggled-on' );
67 secondary.trigger( 'resize' );
68 $( this ).toggleClass( 'toggled-on' );
69 if ( $( this, secondary ).hasClass( 'toggled-on' ) ) {
70 $( this ).attr( 'aria-expanded', 'true' );
71 secondary.attr( 'aria-expanded', 'true' );
73 $( this ).attr( 'aria-expanded', 'false' );
74 secondary.attr( 'aria-expanded', 'false' );
80 * @summary Add or remove ARIA attributes.
81 * Uses jQuery's width() function to determine the size of the window and add
82 * the default ARIA attributes for the menu toggle if it's visible.
83 * @since Twenty Fifteen 1.1
85 function onResizeARIA() {
86 if ( 955 > $window.width() ) {
87 button.attr( 'aria-expanded', 'false' );
88 secondary.attr( 'aria-expanded', 'false' );
89 button.attr( 'aria-controls', 'secondary' );
91 button.removeAttr( 'aria-expanded' );
92 secondary.removeAttr( 'aria-expanded' );
93 button.removeAttr( 'aria-controls' );
99 windowWidth = $window.width();
101 if ( 955 > windowWidth ) {
102 top = bottom = false;
103 $sidebar.removeAttr( 'style' );
108 var windowPos = $window.scrollTop();
110 if ( 955 > windowWidth ) {
114 sidebarHeight = $sidebar.height();
115 windowHeight = $window.height();
116 bodyHeight = $body.height();
118 if ( sidebarHeight + adminbarOffset > windowHeight ) {
119 if ( windowPos > lastWindowPos ) {
122 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
123 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
124 } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top && sidebarHeight + adminbarOffset < bodyHeight ) {
126 $sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
128 } else if ( windowPos < lastWindowPos ) {
131 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
132 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
133 } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) {
135 $sidebar.attr( 'style', 'position: fixed;' );
138 top = bottom = false;
139 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
140 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
142 } else if ( ! top ) {
144 $sidebar.attr( 'style', 'position: fixed;' );
147 lastWindowPos = windowPos;
150 function resizeAndScroll() {
155 $( document ).ready( function() {
156 $body = $( document.body );
157 $window = $( window );
158 $sidebar = $( '#sidebar' ).first();
159 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
162 .on( 'scroll.twentyfifteen', scroll )
163 .on( 'load.twentyfifteen', onResizeARIA )
164 .on( 'resize.twentyfifteen', function() {
165 clearTimeout( resizeTimer );
166 resizeTimer = setTimeout( resizeAndScroll, 500 );
169 $sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
173 for ( var i = 1; i < 6; i++ ) {
174 setTimeout( resizeAndScroll, 100 * i );