|
| 1 | +export class Nav { |
| 2 | + private targetItemSelector: string; |
| 3 | + |
| 4 | + constructor(menu: HTMLElement) { |
| 5 | + this.targetItemSelector = '.c-nav__item.has-children.has-toggle'; |
| 6 | + const selectorArray: string[] = [this.targetItemSelector, '> .c-nav__item-wrapper']; |
| 7 | + |
| 8 | + // Allow to click main item if vertical or extended dropdown |
| 9 | + if (menu.classList.contains('c-nav--vertical')) { |
| 10 | + selectorArray.push('.c-nav__toggle'); |
| 11 | + } |
| 12 | + |
| 13 | + // Extended dropdown open by clicking main items or subitems on the arrow. |
| 14 | + if (menu.classList.contains('c-nav--extended-dropdown')) { |
| 15 | + selectorArray.unshift(':scope ul'); |
| 16 | + selectorArray.push('.c-nav__toggle, :scope > li.has-toggle > .c-nav__item-wrapper'); |
| 17 | + } |
| 18 | + |
| 19 | + const items = [...menu.querySelectorAll(selectorArray.join(' '))] as HTMLElement[]; |
| 20 | + |
| 21 | + if (items.length > 0) { |
| 22 | + this.setListeners(items, menu); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + private setListeners(items: HTMLElement[], menu: HTMLElement) { |
| 27 | + items.forEach((item) => { |
| 28 | + item.addEventListener('click', (e) => { |
| 29 | + e.preventDefault(); |
| 30 | + e.stopPropagation(); |
| 31 | + if (menu.classList.contains('c-nav--horizontal')) { |
| 32 | + this.closeSiblings(item.closest(this.targetItemSelector) as HTMLElement); |
| 33 | + } |
| 34 | + |
| 35 | + this.toggleChildren(item.closest(this.targetItemSelector) as HTMLElement); |
| 36 | + }); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + private closeSiblings(clickItem: HTMLElement) { |
| 41 | + const items = this.getSiblings(clickItem); |
| 42 | + items.forEach((item) => { |
| 43 | + item.classList.remove('is-open'); |
| 44 | + }); |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + private toggleChildren(toggle: HTMLElement) { |
| 49 | + if (!toggle.classList.contains('is-open')) { |
| 50 | + this.openChildren(toggle); |
| 51 | + return true; |
| 52 | + } |
| 53 | + this.closeChildren(toggle); |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + private openChildren(toggle: HTMLElement) { |
| 58 | + toggle.classList.add('is-open'); |
| 59 | + toggle.querySelector('.c-nav__toggle')?.setAttribute('aria-pressed', 'true'); |
| 60 | + } |
| 61 | + |
| 62 | + private closeChildren(toggle: HTMLElement) { |
| 63 | + toggle.classList.remove('is-open'); |
| 64 | + toggle.querySelector('.c-nav__toggle')?.setAttribute('aria-pressed', 'false'); |
| 65 | + } |
| 66 | + |
| 67 | + private getSiblings(elem: HTMLElement) { |
| 68 | + const siblings: HTMLElement[] = []; |
| 69 | + let sibling = elem.parentNode?.firstChild; |
| 70 | + while (sibling) { |
| 71 | + if (sibling.nodeType === Node.ELEMENT_NODE && sibling !== elem) { |
| 72 | + siblings.push(sibling as HTMLElement); |
| 73 | + } |
| 74 | + sibling = sibling.nextSibling; |
| 75 | + } |
| 76 | + return siblings; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Initialize Nav components on DOMContentLoaded |
| 81 | +document.addEventListener('DOMContentLoaded', () => { |
| 82 | + const menus = [...document.querySelectorAll('.c-nav.c-nav--depth-1')] as HTMLElement[]; |
| 83 | + |
| 84 | + menus.forEach((menu) => { |
| 85 | + new Nav(menu); |
| 86 | + const observer = new MutationObserver((mutations) => { |
| 87 | + mutations.forEach((mutation) => { |
| 88 | + if (mutation.type === 'childList' && mutation.addedNodes.length > 0 && ((mutation.target as HTMLElement)?.classList?.contains('c-nav__item') || (mutation.target as HTMLElement)?.classList?.contains('c-nav__extended-content'))) { |
| 89 | + [...mutation.addedNodes].forEach((node) => { |
| 90 | + if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).classList?.contains('c-nav__child-container') && !(node as HTMLElement).querySelector('.c-nav.preloader')) { |
| 91 | + const element = (node as HTMLElement).classList.contains('c-nav') ? node : (node as HTMLElement).querySelector('.c-nav'); |
| 92 | + |
| 93 | + if (element) { |
| 94 | + new Nav(element as HTMLElement); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + }); |
| 100 | + }); |
| 101 | + observer.observe(menu, { childList: true, subtree: true }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments