forked from Jonny-Ringo/the_eye_of_AO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnav.js
More file actions
77 lines (69 loc) · 2.85 KB
/
nav.js
File metadata and controls
77 lines (69 loc) · 2.85 KB
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
document.addEventListener('DOMContentLoaded', function () {
const sections = [
{
links: ['networkStatsLink', 'gamingStatsLink'],
contents: ['networkStatsContent', 'gamingStatsContent'],
default: 'networkStats'
},
{
links: ['theFirst150Link', 'hundredThouNodesLink', 'lCustomNodesLink'],
contents: ['theFirst150Content', 'hundredThouNodesContent', 'lCustomNodesContent'],
default: 'theFirst150'
},
{
links: ['mainnetNodesLink', 'hbCustomNodesLink'],
contents: ['mainnetNodesContent', 'hbCustomNodesContent'],
default: 'mainnetNodes'
}
];
function findSectionConfigByHash(hash) {
return sections.find(config =>
config.links.some(linkId => hash.includes(linkId.replace('Link', '')))
);
}
function switchSection(sectionId, config) {
// Clean up all
config.links.forEach(linkId => {
const el = document.getElementById(linkId);
if (el) el.classList.remove('active');
});
config.contents.forEach(contentId => {
const el = document.getElementById(contentId);
if (el) el.classList.remove('active');
});
// Activate selected
const activeLink = document.getElementById(`${sectionId}Link`);
const activeContent = document.getElementById(`${sectionId}Content`);
if (activeLink) activeLink.classList.add('active');
if (activeContent) activeContent.classList.add('active');
// Push state
window.history.pushState({ section: sectionId }, '', `#${sectionId}`);
}
// Bind click handlers
sections.forEach(config => {
config.links.forEach(linkId => {
const link = document.getElementById(linkId);
if (link) {
const sectionId = linkId.replace('Link', '');
link.addEventListener('click', function (e) {
e.preventDefault();
switchSection(sectionId, config);
});
}
});
});
// Handle browser nav
window.addEventListener('popstate', function (e) {
const hash = window.location.hash.replace('#', '');
const config = findSectionConfigByHash(hash) || sections[0];
switchSection(hash || config.default, config);
});
// Load correct section on page load
const hash = window.location.hash.replace('#', '');
const config = findSectionConfigByHash(hash) || sections.find(cfg =>
cfg.links.some(id => document.getElementById(id))
) || sections[0];
const initial = hash || config.default;
switchSection(initial, config);
window.history.replaceState({ section: initial }, '', `#${initial}`);
});