mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-28 12:07:36 +01:00
b2b3f1c3ff
props westonruter. see #32576. Built from https://develop.svn.wordpress.org/trunk@32807 git-svn-id: http://core.svn.wordpress.org/trunk@32778 1a063a9b-81f0-0310-95a4-ce76da25c4cd
168 lines
5.2 KiB
JavaScript
168 lines
5.2 KiB
JavaScript
/* global screenReaderText */
|
|
/**
|
|
* Theme functions file.
|
|
*
|
|
* Contains handlers for navigation and widget area.
|
|
*/
|
|
|
|
( function( $ ) {
|
|
var $body, $window, $sidebar, adminbarOffset, top = false,
|
|
bottom = false, windowWidth, windowHeight, lastWindowPos = 0,
|
|
topOffset = 0, bodyHeight, sidebarHeight, resizeTimer,
|
|
secondary, button;
|
|
|
|
|
|
function initMainNavigation() {
|
|
// Add dropdown toggle that display child menu items.
|
|
$( '.main-navigation .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
|
|
|
|
// Toggle buttons and submenu items with active children menu items.
|
|
$( '.main-navigation .current-menu-ancestor > button' ).addClass( 'toggle-on' );
|
|
$( '.main-navigation .current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
|
|
|
|
$( '.dropdown-toggle' ).click( function( e ) {
|
|
var _this = $( this );
|
|
e.preventDefault();
|
|
_this.toggleClass( 'toggle-on' );
|
|
_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
|
|
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
|
|
_this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
|
|
} );
|
|
}
|
|
initMainNavigation();
|
|
$( document ).on( 'customize-preview-menu-refreshed', initMainNavigation );
|
|
|
|
secondary = $( '#secondary' );
|
|
button = $( '.site-branding' ).find( '.secondary-toggle' );
|
|
|
|
// Enable menu toggle for small screens.
|
|
( function() {
|
|
var menu, widgets, social;
|
|
if ( ! secondary || ! button ) {
|
|
return;
|
|
}
|
|
|
|
// Hide button if there are no widgets and the menus are missing or empty.
|
|
menu = secondary.find( '.nav-menu' );
|
|
widgets = secondary.find( '#widget-area' );
|
|
social = secondary.find( '#social-navigation' );
|
|
if ( ! widgets.length && ! social.length && ( ! menu || ! menu.children().length ) ) {
|
|
button.hide();
|
|
return;
|
|
}
|
|
|
|
button.on( 'click.twentyfifteen', function() {
|
|
secondary.toggleClass( 'toggled-on' );
|
|
secondary.trigger( 'resize' );
|
|
$( this ).toggleClass( 'toggled-on' );
|
|
if ( $( this, secondary ).hasClass( 'toggled-on' ) ) {
|
|
$( this ).attr( 'aria-expanded', 'true' );
|
|
secondary.attr( 'aria-expanded', 'true' );
|
|
} else {
|
|
$( this ).attr( 'aria-expanded', 'false' );
|
|
secondary.attr( 'aria-expanded', 'false' );
|
|
}
|
|
} );
|
|
} )();
|
|
|
|
/**
|
|
* @summary Add or remove ARIA attributes.
|
|
* Uses jQuery's width() function to determine the size of the window and add
|
|
* the default ARIA attributes for the menu toggle if it's visible.
|
|
* @since Twenty Fifteen 1.1
|
|
*/
|
|
function onResizeARIA() {
|
|
if ( 955 > $window.width() ) {
|
|
button.attr( 'aria-expanded', 'false' );
|
|
secondary.attr( 'aria-expanded', 'false' );
|
|
button.attr( 'aria-controls', 'secondary' );
|
|
} else {
|
|
button.removeAttr( 'aria-expanded' );
|
|
secondary.removeAttr( 'aria-expanded' );
|
|
button.removeAttr( 'aria-controls' );
|
|
}
|
|
}
|
|
|
|
// Sidebar scrolling.
|
|
function resize() {
|
|
windowWidth = $window.width();
|
|
|
|
if ( 955 > windowWidth ) {
|
|
top = bottom = false;
|
|
$sidebar.removeAttr( 'style' );
|
|
}
|
|
}
|
|
|
|
function scroll() {
|
|
var windowPos = $window.scrollTop();
|
|
|
|
if ( 955 > windowWidth ) {
|
|
return;
|
|
}
|
|
|
|
sidebarHeight = $sidebar.height();
|
|
windowHeight = $window.height();
|
|
bodyHeight = $body.height();
|
|
|
|
if ( sidebarHeight + adminbarOffset > windowHeight ) {
|
|
if ( windowPos > lastWindowPos ) {
|
|
if ( top ) {
|
|
top = false;
|
|
topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
|
|
$sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
|
|
} else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top && sidebarHeight + adminbarOffset < bodyHeight ) {
|
|
bottom = true;
|
|
$sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
|
|
}
|
|
} else if ( windowPos < lastWindowPos ) {
|
|
if ( bottom ) {
|
|
bottom = false;
|
|
topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
|
|
$sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
|
|
} else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) {
|
|
top = true;
|
|
$sidebar.attr( 'style', 'position: fixed;' );
|
|
}
|
|
} else {
|
|
top = bottom = false;
|
|
topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
|
|
$sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
|
|
}
|
|
} else if ( ! top ) {
|
|
top = true;
|
|
$sidebar.attr( 'style', 'position: fixed;' );
|
|
}
|
|
|
|
lastWindowPos = windowPos;
|
|
}
|
|
|
|
function resizeAndScroll() {
|
|
resize();
|
|
scroll();
|
|
}
|
|
|
|
$( document ).ready( function() {
|
|
$body = $( document.body );
|
|
$window = $( window );
|
|
$sidebar = $( '#sidebar' ).first();
|
|
adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
|
|
|
|
$window
|
|
.on( 'scroll.twentyfifteen', scroll )
|
|
.on( 'load.twentyfifteen', onResizeARIA )
|
|
.on( 'resize.twentyfifteen', function() {
|
|
clearTimeout( resizeTimer );
|
|
resizeTimer = setTimeout( resizeAndScroll, 500 );
|
|
onResizeARIA();
|
|
} );
|
|
$sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
|
|
|
|
resizeAndScroll();
|
|
|
|
for ( var i = 1; i < 6; i++ ) {
|
|
setTimeout( resizeAndScroll, 100 * i );
|
|
}
|
|
} );
|
|
|
|
} )( jQuery );
|