1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
'use-strict';
let activeModal = null;
let activeLink = null;
let bottomHeightThreshold, sections;
let settingsModal;
let hamburgerToggle;
let sidebar;
function resizeSidebar() {
let rect = sidebar.getBoundingClientRect();
sidebar.style.height = `calc(100vh - 1em - ${rect.top + document.body.offsetTop}px)`;
}
function closeModal(modal) {
activeModal = null;
modal.hidden = true;
}
function openModal(modal) {
if (activeModal) {
closeModal(activeModal);
}
activeModal = modal;
modal.hidden = false;
}
function changeDocumentation(element) {
window.location = element.value;
}
function updateSetting(element) {
localStorage.setItem(element.name, element.checked);
if (element.name in settings) {
settings[element.name](element.checked);
}
}
function getRootAttributeToggle(attributeName, valueName) {
function toggleRootAttribute(set) {
if (set) {
document.documentElement.setAttribute(`data-${attributeName}`, valueName);
} else {
document.documentElement.removeAttribute(`data-${attributeName}`);
}
}
return toggleRootAttribute;
}
const settings = {
useSerifFont: getRootAttributeToggle('font', 'serif'),
useDarkTheme: getRootAttributeToggle('theme', 'dark')
};
Object.entries(settings).forEach(([name, setter]) => {
let value = JSON.parse(localStorage.getItem(name));
try {
setter(value);
} catch (error) {
console.error(`Failed to apply setting "${name}" With value:`, value);
console.error(error);
}
});
document.addEventListener('DOMContentLoaded', () => {
bottomHeightThreshold = document.documentElement.scrollHeight - 30;
sections = document.querySelectorAll('section');
settingsModal = document.querySelector('div#settings.modal');
hamburgerToggle = document.getElementById("hamburger-toggle");
sidebar = document.getElementById("sidebar");
resizeSidebar();
sidebar.addEventListener("click", (e) => {
// If we click a navigation, close the hamburger menu
if (e.target.tagName == "A" && sidebar.classList.contains("sidebar-toggle")) {
sidebar.classList.remove("sidebar-toggle");
let button = hamburgerToggle.firstElementChild;
button.textContent = "menu";
// Scroll a little up to actually see the header
// Note: this is generally around ~55px
// A proper solution is getComputedStyle but it can be slow
// Instead let's just rely on this quirk and call it a day
// This has to be done after the browser actually processes
// the section movement
setTimeout(() => window.scrollBy(0, -100), 75);
}
})
hamburgerToggle.addEventListener("click", (e) => {
sidebar.classList.toggle("sidebar-toggle");
let button = hamburgerToggle.firstElementChild;
if (button.textContent == "menu") {
button.textContent = "close";
}
else {
button.textContent = "menu";
}
});
const tables = document.querySelectorAll('.py-attribute-table[data-move-to-id]');
tables.forEach(table => {
let element = document.getElementById(table.getAttribute('data-move-to-id'));
let parent = element.parentNode;
// insert ourselves after the element
parent.insertBefore(table, element.nextSibling);
});
Object.keys(settings).forEach(name => {
let value = JSON.parse(localStorage.getItem(name));
let element = document.querySelector(`input[name=${name}]`);
if (element) {
element.checked = value === true;
}
});
});
window.addEventListener('scroll', () => {
let currentSection = null;
if (window.scrollY + window.innerHeight > bottomHeightThreshold) {
currentSection = sections[sections.length - 1];
}
else {
sections.forEach(section => {
let rect = section.getBoundingClientRect();
if (rect.top + document.body.offsetTop < 1) {
currentSection = section;
}
});
}
if (activeLink) {
activeLink.parentElement.classList.remove('active');
}
if (currentSection) {
activeLink = document.querySelector(`#sidebar a[href="#${currentSection.id}"]`);
if (activeLink) {
activeLink.parentElement.classList.add('active');
}
}
resizeSidebar();
});
document.addEventListener('keydown', (event) => {
if (event.keyCode == 27 && activeModal) {
closeModal(activeModal);
}
});
|