2012-09-03 17:45:28 +00:00
|
|
|
/**
|
|
|
|
* navigation.js
|
|
|
|
*
|
|
|
|
* Handles toggling the navigation menu for small screens.
|
|
|
|
*/
|
|
|
|
( function() {
|
2013-06-25 08:45:52 +00:00
|
|
|
var container, button, menu;
|
2012-09-03 17:45:28 +00:00
|
|
|
|
2013-07-14 06:39:03 +00:00
|
|
|
container = document.getElementById( 'site-navigation' );
|
2014-11-06 01:37:48 +00:00
|
|
|
if ( ! container ) {
|
2013-06-17 03:29:17 +00:00
|
|
|
return;
|
2014-11-06 01:37:48 +00:00
|
|
|
}
|
2012-09-03 17:45:28 +00:00
|
|
|
|
2014-04-13 09:02:17 +00:00
|
|
|
button = container.getElementsByTagName( 'button' )[0];
|
2014-11-06 01:37:48 +00:00
|
|
|
if ( 'undefined' === typeof button ) {
|
2013-06-17 03:29:17 +00:00
|
|
|
return;
|
2014-11-06 01:37:48 +00:00
|
|
|
}
|
2012-09-03 17:45:28 +00:00
|
|
|
|
2013-06-17 03:29:17 +00:00
|
|
|
menu = container.getElementsByTagName( 'ul' )[0];
|
2012-09-03 17:45:28 +00:00
|
|
|
|
2013-06-17 03:29:17 +00:00
|
|
|
// Hide menu toggle button if menu is empty and return early.
|
2013-07-14 06:38:08 +00:00
|
|
|
if ( 'undefined' === typeof menu ) {
|
2012-09-03 17:45:28 +00:00
|
|
|
button.style.display = 'none';
|
2013-06-17 03:29:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-06 01:37:48 +00:00
|
|
|
menu.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
|
|
|
|
if ( -1 === menu.className.indexOf( 'nav-menu' ) ) {
|
2013-06-25 08:45:52 +00:00
|
|
|
menu.className += ' nav-menu';
|
2014-11-06 01:37:48 +00:00
|
|
|
}
|
2013-06-25 08:45:52 +00:00
|
|
|
|
2013-06-17 03:29:17 +00:00
|
|
|
button.onclick = function() {
|
2014-11-06 01:37:48 +00:00
|
|
|
if ( -1 !== container.className.indexOf( 'toggled' ) ) {
|
2013-06-17 03:29:17 +00:00
|
|
|
container.className = container.className.replace( ' toggled', '' );
|
2014-11-06 01:37:48 +00:00
|
|
|
button.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
menu.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
} else {
|
2013-06-17 03:29:17 +00:00
|
|
|
container.className += ' toggled';
|
2014-11-06 01:37:48 +00:00
|
|
|
button.setAttribute( 'aria-expanded', 'true' );
|
|
|
|
menu.setAttribute( 'aria-expanded', 'true' );
|
|
|
|
}
|
2013-06-17 03:29:17 +00:00
|
|
|
};
|
2014-12-17 20:03:17 +00:00
|
|
|
|
|
|
|
// Fix child menus for touch devices.
|
|
|
|
function fixMenuTouchTaps( container ) {
|
|
|
|
var touchStartFn,
|
|
|
|
parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
|
|
|
|
|
|
|
|
if ( 'ontouchstart' in window ) {
|
|
|
|
touchStartFn = function( e ) {
|
|
|
|
var menuItem = this.parentNode;
|
|
|
|
|
|
|
|
if ( ! menuItem.classList.contains( 'focus' ) ) {
|
|
|
|
e.preventDefault();
|
|
|
|
for( var i = 0; i < menuItem.parentNode.children.length; ++i ) {
|
|
|
|
if ( menuItem === menuItem.parentNode.children[i] ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
menuItem.parentNode.children[i].classList.remove( 'focus' );
|
|
|
|
}
|
|
|
|
menuItem.classList.add( 'focus' );
|
|
|
|
} else {
|
|
|
|
menuItem.classList.remove( 'focus' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for ( var i = 0; i < parentLink.length; ++i ) {
|
|
|
|
parentLink[i].addEventListener( 'touchstart', touchStartFn, false )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fixMenuTouchTaps( container );
|
2013-07-14 06:38:08 +00:00
|
|
|
} )();
|