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 // Add dropdown toggle that display child menu items.
15 $( '.main-navigation .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
17 // Toggle buttons and submenu items with active children menu items.
18 $( '.main-navigation .current-menu-ancestor > button' ).addClass( 'toggle-on' );
19 $( '.main-navigation .current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
21 $( '.dropdown-toggle' ).click( function( e ) {
22 var _this = $( this );
24 _this.toggleClass( 'toggle-on' );
25 _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
26 _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
27 _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
30 secondary = $( '#secondary' );
31 button = $( '.site-branding' ).find( '.secondary-toggle' );
33 // Enable menu toggle for small screens.
35 var menu, widgets, social;
36 if ( ! secondary || ! button ) {
40 // Hide button if there are no widgets and the menus are missing or empty.
41 menu = secondary.find( '.nav-menu' );
42 widgets = secondary.find( '#widget-area' );
43 social = secondary.find( '#social-navigation' );
44 if ( ! widgets.length && ! social.length && ( ! menu || ! menu.children().length ) ) {
49 button.on( 'click.twentyfifteen', function() {
50 secondary.toggleClass( 'toggled-on' );
51 secondary.trigger( 'resize' );
52 $( this ).toggleClass( 'toggled-on' );
53 if ( $( this, secondary ).hasClass( 'toggled-on' ) ) {
54 $( this ).attr( 'aria-expanded', 'true' );
55 secondary.attr( 'aria-expanded', 'true' );
57 $( this ).attr( 'aria-expanded', 'false' );
58 secondary.attr( 'aria-expanded', 'false' );
64 * @summary Add or remove ARIA attributes.
65 * Uses jQuery's width() function to determine the size of the window and add
66 * the default ARIA attributes for the menu toggle if it's visible.
67 * @since Twenty Fifteen 1.1
69 function onResizeARIA() {
70 if ( 955 > $window.width() ) {
71 button.attr( 'aria-expanded', 'false' );
72 secondary.attr( 'aria-expanded', 'false' );
73 button.attr( 'aria-controls', 'secondary' );
75 button.removeAttr( 'aria-expanded' );
76 secondary.removeAttr( 'aria-expanded' );
77 button.removeAttr( 'aria-controls' );
83 windowWidth = $window.width();
85 if ( 955 > windowWidth ) {
87 $sidebar.removeAttr( 'style' );
92 var windowPos = $window.scrollTop();
94 if ( 955 > windowWidth ) {
98 sidebarHeight = $sidebar.height();
99 windowHeight = $window.height();
100 bodyHeight = $body.height();
102 if ( sidebarHeight + adminbarOffset > windowHeight ) {
103 if ( windowPos > lastWindowPos ) {
106 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
107 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
108 } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top && sidebarHeight + adminbarOffset < bodyHeight ) {
110 $sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
112 } else if ( windowPos < lastWindowPos ) {
115 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
116 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
117 } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) {
119 $sidebar.attr( 'style', 'position: fixed;' );
122 top = bottom = false;
123 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
124 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
126 } else if ( ! top ) {
128 $sidebar.attr( 'style', 'position: fixed;' );
131 lastWindowPos = windowPos;
134 function resizeAndScroll() {
139 $( document ).ready( function() {
140 $body = $( document.body );
141 $window = $( window );
142 $sidebar = $( '#sidebar' ).first();
143 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
146 .on( 'scroll.twentyfifteen', scroll )
147 .on( 'load.twentyfifteen', onResizeARIA )
148 .on( 'resize.twentyfifteen', function() {
149 clearTimeout( resizeTimer );
150 resizeTimer = setTimeout( resizeAndScroll, 500 );
153 $sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
157 for ( var i = 1; i < 6; i++ ) {
158 setTimeout( resizeAndScroll, 100 * i );