-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (70 loc) · 1.81 KB
/
script.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
75
76
77
78
79
const parser = new DOMParser();
const pages = {
"home" : {
"nav" : "nav-home",
"name" : "Home",
"url" : "home.html"
},
"calculator" : {
"nav" : "nav-calculator",
"name" : "Calculator",
"url" : "calculator.html"
},
"events" : {
"nav" : "nav-events",
"name" : "Events",
"url" : "events.html"
},
"about" : {
"nav" : "nav-about",
"name" : "About",
"url" : "about.html"
}
}
async function getContent(target, callback){
fetch("pages/"+pages[target].url)
.then((response) => response.text())
.then((html) => {
var doc = parser.parseFromString(html, "text/html");
callback(doc.getElementById("export").innerHTML);
document.dispatchEvent(new CustomEvent("subpage-load", {
detail : target
}))
})
.catch(err => callback("error: [" + err + "]<br>Please contact me on Discord at .reisalin."))
}
function updateNavbar() {
for(const node of navbar.childNodes) {
node.classList?.remove("active")
}
document.getElementById("nav-"+currentPage()).classList.add("active")
}
function loadContent(){
updateNavbar();
getContent(currentPage(), function (body) {
content.innerHTML = body;
});
}
function createNavbar() {
for(const [ key, value ] of Object.entries(pages)) {
navbar.innerHTML += `<a class="nav${(key==currentPage())? ' active' : ''}" id="${value.nav}" href="#${key}">${value.name}</a>`
}
}
function currentPage() {
return location.hash.substring(1);
}
//Main function
function main() {
if(!location.hash) {
location.hash = "#home";
}
createNavbar();
loadContent();
window.addEventListener("hashchange", loadContent)
}
//Event Listeners
document.addEventListener("DOMContentLoaded", function () {
navbar = document.getElementById('navbar');
content = document.getElementById('content');
main();
});