]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentyfifteen/js/functions.js
WordPress 4.1
[autoinstalls/wordpress.git] / wp-content / themes / twentyfifteen / js / functions.js
1 /* global screenReaderText */
2 /**
3  * Theme functions file.
4  *
5  * Contains handlers for navigation and widget area.
6  */
7
8 ( function( $ ) {
9         var $body, $window, $sidebar, adminbarOffset, top = false,
10             bottom = false, windowWidth, windowHeight, lastWindowPos = 0,
11             topOffset = 0, bodyHeight, sidebarHeight, resizeTimer;
12
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>' );
15
16         $( '.dropdown-toggle' ).click( function( e ) {
17                 var _this = $( this );
18                 e.preventDefault();
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 );
23         } );
24
25         // Enable menu toggle for small screens.
26         ( function() {
27                 var secondary = $( '#secondary' ), button, menu, widgets, social;
28                 if ( ! secondary ) {
29                         return;
30                 }
31
32                 button = $( '.site-branding' ).find( '.secondary-toggle' );
33                 if ( ! button ) {
34                         return;
35                 }
36
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 ) ) {
42                         button.hide();
43                         return;
44                 }
45
46                 button.on( 'click.twentyfifteen', function() {
47                         secondary.toggleClass( 'toggled-on' );
48                         secondary.trigger( 'resize' );
49                         $( this ).toggleClass( 'toggled-on' );
50                 } );
51         } )();
52
53         // Sidebar scrolling.
54         function resize() {
55                 windowWidth   = $window.width();
56                 windowHeight  = $window.height();
57                 bodyHeight    = $body.height();
58                 sidebarHeight = $sidebar.height();
59
60                 if ( 955 > windowWidth ) {
61                         top = bottom = false;
62                         $sidebar.removeAttr( 'style' );
63                 }
64         }
65
66         function scroll() {
67                 var windowPos = $window.scrollTop();
68
69                 if ( 955 > windowWidth ) {
70                         return;
71                 }
72
73                 if ( sidebarHeight + adminbarOffset > windowHeight ) {
74                         if ( windowPos > lastWindowPos ) {
75                                 if ( top ) {
76                                         top = false;
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 ) {
80                                         bottom = true;
81                                         $sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
82                                 }
83                         } else if ( windowPos < lastWindowPos ) {
84                                 if ( bottom ) {
85                                         bottom = false;
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 ) {
89                                         top = true;
90                                         $sidebar.attr( 'style', 'position: fixed;' );
91                                 }
92                         } else {
93                                 top = bottom = false;
94                                 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
95                                 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
96                         }
97                 } else if ( ! top ) {
98                         top = true;
99                         $sidebar.attr( 'style', 'position: fixed;' );
100                 }
101
102                 lastWindowPos = windowPos;
103         }
104
105         function resizeAndScroll() {
106                 resize();
107                 scroll();
108         }
109
110         $( document ).ready( function() {
111                 $body          = $( document.body );
112                 $window        = $( window );
113                 $sidebar       = $( '#sidebar' ).first();
114                 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
115
116                 $window
117                         .on( 'scroll.twentyfifteen', scroll )
118                         .on( 'resize.twentyfifteen', function() {
119                                 clearTimeout( resizeTimer );
120                                 resizeTimer = setTimeout( resizeAndScroll, 500 );
121                         } );
122                 $sidebar.on( 'click keydown', 'button', resizeAndScroll );
123
124                 resizeAndScroll();
125
126                 for ( var i = 1; i < 6; i++ ) {
127                         setTimeout( resizeAndScroll, 100 * i );
128                 }
129         } );
130
131 } )( jQuery );