|
13 | 13 | if (!menuPanel) return
|
14 | 14 | var nav = navContainer.querySelector('.nav')
|
15 | 15 |
|
16 |
| - var currentPageItem = menuPanel.querySelector('.is-current-page') |
17 |
| - var originalPageItem = currentPageItem |
| 16 | + var currentPageItem = findItemForHash() || menuPanel.querySelector('.is-current-url') |
18 | 17 | if (currentPageItem) {
|
19 | 18 | activateCurrentPath(currentPageItem)
|
20 | 19 | scrollItemToMidpoint(menuPanel, currentPageItem)
|
|
28 | 27 | collapse ? btn.parentElement.classList.remove('is-active') : btn.parentElement.classList.add('is-active')
|
29 | 28 | })
|
30 | 29 | if (currentPageItem) {
|
31 |
| - if (collapse) activateCurrentPath(currentPageItem, false) |
| 30 | + if (collapse) activateCurrentPath(currentPageItem) |
32 | 31 | scrollItemToMidpoint(menuPanel, currentPageItem)
|
33 | 32 | } else if (collapse) {
|
34 | 33 | menuPanel.scrollTop = 0
|
|
56 | 55 | })
|
57 | 56 |
|
58 | 57 | 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 |
85 | 60 | find(menuPanel, '.nav-item.is-active').forEach(function (el) {
|
86 | 61 | el.classList.remove('is-current-path', 'is-current-page', 'is-active')
|
87 | 62 | })
|
88 |
| - ;(currentPageItem = navItem).classList.add('is-current-page') |
89 |
| - activateCurrentPath(currentPageItem) |
| 63 | + activateCurrentPath((currentPageItem = navItem)) |
90 | 64 | scrollItemToMidpoint(menuPanel, currentPageItem)
|
91 | 65 | }
|
92 | 66 |
|
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) |
97 | 68 |
|
98 |
| - function activateCurrentPath (navItem, trace) { |
99 |
| - var ancestorClassList |
| 69 | + function activateCurrentPath (navItem) { |
100 | 70 | var ancestor = navItem
|
101 | 71 | 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') |
105 | 73 | }
|
106 |
| - navItem.classList.add('is-active') |
| 74 | + navItem.classList.add('is-current-page', 'is-active') |
107 | 75 | }
|
108 | 76 |
|
109 | 77 | function toggleActive () {
|
|
146 | 114 | e.stopPropagation()
|
147 | 115 | }
|
148 | 116 |
|
| 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 | + |
149 | 135 | function scrollItemToMidpoint (panel, item) {
|
150 | 136 | if (panel.scrollHeight === panel.clientHeight) return // not scrollable
|
151 | 137 | var panelRect = panel.getBoundingClientRect()
|
|
0 commit comments