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 |
/** |
2 |
* Theme functions file |
|
3 |
* |
|
4 |
* Contains handlers for navigation, accessibility, header sizing |
|
5 |
* footer widgets and Featured Content slider |
|
6 |
* |
|
7 |
*/ |
|
8 |
( function( $ ) { |
|
9 |
var body = $( 'body' ), |
|
10 |
_window = $( window ), |
|
11 |
nav, button, menu; |
|
12 |
||
13 |
nav = $( '#primary-navigation' ); |
|
14 |
button = nav.find( '.menu-toggle' ); |
|
15 |
menu = nav.find( '.nav-menu' ); |
|
16 |
||
17 |
// Enable menu toggle for small screens. |
|
18 |
( function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
if ( ! nav.length || ! button.length ) { |
5 | 20 |
return; |
21 |
} |
|
22 |
||
23 |
// Hide button if menu is missing or empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
if ( ! menu.length || ! menu.children().length ) { |
5 | 25 |
button.hide(); |
26 |
return; |
|
27 |
} |
|
28 |
||
29 |
button.on( 'click.twentyfourteen', function() { |
|
30 |
nav.toggleClass( 'toggled-on' ); |
|
31 |
if ( nav.hasClass( 'toggled-on' ) ) { |
|
32 |
$( this ).attr( 'aria-expanded', 'true' ); |
|
33 |
menu.attr( 'aria-expanded', 'true' ); |
|
34 |
} else { |
|
35 |
$( this ).attr( 'aria-expanded', 'false' ); |
|
36 |
menu.attr( 'aria-expanded', 'false' ); |
|
37 |
} |
|
38 |
} ); |
|
39 |
} )(); |
|
40 |
||
41 |
/* |
|
42 |
* Makes "skip to content" link work correctly in IE9 and Chrome for better |
|
43 |
* accessibility. |
|
44 |
* |
|
45 |
* @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/ |
|
46 |
*/ |
|
47 |
_window.on( 'hashchange.twentyfourteen', function() { |
|
48 |
var hash = location.hash.substring( 1 ), element; |
|
49 |
||
50 |
if ( ! hash ) { |
|
51 |
return; |
|
52 |
} |
|
53 |
||
54 |
element = document.getElementById( hash ); |
|
55 |
||
56 |
if ( element ) { |
|
57 |
if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) { |
|
58 |
element.tabIndex = -1; |
|
59 |
} |
|
60 |
||
61 |
element.focus(); |
|
62 |
||
63 |
// Repositions the window on jump-to-anchor to account for header height. |
|
64 |
window.scrollBy( 0, -80 ); |
|
65 |
} |
|
66 |
} ); |
|
67 |
||
68 |
$( function() { |
|
69 |
// Search toggle. |
|
70 |
$( '.search-toggle' ).on( 'click.twentyfourteen', function( event ) { |
|
71 |
var that = $( this ), |
|
72 |
wrapper = $( '#search-container' ), |
|
73 |
container = that.find( 'a' ); |
|
74 |
||
75 |
that.toggleClass( 'active' ); |
|
76 |
wrapper.toggleClass( 'hide' ); |
|
77 |
||
78 |
if ( that.hasClass( 'active' ) ) { |
|
79 |
container.attr( 'aria-expanded', 'true' ); |
|
80 |
} else { |
|
81 |
container.attr( 'aria-expanded', 'false' ); |
|
82 |
} |
|
83 |
||
84 |
if ( that.is( '.active' ) || $( '.search-toggle .screen-reader-text' )[0] === event.target ) { |
|
85 |
wrapper.find( '.search-field' ).focus(); |
|
86 |
} |
|
87 |
} ); |
|
88 |
||
89 |
/* |
|
90 |
* Fixed header for large screen. |
|
91 |
* If the header becomes more than 48px tall, unfix the header. |
|
92 |
* |
|
93 |
* The callback on the scroll event is only added if there is a header |
|
94 |
* image and we are not on mobile. |
|
95 |
*/ |
|
96 |
if ( _window.width() > 781 ) { |
|
97 |
var mastheadHeight = $( '#masthead' ).height(), |
|
98 |
toolbarOffset, mastheadOffset; |
|
99 |
||
100 |
if ( mastheadHeight > 48 ) { |
|
101 |
body.removeClass( 'masthead-fixed' ); |
|
102 |
} |
|
103 |
||
104 |
if ( body.is( '.header-image' ) ) { |
|
105 |
toolbarOffset = body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0; |
|
106 |
mastheadOffset = $( '#masthead' ).offset().top - toolbarOffset; |
|
107 |
||
108 |
_window.on( 'scroll.twentyfourteen', function() { |
|
109 |
if ( _window.scrollTop() > mastheadOffset && mastheadHeight < 49 ) { |
|
110 |
body.addClass( 'masthead-fixed' ); |
|
111 |
} else { |
|
112 |
body.removeClass( 'masthead-fixed' ); |
|
113 |
} |
|
114 |
} ); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
// Focus styles for menus. |
|
119 |
$( '.primary-navigation, .secondary-navigation' ).find( 'a' ).on( 'focus.twentyfourteen blur.twentyfourteen', function() { |
|
120 |
$( this ).parents().toggleClass( 'focus' ); |
|
121 |
} ); |
|
122 |
} ); |
|
123 |
||
124 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* Add or remove ARIA attributes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* |
5 | 127 |
* Uses jQuery's width() function to determine the size of the window and add |
128 |
* the default ARIA attributes for the menu toggle if it's visible. |
|
129 |
* @since Twenty Fourteen 1.4 |
|
130 |
*/ |
|
131 |
function onResizeARIA() { |
|
132 |
if ( 781 > _window.width() ) { |
|
133 |
button.attr( 'aria-expanded', 'false' ); |
|
134 |
menu.attr( 'aria-expanded', 'false' ); |
|
135 |
button.attr( 'aria-controls', 'primary-menu' ); |
|
136 |
} else { |
|
137 |
button.removeAttr( 'aria-expanded' ); |
|
138 |
menu.removeAttr( 'aria-expanded' ); |
|
139 |
button.removeAttr( 'aria-controls' ); |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
_window |
|
144 |
.on( 'load.twentyfourteen', onResizeARIA ) |
|
145 |
.on( 'resize.twentyfourteen', function() { |
|
146 |
onResizeARIA(); |
|
147 |
} ); |
|
148 |
||
149 |
_window.load( function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
var footerSidebar, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
isCustomizeSelectiveRefresh = ( 'undefined' !== typeof wp && wp.customize && wp.customize.selectiveRefresh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
|
5 | 153 |
// Arrange footer widgets vertically. |
154 |
if ( $.isFunction( $.fn.masonry ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
footerSidebar = $( '#footer-sidebar' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
footerSidebar.masonry( { |
5 | 157 |
itemSelector: '.widget', |
158 |
columnWidth: function( containerWidth ) { |
|
159 |
return containerWidth / 4; |
|
160 |
}, |
|
161 |
gutterWidth: 0, |
|
162 |
isResizable: true, |
|
163 |
isRTL: $( 'body' ).is( '.rtl' ) |
|
164 |
} ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
if ( isCustomizeSelectiveRefresh ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
// Retain previous masonry-brick initial position. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
var copyPosition = ( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
placement.partial.extended( wp.customize.widgetsPreview.WidgetPartial ) && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
placement.removedNodes instanceof jQuery && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
placement.removedNodes.is( '.masonry-brick' ) && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
placement.container instanceof jQuery |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
if ( copyPosition ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
placement.container.css( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
position: placement.removedNodes.css( 'position' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
top: placement.removedNodes.css( 'top' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
left: placement.removedNodes.css( 'left' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
// Re-arrange footer widgets after selective refresh event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
wp.customize.selectiveRefresh.bind( 'sidebar-updated', function( sidebarPartial ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
if ( 'sidebar-3' === sidebarPartial.sidebarId ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
footerSidebar.masonry( 'reloadItems' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
footerSidebar.masonry( 'layout' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
// Initialize audio and video players in Twenty_Fourteen_Ephemera_Widget widget when selectively refreshed in Customizer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
if ( isCustomizeSelectiveRefresh && wp.mediaelement ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
wp.mediaelement.initialize(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
} ); |
5 | 200 |
} |
201 |
||
202 |
// Initialize Featured Content slider. |
|
203 |
if ( body.is( '.slider' ) ) { |
|
204 |
$( '.featured-content' ).featuredslider( { |
|
205 |
selector: '.featured-content-inner > article', |
|
206 |
controlsContainer: '.featured-content' |
|
207 |
} ); |
|
208 |
} |
|
209 |
} ); |
|
210 |
} )( jQuery ); |