author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
permissions | -rw-r--r-- |
5 | 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, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
secondary, button; |
5 | 13 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
function initMainNavigation( container ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
// Add dropdown toggle that display child menu items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
container.find( '.menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' ); |
5 | 17 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
// Toggle buttons and submenu items with active children menu items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' ); |
5 | 21 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
container.find( '.dropdown-toggle' ).click( function( e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
var _this = $( this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
e.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
_this.toggleClass( 'toggle-on' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
_this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
initMainNavigation( $( '.main-navigation' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
// Re-initialize the main navigation when it is updated, persisting any existing submenu expanded states. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
$( document ).on( 'customize-preview-menu-refreshed', function( e, params ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
if ( 'primary' === params.wpNavMenuArgs.theme_location ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
initMainNavigation( params.newContainer ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
// Re-sync expanded states from oldContainer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
params.oldContainer.find( '.dropdown-toggle.toggle-on' ).each(function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
var containerId = $( this ).parent().prop( 'id' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
$( params.newContainer ).find( '#' + containerId + ' > .dropdown-toggle' ).triggerHandler( 'click' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
}); |
5 | 45 |
|
46 |
secondary = $( '#secondary' ); |
|
47 |
button = $( '.site-branding' ).find( '.secondary-toggle' ); |
|
48 |
||
49 |
// Enable menu toggle for small screens. |
|
50 |
( function() { |
|
51 |
var menu, widgets, social; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
if ( ! secondary.length || ! button.length ) { |
5 | 53 |
return; |
54 |
} |
|
55 |
||
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' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
if ( ! widgets.length && ! social.length && ( ! menu.length || ! menu.children().length ) ) { |
5 | 61 |
button.hide(); |
62 |
return; |
|
63 |
} |
|
64 |
||
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' ); |
|
72 |
} else { |
|
73 |
$( this ).attr( 'aria-expanded', 'false' ); |
|
74 |
secondary.attr( 'aria-expanded', 'false' ); |
|
75 |
} |
|
76 |
} ); |
|
77 |
} )(); |
|
78 |
||
79 |
/** |
|
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 |
|
84 |
*/ |
|
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' ); |
|
90 |
} else { |
|
91 |
button.removeAttr( 'aria-expanded' ); |
|
92 |
secondary.removeAttr( 'aria-expanded' ); |
|
93 |
button.removeAttr( 'aria-controls' ); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
// Sidebar scrolling. |
|
98 |
function resize() { |
|
99 |
windowWidth = $window.width(); |
|
100 |
||
101 |
if ( 955 > windowWidth ) { |
|
102 |
top = bottom = false; |
|
103 |
$sidebar.removeAttr( 'style' ); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
function scroll() { |
|
108 |
var windowPos = $window.scrollTop(); |
|
109 |
||
110 |
if ( 955 > windowWidth ) { |
|
111 |
return; |
|
112 |
} |
|
113 |
||
114 |
sidebarHeight = $sidebar.height(); |
|
115 |
windowHeight = $window.height(); |
|
116 |
bodyHeight = $body.height(); |
|
117 |
||
118 |
if ( sidebarHeight + adminbarOffset > windowHeight ) { |
|
119 |
if ( windowPos > lastWindowPos ) { |
|
120 |
if ( top ) { |
|
121 |
top = false; |
|
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 ) { |
|
125 |
bottom = true; |
|
126 |
$sidebar.attr( 'style', 'position: fixed; bottom: 0;' ); |
|
127 |
} |
|
128 |
} else if ( windowPos < lastWindowPos ) { |
|
129 |
if ( bottom ) { |
|
130 |
bottom = false; |
|
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 ) { |
|
134 |
top = true; |
|
135 |
$sidebar.attr( 'style', 'position: fixed;' ); |
|
136 |
} |
|
137 |
} else { |
|
138 |
top = bottom = false; |
|
139 |
topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0; |
|
140 |
$sidebar.attr( 'style', 'top: ' + topOffset + 'px;' ); |
|
141 |
} |
|
142 |
} else if ( ! top ) { |
|
143 |
top = true; |
|
144 |
$sidebar.attr( 'style', 'position: fixed;' ); |
|
145 |
} |
|
146 |
||
147 |
lastWindowPos = windowPos; |
|
148 |
} |
|
149 |
||
150 |
function resizeAndScroll() { |
|
151 |
resize(); |
|
152 |
scroll(); |
|
153 |
} |
|
154 |
||
155 |
$( document ).ready( function() { |
|
156 |
$body = $( document.body ); |
|
157 |
$window = $( window ); |
|
158 |
$sidebar = $( '#sidebar' ).first(); |
|
159 |
adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0; |
|
160 |
||
161 |
$window |
|
162 |
.on( 'scroll.twentyfifteen', scroll ) |
|
163 |
.on( 'load.twentyfifteen', onResizeARIA ) |
|
164 |
.on( 'resize.twentyfifteen', function() { |
|
165 |
clearTimeout( resizeTimer ); |
|
166 |
resizeTimer = setTimeout( resizeAndScroll, 500 ); |
|
167 |
onResizeARIA(); |
|
168 |
} ); |
|
169 |
$sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll ); |
|
170 |
||
171 |
resizeAndScroll(); |
|
172 |
||
173 |
for ( var i = 1; i < 6; i++ ) { |
|
174 |
setTimeout( resizeAndScroll, 100 * i ); |
|
175 |
} |
|
176 |
} ); |
|
177 |
||
178 |
} )( jQuery ); |