2021-12-20 13:42:43 +00:00
|
|
|
( function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if ( 'querySelector' in document && 'addEventListener' in window ) {
|
|
|
|
var body = document.body,
|
|
|
|
i;
|
2024-10-09 12:47:30 +00:00
|
|
|
|
2021-12-20 13:42:43 +00:00
|
|
|
/**
|
|
|
|
* Dropdown click
|
|
|
|
*
|
2024-10-09 12:47:30 +00:00
|
|
|
* @param {Object} e The event.
|
2021-12-20 13:42:43 +00:00
|
|
|
* @param {Object} _this The clicked item.
|
|
|
|
*/
|
|
|
|
var dropdownClick = function( e, _this ) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if ( ! _this ) {
|
|
|
|
_this = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
var closestLi = _this.closest( 'li' );
|
|
|
|
|
|
|
|
// Close other sub-menus
|
|
|
|
var openedSubMenus = _this.closest( 'nav' ).querySelectorAll( 'ul.toggled-on' );
|
|
|
|
if ( openedSubMenus && ! _this.closest( 'ul' ).classList.contains( 'toggled-on' ) && ! _this.closest( 'li' ).classList.contains( 'sfHover' ) ) {
|
|
|
|
for ( var o = 0; o < openedSubMenus.length; o++ ) {
|
|
|
|
openedSubMenus[ o ].classList.remove( 'toggled-on' );
|
|
|
|
openedSubMenus[ o ].closest( 'li' ).classList.remove( 'sfHover' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add sfHover class to parent li
|
|
|
|
closestLi.classList.toggle( 'sfHover' );
|
|
|
|
|
2024-10-09 12:47:30 +00:00
|
|
|
if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
|
|
|
|
// Set aria-expanded on arrow
|
|
|
|
var dropdownToggle = closestLi.querySelector( '.dropdown-menu-toggle' );
|
|
|
|
if ( 'false' === dropdownToggle.getAttribute( 'aria-expanded' ) || ! dropdownToggle.getAttribute( 'aria-expanded' ) ) {
|
|
|
|
dropdownToggle.setAttribute( 'aria-expanded', 'true' );
|
|
|
|
} else {
|
|
|
|
dropdownToggle.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
}
|
2021-12-20 13:42:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 12:47:30 +00:00
|
|
|
if ( body.classList.contains( 'dropdown-click-menu-item' ) && _this.tagName && 'A' === _this.tagName.toUpperCase() ) {
|
|
|
|
if ( 'false' === _this.getAttribute( 'aria-expanded' ) || ! _this.getAttribute( 'aria-expanded' ) ) {
|
|
|
|
_this.setAttribute( 'aria-expanded', 'true' );
|
|
|
|
_this.setAttribute( 'aria-label', generatepressDropdownClick.closeSubMenuLabel );
|
|
|
|
} else {
|
|
|
|
_this.setAttribute( 'aria-expanded', 'false' );
|
|
|
|
_this.setAttribute( 'aria-label', generatepressDropdownClick.openSubMenuLabel );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var subMenuSelector = '.children';
|
|
|
|
|
2021-12-20 13:42:43 +00:00
|
|
|
if ( closestLi.querySelector( '.sub-menu' ) ) {
|
2024-10-09 12:47:30 +00:00
|
|
|
subMenuSelector = '.sub-menu';
|
2021-12-20 13:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open the sub-menu
|
|
|
|
if ( body.classList.contains( 'dropdown-click-menu-item' ) ) {
|
|
|
|
_this.parentNode.querySelector( subMenuSelector ).classList.toggle( 'toggled-on' );
|
|
|
|
} else if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
|
|
|
|
closestLi.querySelector( subMenuSelector ).classList.toggle( 'toggled-on' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do stuff if click dropdown if enabled
|
|
|
|
var parentElementLinks = document.querySelectorAll( '.main-nav .menu-item-has-children > a' );
|
|
|
|
|
|
|
|
// Open the sub-menu by clicking on the entire link element
|
|
|
|
if ( body.classList.contains( 'dropdown-click-menu-item' ) ) {
|
|
|
|
for ( i = 0; i < parentElementLinks.length; i++ ) {
|
|
|
|
parentElementLinks[ i ].addEventListener( 'click', dropdownClick, true );
|
2024-10-09 12:47:30 +00:00
|
|
|
|
|
|
|
parentElementLinks[ i ].addEventListener( 'keydown', function( e ) {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
if ( 'Enter' === e.key || ' ' === e.key ) {
|
|
|
|
e.preventDefault();
|
|
|
|
dropdownClick( e, _this );
|
|
|
|
}
|
|
|
|
}, false );
|
2021-12-20 13:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the sub-menu by clicking on a dropdown arrow
|
|
|
|
if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
|
|
|
|
// Add a class to sub-menu items that are set to #
|
2024-10-09 12:47:30 +00:00
|
|
|
for ( i = 0; i < parentElementLinks.length; i++ ) {
|
|
|
|
if ( '#' === parentElementLinks[ i ].getAttribute( 'href' ) ) {
|
|
|
|
parentElementLinks[ i ].classList.add( 'menu-item-dropdown-click' );
|
2021-12-20 13:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var dropdownToggleLinks = document.querySelectorAll( '.main-nav .menu-item-has-children > a .dropdown-menu-toggle' );
|
|
|
|
for ( i = 0; i < dropdownToggleLinks.length; i++ ) {
|
|
|
|
dropdownToggleLinks[ i ].addEventListener( 'click', dropdownClick, false );
|
|
|
|
|
|
|
|
dropdownToggleLinks[ i ].addEventListener( 'keydown', function( e ) {
|
|
|
|
var _this = this;
|
|
|
|
|
2024-10-09 12:47:30 +00:00
|
|
|
if ( 'Enter' === e.key || ' ' === e.key ) {
|
|
|
|
e.preventDefault();
|
2021-12-20 13:42:43 +00:00
|
|
|
dropdownClick( e, _this );
|
|
|
|
}
|
|
|
|
}, false );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:47:30 +00:00
|
|
|
const menuItemDropdownClick = document.querySelectorAll( '.main-nav .menu-item-has-children > a.menu-item-dropdown-click' );
|
|
|
|
|
|
|
|
for ( i = 0; i < menuItemDropdownClick.length; i++ ) {
|
|
|
|
menuItemDropdownClick[ i ].addEventListener( 'click', dropdownClick, false );
|
|
|
|
|
|
|
|
menuItemDropdownClick[ i ].addEventListener( 'keydown', function( e ) {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
if ( 'Enter' === e.key || ' ' === e.key ) {
|
|
|
|
e.preventDefault();
|
|
|
|
dropdownClick( e, _this );
|
|
|
|
}
|
|
|
|
}, false );
|
2021-12-20 13:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeSubMenus = function() {
|
|
|
|
if ( document.querySelector( 'nav ul .toggled-on' ) ) {
|
|
|
|
var activeSubMenus = document.querySelectorAll( 'nav ul .toggled-on' );
|
2024-10-09 12:47:30 +00:00
|
|
|
|
2021-12-20 13:42:43 +00:00
|
|
|
for ( i = 0; i < activeSubMenus.length; i++ ) {
|
|
|
|
activeSubMenus[ i ].classList.remove( 'toggled-on' );
|
|
|
|
activeSubMenus[ i ].closest( '.sfHover' ).classList.remove( 'sfHover' );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:47:30 +00:00
|
|
|
if ( body.classList.contains( 'dropdown-click-arrow' ) ) {
|
|
|
|
var activeDropdownToggles = document.querySelectorAll( 'nav .dropdown-menu-toggle' );
|
|
|
|
|
|
|
|
for ( i = 0; i < activeDropdownToggles.length; i++ ) {
|
|
|
|
activeDropdownToggles[ i ].setAttribute( 'aria-expanded', 'false' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( body.classList.contains( 'dropdown-click-menu-item' ) ) {
|
|
|
|
var activeDropdownLinks = document.querySelectorAll( 'nav .menu-item-has-children > a' );
|
|
|
|
|
|
|
|
for ( i = 0; i < activeDropdownLinks.length; i++ ) {
|
|
|
|
activeDropdownLinks[ i ].setAttribute( 'aria-expanded', 'false' );
|
|
|
|
activeDropdownLinks[ i ].setAttribute( 'aria-label', generatepressDropdownClick.openSubMenuLabel );
|
|
|
|
}
|
2021-12-20 13:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Close sub-menus when clicking elsewhere
|
|
|
|
document.addEventListener( 'click', function( event ) {
|
|
|
|
if ( ! event.target.closest( '.sfHover' ) ) {
|
|
|
|
closeSubMenus();
|
|
|
|
}
|
|
|
|
}, false );
|
|
|
|
|
|
|
|
// Close sub-menus on escape key
|
|
|
|
document.addEventListener( 'keydown', function( e ) {
|
|
|
|
if ( 'Escape' === e.key ) { // 27 is esc
|
|
|
|
closeSubMenus();
|
|
|
|
}
|
|
|
|
}, false );
|
|
|
|
}
|
|
|
|
}() );
|