-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
66 lines (46 loc) · 2.1 KB
/
ui.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
window.addEventListener('load', init);
let menuLinks, closures, contentPages;
function init() {
menuLinks = document.getElementById('links').children;
for (let link of menuLinks) {
link.addEventListener('click', showPage);
}
closures = document.getElementsByClassName('closePage');
for (let closure of closures) {
closure.addEventListener('click', closePage);
}
contentPages = document.getElementsByClassName('content');
}
function closePage(ev) {
const closeButton = ev.target;
const parentElement = ev.target.parentElement;
const menuItem = document.getElementById(closeButton.id.replace('close-', 'menu-'));
parentElement.classList.add('hidden');
menuItem.classList.remove('activelink');
}
function showPage(ev) {
const menuItem = ev.target;
if (menuItem.id == 'menu-blog') { // blog is a normal link, so don't need to do anything
return;
}
ev.preventDefault();
// add active state to clicked link, remove it from all others
for (let link of menuLinks) {
link == menuItem ? link.classList.add('activelink') : link.classList.remove('activelink');
}
const pageToShow = document.getElementById(ev.target.id.substring(5));
// the challenge here is to synchronize animations if switching from one page to another (otherwise two pages are visible at the same time)
// Step 1: are any content pages actually visible? If not, we don't need to worry about any of this.
let pageVisible = null;
for (page of contentPages) {
if (!page.classList.contains('hidden')) { pageVisible = page; }
}
// Step 2: If no pages are currently visible, no syncing is needed. But if one is, then we have to "close" it before showing the next one.
if (!pageVisible) {
pageToShow.classList.remove('hidden');
} else {
pageVisible.classList.add('hidden');
const delayedShow = new Promise((done) => setTimeout(done, 400)); // delay by the time it takes the first one to hide itself again
delayedShow.then(() => pageToShow.classList.remove('hidden'));
}
}