1
/* global screenReaderText */
3
* Theme functions file.
5
* Contains handlers for navigation and widget area.
9
var body, masthead, menuToggle, siteNavigation, socialNavigation, siteHeaderMenu, resizeTimer;
11
function initMainNavigation( container ) {
13
// Add dropdown toggle that displays child menu items.
14
var dropdownToggle = $( '<button />', {
15
'class': 'dropdown-toggle',
16
'aria-expanded': false
17
} ).append( $( '<span />', {
18
'class': 'screen-reader-text',
19
text: screenReaderText.expand
22
container.find( '.menu-item-has-children > a' ).after( dropdownToggle );
24
// Toggle buttons and submenu items with active children menu items.
25
container.find( '.current-menu-ancestor > button' ).addClass( 'toggled-on' );
26
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
28
// Add menu items with submenus to aria-haspopup="true".
29
container.find( '.menu-item-has-children' ).attr( 'aria-haspopup', 'true' );
31
container.find( '.dropdown-toggle' ).click( function( e ) {
32
var _this = $( this ),
33
screenReaderSpan = _this.find( '.screen-reader-text' );
36
_this.toggleClass( 'toggled-on' );
37
_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
40
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
42
screenReaderSpan.text( screenReaderSpan.text() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
45
initMainNavigation( $( '.main-navigation' ) );
47
masthead = $( '#masthead' );
48
menuToggle = masthead.find( '#menu-toggle' );
49
siteHeaderMenu = masthead.find( '#site-header-menu' );
50
siteNavigation = masthead.find( '#site-navigation' );
51
socialNavigation = masthead.find( '#social-navigation' );
56
// Return early if menuToggle is missing.
57
if ( ! menuToggle.length ) {
61
// Add an initial values for the attribute.
62
menuToggle.add( siteNavigation ).add( socialNavigation ).attr( 'aria-expanded', 'false' );
64
menuToggle.on( 'click.twentysixteen', function() {
65
$( this ).add( siteHeaderMenu ).toggleClass( 'toggled-on' );
68
$( this ).add( siteNavigation ).add( socialNavigation ).attr( 'aria-expanded', $( this ).add( siteNavigation ).add( socialNavigation ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
73
// Fix sub-menus for touch devices and better focus for hidden submenu items for accessibility.
75
if ( ! siteNavigation.length || ! siteNavigation.children().length ) {
79
// Toggle `focus` class to allow submenu access on tablets.
80
function toggleFocusClassTouchScreen() {
81
if ( window.innerWidth >= 910 ) {
82
$( document.body ).on( 'touchstart.twentysixteen', function( e ) {
83
if ( ! $( e.target ).closest( '.main-navigation li' ).length ) {
84
$( '.main-navigation li' ).removeClass( 'focus' );
87
siteNavigation.find( '.menu-item-has-children > a' ).on( 'touchstart.twentysixteen', function( e ) {
88
var el = $( this ).parent( 'li' );
90
if ( ! el.hasClass( 'focus' ) ) {
92
el.toggleClass( 'focus' );
93
el.siblings( '.focus' ).removeClass( 'focus' );
97
siteNavigation.find( '.menu-item-has-children > a' ).unbind( 'touchstart.twentysixteen' );
101
if ( 'ontouchstart' in window ) {
102
$( window ).on( 'resize.twentysixteen', toggleFocusClassTouchScreen );
103
toggleFocusClassTouchScreen();
106
siteNavigation.find( 'a' ).on( 'focus.twentysixteen blur.twentysixteen', function() {
107
$( this ).parents( '.menu-item' ).toggleClass( 'focus' );
111
// Add the default ARIA attributes for the menu toggle and the navigations.
112
function onResizeARIA() {
113
if ( window.innerWidth < 910 ) {
114
if ( menuToggle.hasClass( 'toggled-on' ) ) {
115
menuToggle.attr( 'aria-expanded', 'true' );
117
menuToggle.attr( 'aria-expanded', 'false' );
120
if ( siteHeaderMenu.hasClass( 'toggled-on' ) ) {
121
siteNavigation.attr( 'aria-expanded', 'true' );
122
socialNavigation.attr( 'aria-expanded', 'true' );
124
siteNavigation.attr( 'aria-expanded', 'false' );
125
socialNavigation.attr( 'aria-expanded', 'false' );
128
menuToggle.attr( 'aria-controls', 'site-navigation social-navigation' );
130
menuToggle.removeAttr( 'aria-expanded' );
131
siteNavigation.removeAttr( 'aria-expanded' );
132
socialNavigation.removeAttr( 'aria-expanded' );
133
menuToggle.removeAttr( 'aria-controls' );
137
// Add 'below-entry-meta' class to elements.
138
function belowEntryMetaClass( param ) {
139
if ( body.hasClass( 'page' ) || body.hasClass( 'search' ) || body.hasClass( 'single-attachment' ) || body.hasClass( 'error404' ) ) {
143
$( '.entry-content' ).find( param ).each( function() {
144
var element = $( this ),
145
elementPos = element.offset(),
146
elementPosTop = elementPos.top,
147
entryFooter = element.closest( 'article' ).find( '.entry-footer' ),
148
entryFooterPos = entryFooter.offset(),
149
entryFooterPosBottom = entryFooterPos.top + ( entryFooter.height() + 28 ),
150
caption = element.closest( 'figure' ),
153
// Add 'below-entry-meta' to elements below the entry meta.
154
if ( elementPosTop > entryFooterPosBottom ) {
156
// Check if full-size images and captions are larger than or equal to 840px.
157
if ( 'img.size-full' === param ) {
159
// Create an image to find native image width of resized images (i.e. max-width: 100%).
160
newImg = new Image();
161
newImg.src = element.attr( 'src' );
163
$( newImg ).load( function() {
164
if ( newImg.width >= 840 ) {
165
element.addClass( 'below-entry-meta' );
167
if ( caption.hasClass( 'wp-caption' ) ) {
168
caption.addClass( 'below-entry-meta' );
169
caption.removeAttr( 'style' );
174
element.addClass( 'below-entry-meta' );
177
element.removeClass( 'below-entry-meta' );
178
caption.removeClass( 'below-entry-meta' );
183
$( document ).ready( function() {
184
body = $( document.body );
187
.on( 'load.twentysixteen', onResizeARIA )
188
.on( 'resize.twentysixteen', function() {
189
clearTimeout( resizeTimer );
190
resizeTimer = setTimeout( function() {
191
belowEntryMetaClass( 'img.size-full' );
192
belowEntryMetaClass( 'blockquote.alignleft, blockquote.alignright' );
197
belowEntryMetaClass( 'img.size-full' );
198
belowEntryMetaClass( 'blockquote.alignleft, blockquote.alignright' );