Skip to content

Commit db7841e

Browse files
authored
refactor the nav script (PR #74)
- use is-current-url class to mark nav item that matches current url instead of is-current-page - resolve item for hash before looking for is-current-url match - move logic to resolve nav item for hash to findNavItemForHash function - add is-current-page class in activateCurrentPath function - remove trace argument from activateCurrentPath - if fragment is not found, revert to bare page item, if found
1 parent 033557c commit db7841e

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src/js/01-nav.js

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
if (!menuPanel) return
1414
var nav = navContainer.querySelector('.nav')
1515

16-
var currentPageItem = menuPanel.querySelector('.is-current-page')
17-
var originalPageItem = currentPageItem
16+
var currentPageItem = findItemForHash() || menuPanel.querySelector('.is-current-url')
1817
if (currentPageItem) {
1918
activateCurrentPath(currentPageItem)
2019
scrollItemToMidpoint(menuPanel, currentPageItem)
@@ -28,7 +27,7 @@
2827
collapse ? btn.parentElement.classList.remove('is-active') : btn.parentElement.classList.add('is-active')
2928
})
3029
if (currentPageItem) {
31-
if (collapse) activateCurrentPath(currentPageItem, false)
30+
if (collapse) activateCurrentPath(currentPageItem)
3231
scrollItemToMidpoint(menuPanel, currentPageItem)
3332
} else if (collapse) {
3433
menuPanel.scrollTop = 0
@@ -56,54 +55,23 @@
5655
})
5756

5857
function onHashChange () {
59-
var navItem, navLink
60-
var hash = window.location.hash
61-
if (hash) {
62-
if (hash.indexOf('%')) hash = decodeURIComponent(hash)
63-
if (!(navLink = menuPanel.querySelector('.nav-link[href="' + hash + '"]'))) {
64-
var targetNode = document.getElementById(hash.slice(1))
65-
if (targetNode) {
66-
var current = targetNode
67-
var ceiling = document.querySelector('article.doc')
68-
while ((current = current.parentNode) && current !== ceiling) {
69-
var id = current.id
70-
// NOTE: look for section heading
71-
if (!id && (id = SECT_CLASS_RX.test(current.className))) id = (current.firstElementChild || {}).id
72-
if (id && (navLink = menuPanel.querySelector('.nav-link[href="#' + id + '"]'))) break
73-
}
74-
}
75-
}
76-
}
77-
if (navLink) {
78-
navItem = navLink.parentNode
79-
} else if (originalPageItem) {
80-
navLink = (navItem = originalPageItem).querySelector('.nav-link')
81-
} else {
82-
return
83-
}
84-
if (navItem === currentPageItem) return
58+
var navItem = findItemForHash() || menuPanel.querySelector('.is-current-url')
59+
if (!navItem || navItem === currentPageItem) return
8560
find(menuPanel, '.nav-item.is-active').forEach(function (el) {
8661
el.classList.remove('is-current-path', 'is-current-page', 'is-active')
8762
})
88-
;(currentPageItem = navItem).classList.add('is-current-page')
89-
activateCurrentPath(currentPageItem)
63+
activateCurrentPath((currentPageItem = navItem))
9064
scrollItemToMidpoint(menuPanel, currentPageItem)
9165
}
9266

93-
if (menuPanel.querySelector('.nav-link[href^="#"]')) {
94-
if (window.location.hash) onHashChange()
95-
window.addEventListener('hashchange', onHashChange)
96-
}
67+
if (menuPanel.querySelector('.nav-link[href^="#"]')) window.addEventListener('hashchange', onHashChange)
9768

98-
function activateCurrentPath (navItem, trace) {
99-
var ancestorClassList
69+
function activateCurrentPath (navItem) {
10070
var ancestor = navItem
10171
while ((ancestor = ancestor.parentNode) && ancestor !== menuPanel) {
102-
if (!(ancestorClassList = ancestor.classList).contains('nav-item')) continue
103-
if (trace !== false) ancestorClassList.add('is-current-path')
104-
ancestorClassList.add('is-active')
72+
if (ancestor.classList.contains('nav-item')) ancestor.classList.add('is-current-path', 'is-active')
10573
}
106-
navItem.classList.add('is-active')
74+
navItem.classList.add('is-current-page', 'is-active')
10775
}
10876

10977
function toggleActive () {
@@ -146,6 +114,24 @@
146114
e.stopPropagation()
147115
}
148116

117+
function findItemForHash () {
118+
var hash = window.location.hash
119+
if (!hash) return
120+
if (hash.indexOf('%')) hash = decodeURIComponent(hash)
121+
if (hash.indexOf('"')) hash = hash.replace(/(?=")/g, '\\')
122+
var navLink = menuPanel.querySelector('.nav-link[href="' + hash + '"]')
123+
if (navLink) return navLink.parentNode
124+
var target = document.getElementById(hash.slice(1))
125+
if (!target) return
126+
var scope = document.querySelector('article.doc')
127+
var ancestor = target
128+
while ((ancestor = ancestor.parentNode) && ancestor !== scope) {
129+
var id = ancestor.id
130+
if (!id) id = SECT_CLASS_RX.test(ancestor.className) && (ancestor.firstElementChild || {}).id
131+
if (id && (navLink = menuPanel.querySelector('.nav-link[href="#' + id + '"]'))) return navLink.parentNode
132+
}
133+
}
134+
149135
function scrollItemToMidpoint (panel, item) {
150136
if (panel.scrollHeight === panel.clientHeight) return // not scrollable
151137
var panelRect = panel.getBoundingClientRect()

src/partials/nav-tree.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{#if navigation.length}}
22
<ul class="nav-list">
33
{{#each navigation}}
4-
<li class="nav-item{{#if (eq ./url @root.page.url)}} is-current-page{{/if}}" data-depth="{{or ../level 0}}">
4+
<li class="nav-item{{#if (eq ./url @root.page.url)}} is-current-url{{/if}}" data-depth="{{or ../level 0}}">
55
{{#if ./content}}
66
{{#if ./items.length}}
77
<button class="nav-item-toggle"></button>

0 commit comments

Comments
 (0)