-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
182 lines (171 loc) · 6.69 KB
/
Copy pathapp.js
File metadata and controls
182 lines (171 loc) · 6.69 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const obs = new IntersectionObserver(entries => {
entries.forEach((e, i) => {
if (e.isIntersecting) {
setTimeout(() => e.target.classList.add('in'), i * 60);
obs.unobserve(e.target);
}
});
}, { threshold: 0.08 });
document.querySelectorAll('.r').forEach(el => obs.observe(el));
document.querySelectorAll('details').forEach(d => {
d.addEventListener('toggle', () => {
const icon = d.querySelector('.sq i');
icon.className = d.open ? 'fa-solid fa-minus' : 'fa-solid fa-plus';
});
});
const hamburger = document.getElementById('hamburger');
const mobileMenu = document.getElementById('mobile-menu');
const hamburgerIcon = document.getElementById('hamburger-icon');
hamburger.addEventListener('click', () => {
const isOpen = mobileMenu.classList.toggle('open');
hamburger.setAttribute('aria-expanded', isOpen);
hamburgerIcon.className = isOpen ? 'fa-solid fa-xmark' : 'fa-solid fa-bars';
});
document.querySelectorAll('.mobile-link').forEach(a => {
a.addEventListener('click', () => {
mobileMenu.classList.remove('open');
hamburger.setAttribute('aria-expanded', 'false');
hamburgerIcon.className = 'fa-solid fa-bars';
});
});
// Escape key to close mobile menu and dropdowns
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
if (mobileMenu.classList.contains('open')) {
mobileMenu.classList.remove('open');
hamburger.setAttribute('aria-expanded', 'false');
hamburgerIcon.className = 'fa-solid fa-bars';
}
document.querySelectorAll('.dl-dropdown.open').forEach(d => {
d.classList.remove('open');
d.querySelector('.dl-dropdown-btn').setAttribute('aria-expanded', 'false');
});
}
});
let allReleases = [];
const PLATFORM_CONFIG = [
{ dropdownId: 'dl-win-ver', cardId: 'dl-win', match: r => r.assets.find(a => a.name.toLowerCase().includes('setup') && a.name.endsWith('.exe')) },
{ dropdownId: 'dl-linux-ver', cardId: 'dl-linux', match: r => r.assets.find(a => a.name.endsWith('.AppImage')) },
{ dropdownId: 'dl-mac-ver', cardId: 'dl-mac', match: r => r.assets.find(a => a.name.includes('macOS') && a.name.endsWith('.dmg')) },
];
document.addEventListener('click', () => {
document.querySelectorAll('.dl-dropdown.open').forEach(d => {
d.classList.remove('open');
d.querySelector('.dl-dropdown-btn').setAttribute('aria-expanded', 'false');
});
});
function initDropdown(id) {
const dd = document.getElementById(id);
if (!dd) return;
const btn = dd.querySelector('.dl-dropdown-btn');
btn.addEventListener('click', (e) => {
e.stopPropagation();
const isOpen = dd.classList.contains('open');
document.querySelectorAll('.dl-dropdown.open').forEach(d => {
d.classList.remove('open');
d.querySelector('.dl-dropdown-btn').setAttribute('aria-expanded', 'false');
});
if (!isOpen) {
dd.classList.add('open');
btn.setAttribute('aria-expanded', 'true');
}
});
}
function setDropdownValue(id, tag) {
const dd = document.getElementById(id);
if (!dd) return;
const valueEl = dd.querySelector('.dl-dropdown-value');
if (valueEl) valueEl.textContent = tag;
dd.querySelectorAll('.dl-dropdown-item').forEach(item => {
item.classList.toggle('selected', item.dataset.value === tag);
item.setAttribute('aria-selected', item.dataset.value === tag);
});
}
function setDownloadCard(id, asset) {
const card = document.getElementById(id);
const file = document.getElementById(id + '-filename');
const arrow = document.getElementById(id + '-arrow');
const badge = document.getElementById(id + '-badge');
card.classList.remove('wip');
if (arrow) arrow.style.display = '';
if (badge) badge.style.display = 'none';
if (asset) {
card.href = asset.browser_download_url;
file.textContent = asset.name;
} else {
card.classList.add('wip');
file.textContent = 'Work in progress';
if (arrow) arrow.style.display = 'none';
if (badge) badge.style.display = 'inline';
}
}
async function fetchReleases() {
try {
const response = await fetch('https://api.github.com/repos/corecommit/PollyMC-Continued/releases?per_page=10');
if (!response.ok) throw new Error('Failed to fetch');
allReleases = await response.json();
if (!allReleases.length) throw new Error('No releases');
document.querySelectorAll('.app-version').forEach(el => { el.textContent = allReleases[0].tag_name; });
PLATFORM_CONFIG.forEach(({ dropdownId, cardId, match }) => {
const dd = document.getElementById(dropdownId);
if (!dd) return;
const available = allReleases.filter(r => match(r));
const menu = dd.querySelector('.dl-dropdown-menu');
menu.innerHTML = '';
if (!available.length) {
const valueEl = dd.querySelector('.dl-dropdown-value');
const btn = dd.querySelector('.dl-dropdown-btn');
if (valueEl) valueEl.textContent = 'No builds yet';
if (btn) btn.disabled = true;
setDownloadCard(cardId, null);
return;
}
available.forEach(r => {
const asset = match(r);
const item = document.createElement('div');
item.className = 'dl-dropdown-item';
item.dataset.value = r.tag_name;
item.setAttribute('role', 'option');
item.setAttribute('aria-selected', 'false');
item.textContent = r.tag_name;
item.addEventListener('click', (e) => {
e.stopPropagation();
setDropdownValue(dropdownId, r.tag_name);
setDownloadCard(cardId, asset);
dd.classList.remove('open');
dd.querySelector('.dl-dropdown-btn').setAttribute('aria-expanded', 'false');
});
menu.appendChild(item);
});
initDropdown(dropdownId);
const first = available[0];
setDropdownValue(dropdownId, first.tag_name);
setDownloadCard(cardId, match(first));
});
} catch (error) {
console.error('Error fetching releases:', error);
// Fallback if API fails
document.querySelectorAll('.dl-filename').forEach(el => el.textContent = 'Check GitHub');
document.querySelectorAll('.dl-dropdown-value').forEach(el => el.textContent = 'Unavailable');
document.querySelectorAll('.dl-dropdown-btn').forEach(btn => btn.disabled = true);
}
}
fetchReleases();
// Scrollspy for navigation
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.nav-center a');
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop - 100;
if (window.scrollY >= sectionTop) {
current = section.getAttribute('id');
}
});
navLinks.forEach(a => {
a.classList.remove('active');
if (a.getAttribute('href') === `#${current}`) {
a.classList.add('active');
}
});
});