Files

80 lines
4.6 KiB
JavaScript

// Home page gate: (1) the sponsor's newest spot, (2) the whole board ~10 s, (3) the 3 newest spots, each on a timer,
// then Join unlocks. The server keeps the plan and checks every step's time; this script only drives the screens.
// Visits load inside a frame under our timer bar (IAP login-gate pattern); a site that refuses framing opens in a
// new tab and the timer runs from the moment it is opened.
(async function () {
await BL.renderNav('/'); BL.renderFooter();
const $ = BL.$;
const me = await BL.getJSON('/api/me').catch(() => ({}));
const g = await BL.getJSON('/api/gate');
if (g.error) { BL.status(g.error, 'err'); return; }
if (g.passed) return showOpen();
$('gate').hidden = false;
const labels = g.steps.map(s => s.type === 'board' ? 'The board' : s.role === 'sponsor' ? 'Your sponsor' : 'Newest spot');
const paint = i => { $('gateSteps').innerHTML = labels.map((l, k) => '<li class="' + (k < i ? 'done' : k === i ? 'on' : '') + '">' + (k + 1) + '. ' + BL.esc(l) + '</li>').join(''); };
let i = 0;
async function run() {
paint(i);
const st = g.steps[i]; const stage = $('gStage'); stage.innerHTML = ''; $('gNext').hidden = true; $('gOpen').hidden = true; $('gProg').style.width = '0';
let opened = st.type === 'board' || st.frame; // a new-tab visit starts its clock when the visitor opens it
const startClock = async () => {
const b = await BL.post('/api/gate/begin', { gid: g.gid, i });
if (b.error) { BL.status(b.error, 'err'); return; }
countdown(st.seconds, () => finish());
};
if (st.type === 'board') {
$('gWhat').textContent = 'The board: every spot shown here counts as a view.';
const box = document.createElement('div'); box.className = 'lots'; for (const t of st.tiles) box.appendChild(BL.tile(t)); stage.appendChild(box);
startClock();
} else {
const href = st.open + '?g=' + encodeURIComponent(g.gid) + '&i=' + i;
$('gOpen').href = href;
if (st.frame) {
$('gWhat').textContent = (st.role === 'sponsor' ? 'Your sponsor\'s newest spot' : st.title || 'Newest spot') + '. Have a look while the timer runs.';
const f = document.createElement('iframe'); f.className = 'gate-frame'; f.src = href; f.referrerPolicy = 'no-referrer';
f.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox');
stage.appendChild(f); $('gOpen').hidden = false; $('gOpen').textContent = 'Open site in a new tab';
startClock();
} else {
$('gWhat').textContent = (st.role === 'sponsor' ? 'Your sponsor\'s newest spot' : st.title || 'Newest spot') + ' opens in a new tab. The timer starts when you open it.';
const box = document.createElement('div'); box.className = 'gate-out';
const pic = BL.tile({ t: 'spot', img: st.img, href, id: st.ref }); box.appendChild(pic);
const b = document.createElement('a'); b.className = 'btn'; b.href = href; b.target = '_blank'; b.rel = 'noopener'; b.textContent = 'Open the site'; b.style.marginTop = '14px';
box.appendChild(b); stage.appendChild(box);
const go = () => { if (opened) return; opened = true; startClock(); };
b.addEventListener('click', go); pic.addEventListener('click', go);
$('gCount').textContent = st.seconds;
}
}
}
function countdown(sec, done) {
let left = sec * 10; $('gCount').textContent = sec;
const t = setInterval(() => {
left -= 1;
$('gCount').textContent = Math.max(0, Math.ceil(left / 10));
$('gProg').style.width = Math.min(100, 100 * (1 - left / (sec * 10))) + '%';
if (left <= 0) { clearInterval(t); done(); }
}, 100);
}
async function finish() {
const r = await BL.post('/api/gate/done', { gid: g.gid, i });
if (r.error) { BL.status(r.error, 'err'); return; }
if (r.passed) { paint(g.steps.length); $('gNext').hidden = false; $('gNext').textContent = 'Continue'; $('gWhat').textContent = 'Done. Join is open.'; $('gNext').onclick = () => { $('gate').hidden = true; showOpen(); }; return; }
$('gNext').hidden = false; $('gNext').textContent = 'Next';
$('gNext').onclick = () => { i += 1; run(); };
}
run();
async function showOpen() {
$('open').hidden = false;
const sp = me && me.sponsor;
if (BL.framed) { const j = $('joinBtn'); j.href = BL.withRef('/join', sp); j.target = '_blank'; j.rel = 'noopener'; }
const b = await BL.getJSON('/api/board');
const box = $('lots'); box.innerHTML = '';
for (const t of b.tiles || []) box.appendChild(BL.tile(t));
if (b.token) setTimeout(() => { if (document.visibilityState === 'visible') BL.post('/api/board/seen', { token: b.token }); }, 5500);
}
})();