Skip to content

Commit b17180f

Browse files
authored
Merge pull request #10298 from romanowski/scala3doc/fast-navigation-menu
page navigation loads as soon as possible
2 parents 462a72f + bc3ce80 commit b17180f

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
navigationPageText = fetch(pathToRoot + "navigation.html").then(response => response.text())
2+
3+
window.addEventListener('DOMContentLoaded', () => {
4+
navigationPageText.then(data => {
5+
document.getElementById("sideMenu").innerHTML = data;
6+
}).then(() => {
7+
document.querySelectorAll(".overview > a").forEach(link => {
8+
link.setAttribute("href", pathToRoot + link.getAttribute("href"));
9+
})
10+
}).then(() => {
11+
document.querySelectorAll(".sideMenuPart").forEach(nav => {
12+
if (!nav.classList.contains("hidden")) nav.classList.add("hidden")
13+
})
14+
}).then(() => {
15+
revealNavigationForCurrentPage()
16+
})
17+
18+
/* Smooth scrolling support for going to the top of the page */
19+
document.querySelectorAll('.footer a[href^="#"]').forEach(anchor => {
20+
anchor.addEventListener('click', function (e) {
21+
e.preventDefault();
22+
23+
document.querySelector(this.getAttribute('href')).scrollIntoView({
24+
behavior: 'smooth'
25+
});
26+
});
27+
});
28+
})
29+
30+
revealNavigationForCurrentPage = () => {
31+
let pageId = document.getElementById("content").attributes["pageIds"].value.toString();
32+
let parts = document.querySelectorAll(".sideMenuPart");
33+
let found = 0;
34+
do {
35+
parts.forEach(part => {
36+
if (part.attributes['pageId'].value.indexOf(pageId) !== -1 && found === 0) {
37+
found = 1;
38+
if (part.classList.contains("hidden")){
39+
part.classList.remove("hidden");
40+
part.setAttribute('data-active',"");
41+
}
42+
revealParents(part)
43+
}
44+
});
45+
pageId = pageId.substring(0, pageId.lastIndexOf("/"))
46+
} while (pageId.indexOf("/") !== -1 && found === 0)
47+
};
48+
49+
revealParents = (part) => {
50+
if (part.classList.contains("sideMenuPart")) {
51+
if (part.classList.contains("hidden")) part.classList.remove("hidden");
52+
revealParents(part.parentNode)
53+
}
54+
};

scala3doc/src/dotty/dokka/preprocessors/ScalaEmbeddedResourceApppender.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class ScalaEmbeddedResourceAppender extends PageTransformer {
1111
page.getName,
1212
page.getContent,
1313
page.getDri,
14-
// Remove default CSS and add our own
14+
// Remove default CSS and navigation loader and add our own versions
1515
(page.getEmbeddedResources.asScala
16+
.filterNot(_ == "scripts/navigation-loader.js")
1617
.filterNot(_.endsWith(".css")) ++ Seq(
1718
"styles/nord-light.css",
1819
"styles/scalastyle.css",
@@ -37,6 +38,7 @@ class ScalaEmbeddedResourceAppender extends PageTransformer {
3738
"scripts/components/FilterGroup.js",
3839
"scripts/components/Filter.js",
3940
"scripts/data.js",
41+
"scripts/fast-navigation-loader.js"
4042
)).asJava,
4143
page.getChildren
4244
)

0 commit comments

Comments
 (0)