Skip to content

Commit a2ae349

Browse files
committed
fix dynamic loading
1 parent f6286f0 commit a2ae349

File tree

11 files changed

+97
-129
lines changed

11 files changed

+97
-129
lines changed

scaladoc-js/common/src/code-snippets/CodeSnippets.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,7 @@ class CodeSnippets:
147147
)
148148
}
149149

150-
enrichSnippets()
150+
window.addEventListener("dynamicPageLoad", (e: Event) => {
151+
enrichSnippets()
152+
})
151153

scaladoc-js/contributors/src/Globals.scala

Lines changed: 0 additions & 12 deletions
This file was deleted.

scaladoc-js/contributors/src/content-contributors/ContentContributors.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ trait FileChange extends js.Object:
4949

5050
class ContentContributors:
5151
val indenticonsUrl = "https://github.com/identicons"
52-
def linkForFilename(filename: String) = Globals.githubContributorsUrl + s"/commits?path=$filename"
52+
val htmlElement = window.document.documentElement
53+
def githubContributorsUrl() = htmlElement.getAttribute("data-githubContributorsUrl")
54+
def githubContributorsFilename() = htmlElement.getAttribute("data-githubContributorsFilename")
55+
def linkForFilename(filename: String) = githubContributorsUrl() + s"/commits?path=$filename"
5356
def getAuthorsForFilename(filename: String): Future[List[FullAuthor]] = {
5457
val link = linkForFilename(filename)
5558
Ajax.get(link).map(_.responseText).flatMap { json =>
@@ -85,11 +88,15 @@ class ContentContributors:
8588
.map(_.previous_filename)
8689
}
8790
}
88-
document.addEventListener("DOMContentLoaded", (e: Event) => {
89-
if js.typeOf(Globals.githubContributorsUrl) != "undefined" &&
90-
js.typeOf(Globals.githubContributorsFilename) != "undefined"
91+
window.addEventListener("dynamicPageLoad", (e: Event) => {
92+
println("Hello!")
93+
println(githubContributorsUrl())
94+
println(githubContributorsFilename())
95+
println("Goodbye!")
96+
if js.typeOf(githubContributorsUrl()) == "string" &&
97+
js.typeOf(githubContributorsFilename()) == "string"
9198
then {
92-
getAuthorsForFilename(Globals.githubContributorsFilename.stripPrefix("/")).onComplete {
99+
getAuthorsForFilename(githubContributorsFilename().stripPrefix("/")).onComplete {
93100
case Success(authors) =>
94101
val maybeDiv = Option(document.getElementById("documentation-contributors"))
95102
maybeDiv.foreach { mdiv =>

scaladoc-js/main/src/Main.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import scala.scalajs.js.annotation._
33

44
object Main extends App:
55
Searchbar()
6-
SocialLinks()
76
DropdownHandler()
8-
Ux()
97
TooltipNormalizer()
108
CodeSnippets()

scaladoc-js/main/src/searchbar/SearchbarComponent.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import java.net.URI
1717
class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearchEngine, parser: QueryParser):
1818
val initialChunkSize = 5
1919
val resultsChunkSize = 20
20+
def pathToRoot() = window.document.documentElement.getAttribute("data-pathToRoot")
2021
extension (p: PageEntry)
2122
def toHTML(boldChars: Set[Int]) =
2223
val location = if (p.isLocationExternal) {
2324
p.location
2425
} else {
25-
Globals.pathToRoot + p.location
26+
pathToRoot() + p.location
2627
}
2728

2829
val extensionTargetMessage = if (p.extensionTarget.isEmpty()) {
@@ -56,7 +57,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
5657
val location = if (m.pageLocation(0) == 'e') {
5758
m.pageLocation.substring(1)
5859
} else {
59-
Globals.pathToRoot + m.pageLocation.substring(1)
60+
pathToRoot() + m.pageLocation.substring(1)
6061
}
6162

6263
div(cls := "scaladoc-searchbar-row mono-small-inline", "result" := "", "inkuire-result" := "", "mq" := m.mq.toString)(

scaladoc-js/main/src/social-links/SocialLinks.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

scaladoc-js/main/src/ux/Ux.scala

Lines changed: 0 additions & 51 deletions
This file was deleted.

scaladoc/resources/dotty_res/scripts/common/utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ const withEvent = (element, listener, callback) => {
99
return () => element && element.removeEventListener(listener, callback);
1010
};
1111

12-
const init = (cb) => window.addEventListener("DOMContentLoaded", cb);
13-
1412
const attachDOM = (element, html) => {
1513
if (element) {
1614
element.innerHTML = htmlToString(html);

scaladoc/resources/dotty_res/scripts/components/FilterBar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@
8989
}
9090
}
9191

92-
init(() => new FilterBar());
92+
window.addEventListener("dynamicPageLoad", () => {
93+
new FilterBar()
94+
})

scaladoc/resources/dotty_res/scripts/ux.js

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
let observer = null;
22

3+
const attrsToCopy = [
4+
"data-githubContributorsUrl",
5+
"data-githubContributorsFilename",
6+
"data-pathToRoot",
7+
]
8+
9+
/**
10+
* @typedef {Object} SavedPageState
11+
* @property {Strign} mainDiv
12+
* @property {String} leftColumn
13+
* @property {String} title
14+
* @property {Record<string, string>} attrs
15+
*/
16+
17+
/**
18+
* @param {Document} doc
19+
* @returns {SavedPageState}
20+
*/
21+
function savePageState(doc) {
22+
const attrs = {}
23+
for (const attr of attrsToCopy) {
24+
attrs[attr] = doc.documentElement.getAttribute(attr)
25+
}
26+
return {
27+
mainDiv: doc.querySelector("#main").innerHTML,
28+
leftColumn: doc.querySelector("#leftColumn").innerHTML,
29+
title: doc.title,
30+
attrs,
31+
}
32+
}
33+
34+
/**
35+
* @param {Document} doc
36+
* @param {SavedPageState} saved
37+
*/
38+
function loadPageState(doc, saved) {
39+
doc.title = saved.title
40+
doc.querySelector("#main").innerHTML = saved.mainDiv
41+
doc.querySelector("#leftColumn").innerHTML = saved.leftColumn
42+
for (const attr of attrsToCopy) {
43+
doc.documentElement.setAttribute(attr, saved.attrs[attr])
44+
}
45+
}
46+
347
function attachAllListeners() {
448
if (observer) {
549
observer.disconnect()
@@ -59,24 +103,15 @@ function attachAllListeners() {
59103
e.preventDefault()
60104
e.stopPropagation()
61105
$.get(href, function (data) {
62-
const html = $.parseHTML(data)
63-
const title = html.find(node => node.nodeName === "TITLE").innerText
64-
const bodyDiv = html.find(node => node.nodeName === "DIV")
65-
const { children } = document.body.firstChild
66106
if (window.history.state === null) {
67-
window.history.replaceState({
68-
leftColumn: children[3].innerHTML,
69-
mainDiv: children[6].innerHTML,
70-
title: document.title,
71-
}, '')
107+
window.history.replaceState(savePageState(document), '')
72108
}
73-
document.title = title
74-
const leftColumn = bodyDiv.children[3].innerHTML
75-
const mainDiv = bodyDiv.children[6].innerHTML
76-
window.history.pushState({ leftColumn, mainDiv, title }, '', href)
77-
children[3].innerHTML = leftColumn
78-
children[6].innerHTML = mainDiv
79-
attachAllListeners()
109+
const parser = new DOMParser()
110+
const parsedDocument = parser.parseFromString(data, "text/html")
111+
const state = savePageState(parsedDocument)
112+
window.history.pushState(state, '', href)
113+
loadPageState(document, state)
114+
window.dispatchEvent(new Event(DYNAMIC_PAGE_LOAD))
80115
})
81116
})
82117
})
@@ -161,11 +196,16 @@ function attachAllListeners() {
161196
// when document is loaded graph needs to be shown
162197
}
163198

199+
const DYNAMIC_PAGE_LOAD = "dynamicPageLoad"
200+
window.addEventListener(DYNAMIC_PAGE_LOAD, () => {
201+
attachAllListeners()
202+
})
203+
164204
window.addEventListener("DOMContentLoaded", () => {
165205
hljs.registerLanguage("scala", highlightDotty);
166206
hljs.registerAliases(["dotty", "scala3"], "scala");
167-
attachAllListeners()
168-
});
207+
window.dispatchEvent(new Event(DYNAMIC_PAGE_LOAD))
208+
})
169209

170210
// show/hide side menu on mobile view
171211
const sideMenuToggler = document.getElementById("mobile-sidebar-toggle")
@@ -189,12 +229,8 @@ document.getElementById("mobile-menu-close").addEventListener('click', _e => {
189229

190230
window.addEventListener('popstate', e => {
191231
if (e.state === null) { return }
192-
const { leftColumn, mainDiv, title } = e.state
193-
document.title = title
194-
const { children } = document.body.firstChild
195-
children[3].innerHTML = leftColumn
196-
children[6].innerHTML = mainDiv
197-
attachAllListeners()
232+
loadPageState(document, e.state)
233+
window.dispatchEvent(new Event(DYNAMIC_PAGE_LOAD))
198234
})
199235

200236
var zoom;

0 commit comments

Comments
 (0)