Skip to content

Commit ccfc44d

Browse files
authored
Merge pull request #24 from szymon-rd/dynamic-loading-fixes
Dynamic loading fixes
2 parents 2c4bfee + fc4241e commit ccfc44d

File tree

17 files changed

+117
-166
lines changed

17 files changed

+117
-166
lines changed

docs/_assets/docsScalaLangResources/scaladoc-assets.html

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

docs/_layouts/main.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
---
22
layout: base
3-
extraJS:
4-
- js/contributors.js
5-
extraCSS:
6-
- css/content-contributors.css
73
---
84

95
<div id="content-wrapper">{{ content }}</div>

project/DocumentationWebsite.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object DocumentationWebsite {
1414

1515

1616
val contributorsTestcasesDestinationFile = Paths.get("scaladoc-testcases", "docs", "_assets", "js", "contributors.js").toFile
17-
val contributorsDestinationFile = Paths.get("docs", "_assets", "js", "contributors.js").toFile
17+
val contributorsDestinationFile = baseDest / "dotty_res" / "scripts" / "contributors.js"
1818
sbt.IO.copyFile(contributorsFile, contributorsTestcasesDestinationFile)
1919
sbt.IO.copyFile(contributorsFile, contributorsDestinationFile)
2020

@@ -25,8 +25,8 @@ object DocumentationWebsite {
2525
val cssCodeSnippetsSourceFile = cssSourceFileBase / "code-snippets.css"
2626
sbt.IO.copyFile(cssCodeSnippetsSourceFile, cssCodeSnippetsDesitnationFile)
2727

28-
val cssContentContributorsTestcasesDesitnationFile = Paths.get("docs", "_assets", "css", "content-contributors.css").toFile
29-
val cssContentContributorsDesitnationFile = Paths.get("scaladoc-testcases", "docs", "_assets", "css", "content-contributors.css").toFile
28+
val cssContentContributorsTestcasesDesitnationFile = Paths.get("scaladoc-testcases", "docs", "_assets", "css", "content-contributors.css").toFile
29+
val cssContentContributorsDesitnationFile = baseDest / "dotty_res" / "styles" / "content-contributors.css"
3030
val cssContentContributorsSourceFile = cssContentContributorsSourceBaseFile / "content-contributors.css"
3131
sbt.IO.copyFile(cssContentContributorsSourceFile, cssContentContributorsTestcasesDesitnationFile)
3232
sbt.IO.copyFile(cssContentContributorsSourceFile, cssContentContributorsDesitnationFile)

project/scripts/expected-links/reference-expected-links.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
./contextual/type-classes.html
4141
./contextual/using-clauses.html
4242
./docs/reference/other-new-features/named-typeargs.html
43-
./docsScalaLangResources/scaladoc-assets.html
4443
./dropped-features.html
4544
./dropped-features/auto-apply.html
4645
./dropped-features/class-shadowing-spec.html

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: 8 additions & 6 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,10 @@ 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-
then {
92-
getAuthorsForFilename(Globals.githubContributorsFilename.stripPrefix("/")).onComplete {
91+
window.addEventListener("dynamicPageLoad", (e: Event) => {
92+
val ghUrl = githubContributorsUrl()
93+
if js.typeOf(ghUrl) == "string" && ghUrl != "null" then {
94+
getAuthorsForFilename(githubContributorsFilename().stripPrefix("/")).onComplete {
9395
case Success(authors) =>
9496
val maybeDiv = Option(document.getElementById("documentation-contributors"))
9597
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-testcases/docs/_layouts/static-site-main.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
---
2-
extraJS:
3-
- js/contributors.js
42
extraCSS:
53
- css/bootstrap.min.css
6-
- css/content-contributors.css
74
---
85

96
<div class="container">

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: 78 additions & 43 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();
@@ -84,29 +128,23 @@ function attachAllListeners() {
84128
e.preventDefault();
85129
e.stopPropagation();
86130
$.get(href, function (data) {
87-
const html = $.parseHTML(data);
88-
const title = html.find((node) => node.nodeName === "TITLE").innerText;
89-
const bodyDiv = html.find((node) => node.nodeName === "DIV");
90-
const { children } = document.body.firstChild;
91131
if (window.history.state === null) {
92-
window.history.replaceState(
93-
{
94-
leftColumn: children[3].innerHTML,
95-
mainDiv: children[6].innerHTML,
96-
title: document.title,
97-
},
98-
"",
99-
);
132+
window.history.replaceState(savePageState(document), '')
100133
}
101-
document.title = title;
102-
const leftColumn = bodyDiv.children[3].innerHTML;
103-
const mainDiv = bodyDiv.children[6].innerHTML;
104-
window.history.pushState({ leftColumn, mainDiv, title }, "", href);
105-
children[3].innerHTML = leftColumn;
106-
children[6].innerHTML = mainDiv;
107-
attachAllListeners();
108-
});
109-
});
134+
const parser = new DOMParser()
135+
const parsedDocument = parser.parseFromString(data, "text/html")
136+
const state = savePageState(parsedDocument)
137+
window.history.pushState(state, '', href)
138+
loadPageState(document, state)
139+
window.dispatchEvent(new Event(DYNAMIC_PAGE_LOAD))
140+
})
141+
})
142+
})
143+
144+
$(".ar").on('click', function (e) {
145+
$(this).parent().parent().toggleClass("expanded")
146+
$(this).toggleClass("expanded")
147+
e.stopPropagation()
110148
});
111149

112150
$(".ar").on("click", function (e) {
@@ -248,11 +286,16 @@ function attachAllListeners() {
248286
// when document is loaded graph needs to be shown
249287
}
250288

289+
const DYNAMIC_PAGE_LOAD = "dynamicPageLoad"
290+
window.addEventListener(DYNAMIC_PAGE_LOAD, () => {
291+
attachAllListeners()
292+
})
293+
251294
window.addEventListener("DOMContentLoaded", () => {
252295
hljs.registerLanguage("scala", highlightDotty);
253296
hljs.registerAliases(["dotty", "scala3"], "scala");
254-
attachAllListeners();
255-
});
297+
window.dispatchEvent(new Event(DYNAMIC_PAGE_LOAD))
298+
})
256299

257300
// show/hide side menu on mobile view
258301
const sideMenuToggler = document.getElementById("mobile-sidebar-toggle");
@@ -267,26 +310,18 @@ sideMenuToggler.addEventListener("click", (_e) => {
267310
});
268311

269312
// show/hide mobile menu on mobile view
270-
document
271-
.getElementById("mobile-menu-toggle")
272-
.addEventListener("click", (_e) => {
273-
document.getElementById("mobile-menu").classList.add("show");
274-
});
275-
document.getElementById("mobile-menu-close").addEventListener("click", (_e) => {
276-
document.getElementById("mobile-menu").classList.remove("show");
277-
});
278-
279-
window.addEventListener("popstate", (e) => {
280-
if (e.state === null) {
281-
return;
282-
}
283-
const { leftColumn, mainDiv, title } = e.state;
284-
document.title = title;
285-
const { children } = document.body.firstChild;
286-
children[3].innerHTML = leftColumn;
287-
children[6].innerHTML = mainDiv;
288-
attachAllListeners();
289-
});
313+
document.getElementById("mobile-menu-toggle").addEventListener('click', _e => {
314+
document.getElementById("mobile-menu").classList.add("show")
315+
})
316+
document.getElementById("mobile-menu-close").addEventListener('click', _e => {
317+
document.getElementById("mobile-menu").classList.remove("show")
318+
})
319+
320+
window.addEventListener('popstate', e => {
321+
if (e.state === null) { return }
322+
loadPageState(document, e.state)
323+
window.dispatchEvent(new Event(DYNAMIC_PAGE_LOAD))
324+
})
290325

291326
var zoom;
292327
var transform;

0 commit comments

Comments
 (0)