-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
106 lines (98 loc) · 4.01 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
function openPage(page, setHistory) {
// get the response of the route using AJAX
ajax.get(`/${page}`, { onlyContent: true }).then(function(succes) {
// parse the JSON string to an object
const response = JSON.parse(succes.response);
// use the parsed response to fill the div with the content
document.title = response.title;
document.getElementById('page-title').innerText = response.title;
document.getElementById('main-content').innerHTML = response.html;
if (setHistory) {
history.pushState({ page: response.page }, response.title, `/${response.page}`);
}
setPageLinks(page);
},
function(failure) {
alert(failure);
}).catch(function(error) {
alert(error);
})
}
function setPageLinks(page) {
document.querySelectorAll(`.pagelink`).forEach(function(x) {
if (x.id.toLowerCase() === page) {
x.classList.toggle("active", true);
} else {
x.classList.toggle("active", false);
}
});
}
function onPageLoad(newFunction) {
if (window.attachEvent) {
window.attachEvent('onload', newFunction);
} else {
if (window.onload) {
var curronload = window.onload;
var newonload = function(evt) {
curronload(evt);
newFunction(evt);
};
window.onload = newonload;
} else {
window.onload = newFunction;
}
}
}
window.addEventListener('popstate', function(e) {
if (e.state) {
console.log(e.state);
openPage(e.state.page);
}
});
onPageLoad(function() {
const page = window.location.pathname.slice(1);
history.replaceState({ page: page }, window.title);
document.querySelectorAll(`.pagelink`).forEach(function(x) {
if (x.id.toLowerCase() === page) {
x.classList.toggle("active", true);
} else {
x.classList.toggle("active", false);
}
});
});
(function() {
function waitUntilInstalled(registration) {
return new Promise(function(resolve, reject) {
if (registration.installing) {
// If the current registration represents the "installing" service worker, then wait
// until the installation step (during which the resources are pre-fetched) completes
// to display the file list.
registration.installing.addEventListener('statechange', function(e) {
if (e.target.state == 'installed') {
resolve();
} else if (e.target.state == 'redundant') {
reject();
}
});
} else {
// Otherwise, if this isn't the "installing" service worker, then installation must have been
// completed during a previous visit to this page, and the resources are already pre-fetched.
// So we can show the list of files right away.
resolve();
}
});
}
if ('serviceWorker' in navigator) {
// navigator.serviceWorker.getRegistrations().then(function(registrations) {
// for (let registration of registrations) {
// registration.unregister()
// }
// });
navigator.serviceWorker.register('serviceworker.js', { scope: './' })
.then(waitUntilInstalled)
.catch(function(error) {});
} else {
console.log('nope')
// The current browser doesn't support service workers.
}
})();