Skip to content

Commit 37a25ca

Browse files
authored
activate path of current page in nav using template instead of JavaScript (PR #72)
1 parent eac6a50 commit 37a25ca

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/helpers/traceCurrentPath.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
module.exports = (
4+
navigation,
5+
{
6+
data: {
7+
root: { page },
8+
},
9+
}
10+
) => {
11+
traceCurrentPathInternal(navigation, page.url)
12+
return navigation
13+
}
14+
15+
function traceCurrentPathInternal (items, currentPageUrl, path = []) {
16+
for (const item of items) {
17+
if (item.url === currentPageUrl) {
18+
for (const ancestor of path) ancestor.current = 'path'
19+
item.current = 'page'
20+
return true
21+
}
22+
if (item.items && traceCurrentPathInternal(item.items, currentPageUrl, path.concat(item))) return true
23+
}
24+
}

src/js/01-nav.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
var currentPageItem = menuPanel.querySelector('.is-current-page')
1717
var originalPageItem = currentPageItem
1818
if (currentPageItem) {
19-
activateCurrentPath(currentPageItem)
19+
//activateCurrentPath(currentPageItem) // enable if not handled by template
2020
scrollItemToMidpoint(menuPanel, currentPageItem.querySelector('.nav-link'))
2121
} else {
2222
menuPanel.scrollTop = 0

src/partials/nav-menu.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{{#with @root.page.componentVersion}}
66
<h3 class="title"><a href="{{{relativize ./url}}}">{{./title}}</a></h3>
77
{{/with}}
8-
{{> nav-tree navigation=this}}
8+
{{> nav-tree navigation=(traceCurrentPath this)}}
99
</nav>
1010
</div>
1111
{{/with}}

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{{#with ./current}} is-current-{{{this}}} is-active{{/with}}" data-depth="{{or ../level 0}}">
55
{{#if ./content}}
66
{{#if ./items.length}}
77
<button class="nav-item-toggle"></button>

0 commit comments

Comments
 (0)