generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
230 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,218 @@ | ||
import component from '../../scripts/init.js'; | ||
import { blockBodyScroll } from '../../scripts/libs.js'; | ||
import Column from '../column/column.js'; | ||
|
||
export default class Navigation extends Column { | ||
static observedAttributes = ['data-icon', 'data-compact', 'data-justify']; | ||
static observedAttributes = ['data-menu-icon', 'data-item-icon', 'data-compact', ...Column.observedAttributes]; | ||
|
||
dependencies = ['icon', 'accordion']; | ||
|
||
createButton() { | ||
const button = document.createElement('button'); | ||
button.setAttribute('aria-label', 'Menu'); | ||
button.setAttribute('aria-expanded', 'false'); | ||
button.setAttribute('aria-controls', 'navigation'); | ||
button.setAttribute('aria-haspopup', 'true'); | ||
button.setAttribute('type', 'button'); | ||
button.setAttribute('tabindex', '0'); | ||
button.innerHTML = '<raqn-icon icon=menu></raqn-icon>'; | ||
button.addEventListener('click', () => { | ||
this.classList.toggle('active'); | ||
button.setAttribute('aria-expanded', this.classList.contains('active')); | ||
}); | ||
return button; | ||
} | ||
attributesValues = { | ||
all: { | ||
data: { | ||
menu: { | ||
icon: 'menu__close', | ||
}, | ||
item: { | ||
icon: 'chevron-right', | ||
}, | ||
}, | ||
}, | ||
m: { | ||
data: { | ||
compact: true, | ||
}, | ||
}, | ||
x: { | ||
data: { | ||
compact: true, | ||
}, | ||
}, | ||
xs: { | ||
data: { | ||
compact: true, | ||
}, | ||
}, | ||
}; | ||
|
||
ready() { | ||
setDefaults() { | ||
super.setDefaults(); | ||
this.active = {}; | ||
this.list = this.querySelector('ul'); | ||
this.isActive = false; | ||
this.navContentInit = false; | ||
this.navCompactedContentInit = false; | ||
} | ||
|
||
async ready() { | ||
this.navContent = this.querySelector('ul'); | ||
this.innerHTML = ''; | ||
this.navCompactedContent = this.navContent.cloneNode(true); // the clone need to be done before `this.navContent` is modified | ||
this.nav = document.createElement('nav'); | ||
this.nav.append(this.list); | ||
this.setAttribute('role', 'navigation'); | ||
this.compact = this.getAttribute('compact') === 'true' || false; | ||
this.icon = this.getAttribute('icon') || 'menu'; | ||
if (this.compact) { | ||
this.nav.append(this.createButton()); | ||
} | ||
this.isCompact = this.dataset.compact === 'true'; | ||
this.append(this.nav); | ||
this.setupClasses(this.list); | ||
if (this.compact) { | ||
this.addEventListener('click', (e) => this.activate(e)); | ||
this.nav.setAttribute('role', 'navigation'); | ||
this.nav.setAttribute('id', 'navigation'); | ||
|
||
if (this.isCompact) { | ||
await this.setupCompactedNav(); | ||
} else { | ||
this.setupNav(); | ||
} | ||
} | ||
|
||
setupNav() { | ||
if (!this.navContentInit) { | ||
this.navContentInit = true; | ||
this.setupClasses(this.navContent); | ||
} | ||
this.navButton?.remove(); | ||
this.nav.append(this.navContent); | ||
} | ||
|
||
createIcon(name = this.icon) { | ||
async setupCompactedNav() { | ||
if (!this.navCompactedContentInit) { | ||
this.navCompactedContentInit = true; | ||
await component.multiLoadAndDefine(['accordion', 'icon']); | ||
this.setupClasses(this.navCompactedContent, true); | ||
this.navCompactedContent.addEventListener('click', (e) => this.activate(e)); | ||
} | ||
|
||
this.prepend(this.createButton()); | ||
this.nav.append(this.navCompactedContent); | ||
} | ||
|
||
onAttributeCompactChanged({ oldValue, newValue }) { | ||
if (!this.initialized) return; | ||
if (oldValue === newValue) return; | ||
this.isCompact = newValue === 'true'; | ||
this.nav.innerHTML = ''; | ||
|
||
if (this.isCompact) { | ||
this.setupCompactedNav(); | ||
} else { | ||
if (this.navButton) { | ||
this.isActive = false; | ||
this.classList.remove('active'); | ||
this.navButton.removeAttribute('aria-expanded'); | ||
this.navIcon.dataset.active = this.isActive; | ||
this.closeAllLevels(); | ||
} | ||
this.setupNav(); | ||
} | ||
} | ||
|
||
onAttributeIconChanged({ newValue }) { | ||
if (!this.initialized) return; | ||
if (!this.isCompact) return; | ||
this.navIcon.dataset.icon = newValue; | ||
} | ||
|
||
createButton() { | ||
this.navButton = document.createElement('button'); | ||
this.navButton.setAttribute('aria-label', 'Menu'); | ||
this.navButton.setAttribute('aria-expanded', 'false'); | ||
this.navButton.setAttribute('aria-controls', 'navigation'); | ||
this.navButton.setAttribute('aria-haspopup', 'true'); | ||
this.navButton.setAttribute('type', 'button'); | ||
this.navButton.innerHTML = `<raqn-icon data-icon=${this.dataset.menuIcon}></raqn-icon>`; | ||
this.navIcon = this.navButton.querySelector('raqn-icon'); | ||
this.navButton.addEventListener('click', () => { | ||
this.isActive = !this.isActive; | ||
this.classList.toggle('active'); | ||
this.navButton.setAttribute('aria-expanded', this.isActive); | ||
this.navIcon.dataset.active = this.isActive; | ||
blockBodyScroll(this.isActive); | ||
this.closeAllLevels(); | ||
}); | ||
return this.navButton; | ||
} | ||
|
||
addIcon(elem) { | ||
const icon = document.createElement('raqn-icon'); | ||
icon.setAttribute('icon', name); | ||
return icon; | ||
icon.dataset.icon = this.dataset.itemIcon; | ||
elem.append(icon); | ||
} | ||
|
||
creaeteAccordion(replaceChildrenElement) { | ||
createAccordionOld(elem) { | ||
component.init({ | ||
componentName: 'accordion', | ||
targets: [elem], | ||
componentConfig: { | ||
addToTargetMethod: 'append', | ||
}, | ||
nestedComponentsConfig: { | ||
button: { active: false }, | ||
}, | ||
}); | ||
} | ||
|
||
createAccordion(replaceChildrenElement) { | ||
const accordion = document.createElement('raqn-accordion'); | ||
const children = Array.from(replaceChildrenElement.children); | ||
accordion.append(...children); | ||
accordion.append(...replaceChildrenElement.childNodes); | ||
replaceChildrenElement.append(accordion); | ||
} | ||
|
||
setupClasses(ul, level = 1) { | ||
setupClasses(ul, isCompact, level = 1) { | ||
const children = Array.from(ul.children); | ||
children.forEach((child) => { | ||
|
||
children.forEach(async (child) => { | ||
const hasChildren = child.querySelector('ul'); | ||
child.classList.add(`level-${level}`); | ||
child.dataset.level = level; | ||
|
||
if (hasChildren) { | ||
const anchor = child.querySelector('a'); | ||
if (this.compact) { | ||
this.creaeteAccordion(child); | ||
if (isCompact) { | ||
this.createAccordion(child); | ||
} else if (level === 1) { | ||
anchor.append(this.createIcon('chevron-right')); | ||
const anchor = child.querySelector('a'); | ||
|
||
this.addIcon(anchor); | ||
} | ||
child.classList.add('has-children'); | ||
this.setupClasses(hasChildren, level + 1); | ||
this.setupClasses(hasChildren, isCompact, level + 1); | ||
} | ||
}); | ||
} | ||
|
||
activate(e) { | ||
e.preventDefault(); | ||
if (e.target.tagName.toLowerCase() === 'a') { | ||
if (e.target.tagName.toLowerCase() === 'raqn-icon' || e.target.closest('raqn-icon')) { | ||
e.preventDefault(); | ||
|
||
const current = e.target.closest('li'); | ||
const { level } = current.dataset; | ||
if (this.active[level] && this.active[level] !== current) { | ||
this.active[level].classList.remove('active'); | ||
const currentLevel = Number(level); | ||
const activeLevel = Number(this.getAttribute('active')); | ||
const isCurrentLevel = this.active[currentLevel] && this.active[currentLevel] === current; | ||
const hasActiveChildren = currentLevel < activeLevel; | ||
|
||
if (!isCurrentLevel || hasActiveChildren) { | ||
const whileCurrentLevel = isCurrentLevel && hasActiveChildren ? currentLevel + 1 : currentLevel; | ||
this.closeLevels(activeLevel, whileCurrentLevel); | ||
} | ||
this.active[level] = current; | ||
this.setAttribute('active', level); | ||
this.active[level].classList.toggle('active'); | ||
|
||
this.setAttribute('active', isCurrentLevel ? Math.max(0, currentLevel - 1) || '' : currentLevel); | ||
this.active[currentLevel]?.classList.toggle('active'); | ||
this.active[currentLevel] = isCurrentLevel ? null : current; | ||
} | ||
} | ||
|
||
closeLevels(activeLevel, currentLevel = 1) { | ||
let whileCurrentLevel = currentLevel; | ||
while (whileCurrentLevel <= activeLevel) { | ||
this.active[whileCurrentLevel].classList.remove('active'); | ||
const accordion = this.active[whileCurrentLevel].querySelector('raqn-accordion'); | ||
const control = accordion.querySelector('.accordion-control'); | ||
accordion.toggleControl(control); | ||
this.active[whileCurrentLevel] = null; | ||
whileCurrentLevel += 1; | ||
} | ||
} | ||
|
||
closeAllLevels() { | ||
const activeLevel = Number(this.getAttribute('active')); | ||
if (activeLevel) { | ||
this.closeLevels(activeLevel); | ||
this.removeAttribute('active'); | ||
} | ||
} | ||
} |
Oops, something went wrong.