-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapp.js
More file actions
377 lines (350 loc) · 12.7 KB
/
Copy pathapp.js
File metadata and controls
377 lines (350 loc) · 12.7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* DigiByte main app — vanilla ES module
* Replaces: main2o.js, plugins (AOS, Slick, Lity, Waypoints, Parallax, jQuery)
*/
const prefersReducedMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
const $ = (sel, root = document) => root.querySelector(sel);
// ---------- Sticky nav state ----------
function initNav() {
const nav = $('.nav');
if (!nav) return;
const onScroll = () => nav.classList.toggle('is-scrolled', window.scrollY > 8);
onScroll();
window.addEventListener('scroll', onScroll, { passive: true });
const toggle = $('.nav__toggle', nav);
if (toggle) {
toggle.addEventListener('click', () => {
const open = nav.classList.toggle('is-open');
toggle.setAttribute('aria-expanded', String(open));
});
}
// Close mobile menu on link click
$$('.nav__list a', nav).forEach(a => {
a.addEventListener('click', () => nav.classList.remove('is-open'));
});
// Active link via IntersectionObserver on sections
const links = $$('.nav__list a[href^="#"]');
const map = new Map();
links.forEach(a => {
const id = a.getAttribute('href').slice(1);
const sec = document.getElementById(id);
if (sec) map.set(sec, a);
});
if (map.size) {
const io = new IntersectionObserver(entries => {
entries.forEach(e => {
const link = map.get(e.target);
if (!link) return;
if (e.isIntersecting) {
links.forEach(l => l.classList.remove('is-active'));
link.classList.add('is-active');
}
});
}, { rootMargin: '-40% 0px -55% 0px', threshold: 0 });
map.forEach((_, sec) => io.observe(sec));
}
}
// ---------- Reveal on scroll ----------
function initReveal() {
const items = $$('[data-reveal]');
if (!items.length || prefersReducedMotion) {
items.forEach(i => i.classList.add('is-visible'));
return;
}
const io = new IntersectionObserver((entries) => {
entries.forEach(e => {
if (e.isIntersecting) {
const delay = parseInt(e.target.dataset.revealDelay || '0', 10);
setTimeout(() => e.target.classList.add('is-visible'), delay);
io.unobserve(e.target);
}
});
}, { threshold: 0.12, rootMargin: '0px 0px -10% 0px' });
items.forEach(i => io.observe(i));
}
// ---------- Count-up ----------
function animateCount(el, target, opts = {}) {
const { duration = 1400, decimals = 0, prefix = '', suffix = '' } = opts;
const start = performance.now();
const from = parseFloat(el.dataset._from || '0');
const ease = t => 1 - Math.pow(1 - t, 3);
const tick = (now) => {
const t = Math.min(1, (now - start) / duration);
const v = from + (target - from) * ease(t);
el.textContent = prefix + v.toLocaleString(undefined, {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
}) + suffix;
if (t < 1) requestAnimationFrame(tick);
else el.dataset._from = String(target);
};
requestAnimationFrame(tick);
}
function initCounters() {
const items = $$('[data-count]');
if (!items.length) return;
if (prefersReducedMotion) {
items.forEach(el => {
const t = parseFloat(el.dataset.count);
el.textContent = (el.dataset.prefix || '') + t.toLocaleString() + (el.dataset.suffix || '');
});
return;
}
const io = new IntersectionObserver((entries) => {
entries.forEach(e => {
if (e.isIntersecting) {
const target = parseFloat(e.target.dataset.count);
animateCount(e.target, target, {
duration: parseInt(e.target.dataset.countDuration || '1400', 10),
decimals: parseInt(e.target.dataset.countDecimals || '0', 10),
prefix: e.target.dataset.prefix || '',
suffix: e.target.dataset.suffix || '',
});
io.unobserve(e.target);
}
});
}, { threshold: 0.4 });
items.forEach(el => io.observe(el));
}
// ---------- Tabs ----------
function initTabs() {
$$('.tabs').forEach(group => {
const btns = $$('.tabs__btn', group);
const panels = $$('.tabs__panel', group);
const activate = (id) => {
btns.forEach(b => b.setAttribute('aria-selected', String(b.dataset.tab === id)));
panels.forEach(p => {
if (p.dataset.tab === id) p.setAttribute('data-active', '');
else p.removeAttribute('data-active');
});
};
btns.forEach(b => b.addEventListener('click', () => activate(b.dataset.tab)));
const initial = btns.find(b => b.getAttribute('aria-selected') === 'true') || btns[0];
if (initial) activate(initial.dataset.tab);
});
}
// ---------- Copy buttons ----------
function initCopy() {
$$('.codeblock').forEach(block => {
if (block.querySelector('.codeblock__copy')) return;
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'codeblock__copy';
btn.textContent = 'Copy';
btn.setAttribute('aria-label', 'Copy code');
block.appendChild(btn);
btn.addEventListener('click', async () => {
const code = block.querySelector('pre, code')?.innerText ?? '';
try {
await navigator.clipboard.writeText(code);
btn.textContent = 'Copied';
btn.classList.add('is-copied');
setTimeout(() => { btn.textContent = 'Copy'; btn.classList.remove('is-copied'); }, 1500);
} catch { btn.textContent = 'Error'; }
});
});
}
// ---------- Smooth in-page scroll ----------
function initSmoothScroll() {
document.addEventListener('click', (e) => {
const a = e.target.closest('a[href^="#"]');
if (!a) return;
const id = a.getAttribute('href').slice(1);
if (!id) return;
const target = document.getElementById(id);
if (!target) return;
e.preventDefault();
target.scrollIntoView({ behavior: prefersReducedMotion ? 'auto' : 'smooth', block: 'start' });
history.replaceState(null, '', '#' + id);
});
}
// ---------- Marquee duplication ----------
function initMarquees() {
$$('.marquee__track, .top-ticker__track').forEach(track => {
// Duplicate children once for seamless loop
if (track.dataset.duplicated) return;
const clone = track.cloneNode(true);
while (clone.firstChild) track.appendChild(clone.firstChild);
track.dataset.duplicated = '1';
});
}
// ---------- Wallet filter ----------
function initWalletFilter() {
const filterEl = $('[data-wallet-filters]');
if (!filterEl) return;
const cards = $$('[data-wallet]');
filterEl.addEventListener('click', (e) => {
const btn = e.target.closest('[data-filter]');
if (!btn) return;
const f = btn.dataset.filter;
$$('[data-filter]', filterEl).forEach(b => b.setAttribute('aria-pressed', String(b === btn)));
cards.forEach(c => {
const types = (c.dataset.wallet || '').split(/\s+/);
c.classList.toggle('is-hidden', f !== 'all' && !types.includes(f));
});
});
}
// ---------- Native dialog lightbox ----------
function initLightbox() {
$$('[data-lightbox]').forEach(trigger => {
trigger.addEventListener('click', (e) => {
e.preventDefault();
const url = trigger.getAttribute('href') || trigger.dataset.lightbox;
const dialog = document.createElement('dialog');
dialog.className = 'dialog';
dialog.innerHTML = `
<div class="dialog__inner">
<button type="button" class="dialog__close" aria-label="Close">×</button>
<div class="aspect-video">
<iframe src="${url}" allow="autoplay; fullscreen" frameborder="0" style="width:100%;height:100%;"></iframe>
</div>
</div>`;
document.body.appendChild(dialog);
dialog.showModal();
const close = () => { dialog.close(); dialog.remove(); };
dialog.addEventListener('click', (ev) => { if (ev.target === dialog) close(); });
dialog.querySelector('.dialog__close').addEventListener('click', close);
});
});
}
// ---------- Language menu ----------
function initLangMenu() {
$$('.lang-menu').forEach(menu => {
const btn = $('.lang-menu__btn', menu);
const panel = $('.lang-menu__panel', menu);
if (!btn || !panel) return;
const close = () => { panel.hidden = true; btn.setAttribute('aria-expanded', 'false'); };
const open = () => { panel.hidden = false; btn.setAttribute('aria-expanded', 'true'); };
btn.addEventListener('click', (e) => { e.stopPropagation(); panel.hidden ? open() : close(); });
document.addEventListener('click', (e) => { if (!menu.contains(e.target)) close(); });
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') close(); });
});
}
// ---------- Year diff helper (legacy compat) ----------
function initYearDiff() {
const start = new Date('2014-01-10');
const years = Math.floor((Date.now() - start) / (365.25 * 24 * 3600 * 1000));
$$('.year-diff').forEach(el => el.textContent = years);
}
// ---------- Scroll-to-top ----------
function initScrollTop() {
let btn = $('.scroll-top');
if (!btn) {
btn = document.createElement('button');
btn.className = 'scroll-top';
btn.type = 'button';
btn.setAttribute('aria-label', 'Back to top');
// This site vendors Font Awesome 5 Pro; `fa-solid` is FA6 syntax and won't set the font-family.
btn.innerHTML = '<i class="fas fa-arrow-up" aria-hidden="true"></i>';
document.body.appendChild(btn);
}
const onScroll = () => btn.classList.toggle('is-visible', window.scrollY > 600);
onScroll();
window.addEventListener('scroll', onScroll, { passive: true });
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: prefersReducedMotion ? 'auto' : 'smooth' });
});
}
// ---------- Partner / contact form ----------
// The DigiByte Interest Form (#collaborate) posts to Web3Forms:
// https://web3forms.com/ (free tier)
//
// The access key below is the ONLY place the form is wired to an inbox —
// there is no server-side secret, no environment variable, no DB. Rotate it
// like this:
// 1. Sign in at https://web3forms.com/ with the address that should receive submissions.
// 2. Regenerate the access key from the dashboard.
// 3. Replace the string below and open a PR / redeploy.
// See also README.md → "Maintainer notes · Web3Forms".
const WEB3FORMS_ACCESS_KEY = '3d4319f1-66fa-4d15-9a53-4058cc60a425';
function initPartnerForm() {
const form = $('#partnerForm');
if (!form) return;
const btn = $('[data-partner-submit]', form);
const status = $('[data-partner-status]', form);
const setStatus = (msg, kind) => {
if (!status) return;
status.textContent = msg || '';
status.classList.remove('is-ok', 'is-error');
if (kind) status.classList.add(`is-${kind}`);
};
form.addEventListener('submit', async (e) => {
e.preventDefault();
if (form.botcheck && form.botcheck.checked) return; // honeypot tripped
if (!form.reportValidity()) return;
if (WEB3FORMS_ACCESS_KEY === 'YOUR_WEB3FORMS_ACCESS_KEY_HERE') {
setStatus('Form is not configured yet. Add a Web3Forms access key in /js/app.js.', 'error');
return;
}
const data = new FormData(form);
data.append('access_key', WEB3FORMS_ACCESS_KEY);
data.append('subject', `[DigiByte Interest Form] ${data.get('interest') || 'General'} — ${data.get('name') || ''}`);
data.append('from_name', 'DigiByte Interest Form');
data.append('replyto', String(data.get('email') || ''));
btn.disabled = true;
const originalLabel = btn.innerHTML;
btn.innerHTML = '<i class="fas fa-circle-notch fa-spin"></i> <span>Sending…</span>';
setStatus('', null);
try {
const res = await fetch('https://api.web3forms.com/submit', {
method: 'POST',
headers: { Accept: 'application/json' },
body: data,
});
const json = await res.json().catch(() => ({}));
if (res.ok && json.success) {
form.reset();
setStatus('Thanks — your message is on its way. A DigiByte community member will reply soon.', 'ok');
} else {
setStatus(json.message || 'Something went wrong. Please try again in a moment.', 'error');
}
} catch {
setStatus('Network error. Please check your connection and try again.', 'error');
} finally {
btn.disabled = false;
btn.innerHTML = originalLabel;
}
});
}
// ---------- Boot ----------
function boot() {
initNav();
initReveal();
initCounters();
initTabs();
initCopy();
initSmoothScroll();
initMarquees();
initWalletFilter();
initLightbox();
initLangMenu();
initScrollTop();
initYearDiff();
initPartnerForm();
// Lazy-load feature modules on demand
if ($('[data-hero-network]')) {
const startHero = () => import('./hero-network.js').then(m => m.init?.()).catch(() => {});
if ('requestIdleCallback' in window) requestIdleCallback(startHero, { timeout: 1500 });
else setTimeout(startHero, 250);
}
if ($('[data-tokenomics-chart]')) {
import('./tokenomics-chart.js').then(m => m.init?.()).catch(() => {});
}
if ($('[data-chain-dashboard]')) {
import('./chain-dashboard.js').then(m => m.init?.()).catch(() => {});
}
if ($('[data-github-release]')) {
import('./github-release.js').then(m => m.init?.()).catch(() => {});
}
// Service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/js/sw.js').catch(() => {});
});
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', boot);
} else {
boot();
}