checkpoint
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// Inbox: loads messages as JSON (GET /messages) and marks them read (POST /messages/{id}/read).
|
||||
const inbox = document.querySelector('.inbox');
|
||||
const list = inbox.querySelector('.inbox-list');
|
||||
const status = inbox.querySelector('.inbox-status');
|
||||
const unreadOnly = inbox.querySelector('.inbox-unread-only');
|
||||
const t = inbox.dataset;
|
||||
const dateFormat = new Intl.DateTimeFormat(document.documentElement.lang, { dateStyle: 'medium', timeStyle: 'short' });
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const res = await fetch(`${t.api}?unreadOnly=${unreadOnly.checked}`, { headers: { Accept: 'application/json' } });
|
||||
if (!res.ok || res.redirected) throw new Error(res.status);
|
||||
render(await res.json());
|
||||
} catch {
|
||||
list.replaceChildren();
|
||||
status.textContent = t.failed;
|
||||
}
|
||||
}
|
||||
|
||||
// built with textContent only: message bodies are user input
|
||||
function render(messages) {
|
||||
status.textContent = messages.length ? '' : t.empty;
|
||||
list.replaceChildren(...messages.map(m => {
|
||||
const li = document.createElement('li');
|
||||
li.className = 'inbox-item' + (m.isRead ? ' is-read' : '');
|
||||
|
||||
const meta = document.createElement('div');
|
||||
meta.className = 'inbox-meta';
|
||||
const from = document.createElement('strong');
|
||||
from.textContent = m.anonymous ? t.anonymous : m.senderUsername;
|
||||
const time = document.createElement('time');
|
||||
time.dateTime = m.createdAt;
|
||||
time.textContent = dateFormat.format(new Date(m.createdAt));
|
||||
meta.append(from, time);
|
||||
|
||||
const body = document.createElement('p');
|
||||
body.className = 'inbox-body';
|
||||
body.textContent = m.body;
|
||||
|
||||
li.append(meta, body);
|
||||
|
||||
if (m.anonymous) {
|
||||
const agent = document.createElement('p');
|
||||
agent.className = 'inbox-agent';
|
||||
agent.textContent = m.senderUsername;
|
||||
li.append(agent);
|
||||
}
|
||||
|
||||
li.append(m.isRead ? readLabel() : markReadButton(m.id, li));
|
||||
return li;
|
||||
}));
|
||||
}
|
||||
|
||||
function readLabel() {
|
||||
const span = document.createElement('span');
|
||||
span.className = 'inbox-read';
|
||||
span.textContent = t.read;
|
||||
return span;
|
||||
}
|
||||
|
||||
function markReadButton(id, li) {
|
||||
const button = document.createElement('button');
|
||||
button.type = 'button';
|
||||
button.className = 'inbox-mark-read';
|
||||
button.textContent = t.markRead;
|
||||
button.addEventListener('click', async () => {
|
||||
button.disabled = true;
|
||||
const res = await fetch(`${t.api}/${id}/read`, {
|
||||
method: 'POST',
|
||||
headers: { RequestVerificationToken: t.token }
|
||||
});
|
||||
if (!res.ok) { button.disabled = false; return; }
|
||||
if (unreadOnly.checked) { li.remove(); if (!list.children.length) status.textContent = t.empty; return; }
|
||||
li.classList.add('is-read');
|
||||
button.replaceWith(readLabel());
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
unreadOnly.addEventListener('change', load);
|
||||
load();
|
||||
@@ -0,0 +1,12 @@
|
||||
// forms marked data-once can only be submitted once (no double messages / double registrations)
|
||||
document.addEventListener('submit', e => {
|
||||
const form = e.target;
|
||||
if (!form.matches('form[data-once]')) return;
|
||||
if (form.dataset.submitted) {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
form.dataset.submitted = 'true';
|
||||
// disable after the browser has collected the form data
|
||||
setTimeout(() => form.querySelectorAll('[type="submit"]').forEach(b => b.disabled = true));
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
// Gallery slideshow: prev/next, dots, arrow keys, autoplay that pauses on hover/focus.
|
||||
// The slides are a scroll-snap strip, so swiping on touch screens works natively.
|
||||
document.querySelectorAll('.slideshow').forEach(show => {
|
||||
const strip = show.querySelector('.slides');
|
||||
const slides = [...show.querySelectorAll('.slide')];
|
||||
const controls = show.querySelector('.slideshow-controls');
|
||||
if (!strip || slides.length < 2) return;
|
||||
|
||||
const dots = [...controls.querySelectorAll('.slide-dots button')];
|
||||
const toggle = controls.querySelector('.slide-toggle');
|
||||
const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||
const intervalMs = 5000;
|
||||
|
||||
let current = 0;
|
||||
let playing = !reducedMotion;
|
||||
let hovered = false;
|
||||
let timer = null;
|
||||
|
||||
controls.hidden = false;
|
||||
|
||||
function goTo(index) {
|
||||
current = (index + slides.length) % slides.length;
|
||||
strip.scrollTo({ left: current * strip.clientWidth, behavior: reducedMotion ? 'auto' : 'smooth' });
|
||||
update();
|
||||
}
|
||||
|
||||
function update() {
|
||||
dots.forEach((d, i) => d.setAttribute('aria-current', i === current ? 'true' : 'false'));
|
||||
toggle.textContent = playing ? '❚❚' : '▶';
|
||||
toggle.setAttribute('aria-label', playing ? toggle.dataset.pause : toggle.dataset.play);
|
||||
strip.setAttribute('aria-live', playing ? 'off' : 'polite');
|
||||
}
|
||||
|
||||
function schedule() {
|
||||
clearInterval(timer);
|
||||
if (playing && !hovered) timer = setInterval(() => goTo(current + 1), intervalMs);
|
||||
}
|
||||
|
||||
controls.querySelector('.slide-prev').addEventListener('click', () => { goTo(current - 1); schedule(); });
|
||||
controls.querySelector('.slide-next').addEventListener('click', () => { goTo(current + 1); schedule(); });
|
||||
dots.forEach((d, i) => d.addEventListener('click', () => { goTo(i); schedule(); }));
|
||||
toggle.addEventListener('click', () => { playing = !playing; update(); schedule(); });
|
||||
|
||||
show.addEventListener('keydown', e => {
|
||||
if (e.key === 'ArrowLeft') { goTo(current - 1); schedule(); }
|
||||
if (e.key === 'ArrowRight') { goTo(current + 1); schedule(); }
|
||||
});
|
||||
|
||||
// pause while the pointer or keyboard focus is inside
|
||||
show.addEventListener('pointerenter', () => { hovered = true; schedule(); });
|
||||
show.addEventListener('pointerleave', () => { hovered = false; schedule(); });
|
||||
show.addEventListener('focusin', () => { hovered = true; schedule(); });
|
||||
show.addEventListener('focusout', e => { if (!show.contains(e.relatedTarget)) { hovered = false; schedule(); } });
|
||||
|
||||
// keep the dots in sync when the user swipes or scrolls the strip
|
||||
let scrollEnd;
|
||||
strip.addEventListener('scroll', () => {
|
||||
clearTimeout(scrollEnd);
|
||||
scrollEnd = setTimeout(() => {
|
||||
current = Math.round(strip.scrollLeft / strip.clientWidth);
|
||||
update();
|
||||
}, 100);
|
||||
});
|
||||
addEventListener('resize', () => strip.scrollTo({ left: current * strip.clientWidth }));
|
||||
|
||||
update();
|
||||
schedule();
|
||||
});
|
||||
Reference in New Issue
Block a user