Files
vbb/public/assets/splash.js
T
2026-11-14 10:07:41 -06:00

72 lines
4.8 KiB
JavaScript

// The splash: the loop grid, the next-shift bar, the waiting queue, and this visitor's verified visit.
(async function () {
VBB.renderNav('/'); VBB.renderFooter();
const $ = VBB.$, esc = VBB.esc;
const c = await VBB.getConfig().catch(() => ({}));
document.querySelectorAll('[data-k=perShift]').forEach(el => { el.textContent = c.perShift || 10; });
document.querySelectorAll('[data-k=views]').forEach(el => { el.textContent = VBB.n(c.viewsPerCycle || 150); });
let lastLoop = null, known = new Set(), firstPaint = true;
function render(d) {
const pct = Math.min(100, Math.round(d.progress / d.perShift * 100));
$('prog').innerHTML = d.progress + '<small> / ' + d.perShift + '</small>';
$('bar').style.width = pct + '%'; $('barWrap').setAttribute('aria-valuemax', d.perShift); $('barWrap').setAttribute('aria-valuenow', d.progress);
$('progLbl').textContent = d.held ? 'bar full: the next banner posted shifts the loop' : 'verified visitors toward the next shift';
const t = d.today || {};
$('today').textContent = VBB.n(t.shifts) + ' shifts and ' + VBB.n(t.cycles) + ' cycles today';
// the loop
const fill = d.fill || { loop: [], queue: [] };
const ids = d.loop.map(x => x.id + (x.look ? '*' + x.teaser : '')).join(',') + '|' + fill.loop.map(x => x.img).join(','); // a new banner look or fill repaints too
if (ids !== lastLoop) {
lastLoop = ids; const g = $('loop'); g.innerHTML = '';
const full = d.loop.length >= d.loopSize;
d.loop.forEach((t, i) => {
const last = full && i === d.loop.length - 1;
const el = VBB.tile(t, { pos: last ? 'NEXT OUT' : String(i + 1), cls: (last ? 'next' : '') + (!firstPaint && !known.has(t.id) ? ' fresh' : '') });
g.appendChild(el); known.add(t.id);
});
// open places: the first stays "post yours", the next ones show members' LinkSpin Banner Spins (never part of the loop)
for (let i = d.loop.length, k = 0; i < d.loopSize; i++) {
const sp = i > d.loop.length ? fill.loop[k] : null;
if (sp) { k++; g.appendChild(VBB.tile(sp, { pos: 'SPIN' })); continue; }
const a = document.createElement('a'); a.className = 'tile open'; a.href = '/my';
a.innerHTML = '<b>+</b>Open place<br>post yours'; g.appendChild(a);
}
firstPaint = false;
}
$('loopInfo').textContent = d.loop.length + ' of ' + d.loopSize + ' places filled';
$('fillNote').hidden = !(fill.loop.length || (!d.waiting && fill.queue.length));
// the queue
const q = $('queue'); q.innerHTML = '';
d.queue.forEach((t, i) => q.appendChild(VBB.tile(t, { pos: '#' + (i + 1) })));
if (!d.waiting) fill.queue.forEach(t => q.appendChild(VBB.tile(t, { pos: 'SPIN' })));
$('queueEmpty').hidden = d.waiting > 0;
$('queueInfo').textContent = d.waiting ? VBB.n(d.waiting) + ' waiting' + (d.waiting > d.queue.length ? ', first ' + d.queue.length + ' shown' : '') : '';
}
async function load() { const d = await VBB.getJSON('/api/splash'); if (!d.error) render(d); }
await load(); setInterval(load, 8000);
// ---- this visit: a ticket now, redeemed once the page has been visible for visitSeconds ----
const note = $('visitNote');
const qs = new URLSearchParams(location.search);
const src = document.referrer || ''; // inside a traffic-exchange frame this is the exchange: the traffic source
const start = await VBB.post('/api/visit/start', { ref: qs.get('ref') || '', src }).catch(() => ({}));
if (!start || !start.ticket) { note.textContent = 'Share the loop: every verified visitor moves it forward.'; return; }
if (start.already) { note.textContent = 'Your visit already counted today. Thanks for pushing the loop.'; note.className = 'visit-note ok'; return; }
const need = (start.seconds || 6) * 1000;
let visible = 0, since = document.visibilityState === 'visible' ? Date.now() : 0, sent = false;
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') since = Date.now();
else if (since) { visible += Date.now() - since; since = 0; }
});
const timer = setInterval(async () => {
const v = visible + (since ? Date.now() - since : 0);
const left = Math.max(0, Math.ceil((need - v) / 1000));
if (left > 0) { note.textContent = 'Your visit counts toward the next shift in ' + left + ' second' + (left === 1 ? '' : 's') + '.'; return; }
if (sent) return; sent = true; clearInterval(timer);
const r = await VBB.post('/api/visit/seen', { ticket: start.ticket, visibleMs: v }).catch(() => ({}));
if (r.counted) { note.className = 'visit-note ok'; note.textContent = r.shifted ? 'Counted, and your visit shifted the loop!' : 'Counted. You pushed the loop one step closer to the next shift.'; load(); }
else note.textContent = r.why === 'already' ? 'Your visit already counted today. Thanks for pushing the loop.' : 'Thanks for visiting the loop.';
}, 500);
})();