|
13 | 13 | if (!menuPanel) return
|
14 | 14 | var nav = navContainer.querySelector('.nav')
|
15 | 15 |
|
16 |
| - var currentPageItem = findItemForHash() || menuPanel.querySelector('.is-current-url') |
17 |
| - if (currentPageItem) { |
18 |
| - activateCurrentPath(currentPageItem) |
19 |
| - scrollItemToMidpoint(menuPanel, currentPageItem) |
| 16 | + var currentPageItem |
| 17 | + if (menuPanel.classList.contains('is-loading')) { |
| 18 | + if ((currentPageItem = findItemForHash() || menuPanel.querySelector('.is-current-url'))) { |
| 19 | + activateCurrentPath(currentPageItem) |
| 20 | + scrollItemToMidpoint(menuPanel, currentPageItem) |
| 21 | + } else { |
| 22 | + menuPanel.scrollTop = 0 |
| 23 | + } |
| 24 | + menuPanel.classList.remove('is-loading') |
20 | 25 | } else {
|
21 |
| - menuPanel.scrollTop = 0 |
| 26 | + var match = (currentPageItem = menuPanel.querySelector('.is-current-page')) |
| 27 | + if ((!match || match.classList.contains('is-provisional')) && (match = findItemForHash(true))) { |
| 28 | + var update = !!currentPageItem |
| 29 | + activateCurrentPath((currentPageItem = match), update) |
| 30 | + scrollItemToMidpoint(menuPanel, currentPageItem) |
| 31 | + } |
22 | 32 | }
|
23 | 33 |
|
24 | 34 | menuPanel.querySelector('.nav-menu-toggle').addEventListener('click', function () {
|
|
29 | 39 | if (currentPageItem) {
|
30 | 40 | if (collapse) activateCurrentPath(currentPageItem)
|
31 | 41 | scrollItemToMidpoint(menuPanel, currentPageItem)
|
32 |
| - } else if (collapse) { |
| 42 | + } else { |
33 | 43 | menuPanel.scrollTop = 0
|
34 | 44 | }
|
35 | 45 | })
|
|
45 | 55 |
|
46 | 56 | nav.querySelector('[data-panel=explore] .context').addEventListener('click', function () {
|
47 | 57 | find(nav, '[data-panel]').forEach(function (panel) {
|
48 |
| - panel.classList.toggle('is-active') // NOTE logic assumes there are only two panels |
| 58 | + // NOTE logic assumes there are only two panels |
| 59 | + panel.classList.toggle('is-active') |
49 | 60 | })
|
50 | 61 | })
|
51 | 62 |
|
|
57 | 68 | function onHashChange () {
|
58 | 69 | var navItem = findItemForHash() || menuPanel.querySelector('.is-current-url')
|
59 | 70 | if (!navItem || navItem === currentPageItem) return
|
60 |
| - find(menuPanel, '.nav-item.is-active').forEach(function (el) { |
61 |
| - el.classList.remove('is-current-path', 'is-current-page', 'is-active') |
62 |
| - }) |
63 |
| - activateCurrentPath((currentPageItem = navItem)) |
| 71 | + activateCurrentPath((currentPageItem = navItem), true) |
64 | 72 | scrollItemToMidpoint(menuPanel, currentPageItem)
|
65 | 73 | }
|
66 | 74 |
|
67 | 75 | if (menuPanel.querySelector('.nav-link[href^="#"]')) window.addEventListener('hashchange', onHashChange)
|
68 | 76 |
|
69 |
| - function activateCurrentPath (navItem) { |
| 77 | + function activateCurrentPath (navItem, update) { |
| 78 | + if (update) { |
| 79 | + find(menuPanel, '.nav-item.is-active').forEach(function (el) { |
| 80 | + el.classList.remove('is-current-path', 'is-current-page', 'is-active') |
| 81 | + }) |
| 82 | + } |
70 | 83 | var ancestor = navItem
|
71 | 84 | while ((ancestor = ancestor.parentNode) && ancestor !== menuPanel) {
|
72 | 85 | if (ancestor.classList.contains('nav-item')) ancestor.classList.add('is-current-path', 'is-active')
|
|
114 | 127 | e.stopPropagation()
|
115 | 128 | }
|
116 | 129 |
|
117 |
| - function findItemForHash () { |
| 130 | + function findItemForHash (articleOnly) { |
118 | 131 | var hash = window.location.hash
|
119 | 132 | if (!hash) return
|
120 | 133 | if (hash.indexOf('%')) hash = decodeURIComponent(hash)
|
121 | 134 | if (hash.indexOf('"')) hash = hash.replace(/(?=")/g, '\\')
|
122 |
| - var navLink = menuPanel.querySelector('.nav-link[href="' + hash + '"]') |
| 135 | + var navLink = !articleOnly && menuPanel.querySelector('.nav-link[href="' + hash + '"]') |
123 | 136 | if (navLink) return navLink.parentNode
|
124 | 137 | var target = document.getElementById(hash.slice(1))
|
125 | 138 | if (!target) return
|
|
0 commit comments