|
| 1 | +// Custom navigation loader for AWS SDK for Kotlin documentation. |
| 2 | + |
| 3 | +// Extracts the module name from a given URL href |
| 4 | +function extractModuleName(href) { |
| 5 | + try{ |
| 6 | + const url = new URL(href, window.location.origin); |
| 7 | + const pathname = url.pathname; |
| 8 | + const pathSegments = pathname.split('/').filter(Boolean); |
| 9 | + |
| 10 | + // For local hosting |
| 11 | + if (url.hostname === 'localhost') { |
| 12 | + return pathSegments.length >= 1 ? pathSegments[0] : null; |
| 13 | + } |
| 14 | + |
| 15 | + return pathSegments.length >= 4 ? pathSegments[3] : null; |
| 16 | + } |
| 17 | + catch (error) { |
| 18 | + return null; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Hides the sidebar and adjusts main content layout |
| 23 | +function hideSidebar() { |
| 24 | + const sidebar = document.getElementById('leftColumn'); |
| 25 | + const main = document.getElementById('main'); |
| 26 | + |
| 27 | + if (sidebar) { |
| 28 | + sidebar.style.display = 'none'; |
| 29 | + } |
| 30 | + |
| 31 | + if (main) { |
| 32 | + main.style.marginLeft = '0'; |
| 33 | + main.style.width = '100%'; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function loadNavigation() { |
| 38 | + const moduleName = extractModuleName(window.location.href); |
| 39 | + |
| 40 | + // Hide sidebar for root index page |
| 41 | + if (moduleName === "index.html") { |
| 42 | + hideSidebar() |
| 43 | + return Promise.resolve(''); |
| 44 | + } |
| 45 | + |
| 46 | + const navigationPath = moduleName |
| 47 | + ? `${pathToRoot}${moduleName}/navigation.html` |
| 48 | + : `${pathToRoot}navigation.html`; |
| 49 | + |
| 50 | + return fetch(navigationPath) |
| 51 | + .then(response => response.text()) |
| 52 | + .catch(error => { |
| 53 | + // Use root navigation as a fallback |
| 54 | + return fetch(pathToRoot + "navigation.html") |
| 55 | + .then(response => response.text()); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +navigationPageText = loadNavigation() |
| 60 | + |
| 61 | +// ================================================================= |
| 62 | +// Everything below this is copied from Dokka's navigation-loader.js |
| 63 | +// ================================================================= |
| 64 | +displayNavigationFromPage = () => { |
| 65 | + navigationPageText.then(data => { |
| 66 | + document.getElementById("sideMenu").innerHTML = data; |
| 67 | + }).then(() => { |
| 68 | + document.querySelectorAll(".overview > a").forEach(link => { |
| 69 | + link.setAttribute("href", pathToRoot + link.getAttribute("href")); |
| 70 | + }) |
| 71 | + }).then(() => { |
| 72 | + document.querySelectorAll(".sideMenuPart").forEach(nav => { |
| 73 | + if (!nav.classList.contains("hidden")) |
| 74 | + nav.classList.add("hidden") |
| 75 | + }) |
| 76 | + }).then(() => { |
| 77 | + revealNavigationForCurrentPage() |
| 78 | + }).then(() => { |
| 79 | + scrollNavigationToSelectedElement() |
| 80 | + }) |
| 81 | + document.querySelectorAll('.footer a[href^="#"]').forEach(anchor => { |
| 82 | + anchor.addEventListener('click', function (e) { |
| 83 | + e.preventDefault(); |
| 84 | + document.querySelector(this.getAttribute('href')).scrollIntoView({ |
| 85 | + behavior: 'smooth' |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +revealNavigationForCurrentPage = () => { |
| 92 | + let pageId = document.getElementById("content").attributes["pageIds"].value.toString(); |
| 93 | + let parts = document.querySelectorAll(".sideMenuPart"); |
| 94 | + let found = 0; |
| 95 | + do { |
| 96 | + parts.forEach(part => { |
| 97 | + if (part.attributes['pageId'].value.indexOf(pageId) !== -1 && found === 0) { |
| 98 | + found = 1; |
| 99 | + if (part.classList.contains("hidden")) { |
| 100 | + part.classList.remove("hidden"); |
| 101 | + part.setAttribute('data-active', ""); |
| 102 | + } |
| 103 | + revealParents(part) |
| 104 | + } |
| 105 | + }); |
| 106 | + pageId = pageId.substring(0, pageId.lastIndexOf("/")) |
| 107 | + } while (pageId.indexOf("/") !== -1 && found === 0) |
| 108 | +}; |
| 109 | +revealParents = (part) => { |
| 110 | + if (part.classList.contains("sideMenuPart")) { |
| 111 | + if (part.classList.contains("hidden")) |
| 112 | + part.classList.remove("hidden"); |
| 113 | + revealParents(part.parentNode) |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +scrollNavigationToSelectedElement = () => { |
| 118 | + let selectedElement = document.querySelector('div.sideMenuPart[data-active]') |
| 119 | + if (selectedElement == null) { // nothing selected, probably just the main page opened |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + let hasIcon = selectedElement.querySelectorAll(":scope > div.overview span.nav-icon").length > 0 |
| 124 | + |
| 125 | + // for instance enums also have children and are expandable, but are not package/module elements |
| 126 | + let isPackageElement = selectedElement.children.length > 1 && !hasIcon |
| 127 | + if (isPackageElement) { |
| 128 | + // if package is selected or linked, it makes sense to align it to top |
| 129 | + // so that you can see all the members it contains |
| 130 | + selectedElement.scrollIntoView(true) |
| 131 | + } else { |
| 132 | + // if a member within a package is linked, it makes sense to center it since it, |
| 133 | + // this should make it easier to look at surrounding members |
| 134 | + selectedElement.scrollIntoView({ |
| 135 | + behavior: 'auto', |
| 136 | + block: 'center', |
| 137 | + inline: 'center' |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/* |
| 143 | + This is a work-around for safari being IE of our times. |
| 144 | + It doesn't fire a DOMContentLoaded, presumabely because eventListener is added after it wants to do it |
| 145 | +*/ |
| 146 | +if (document.readyState == 'loading') { |
| 147 | + window.addEventListener('DOMContentLoaded', () => { |
| 148 | + displayNavigationFromPage() |
| 149 | + }) |
| 150 | +} else { |
| 151 | + displayNavigationFromPage() |
| 152 | +} |
0 commit comments