Skip to content

Commit 100445d

Browse files
committed
AV-54034 top nav highlighting WIP
This is more complicated than it looks, partly because the Nav data-structure is a bit odd (e.g. the Home icon isn't generated from the Home configuration for... reasons?) This involves grubbing around in the site keys, and is therefore dependent on magic globals that are created while running templating code... (e.g. it works, but will require some documentation or rework, as will be fun to debug in future)
1 parent 6e60753 commit 100445d

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

src/helpers/nav-group-selected.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
module.exports = module.exports = (navGroup, {
4+
data: {
5+
root: { page, site },
6+
},
7+
}) => {
8+
if (navGroup === 'home') {
9+
// FAKE value to signal that we instead check all the navgroups
10+
const navGroups = site.keys.navGroups
11+
// NB this must be called after nav-groups has already prepared the data (:scream:)
12+
const possible = navGroups.filter((it) => selected(it, page))
13+
return possible.length === 1 && possible[0].title === 'Home'
14+
} else {
15+
return selected(navGroup, page)
16+
}
17+
}
18+
19+
function selected (navGroup, page) {
20+
return (navGroup.url === page?.url) ||
21+
(navGroup.components?.includes(page.component?.name))
22+
}

src/helpers/nav-groups.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22

33
module.exports = ({
44
data: {
5-
root: { contentCatalog = { resolvePage: () => undefined }, site, page },
5+
root: { contentCatalog = { resolvePage: () => undefined }, site },
66
},
77
}) => {
88
let navGroups = site.keys.navGroups
9+
910
if (!navGroups) return []
1011
if (navGroups._compiled) return navGroups
12+
1113
const components = site.components
1214
const componentNames = Object.keys(components)
1315
const claimed = []
14-
navGroups = JSON.parse(navGroups).reduce((accum, navGroup) => {
15-
const componentNamesInGroup = (navGroup.components || []).reduce((matched, componentName) => {
16-
if (~componentName.indexOf('*')) {
17-
const rx = new RegExp(`^${componentName.replace(/[*]/g, '.*?')}$`)
18-
return matched.concat(componentNames.filter((candidate) => rx.test(candidate)))
19-
} else if (componentName in components) {
20-
return matched.concat(componentName)
21-
}
22-
return matched
23-
}, [])
16+
17+
navGroups = JSON.parse(navGroups).map((navGroup) => {
18+
const componentNamesInGroup =
19+
(navGroup.components || []).flatMap(
20+
(componentName) => componentNames.filter(globbify(componentName)))
21+
2422
claimed.push(...componentNamesInGroup)
25-
return accum.concat(compileNavGroup(navGroup, componentNamesInGroup, contentCatalog, components))
26-
}, [])
27-
const orphaned = componentNames.filter((it) => claimed.indexOf(it) < 0)
23+
24+
return compileNavGroup(
25+
navGroup,
26+
componentNamesInGroup,
27+
contentCatalog,
28+
components)
29+
})
30+
31+
const orphaned = componentNames.filter((it) => !claimed.includes(it))
32+
2833
if (orphaned.length) {
2934
const homeIdx = orphaned.indexOf('home')
3035
if (~homeIdx) {
@@ -41,25 +46,36 @@ module.exports = ({
4146
: navGroups.push(compileNavGroup({ title: 'General' }, orphaned, contentCatalog, components))
4247
}
4348
}
49+
4450
navGroups._compiled = true
4551
console.log(navGroups)
46-
console.log(page)
47-
return (site.keys.navGroups = navGroups)
52+
53+
site.keys.navGroups = navGroups
54+
return navGroups
4855
}
4956

5057
function compileNavGroup (navGroup, componentNamesInGroup, contentCatalog, components) {
51-
navGroup.components = componentNamesInGroup
5258
let startPage = navGroup.startPage
5359
if (startPage) {
5460
startPage = contentCatalog.resolvePage(startPage)
5561
if (startPage) navGroup.url = startPage.pub.url
5662
delete navGroup.startPage
5763
}
64+
65+
navGroup.components = componentNamesInGroup
5866
if (componentNamesInGroup.length) {
5967
navGroup.latestVersions = componentNamesInGroup.reduce((latestVersionMap, it) => {
6068
latestVersionMap[it] = components[it].latest.version
6169
return latestVersionMap
6270
}, {})
6371
}
72+
// if ((navGroup.url === page?.url) || componentNamesInGroup.includes(page.component.name)) {
73+
// navGroup.selected = true
74+
// }
6475
return navGroup
6576
}
77+
78+
function globbify (componentName) {
79+
const rx = new RegExp(`^${componentName.replace(/[*]/g, '.*?')}$`)
80+
return (it) => rx.test(it)
81+
}

src/partials/header-content.hbs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
</a>
1010
</li>
1111
<li>
12-
<a class="navbar-brand cb-documentation" href="{{#with (and site.url site.homeUrl)}}{{{@root.site.url}}}{{{this}}}{{else}}{{{siteRootPath}}}{{/with}}">
12+
<a class="navbar-brand cb-documentation"
13+
href="{{#if (and site.url site.homeUrl)}}{{{site.url}}}{{{site.homeUrl}}}{{else}}{{{siteRootPath}}}{{/if}}">
1314
<img src="{{{uiRootPath}}}/img/cb-documentation.svg" alt="Couchbase Documentation" class="cb-docs" />
1415
<img src="{{{uiRootPath}}}/img/cb-docs-hover.svg" alt="Couchbase Documentation" class="hide cb-hover-docs" />
1516
</a>
@@ -33,14 +34,16 @@
3334

3435
<div class="navbar-collapse collapse" id="navbar2">
3536
<ul class="navbar-nav w-100 justify-content-start">
36-
<li class="nav-item {{#if (eq "home" page.component.name)}}nav-item-selected{{/if}}"">
37-
<a href="{{#with (and site.url site.homeUrl)}}{{@root.site.url}}{{this}}{{else}}{{siteRootPath}}{{/with}}" class="nav-link">
37+
{{#with (nav-groups)}}
38+
39+
<li class="nav-item {{#if (nav-group-selected "home")}}nav-item-selected{{/if}}"">
40+
<a href="{{#if (and @root.site.url @root.site.homeUrl)}}{{@root.site.url}}{{@root.site.homeUrl}}{{else}}{{siteRootPath}}{{/if}}" class="nav-link">
3841
<i class="fas fa-home"></i>
3942
</a>
4043
</li>
41-
{{#each (nav-groups)}}
44+
{{#each this}}
4245
{{#if ./url}}
43-
<li class="nav-item {{#if (or (eq ./url @root.page.url) (includes ./components @root.page.component.name))}}nav-item-selected{{/if}}">
46+
<li class="nav-item {{#if (nav-group-selected this)}}nav-item-selected{{/if}}">
4447
<a class="nav-link" href="{{relativize ./url}}">
4548
{{{./title}}}
4649
@@ -53,6 +56,7 @@
5356
</li>
5457
{{/if}}
5558
{{/each}}
59+
{{/with}}
5660
</ul>
5761
</div>
5862
<div class="primary-action">

0 commit comments

Comments
 (0)