Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions build/media_source/mod_menu/js/menu.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
// Default settings for the Nav class
static defaultSettings = {
menuHoverClass: 'show-menu',
dir: 'ltr'
dir: 'ltr',
preventSubmenoOpenOnload: 'nav-active-open'
};

constructor(nav, settings = {}) {
Expand Down Expand Up @@ -79,6 +80,10 @@

nav.addEventListener('keydown', this.onMenuKeyDown.bind(this));
nav.addEventListener('click', this.onClick.bind(this));

if (this.nav.classList.contains(this.settings.preventSubmenoOpenOnload)) {
this.toggleAllForCurrentActive();
}
}

onMenuKeyDown(event) {
Expand Down Expand Up @@ -203,7 +208,7 @@
ulChild.setAttribute('aria-hidden', open ? 'false' : 'true');
ulChild.classList.toggle(this.settings.menuHoverClass, open);
});
target.querySelector(':scope > [aria-expanded]').setAttribute('aria-expanded', open ? 'true' : 'false');
target.querySelector(':scope > [aria-expanded]')?.setAttribute('aria-expanded', open ? 'true' : 'false');
}

focusTabbable(direction = 1) {
Expand Down Expand Up @@ -232,6 +237,22 @@
}
return currentLi; // top-level li or null if not found, or the
}

toggleAllForCurrentActive() {
const active = this.nav.querySelector('.current.active');
if (active) {
const $parentTopLevel = this.getTopLevelParentLi(active);
let currentLi = active;
while (currentLi && !Array.from(this.topLevelNodes).includes(currentLi)) {
const parentUl = currentLi.parentElement.closest('ul');
currentLi = parentUl ? parentUl.closest('li') : null;
if (currentLi) {
const subLists = currentLi.querySelectorAll('ul');
this.toggleSubMenu(currentLi, subLists, subLists[0]?.getAttribute('aria-hidden') === 'true');
}
}
}
}
}

// static idCounter for unique id generation of submenus
Expand Down