Skip to content

Commit ad0b88e

Browse files
authored
Merge pull request #15 from szymon-rd/dynamic-page-reload
Add dynamic page reload
2 parents 7994b8f + 1be314d commit ad0b88e

File tree

2 files changed

+78
-99
lines changed

2 files changed

+78
-99
lines changed

scaladoc/resources/dotty_res/scripts/ux.js

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
window.addEventListener("DOMContentLoaded", () => {
1+
let observer = null;
22

3-
var toggler = document.getElementById("leftToggler");
4-
if (toggler) {
5-
toggler.onclick = function () {
6-
document.getElementById("leftColumn").classList.toggle("open");
7-
};
3+
function attachAllListeners() {
4+
if (observer) {
5+
observer.disconnect()
86
}
97

108
var elements = document.getElementsByClassName("documentableElement")
@@ -25,7 +23,7 @@ window.addEventListener("DOMContentLoaded", () => {
2523
var documentableLists = document.getElementsByClassName("documentableList")
2624
if (documentableLists) {
2725
for (i = 0; i < documentableLists.length; i++) {
28-
documentableLists[i].children[0].onclick = function(e) {
26+
documentableLists[i].children[0].onclick = function (e) {
2927
this.classList.toggle("expand");
3028
this.parentElement.classList.toggle("expand");
3129
}
@@ -36,14 +34,47 @@ window.addEventListener("DOMContentLoaded", () => {
3634
if (memberLists) {
3735
for (i = 0; i < memberLists.length; i++) {
3836
if ($(memberLists[i].children[0]).is("button")) {
39-
memberLists[i].children[0].onclick = function(e) {
37+
memberLists[i].children[0].onclick = function (e) {
4038
this.classList.toggle("expand");
4139
this.parentElement.classList.toggle("expand");
4240
}
4341
}
4442
}
4543
}
4644

45+
document.querySelectorAll('a').forEach(el => {
46+
const href = el.href
47+
if (href === "") { return }
48+
const url = new URL(href)
49+
el.addEventListener('click', e => {
50+
if (url.href.replace("#", "") === window.location.href.replace("#", "")) { return }
51+
if (url.origin !== window.location.origin) { return }
52+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) { return }
53+
e.preventDefault()
54+
e.stopPropagation()
55+
$.get(href, function (data) {
56+
const html = $.parseHTML(data)
57+
const title = html.find(node => node.nodeName === "TITLE").innerText
58+
const bodyDiv = html.find(node => node.nodeName === "DIV")
59+
const { children } = document.body.firstChild
60+
if (window.history.state === null) {
61+
window.history.replaceState({
62+
leftColumn: children[3].innerHTML,
63+
mainDiv: children[6].innerHTML,
64+
title: document.title,
65+
}, '')
66+
}
67+
document.title = title
68+
const leftColumn = bodyDiv.children[3].innerHTML
69+
const mainDiv = bodyDiv.children[6].innerHTML
70+
window.history.pushState({ leftColumn, mainDiv, title }, '', href)
71+
children[3].innerHTML = leftColumn
72+
children[6].innerHTML = mainDiv
73+
attachAllListeners()
74+
})
75+
})
76+
})
77+
4778
$(".ar").on('click', function (e) {
4879
$(this).parent().parent().toggleClass("expanded")
4980
$(this).toggleClass("expanded")
@@ -70,7 +101,7 @@ window.addEventListener("DOMContentLoaded", () => {
70101
el.firstChild.classList.toggle("expand");
71102
}))
72103

73-
const observer = new IntersectionObserver(entries => {
104+
observer = new IntersectionObserver(entries => {
74105
entries.forEach(entry => {
75106
const id = entry.target.getAttribute('id');
76107
if (entry.intersectionRatio > 0) {
@@ -97,28 +128,9 @@ window.addEventListener("DOMContentLoaded", () => {
97128
}
98129
}
99130

100-
var logo = document.getElementById("logo");
101-
if (logo) {
102-
logo.onclick = function () {
103-
window.location = pathToRoot; // global variable pathToRoot is created by the html renderer
104-
};
105-
}
106-
107-
document.querySelectorAll('.documentableAnchor').forEach(elem => {
108-
elem.addEventListener('click', event => {
109-
var $temp = $("<input>")
110-
$("body").append($temp)
111-
var a = document.createElement('a')
112-
a.href = $(elem).attr("link")
113-
$temp.val(a.href).select();
114-
document.execCommand("copy")
115-
$temp.remove();
116-
})
117-
})
118-
119-
hljs.registerLanguage("scala", highlightDotty);
120-
hljs.registerAliases(["dotty", "scala3"], "scala");
121-
hljs.initHighlighting();
131+
document.querySelectorAll('pre code').forEach(el => {
132+
hljs.highlightBlock(el);
133+
});
122134

123135
/* listen for the `F` key to be pressed, to focus on the member filter input (if it's present) */
124136
document.body.addEventListener('keydown', e => {
@@ -134,32 +146,44 @@ window.addEventListener("DOMContentLoaded", () => {
134146
}
135147
})
136148

137-
// show/hide side menu on mobile view
138-
const sideMenuToggler = document.getElementById("mobile-sidebar-toggle");
139-
sideMenuToggler.addEventListener('click', _e => {
140-
document.getElementById("leftColumn").classList.toggle("show")
141-
document.getElementById("content").classList.toggle("sidebar-shown")
142-
const toc = document.getElementById("toc");
143-
if(toc && toc.childElementCount > 0) {
144-
toc.classList.toggle("sidebar-shown")
145-
}
146-
sideMenuToggler.classList.toggle("menu-shown")
147-
})
148-
149-
// show/hide mobile menu on mobile view
150-
const mobileMenuOpenIcon = document.getElementById("mobile-menu-toggle");
151-
const mobileMenuCloseIcon = document.getElementById("mobile-menu-close");
152-
mobileMenuOpenIcon.addEventListener('click', _e => {
153-
document.getElementById("mobile-menu").classList.add("show")
154-
})
155-
mobileMenuCloseIcon.addEventListener('click', _e => {
156-
document.getElementById("mobile-menu").classList.remove("show")
157-
})
158-
159-
160149
// when document is loaded graph needs to be shown
150+
}
151+
152+
window.addEventListener("DOMContentLoaded", () => {
153+
hljs.registerLanguage("scala", highlightDotty);
154+
hljs.registerAliases(["dotty", "scala3"], "scala");
155+
attachAllListeners()
161156
});
162157

158+
// show/hide side menu on mobile view
159+
const sideMenuToggler = document.getElementById("mobile-sidebar-toggle")
160+
sideMenuToggler.addEventListener('click', _e => {
161+
document.getElementById("leftColumn").classList.toggle("show")
162+
document.getElementById("content").classList.toggle("sidebar-shown")
163+
const toc = document.getElementById("toc");
164+
if (toc && toc.childElementCount > 0) {
165+
toc.classList.toggle("sidebar-shown")
166+
}
167+
sideMenuToggler.classList.toggle("menu-shown")
168+
})
169+
170+
// show/hide mobile menu on mobile view
171+
document.getElementById("mobile-menu-toggle").addEventListener('click', _e => {
172+
document.getElementById("mobile-menu").classList.add("show")
173+
})
174+
document.getElementById("mobile-menu-close").addEventListener('click', _e => {
175+
document.getElementById("mobile-menu").classList.remove("show")
176+
})
177+
178+
window.addEventListener('popstate', e => {
179+
const { leftColumn, mainDiv, title } = e.state
180+
document.title = title
181+
const { children } = document.body.firstChild
182+
children[3].innerHTML = leftColumn
183+
children[6].innerHTML = mainDiv
184+
attachAllListeners()
185+
})
186+
163187
var zoom;
164188
var transform;
165189

scaladoc/resources/dotty_res/styles/scalastyle.css

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ th {
136136
border-bottom: 2px solid var(--border-medium);
137137
}
138138

139-
/* Left bar toggler, only on small screens */
140-
#leftToggler {
141-
display: none;
142-
color: var(--icon-color);
143-
cursor: pointer;
144-
}
145-
146139
/* Left bar */
147140
#paneSearch {
148141
display: none;
@@ -732,30 +725,6 @@ footer .mode {
732725
display: flex;
733726
} */
734727

735-
.documentableAnchor:before {
736-
content: "\e901"; /* arrow down */
737-
font-family: "dotty-icons" !important;
738-
transform: rotate(-45deg);
739-
font-size: 20px;
740-
color: var(--link-fg);
741-
display: none;
742-
flex-direction: row;
743-
align-items: center;
744-
justify-content: center;
745-
position: absolute;
746-
top: 6px;
747-
left: -32px;
748-
}
749-
750-
.documentableAnchor:hover:before {
751-
color: var(--link-hover-fg);
752-
}
753-
754-
.documentableAnchor:active:before {
755-
color: var(--link-hover-fg);
756-
top: 8px;
757-
}
758-
759728
.memberDocumentation {
760729
font-size: 15px;
761730
line-height: 1.5;
@@ -1063,20 +1032,6 @@ footer .socials {
10631032
display: none;
10641033
}
10651034

1066-
#leftToggler {
1067-
display: unset;
1068-
position: absolute;
1069-
top: 5px;
1070-
left: 12px;
1071-
z-index: 5;
1072-
font-size: 30px;
1073-
}
1074-
#leftColumn.open ~ #main #leftToggler {
1075-
position: fixed;
1076-
left: unset;
1077-
right: 16vw;
1078-
color: var(--leftbar-fg);
1079-
}
10801035
.icon-toggler::before {
10811036
content: "\e90a"; /* menu icon */
10821037
}

0 commit comments

Comments
 (0)