/* Live poll card for the overlay. Not part of the alert queue: it stays up while the poll runs. PollCard.update(phase, poll) phase: begin | progress | end poll: { id, title, choices:[{ id, title, votes }], ends_at, status } (status on end: completed | archived | terminated) Votes animate live; on 'completed' the winner(s) get a heart and a WINNER stamp, then the card drops away. */ (function () { const COLORS = ['#f4a0a8', '#a88be0', '#7fcfa6', '#f6c75b', '#8cc8f0']; const HEART = (window.ALERTS_BASE || '/alerts/') + 'art/Heart.png'; const SHOW_RESULT_MS = 9000; const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"' }[c])); let el = null, pollId = null, timer = null, hideTimer = null, endsAt = 0; function root() { if (!el) { el = document.createElement('div'); el.id = 'poll-card'; document.body.appendChild(el); } return el; } function build(poll) { const el = root(); clearTimeout(hideTimer); el.getAnimations().forEach(a => a.cancel()); pollId = poll.id; el.innerHTML = `
POLL
${esc(poll.title)}
${poll.choices.map((c, i) => `
${esc(c.title)}0
`).join('')}
WINNER
`; el.style.visibility = 'visible'; const tilt = (Math.random() * 4 - 2).toFixed(2); el.animate([ { transform:`translateX(-560px) rotate(-8deg)`, opacity:0 }, { transform:`translateX(10px) rotate(${+tilt + 1}deg)`, opacity:1, offset:.75 }, { transform:`translateX(0) rotate(${tilt}deg)`, opacity:1 }, ], { duration:650, easing:'cubic-bezier(.2,.8,.3,1)', fill:'forwards' }); } function votes(poll) { const total = poll.choices.reduce((s, c) => s + c.votes, 0); for (const c of poll.choices) { const row = el.querySelector(`.c[data-id="${CSS.escape(c.id)}"]`); if (!row) continue; const pct = total ? Math.round(100 * c.votes / total) : 0; row.querySelector('.n').textContent = `${c.votes} ยท ${pct}%`; row.querySelector('.fill').style.width = `${total ? Math.max(3, pct) : 0}%`; } return total; } function tick() { const s = Math.max(0, Math.round((endsAt - Date.now()) / 1000)); const t = el.querySelector('.timer'); if (t) t.textContent = `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')} left`; } function hide(delay) { clearTimeout(hideTimer); hideTimer = setTimeout(() => { el.animate([{ transform:'translateY(0)', opacity:1 }, { transform:'translateY(160px) rotate(10deg)', opacity:0 }], { duration:550, easing:'ease-in', fill:'forwards' }).finished.then(() => { el.style.visibility = 'hidden'; pollId = null; }).catch(() => {}); }, delay); } function update(phase, poll) { if (!poll || !poll.choices) return; if (phase === 'begin' || pollId !== poll.id) build(poll); votes(poll); if (poll.ends_at) { endsAt = Date.parse(poll.ends_at); clearInterval(timer); timer = setInterval(tick, 500); tick(); } if (phase !== 'end') return; clearInterval(timer); const t = el.querySelector('.timer'); if (poll.status !== 'completed') { if (t) t.textContent = 'cancelled'; hide(1500); return; } if (t) t.textContent = ''; // the WINNER stamp takes this corner const max = Math.max(...poll.choices.map(c => c.votes)); el.querySelectorAll('.c').forEach(row => { const c = poll.choices.find(x => x.id === row.dataset.id); const win = max > 0 && c.votes === max; row.classList.add(win ? 'win' : 'lose'); if (win) row.querySelector('.heart').animate([ { transform:'rotate(-40deg) scale(0)', opacity:0 }, { transform:'rotate(10deg) scale(1.3)', opacity:1, offset:.6 }, { transform:'rotate(6deg) scale(1)', opacity:1 }, ], { delay:300, duration:450, easing:'ease-out', fill:'forwards' }); }); if (max > 0) el.querySelector('.stamp').animate([ { transform:'rotate(-12deg) scale(2.2)', opacity:0 }, { transform:'rotate(-12deg) scale(.9)', opacity:1, offset:.7 }, { transform:'rotate(-12deg) scale(1)', opacity:1 }, ], { delay:700, duration:380, easing:'ease-in', fill:'forwards' }); hide(SHOW_RESULT_MS); } window.PollCard = { update }; })();