-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
141 lines (120 loc) · 3.95 KB
/
main.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
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
function resetScroll(){
if(window.innerWidth <= 700){
window.scrollTo(0,775);
} else {
window.scrollTo(0,0);
}
}
class Page {
path = location.pathname
constructor() {
pageChanger.add(this) // add this page to navigation
this.init()
}
init(){
console.warn("init() needs to be a part of: "+this)
}
}
class PageChanger {
domainName = location.origin
loadedScripts = []
loadedPageScripts = []
constructor() {
this.loadInitScripts()
}
add(script){
this.loadedPageScripts.push(script)
}
init(){
this.loadedPageScripts.forEach((page)=>{
if(location.pathname===page.path){
page.init()
}
})
}
loadInitScripts(){
for (let i = 0; i < document.head.children.length; i++) {
if(document.head.children[i].nodeName==="SCRIPT" && document.head.children[i].src !== ""){
this.loadedScripts.push(document.head.children[i].src)
}
}
this.setWindowOnPopState()
}
setWindowOnPopState(){
window.onpopstate = () => {
this.changePage(location.pathname, ()=>{
history.replaceState(location.pathname,document.title,document.location);
pageChanger.init()
})
};
}
changeURL(page){
history.pushState(page, document.title, page)
this.setWindowOnPopState()
this.init()
}
changeTo(page){
if(location.pathname!==page){
this.changePage(page, ()=>{
this.changeURL(page)
})
}
}
changeHead(head){
// Clear head
document.head.innerHTML = null
// Create head children tags manually
for (let i = 0; i < head.children.length; i++) {
let duplicate = false
const isScriptWithSource = (head.children[i].nodeName === "SCRIPT" && head.children[i].src.length>0)
// Checking for duplicate - Register scripts with src (ensure no duplicates getting loaded)
if(isScriptWithSource){
// check for script already loaded
for (let j = 0; j < this.loadedScripts.length; j++) {
if (this.loadedScripts[j] === head.children[i].src){
duplicate = true
break
}
}
if(!duplicate){
this.loadedScripts.push(head.children[i].src)
}
}
// Also add all head tags to head
if(!duplicate){
const node = document.createElement(head.children[i].nodeName)
// Assign attributes
for (let j = 0; j < head.children[i].getAttributeNames().length; j++) {
const attributeName = head.children[i].getAttributeNames()[j]
const attributeValue = head.children[i].getAttribute(attributeName)
node.setAttribute(attributeName,attributeValue)
}
node.innerHTML = head.children[i].innerHTML
document.head.append(node)
}
}
}
changeBody(body){
document.body.innerHTML = body.innerHTML
}
changePage(page, then){
const request = new Request(this.domainName+page);
fetch(request).then((response) => {
return response.text()
}).then((html) => {
try {
const parser = new DOMParser();
html = parser.parseFromString(html, 'text/html');
} catch(error) {
console.error("Could not parse html: "+error)
}
this.changeHead(html.head)
this.changeBody(html.body)
// start from the top of the page
//window.scrollTo(0, 0);
resetScroll()
return then()
})
}
}
const pageChanger = new PageChanger()