-
Notifications
You must be signed in to change notification settings - Fork 2
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
7 changed files
with
68 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,82 @@ | ||
//! hamburger-menu v0.5.2 ☰ https://github.com/center-key/hamburger-menu ☰ MIT License | ||
//! hamburger-menu v0.6.0 ☰ https://github.com/center-key/hamburger-menu ☰ MIT License | ||
|
||
const hamburgerMenu = { | ||
// <nav class=hamburger-menu> | ||
// <a class=hamburger href=#>☰</a> | ||
// <aside> | ||
// <ul> | ||
// <li><a href=.>Home</a></li> | ||
// <li><a href=page1.html>Page 1</a></li> | ||
// <li><a href=page2.html>Page 2</a></li> | ||
// <li><a href=page3.html>Page 3</a></li> | ||
// </ul> | ||
// </aside> | ||
// </nav> | ||
|
||
version: '0.5.2', | ||
version: '0.6.0', | ||
|
||
selectItem(event) { | ||
const item = $(event.target).closest('li'); | ||
item.closest('aside').find('li').removeClass('current'); | ||
item.addClass('current'); | ||
const nav = item.closest('.hamburger-menu').addClass('collapse-menu'); | ||
const eventRoutes = {}; | ||
selectItem(elem) { | ||
const menuItem = elem.closest('li'); | ||
const navMenu = menuItem.closest('.hamburger-menu'); | ||
const reset = (elem) => elem.classList.remove('current'); | ||
menuItem.closest('aside').querySelectorAll('li').forEach(reset); | ||
menuItem.classList.add('current'); | ||
navMenu.classList.add('collapse-menu'); | ||
navMenu.dataset.menuCollapsed = String(Date.now()); | ||
const restoreAllowExand = () => { | ||
nav.removeClass('collapse-menu'); | ||
console.log('restoreAllowExand'); | ||
$(globalThis.document).off(eventRoutes); | ||
navMenu.classList.remove('collapse-menu'); | ||
globalThis.document.removeEventListener('click', restoreAllowExand); | ||
globalThis.document.removeEventListener('mousemove', restoreAllowExand); | ||
}; | ||
const listen = () => { | ||
globalThis.document.addEventListener('click', restoreAllowExand); | ||
globalThis.document.addEventListener('mousemove', restoreAllowExand); | ||
}; | ||
eventRoutes.click = restoreAllowExand; | ||
eventRoutes.mousemove = restoreAllowExand; | ||
const afterCurrentClick = 100; | ||
globalThis.setTimeout(() => $(globalThis.document).on(eventRoutes), afterCurrentClick); | ||
globalThis.setTimeout(listen, afterCurrentClick); | ||
}, | ||
|
||
setup() { | ||
$(globalThis.document).on({ click: $.noop }); //workaround for sticky hover on mobile | ||
const nav = $('nav.hamburger-menu'); | ||
globalThis.document.addEventListener('click', () => {}); //workaround for sticky hover on mobile | ||
const navMenu = globalThis.document.querySelector('.hamburger-menu'); | ||
const aside = navMenu?.querySelector('aside'); | ||
const autoHighlightMultiPage = () => { | ||
const current = { | ||
url: new globalThis.URL(globalThis.location.href), | ||
path: globalThis.location.pathname.replace(/\/$/, ''), | ||
}; | ||
const isCurrent = (i, elem) => { | ||
const linkUrl = new globalThis.URL($(elem).attr('href'), current.url); | ||
return linkUrl.pathname.replace(/\/$/, '') === current.path; | ||
const currentUrl = new globalThis.URL(globalThis.location.href); | ||
const currentPath = globalThis.location.pathname.replace(/\/$/, ''); | ||
const setCurrent = (elem) => { | ||
const linkUrl = new globalThis.URL(elem.href, currentUrl); | ||
const isCurrent = linkUrl.pathname.replace(/\/$/, '') === currentPath; | ||
elem.parentElement.classList.add(isCurrent ? 'current' : 'other-page'); | ||
}; | ||
nav.find('li >a').filter(isCurrent).first().closest('li').addClass('current'); | ||
navMenu.querySelectorAll('li >a').forEach(setCurrent); | ||
}; | ||
const delegateSelectItem = (event) => { | ||
const elem = event.target.closest('.hamburger-menu li'); | ||
if (elem) | ||
hamburgerMenu.selectItem(elem); | ||
}; | ||
const autoHighlightSinglePageApp = () => | ||
nav.find('>aside li').on({ click: hamburgerMenu.selectItem }); | ||
globalThis.document.addEventListener('click', delegateSelectItem); | ||
const autoHighlight = () => { | ||
autoHighlightMultiPage(); | ||
autoHighlightSinglePageApp(); | ||
}; | ||
if (!nav.find('>aside').hasClass('disable-auto-highlight')) | ||
if (aside && !aside.classList.contains('disable-auto-highlight')) | ||
autoHighlight(); | ||
}, | ||
|
||
dom: { | ||
onReady(callback) { | ||
// Calls the specified function once the web page is loaded and ready. | ||
// Example (execute myApp.setup() as soon as the DOM is interactive): | ||
// hamburgerMenu.dom.onReady(myApp.setup); | ||
if (globalThis.document.readyState === 'complete') | ||
callback(); | ||
else | ||
globalThis.window.addEventListener('DOMContentLoaded', callback); | ||
}, | ||
}, | ||
|
||
}; | ||
|
||
$(hamburgerMenu.setup); | ||
hamburgerMenu.dom.onReady(hamburgerMenu.setup); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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