Skip to content

Commit e385671

Browse files
committed
add support for navigation entries that contain a fragment identifier
1 parent 8642f41 commit e385671

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

preview-src/ui-model.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
antoraVersion: '2.3.1'
2-
navMode: template # set value to client to generate navigation on the client
2+
navMode: client # set value to client to generate navigation on the client
33
site:
44
title: Couchbase Docs
55
url: http://localhost:5252
@@ -65,6 +65,8 @@ site:
6565
urlType: fragment
6666
- content: Monitoring Statistics
6767
url: /index.html
68+
- content: Glossaries
69+
url: /index.html#_glossaries
6870
- content: Monitoring XDCR Timestamp-based Conflict Resolution
6971
url: '#'
7072
urlType: fragment

src/js/01-nav.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
navTextEl.href = relativize(page.url, item.url)
219219
if (page.url === item.url) {
220220
navItemEl.classList.add('is-current-page')
221-
navTextEl.classList.add('is-current-page')
221+
navTextEl.classList.add('is-current-page', 'is-initial-page')
222222
}
223223
} else {
224224
navTextEl = createElement('span', 'menu_title menu_text')
@@ -247,6 +247,39 @@
247247
return parent.appendChild(navListEl)
248248
}
249249

250+
function onHashChange () {
251+
var navLink
252+
var hash = window.location.hash
253+
if (hash) {
254+
if (hash.indexOf('%')) hash = decodeURIComponent(hash)
255+
navLink = navContainer.querySelector('a.menu_link[href="' + hash + '"]')
256+
}
257+
if (!(navLink || (navLink = navContainer.querySelector('a.is-initial-page')))) return
258+
var currentPageLink = navContainer.querySelector('a.is-current-page')
259+
if (navLink === currentPageLink) return
260+
if (currentPageLink) toggleCurrentPath(navContainer, currentPageLink, 'clear')
261+
toggleCurrentPath(navContainer, navLink, 'activate')
262+
scrollItemToMidpoint(nav.querySelector('.components'), navLink)
263+
return true
264+
}
265+
266+
function toggleCurrentPath (container, navLink, operation) {
267+
navLink.classList[operation === 'clear' ? 'remove' : 'add']('is-current-page')
268+
var navItem = navLink.parentNode.parentNode
269+
var ancestor = navLink.parentNode
270+
while (ancestor !== container) {
271+
if (ancestor.tagName === 'LI') {
272+
var ancestorClassList = ancestor.classList
273+
if (ancestor === navItem) {
274+
ancestorClassList[operation === 'clear' ? 'remove' : 'add']('is-current-page')
275+
} else if (ancestorClassList.contains('is-parent')) {
276+
ancestorClassList[operation === 'clear' ? 'add' : 'remove']('closed')
277+
}
278+
}
279+
ancestor = ancestor.parentNode
280+
}
281+
}
282+
250283
function relativize (from, to) {
251284
if (!(from && to.charAt() === '/')) { return to }
252285
var hash = ''
@@ -316,7 +349,12 @@
316349

317350
var components = container.querySelector('.components')
318351

319-
scrollItemToMidpoint(components, container.querySelector('a.is-current-page'))
352+
let scrolled
353+
if (container.querySelector('a.menu_link[href^="#"]')) {
354+
window.location.hash && (scrolled = onHashChange())
355+
window.addEventListener('hashchange', onHashChange)
356+
}
357+
scrolled || scrollItemToMidpoint(components, container.querySelector('a.is-current-page'))
320358

321359
if (!components.classList.contains('is-revealed')) {
322360
find('a.is-current-page', container).forEach(function (currentPage) {

0 commit comments

Comments
 (0)