2012-09-03 17:45:28 +00:00
|
|
|
/**
|
2015-12-13 14:03:43 +00:00
|
|
|
* File navigation.js.
|
2012-09-03 17:45:28 +00:00
|
|
|
*
|
2016-02-26 15:05:46 +00:00
|
|
|
* Handles toggling the navigation menu for small screens and enables TAB key
|
|
|
|
* navigation support for dropdown menus.
|
2012-09-03 17:45:28 +00:00
|
|
|
*/
|
|
|
|
( function() {
|
2016-11-26 17:35:00 +00:00
|
|
|
var container, button, menu, links, i, len;
|
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-06-30 22:19:39 +00:00
|
|
|
|
2020-04-10 02:04:28 +00:00
|
|
|
// Close small menu when user clicks outside
|
|
|
|
document.addEventListener( 'click', function( event ) {
|
|
|
|
var isClickInside = container.contains( event.target );
|
2019-11-08 08:14:40 +00:00
|
|
|
|
2020-04-10 02:04:28 +00:00
|
|
|
if ( ! isClickInside ) {
|
|
|
|
container.className = container.className.replace( ' toggled', '' );
|
|
|
|
button.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
menu.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
}
|
|
|
|
} );
|
2019-11-08 08:14:40 +00:00
|
|
|
|
2014-06-30 22:19:39 +00:00
|
|
|
// Get all the link elements within the menu.
|
2020-04-05 20:49:27 +00:00
|
|
|
links = menu.getElementsByTagName( 'a' );
|
2014-06-30 22:19:39 +00:00
|
|
|
|
|
|
|
// Each time a menu link is focused or blurred, toggle focus.
|
|
|
|
for ( i = 0, len = links.length; i < len; i++ ) {
|
|
|
|
links[i].addEventListener( 'focus', toggleFocus, true );
|
|
|
|
links[i].addEventListener( 'blur', toggleFocus, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets or removes .focus class on an element.
|
|
|
|
*/
|
|
|
|
function toggleFocus() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Move up through the ancestors of the current link until we hit .nav-menu.
|
|
|
|
while ( -1 === self.className.indexOf( 'nav-menu' ) ) {
|
|
|
|
// On li elements toggle the class .focus.
|
|
|
|
if ( 'li' === self.tagName.toLowerCase() ) {
|
|
|
|
if ( -1 !== self.className.indexOf( 'focus' ) ) {
|
|
|
|
self.className = self.className.replace( ' focus', '' );
|
|
|
|
} else {
|
|
|
|
self.className += ' focus';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self = self.parentElement;
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 11:13:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles `focus` class to allow submenu access on tablets.
|
|
|
|
*/
|
2020-04-05 20:49:27 +00:00
|
|
|
( function() {
|
|
|
|
var touchStartFn,
|
2016-01-13 11:13:59 +00:00
|
|
|
parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
|
|
|
|
|
|
|
|
if ( 'ontouchstart' in window ) {
|
|
|
|
touchStartFn = function( e ) {
|
2020-04-05 20:49:27 +00:00
|
|
|
var menuItem = this.parentNode;
|
2016-01-13 11:13:59 +00:00
|
|
|
|
|
|
|
if ( ! menuItem.classList.contains( 'focus' ) ) {
|
|
|
|
e.preventDefault();
|
2016-05-26 21:47:24 +00:00
|
|
|
for ( i = 0; i < menuItem.parentNode.children.length; ++i ) {
|
2016-01-13 11:13:59 +00:00
|
|
|
if ( menuItem === menuItem.parentNode.children[i] ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
menuItem.parentNode.children[i].classList.remove( 'focus' );
|
|
|
|
}
|
|
|
|
menuItem.classList.add( 'focus' );
|
|
|
|
} else {
|
|
|
|
menuItem.classList.remove( 'focus' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-26 21:47:24 +00:00
|
|
|
for ( i = 0; i < parentLink.length; ++i ) {
|
2016-01-13 11:13:59 +00:00
|
|
|
parentLink[i].addEventListener( 'touchstart', touchStartFn, false );
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 21:47:24 +00:00
|
|
|
}( container ) );
|
2020-04-05 20:49:27 +00:00
|
|
|
}() );
|