-
Notifications
You must be signed in to change notification settings - Fork 4
/
menu.js
74 lines (60 loc) · 2.17 KB
/
menu.js
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
// HTML INCLUDE FUNCTIONS
document.addEventListener("DOMContentLoaded", function(e) {
includeHTML();
});
function includeFinished() {
try {
// Call all normal functions here
setupMenuHandlers();
} catch (e) {
if (e instanceof TypeError) {
// Something is missing - the required include will come later,
// let's wait for next call to includeFinished()
return;
} else {
throw e;
}
}
}
function includeHTML() {
// Loop through all HTML elements
let elements = document.getElementsByTagName("*");
for (let i = 0; i < elements.length; i++) {
let elem = elements[i];
// search for elements with a the right attribute
let include_file = elem.getAttribute("include-html");
if (include_file) {
// Make a HTTP request using the attribute value as the file name
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
// Since this isn't synchronous, double-check
// if the attribute is still there
if (this.status == 200 &&
elem.getAttribute("include-html") !== null) {
elem.innerHTML = this.responseText + elem.innerHTML;
}
if (this.status == 404) {
elem.innerHTML = "Page not found.";
}
includeFinished();
/* Remove the attribute, and call this function once more: */
elem.removeAttribute("include-html");
includeHTML();
}
}
xhttp.open("GET", include_file, true);
xhttp.send();
/* Exit the function: */
return;
}
}
}
// NORMAL FUNCTIONS
function setupMenuHandlers() {
let menu_icon_elem = document.getElementById("menu_icon");
let menu_elem = document.getElementById("menu");
menu_icon_elem.addEventListener("click", function () {
menu_elem.classList.toggle("open");
}, false);
}