2020-10-21 10:41:33 +00:00
|
|
|
((Drupal) => {
|
Issue #3111409 by mherchel, proeung, larowlan, alexpott, lauriii, Gábor Hojtsy, andrewmacpherson, catch, xjm, andypost, rahulrasgon, rabbitlair, mrconnerton, poojakural, boulaffasae, jerseycheese, shimpy, Indrajith KB, DuneBL, kiran.kadam911, komalkolekar, keboca, hawkeye.twolf, andrewozone, Maithri Shetty, alexdmccabe, sd9121, Sreenivas Bttv, katannshaw, mgifford, nod_, webchick, mtift, rainbreaw, Berdir, brianperry, thejimbirch, Dom., q0rban, nitesh624, hansa11, himanshu_sindhwani, kostyashupenko, shaktik, bnjmnm, sharma.amitt16, sonam.chaturvedi, steinmb, shaal, ressa, Ramya Balasubramanian, Sebacic, pradeepjha, tanmaykadam, thedrupalkid, vinitk, volkswagenchick, viappidu, vebrovski, Ujval Shah, Webbeh, Yuri, trebormc, msuthars, mrinalini9, bash247, chetanbharambe, ellenoise, anevins, andriyun, CocoM, JayKandari, ambuj_gupta, hussainweb, jhodgdon, Pooja Ganjage, ju.vanderw, jwitkowski79, MaxPah, Lokender Singh2, jponch, KarenS, KarinG, Lal_: Add new Olivero frontend theme to Drupal 9.1 core as beta; later make it stable and the default
2020-10-16 09:43:55 +00:00
|
|
|
/**
|
|
|
|
* Checks if navWrapper contains "is-active" class.
|
|
|
|
* @param {object} navWrapper
|
|
|
|
* Header navigation.
|
|
|
|
* @return {boolean}
|
|
|
|
* True if navWrapper contains "is-active" class, false if not.
|
|
|
|
*/
|
|
|
|
function isNavOpen(navWrapper) {
|
|
|
|
return navWrapper.classList.contains('is-active');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens or closes the header navigation.
|
|
|
|
* @param {object} props
|
|
|
|
* Navigation props.
|
|
|
|
* @param {boolean} state
|
|
|
|
* State which to transition the header navigation menu into.
|
|
|
|
*/
|
|
|
|
function toggleNav(props, state) {
|
|
|
|
const value = !!state;
|
|
|
|
props.navButton.setAttribute('aria-expanded', value);
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
props.body.classList.add('js-overlay-active');
|
|
|
|
props.body.classList.add('js-fixed');
|
|
|
|
props.navWrapper.classList.add('is-active');
|
|
|
|
} else {
|
|
|
|
props.body.classList.remove('js-overlay-active');
|
|
|
|
props.body.classList.remove('js-fixed');
|
|
|
|
props.navWrapper.classList.remove('is-active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init function for header navigation.
|
|
|
|
* @param {object} props
|
|
|
|
* Navigation props.
|
|
|
|
*/
|
|
|
|
function init(props) {
|
|
|
|
props.navButton.setAttribute('aria-controls', props.navWrapperId);
|
|
|
|
props.navButton.setAttribute('aria-expanded', 'false');
|
|
|
|
|
|
|
|
props.navButton.addEventListener('click', () => {
|
|
|
|
toggleNav(props, !isNavOpen(props.navWrapper));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Closes any open sub navigation first, then close header navigation.
|
2020-10-21 10:41:33 +00:00
|
|
|
document.addEventListener('keyup', (e) => {
|
Issue #3111409 by mherchel, proeung, larowlan, alexpott, lauriii, Gábor Hojtsy, andrewmacpherson, catch, xjm, andypost, rahulrasgon, rabbitlair, mrconnerton, poojakural, boulaffasae, jerseycheese, shimpy, Indrajith KB, DuneBL, kiran.kadam911, komalkolekar, keboca, hawkeye.twolf, andrewozone, Maithri Shetty, alexdmccabe, sd9121, Sreenivas Bttv, katannshaw, mgifford, nod_, webchick, mtift, rainbreaw, Berdir, brianperry, thejimbirch, Dom., q0rban, nitesh624, hansa11, himanshu_sindhwani, kostyashupenko, shaktik, bnjmnm, sharma.amitt16, sonam.chaturvedi, steinmb, shaal, ressa, Ramya Balasubramanian, Sebacic, pradeepjha, tanmaykadam, thedrupalkid, vinitk, volkswagenchick, viappidu, vebrovski, Ujval Shah, Webbeh, Yuri, trebormc, msuthars, mrinalini9, bash247, chetanbharambe, ellenoise, anevins, andriyun, CocoM, JayKandari, ambuj_gupta, hussainweb, jhodgdon, Pooja Ganjage, ju.vanderw, jwitkowski79, MaxPah, Lokender Singh2, jponch, KarenS, KarinG, Lal_: Add new Olivero frontend theme to Drupal 9.1 core as beta; later make it stable and the default
2020-10-16 09:43:55 +00:00
|
|
|
if (e.key === 'Escape') {
|
|
|
|
if (props.olivero.areAnySubNavsOpen()) {
|
|
|
|
props.olivero.closeAllSubNav();
|
|
|
|
} else {
|
|
|
|
toggleNav(props, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
props.overlay.addEventListener('click', () => {
|
|
|
|
toggleNav(props, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
props.overlay.addEventListener('touchstart', () => {
|
|
|
|
toggleNav(props, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Focus trap.
|
2020-10-21 10:41:33 +00:00
|
|
|
props.navWrapper.addEventListener('keydown', (e) => {
|
Issue #3111409 by mherchel, proeung, larowlan, alexpott, lauriii, Gábor Hojtsy, andrewmacpherson, catch, xjm, andypost, rahulrasgon, rabbitlair, mrconnerton, poojakural, boulaffasae, jerseycheese, shimpy, Indrajith KB, DuneBL, kiran.kadam911, komalkolekar, keboca, hawkeye.twolf, andrewozone, Maithri Shetty, alexdmccabe, sd9121, Sreenivas Bttv, katannshaw, mgifford, nod_, webchick, mtift, rainbreaw, Berdir, brianperry, thejimbirch, Dom., q0rban, nitesh624, hansa11, himanshu_sindhwani, kostyashupenko, shaktik, bnjmnm, sharma.amitt16, sonam.chaturvedi, steinmb, shaal, ressa, Ramya Balasubramanian, Sebacic, pradeepjha, tanmaykadam, thedrupalkid, vinitk, volkswagenchick, viappidu, vebrovski, Ujval Shah, Webbeh, Yuri, trebormc, msuthars, mrinalini9, bash247, chetanbharambe, ellenoise, anevins, andriyun, CocoM, JayKandari, ambuj_gupta, hussainweb, jhodgdon, Pooja Ganjage, ju.vanderw, jwitkowski79, MaxPah, Lokender Singh2, jponch, KarenS, KarinG, Lal_: Add new Olivero frontend theme to Drupal 9.1 core as beta; later make it stable and the default
2020-10-16 09:43:55 +00:00
|
|
|
if (e.key === 'Tab') {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
if (
|
|
|
|
document.activeElement === props.firstFocusableEl &&
|
|
|
|
!props.olivero.isDesktopNav()
|
|
|
|
) {
|
|
|
|
props.navButton.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
document.activeElement === props.lastFocusableEl &&
|
|
|
|
!props.olivero.isDesktopNav()
|
|
|
|
) {
|
|
|
|
props.navButton.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove overlays when browser is resized and desktop nav appears.
|
|
|
|
// @todo Use core/drupal.debounce library to throttle when we move into theming.
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
if (props.olivero.isDesktopNav()) {
|
|
|
|
toggleNav(props, false);
|
|
|
|
props.body.classList.remove('js-overlay-active', 'js-fixed');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the navigation JS.
|
|
|
|
*/
|
|
|
|
Drupal.behaviors.oliveroNavigation = {
|
|
|
|
attach(context, settings) {
|
|
|
|
const navWrapperId = 'header-nav';
|
|
|
|
const navWrapper = context.querySelector(
|
|
|
|
`#${navWrapperId}:not(.${navWrapperId}-processed)`,
|
|
|
|
);
|
|
|
|
if (navWrapper) {
|
|
|
|
navWrapper.classList.add(`${navWrapperId}-processed`);
|
|
|
|
const olivero = Drupal.olivero;
|
|
|
|
const navButton = context.querySelector('.mobile-nav-button');
|
|
|
|
const body = context.querySelector('body');
|
|
|
|
const overlay = context.querySelector('.overlay');
|
|
|
|
const focusableNavElements = navWrapper.querySelectorAll(
|
|
|
|
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
|
|
|
|
);
|
|
|
|
const firstFocusableEl = focusableNavElements[0];
|
|
|
|
const lastFocusableEl =
|
|
|
|
focusableNavElements[focusableNavElements.length - 1];
|
|
|
|
|
|
|
|
init({
|
|
|
|
settings,
|
|
|
|
olivero,
|
|
|
|
navWrapperId,
|
|
|
|
navWrapper,
|
|
|
|
navButton,
|
|
|
|
body,
|
|
|
|
overlay,
|
|
|
|
firstFocusableEl,
|
|
|
|
lastFocusableEl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})(Drupal);
|