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;
13 // Add dropdown toggle that display child menu items.
14 $( '.main-navigation .page_item_has_children > a, .main-navigation .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
16 $( '.dropdown-toggle' ).click( function( e ) {
17 var _this = $( this );
19 _this.toggleClass( 'toggle-on' );
20 _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
21 _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
22 _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
25 // Enable menu toggle for small screens.
27 var secondary = $( '#secondary' ), button, menu, widgets, social;
32 button = $( '.site-branding' ).find( '.secondary-toggle' );
37 // Hide button if there are no widgets and the menus are missing or empty.
38 menu = secondary.find( '.nav-menu' );
39 widgets = secondary.find( '#widget-area' );
40 social = secondary.find( '#social-navigation' );
41 if ( ! widgets.length && ! social.length && ( ! menu || ! menu.children().length ) ) {
46 button.on( 'click.twentyfifteen', function() {
47 secondary.toggleClass( 'toggled-on' );
48 secondary.trigger( 'resize' );
49 $( this ).toggleClass( 'toggled-on' );
55 windowWidth = $window.width();
56 windowHeight = $window.height();
57 bodyHeight = $body.height();
58 sidebarHeight = $sidebar.height();
60 if ( 955 > windowWidth ) {
62 $sidebar.removeAttr( 'style' );
67 var windowPos = $window.scrollTop();
69 if ( 955 > windowWidth ) {
73 if ( sidebarHeight + adminbarOffset > windowHeight ) {
74 if ( windowPos > lastWindowPos ) {
77 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
78 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
79 } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top && sidebarHeight + adminbarOffset < bodyHeight ) {
81 $sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
83 } else if ( windowPos < lastWindowPos ) {
86 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
87 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
88 } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) {
90 $sidebar.attr( 'style', 'position: fixed;' );
94 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
95 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
99 $sidebar.attr( 'style', 'position: fixed;' );
102 lastWindowPos = windowPos;
105 function resizeAndScroll() {
110 $( document ).ready( function() {
111 $body = $( document.body );
112 $window = $( window );
113 $sidebar = $( '#sidebar' ).first();
114 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
117 .on( 'scroll.twentyfifteen', scroll )
118 .on( 'resize.twentyfifteen', function() {
119 clearTimeout( resizeTimer );
120 resizeTimer = setTimeout( resizeAndScroll, 500 );
122 $sidebar.on( 'click keydown', 'button', resizeAndScroll );
126 for ( var i = 1; i < 6; i++ ) {
127 setTimeout( resizeAndScroll, 100 * i );