/* Visitor sign in — admin console. */ const $ = (sel) => document.querySelector(sel); const $$ = (sel) => Array.from(document.querySelectorAll(sel)); let me = null; // the signed in admin let sites = []; let hosts = []; let activeSiteId = 'all'; // which site the console is showing /* ------------------------------------------------------------ plumbing */ async function api(path, { method = 'GET', body } = {}) { const res = await fetch(`/admin/api${path}`, { method, headers: body ? { 'Content-Type': 'application/json' } : undefined, body: body ? JSON.stringify(body) : undefined, }); const data = await res.json().catch(() => ({})); if (!res.ok) { // The session lapsed or was signed out in another tab. if (res.status === 401 || data.mustChangePassword) { toLogin(); } const error = new Error(data.error || `Request failed (${res.status}).`); error.payload = data; error.status = res.status; throw error; } return data; } /** Adds the site the console is currently scoped to. */ function scoped(path) { if (activeSiteId === 'all') return path; return path + (path.includes('?') ? '&' : '?') + `siteId=${activeSiteId}`; } let toastTimer = null; function toast(message, bad = false) { const el = $('#toast'); el.textContent = message; el.className = bad ? 'toast bad' : 'toast'; el.hidden = false; clearTimeout(toastTimer); toastTimer = setTimeout(() => { el.hidden = true; }, 5500); } const esc = (v) => String(v ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c] ); const stamp = (iso) => iso ? new Date(iso).toLocaleString('en-AU', { day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit', hour12: false, }) : '—'; function table(headings, rows, emptyMessage) { if (!rows.length) return `

${esc(emptyMessage)}

`; return `${headings.map((h) => ``).join('')}${rows.join('')}
${esc(h)}
`; } function field(label, name, value = '', type = 'text') { return ``; } /* ------------------------------------------------------------ session */ // Signing in happens on /admin/login, a page of its own. If the session is gone // or expires mid-use, go back there rather than trying to render a form here. function toLogin() { window.location.href = '/admin/login'; } $('#logout').addEventListener('click', async () => { await api('/logout', { method: 'POST' }).catch(() => {}); toLogin(); }); /* ---------------------------------------------------------------- tabs */ $$('.tab').forEach((tab) => { tab.addEventListener('click', () => { $$('.tab').forEach((t) => t.classList.toggle('on', t === tab)); $$('.panel').forEach((p) => p.classList.toggle('on', p.id === `panel-${tab.dataset.tab}`)); loadTab(tab.dataset.tab); }); }); function currentTab() { return $('.tab.on')?.dataset.tab || 'onsite'; } function loadTab(name) { const loaders = { onsite: loadOnsite, log: loadLog, recurring: loadRecurring, hosts: loadHosts, sites: loadSites, admins: loadAdmins, system: loadSystem, }; loaders[name]?.().catch((err) => toast(err.message, true)); } $('#site-filter').addEventListener('change', (event) => { activeSiteId = event.target.value; $('#csv-link').href = scoped('/admin/api/visits.csv'); loadAlerts(); loadTab(currentTab()); }); function siteName(id) { return sites.find((s) => s.id === id)?.name || '—'; } /* ------------------------------------------------------------- alerts */ async function loadAlerts() { const data = await api(scoped('/alerts')); const total = data.expired.length + data.expiring.length; const count = $('#alert-count'); count.hidden = total === 0; count.textContent = total; const banner = $('#expiry-banner'); if (!total) { banner.hidden = true; } else { const parts = []; if (data.expired.length) parts.push(`${data.expired.length} expired`); if (data.expiring.length) parts.push(`${data.expiring.length} expiring within ${data.warningDays} days`); banner.hidden = false; banner.className = data.expired.length ? 'banner bad' : 'banner'; banner.textContent = `WWCC / VIT checks need attention: ${parts.join(', ')}.`; } return data; } function expiryCell(expiry, checkExpiry) { if (!checkExpiry) return 'No date'; if (expiry.status === 'expired') { return `Expired ${Math.abs(expiry.daysLeft)}d ago`; } if (expiry.status === 'expiring') { return `${expiry.daysLeft}d left`; } return `${esc(checkExpiry)}`; } /* -------------------------------------------------------------- on site */ async function loadOnsite() { const rows = await api(scoped('/onsite')); $('#onsite-count').textContent = rows.length; const showSite = activeSiteId === 'all' && sites.length > 1; $('#onsite-table').innerHTML = table( ['', 'Visitor', showSite ? 'Site' : 'Visiting', ...(showSite ? ['Visiting'] : []), 'Check', 'Contact', 'Signed in', ''], rows.map( (v) => ` ${v.hasPhoto ? `` : ''} ${esc(v.firstName)} ${esc(v.lastName)} ${v.visitorType === 'frequent' ? 'Recurring' : ''} ${v.company ? `${esc(v.company)}` : ''} ${showSite ? `${esc(v.siteName || '—')}` : ''} ${esc(v.hostName)} ${v.checkType === 'NONE' ? 'None' : `${esc(v.checkType)} ${esc(v.checkNumber || '')}`} ${esc(v.phone || v.email || '—')} ${stamp(v.signedInAt)} ` ), 'Nobody is signed in right now.' ); $$('[data-signout]').forEach((btn) => btn.addEventListener('click', async () => { await api(`/visits/${btn.dataset.signout}/signout`, { method: 'POST' }); toast('Signed out.'); loadOnsite(); }) ); $$('[data-badge]').forEach((btn) => btn.addEventListener('click', () => window.open(`/admin/api/badge/${btn.dataset.badge}`, '_blank')) ); $$('[data-print]').forEach((btn) => btn.addEventListener('click', async () => { btn.disabled = true; try { await api(`/visits/${btn.dataset.print}/print`, { method: 'POST' }); toast('Sent to the printer.'); } catch (err) { toast(err.message, true); } finally { btn.disabled = false; } }) ); } $('#refresh-onsite').addEventListener('click', () => loadOnsite()); /* ------------------------------------------------------------ visit log */ async function loadLog() { const params = new URLSearchParams(); if ($('#log-from').value) params.set('from', $('#log-from').value); if ($('#log-to').value) params.set('to', $('#log-to').value); if ($('#log-q').value.trim()) params.set('q', $('#log-q').value.trim()); const rows = await api(scoped(`/visits?${params}`)); const showSite = activeSiteId === 'all' && sites.length > 1; $('#log-table').innerHTML = table( [...(showSite ? ['Site'] : []), 'Visitor', 'Visiting', 'Check', 'Contact', 'In', 'Out', 'Photo'], rows.map( (v) => ` ${showSite ? `${esc(v.siteName || '—')}` : ''} ${esc(v.firstName)} ${esc(v.lastName)} ${v.company ? `${esc(v.company)}` : ''} ${esc(v.hostName)} ${v.checkType === 'NONE' ? '—' : `${esc(v.checkType)} ${esc(v.checkNumber || '')}`} ${esc(v.phone || v.email || '—')} ${stamp(v.signedInAt)} ${ v.signedOutAt ? `${stamp(v.signedOutAt)}${v.signedOutBy && v.signedOutBy !== 'visitor' ? ` ${esc(v.signedOutBy)}` : ''}` : 'On site' } ${v.hasPhoto ? `View` : '—'} ` ), 'No visits match those filters.' ); } $('#log-search').addEventListener('click', () => loadLog()); $('#log-q').addEventListener('keydown', (e) => { if (e.key === 'Enter') loadLog(); }); /* ------------------------------------------------- recurring visitors */ async function loadRecurring() { const [rows, alerts] = await Promise.all([api(scoped('/frequent')), loadAlerts()]); $('#expiry-summary').innerHTML = alerts.expired.length + alerts.expiring.length ? `

${alerts.expired.length} expired, ${alerts.expiring.length} expiring within ${alerts.warningDays} days. Ask them for an updated card before their next visit.

` : ''; $('#recurring-table').innerHTML = table( ['', 'Visitor', 'Mobile', 'Site', 'Check', 'Expiry', 'Status', ''], rows.map( (p) => ` ${esc(p.firstName)} ${esc(p.lastName)} ${p.company ? `${esc(p.company)}` : ''} ${p.email ? `${esc(p.email)}` : ''} ${esc(p.phone)} ${p.siteId ? esc(siteName(p.siteId)) : 'Any site'} ${p.checkType === 'NONE' ? 'None' : `${esc(p.checkType)} ${esc(p.checkNumber || '')}`} ${p.checkType === 'NONE' ? '—' : expiryCell(p.expiry, p.checkExpiry)} ${p.active ? 'Active' : 'Inactive'} ` ), 'No recurring visitors yet. Add one so they can sign in with a PIN.' ); $$('[data-pass]').forEach((btn) => btn.addEventListener('click', () => window.open(`/admin/api/pass/${btn.dataset.pass}`, '_blank')) ); $$('[data-pin]').forEach((btn) => btn.addEventListener('click', async () => { if (!confirm('Issue a new PIN? The old one stops working straight away.')) return; const { pin } = await api(`/frequent/${btn.dataset.pin}/pin`, { method: 'POST' }); showPin(pin, btn.dataset.pin); }) ); $$('[data-edit-freq]').forEach((btn) => btn.addEventListener('click', async () => { openRecurringModal(await api(`/frequent/${btn.dataset.editFreq}`)); }) ); $$('[data-remove-freq]').forEach((btn) => btn.addEventListener('click', async () => { try { confirmRemoveFrequent(await api(`/frequent/${btn.dataset.removeFreq}`)); } catch (err) { toast(err.message, true); } }) ); } function hostOptions(selectedId) { return ( '' + hosts .filter((h) => h.active) .map( (h) => `` ) .join('') ); } function siteOptions(selectedId, { anyLabel = 'Any site' } = {}) { return ( `` + sites .map( (s) => `` ) .join('') ); } function openModal( title, bodyHtml, onSave, { saveLabel = 'Save', hideCancel = false, destructive = false, onOpen, onClose } = {} ) { $('#modal-title').textContent = title; $('#modal-body').innerHTML = bodyHtml; $('#modal-save').textContent = saveLabel; $('#modal-cancel').hidden = hideCancel; const modal = $('#modal'); modal.classList.toggle('destructive', destructive); modal.returnValue = ''; modal.showModal(); onOpen?.(); modal.onclose = () => { $('#modal-cancel').hidden = false; $('#modal-save').textContent = 'Save'; modal.classList.remove('destructive'); onClose?.(); if (modal.returnValue === 'save') onSave?.(new FormData($('#modal-form'))); }; } /* --------------------------------------------------- visitor photo editor */ // A recurring visitor can have a photo kept on file, so the kiosk never asks them // to pose again. It can come from this machine's camera or from a file. const photoEditor = { dataUrl: null, remove: false, stream: null }; function photoEditorMarkup(person) { const existing = person?.hasPhoto ? `/admin/api/frequent/${person.id}/photo?t=${Date.now()}` : null; return `
Photo on file

No photo on file

With a photo saved here, this visitor signs in with their PIN and their pass prints straight away — the kiosk does not ask them to pose.

`; } function wirePhotoEditor() { photoEditor.dataUrl = null; photoEditor.remove = false; const preview = $('#photo-preview'); const video = $('#photo-video'); const empty = $('#photo-empty'); const showImage = (src) => { preview.src = src; preview.hidden = false; video.hidden = true; empty.hidden = true; $('#photo-shoot').hidden = true; $('#photo-camera').textContent = 'Retake'; $('#photo-clear').hidden = false; }; $('#photo-camera').addEventListener('click', async () => { try { photoEditor.stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user', width: { ideal: 960 } }, audio: false, }); video.srcObject = photoEditor.stream; video.hidden = false; preview.hidden = true; empty.hidden = true; $('#photo-shoot').hidden = false; } catch { toast('No camera available on this machine. Upload a file instead.', true); } }); $('#photo-shoot').addEventListener('click', () => { // Square, cropped from the centre, to match the kiosk camera and the badge. const side = Math.min(video.videoWidth, video.videoHeight); if (!side) return toast('The camera is not ready yet. Try again in a moment.', true); const canvas = document.createElement('canvas'); canvas.width = 640; canvas.height = 640; canvas .getContext('2d') .drawImage(video, (video.videoWidth - side) / 2, (video.videoHeight - side) / 2, side, side, 0, 0, 640, 640); photoEditor.dataUrl = canvas.toDataURL('image/jpeg', 0.72); photoEditor.remove = false; stopPhotoCamera(); showImage(photoEditor.dataUrl); }); $('#photo-file').addEventListener('change', async (event) => { const file = event.target.files[0]; if (!file) return; if (file.size > 4 * 1024 * 1024) return toast('That image is over 4 MB. Use a smaller one.', true); const reader = new FileReader(); reader.onload = () => { photoEditor.dataUrl = reader.result; photoEditor.remove = false; stopPhotoCamera(); showImage(reader.result); }; reader.readAsDataURL(file); }); $('#photo-clear').addEventListener('click', () => { photoEditor.dataUrl = null; photoEditor.remove = true; stopPhotoCamera(); preview.hidden = true; video.hidden = true; empty.hidden = false; $('#photo-shoot').hidden = true; $('#photo-clear').hidden = true; $('#photo-camera').textContent = 'Use the camera'; }); } function stopPhotoCamera() { if (!photoEditor.stream) return; photoEditor.stream.getTracks().forEach((t) => t.stop()); photoEditor.stream = null; const video = $('#photo-video'); if (video) video.srcObject = null; } function openRecurringModal(person = null) { const editing = Boolean(person); openModal( editing ? `Edit ${person.firstName} ${person.lastName}` : 'New recurring visitor', ` ${field('First name', 'firstName', person?.firstName)} ${field('Last name', 'lastName', person?.lastName)} ${field('Company or organisation (optional)', 'company', person?.company)} ${field('Mobile number (their username)', 'phone', person?.phone, 'tel')} ${field('Email address', 'email', person?.email, 'email')} ${field('Check number', 'checkNumber', person?.checkNumber)} ${field('Check expires', 'checkExpiry', person?.checkExpiry, 'date')} ${field('PIN (leave blank to generate one)', 'pin', '')} ${field('Notes', 'notes', person?.notes)} ${photoEditorMarkup(person)} ${editing ? `` : ''} `, async (form) => { const payload = Object.fromEntries(form.entries()); payload.active = editing ? form.has('active') : true; if (!payload.pin) delete payload.pin; if (photoEditor.dataUrl) payload.photo = photoEditor.dataUrl; if (photoEditor.remove) payload.removePhoto = true; try { const saved = editing ? await api(`/frequent/${person.id}`, { method: 'PATCH', body: payload }) : await api('/frequent', { method: 'POST', body: payload }); loadRecurring(); if (editing) toast('Saved.'); else showPin(saved.pin, saved.id); } catch (err) { toast(err.message, true); } }, { onOpen: wirePhotoEditor, onClose: stopPhotoCamera } ); } /** * Removing a saved visitor is permanent, so the confirmation spells out what * happens: the record and its PIN go, the visit history stays. Deactivating is * offered alongside for the common case of someone who has simply stopped coming. */ function confirmRemoveFrequent(person) { const name = `${person.firstName} ${person.lastName}`; openModal( `Remove ${name}?`, `${ person.onSite ? `

${esc(person.firstName)} is signed in right now. Removing the record will not sign them out — their visit stays open and they can still sign out with their last name and mobile number.

` : '' }

If they might come back, untick Active in Edit instead — that keeps the record and their history intact.

`, async () => { try { const result = await api(`/frequent/${person.id}?force=1`, { method: 'DELETE' }); toast(`${result.name} removed. ${result.visitsKept} past visit(s) kept in the log.`); loadRecurring(); } catch (err) { toast(err.message, true); } }, { saveLabel: 'Remove permanently', destructive: true } ); } function showPin(pin, id) { openModal( 'PIN issued', `

${esc(pin)}

Print the card now, or write this down. You can reprint it later from the recurring visitors list.

`, () => window.open(`/admin/api/pass/${id}`, '_blank'), { saveLabel: 'Print card' } ); } $('#new-recurring').addEventListener('click', () => openRecurringModal()); /* --------------------------------------------------------------- hosts */ async function loadHosts() { hosts = await api(scoped('/hosts')); $('#hosts-scope').textContent = activeSiteId === 'all' ? 'Showing every site. Pick a single site above before adding or importing people.' : `Showing ${siteName(Number(activeSiteId))}.`; const showSite = activeSiteId === 'all' && sites.length > 1; $('#hosts-table').innerHTML = table( [...(showSite ? ['Site'] : []), 'Name', 'Area', 'Email', 'Status', ''], hosts.map( (h) => ` ${showSite ? `${esc(siteName(h.site_id))}` : ''} ${esc(h.name)} ${esc(h.area || '—')} ${esc(h.email || '—')} ${h.active ? 'Shown' : 'Hidden'} ` ), 'No one is listed yet. Add people, or import a CSV, so visitors can say who they are seeing.' ); $$('[data-edit-host]').forEach((btn) => btn.addEventListener('click', () => openHostModal(hosts.find((h) => h.id === Number(btn.dataset.editHost))) ) ); $$('[data-toggle-host]').forEach((btn) => btn.addEventListener('click', async () => { const host = hosts.find((h) => h.id === Number(btn.dataset.toggleHost)); await api(`/hosts/${host.id}`, { method: 'PATCH', body: { active: !host.active } }); loadHosts(); }) ); } function openHostModal(host = null) { if (!host && activeSiteId === 'all' && sites.length > 1) { return toast('Choose a single site above first, so the person lands in the right list.', true); } openModal( host ? `Edit ${host.name}` : `Add a person to ${siteName(Number(activeSiteId))}`, `${field('Name', 'name', host?.name)} ${field('Area, team or role', 'area', host?.area)} ${field('Email address', 'email', host?.email, 'email')}`, async (form) => { const payload = Object.fromEntries(form.entries()); try { if (host) await api(`/hosts/${host.id}`, { method: 'PATCH', body: payload }); else await api('/hosts', { method: 'POST', body: { ...payload, siteId: activeSiteId } }); loadHosts(); toast('Saved.'); } catch (err) { toast(err.message, true); } } ); } $('#new-host').addEventListener('click', () => openHostModal()); $('#host-file').addEventListener('change', async (event) => { const file = event.target.files[0]; if (file) $('#host-csv').value = await file.text(); }); $('#do-host-import').addEventListener('click', async () => { const csv = $('#host-csv').value.trim(); if (!csv) return toast('Paste a CSV or choose a file first.', true); if (activeSiteId === 'all' && sites.length > 1) { return toast('Choose a single site above before importing.', true); } try { const result = await api('/hosts/import', { method: 'POST', body: { csv, replace: $('#host-replace').checked, siteId: activeSiteId }, }); toast(`${result.added} added, ${result.updated} updated. ${result.total} people listed.`); $('#host-csv').value = ''; loadHosts(); } catch (err) { toast(err.message, true); } }); /* --------------------------------------------------------------- sites */ async function loadSites() { sites = await api('/sites'); renderSiteFilter(); $('#sites-list').innerHTML = sites .map( (s) => `

${esc(s.name)} ${s.active ? '' : 'Inactive'}

Kiosk address: /?site=${esc(s.slug)}

${s.printer.enabled ? `` : ''}
Badge printing
${ s.badge.enabled ? `On — ${s.badge.widthMm} × ${s.badge.heightMm} mm${s.badge.showPhoto ? ', with photo' : ''}${s.badge.accent ? ', two-colour' : ''}` : 'Off' }
${s.badge.note ? `
Badge note
${esc(s.badge.note)}
` : ''}
Printer
${ s.printer.enabled && s.printer.host ? `${esc(s.printer.model)} at ${esc(s.printer.host)}:${s.printer.port}${ s.printer.rotate ? `, rotated ${s.printer.rotate}°` : '' }${ s.printerStatus ? s.printerStatus.ok ? ` last print ok, ${stamp(s.printerStatus.at)}` : ` ${esc(s.printerStatus.message)}` : '' }` : 'Printed by the kiosk browser' }
Kiosk branding
${s.branding.hasBanner ? `Banner set, ${s.branding.bannerAlign === 'center' ? 'centred' : 'left'}` : 'No banner'} ·
` ) .join(''); $$('[data-edit-site]').forEach((btn) => btn.addEventListener('click', () => openSiteModal(sites.find((s) => s.id === Number(btn.dataset.editSite))) ) ); $$('[data-preview-badge]').forEach((btn) => btn.addEventListener('click', () => window.open(`/admin/api/sites/${btn.dataset.previewBadge}/badge-preview`, '_blank') ) ); $$('[data-bitmap]').forEach((btn) => btn.addEventListener('click', () => window.open(`/admin/api/sites/${btn.dataset.bitmap}/badge-bitmap`, '_blank') ) ); $$('[data-test-print]').forEach((btn) => btn.addEventListener('click', async () => { btn.disabled = true; btn.textContent = 'Printing…'; try { await api(`/sites/${btn.dataset.testPrint}/test-print`, { method: 'POST' }); toast('Sent to the printer.'); } catch (err) { toast(err.message, true); } finally { btn.disabled = false; btn.textContent = 'Test print'; loadSites(); } }) ); } /** * Label stock, so nobody has to measure a roll. The Brother QL-820NWB takes * 12–62 mm wide media and prints up to 60.96 mm across, so anything wider than * 62 mm is for a different printer. */ const LABEL_PRESETS = [ { id: 'dk22205-90', label: 'Brother DK-22205 continuous, cut at 90 mm', w: 62, h: 90, photo: true }, { id: 'dk11202', label: 'Brother DK-11202 die-cut 62 × 100 mm', w: 62, h: 100, photo: true }, { id: 'dk22251-90', label: 'Brother DK-22251 black/red continuous, cut at 90 mm', w: 62, h: 90, photo: true, accent: true }, { id: 'dk11208', label: 'Brother DK-11208 die-cut 38 × 90 mm', w: 38, h: 90, photo: false }, { id: 'dk11209', label: 'Brother DK-11209 die-cut 29 × 62 mm', w: 29, h: 62, photo: false }, { id: 'dk11201', label: 'Brother DK-11201 die-cut 29 × 90 mm', w: 29, h: 90, photo: false }, { id: 'card', label: 'Card size 86 × 54 mm (not a QL-820NWB size)', w: 86, h: 54, photo: true }, { id: 'dymo99014', label: 'Dymo 99014 101 × 54 mm', w: 101, h: 54, photo: true }, ]; function openSiteModal(site) { openModal( `Edit ${site.name}`, `${field('Site name', 'name', site.name)} ${field('Kiosk slug', 'slug', site.slug)}

Red needs a two-colour roll such as the Brother DK-22251. On any other roll it prints as grey. Two-colour printing is also much slower than black alone.

With this on, the kiosk does not print at all — the server sends the badge to the printer over the network, so a tablet needs no driver and no default printer. At 90° the badge is laid out along the length of the label and turned, which reads correctly when the label hangs from its short edge. Check it with Bitmap preview before using a roll.

${field('Line printed at the bottom', 'note', site.badge.note)} ${colourField('Body text', 'text', site.branding.text, site.branding.theme.ink)}

Text on the bar and on buttons is chosen automatically for contrast, so a pale brand colour gets dark text rather than white. Labels and hints are a softened version of the body text, kept readable against the background. Clear a box for the default.

`, async (form) => { const data = Object.fromEntries(form.entries()); try { await api(`/sites/${site.id}`, { method: 'PATCH', body: { name: data.name, slug: data.slug, active: form.has('active'), badge: { enabled: form.has('badgeEnabled'), widthMm: Number(data.widthMm), heightMm: Number(data.heightMm), showPhoto: form.has('showPhoto'), accent: form.has('accent'), note: data.note, }, printer: { enabled: form.has('printerEnabled'), host: data.printerHost, port: Number(data.printerPort) || 9100, model: data.printerModel, rotate: Number(data.printerRotate) || 0, }, branding: { brand: data.brand || null, signout: data.signout || null, page: data.page || null, text: data.text || null, bannerHeight: Number(data.bannerHeight) || 64, bannerAlign: data.bannerAlign, }, }, }); if (bannerEditor.dataUrl) { await api(`/sites/${site.id}/banner`, { method: 'POST', body: { image: bannerEditor.dataUrl }, }); } else if (bannerEditor.remove) { await api(`/sites/${site.id}/banner`, { method: 'DELETE' }); } toast('Site saved. Reload the kiosk to see the change.'); loadSites(); } catch (err) { toast(err.message, true); } }, { onOpen: () => { wireBadgePreset(); wireBannerEditor(); }, } ); } /** * A colour box paired with a text field, so a colour can be picked by eye or * pasted from a brand guide, and cleared entirely to fall back to the default. */ function colourField(label, name, value, fallback) { const current = value || ''; return ``; } /** WCAG contrast ratio, mirroring the server so the console can warn as you type. */ function contrastRatio(a, b) { const lum = (hex) => { const [r, g, bl] = [1, 3, 5] .map((i) => parseInt(hex.slice(i, i + 2), 16) / 255) .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4)); return 0.2126 * r + 0.7152 * g + 0.0722 * bl; }; const [hi, lo] = [lum(a), lum(b)].sort((x, y) => y - x); return (hi + 0.05) / (lo + 0.05); } const bannerEditor = { dataUrl: null, remove: false }; function wireBannerEditor() { bannerEditor.dataUrl = null; bannerEditor.remove = false; // Keep the swatch and the hex box in step, in both directions. $$('#modal-form [data-colour-for]').forEach((swatch) => { const text = $(`#modal-form [name="${swatch.dataset.colourFor}"]`); swatch.addEventListener('input', () => { text.value = swatch.value; }); text.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(text.value.trim())) swatch.value = text.value.trim(); }); }); // Live contrast readout, because a colour that looks fine in a swatch can be // unreadable as body text. const pageInput = $('#modal-form [name="page"]'); const textInput = $('#modal-form [name="text"]'); const note = $('#contrast-note'); const showContrast = () => { const page = pageInput.value.trim() || pageInput.placeholder; const text = textInput.value.trim() || textInput.placeholder; if (!/^#[0-9a-fA-F]{6}$/.test(page) || !/^#[0-9a-fA-F]{6}$/.test(text)) { note.hidden = true; return; } const ratio = contrastRatio(text, page); note.hidden = false; if (ratio >= 7) { note.className = 'hint'; note.textContent = `Contrast ${ratio.toFixed(1)}:1 — comfortable at arm's length.`; } else if (ratio >= 4.5) { note.className = 'hint'; note.textContent = `Contrast ${ratio.toFixed(1)}:1 — readable, but aim for 7:1 on a kiosk people read standing up.`; } else { note.className = 'hint warn'; note.textContent = `Contrast only ${ratio.toFixed(1)}:1. This will be hard to read — pick a darker or lighter body text.`; } }; [pageInput, textInput].forEach((el) => el.addEventListener('input', showContrast)); showContrast(); $('#banner-file').addEventListener('change', (event) => { const file = event.target.files[0]; if (!file) return; if (file.size > 2 * 1024 * 1024) return toast('That image is over 2 MB. Use a smaller one.', true); const reader = new FileReader(); reader.onload = () => { bannerEditor.dataUrl = reader.result; bannerEditor.remove = false; $('#banner-preview').src = reader.result; $('#banner-preview').hidden = false; $('#banner-empty').hidden = true; $('#banner-clear').hidden = false; }; reader.readAsDataURL(file); }); $('#banner-clear').addEventListener('click', () => { bannerEditor.dataUrl = null; bannerEditor.remove = true; $('#banner-preview').hidden = true; $('#banner-preview').removeAttribute('src'); $('#banner-empty').hidden = false; $('#banner-clear').hidden = true; }); } function wireBadgePreset() { const width = $('#modal-form [name="widthMm"]'); const height = $('#modal-form [name="heightMm"]'); const warning = $('#badge-warning'); const check = () => { const w = Number(width.value); const h = Number(height.value); if (w > 62) { warning.hidden = false; warning.className = 'hint warn'; warning.textContent = `${w} mm is wider than a QL-820NWB can take — it handles 12 to 62 mm media, printing up to 60.96 mm across. Fine for a different printer.`; } else if (h < w * 1.2 && $('#badge-photo').checked && w <= 40) { warning.hidden = false; warning.className = 'hint warn'; warning.textContent = 'A photo on a label this narrow leaves very little room for the name. Consider turning the photo off.'; } else { warning.hidden = true; } }; $('#badge-preset').addEventListener('change', (event) => { const preset = LABEL_PRESETS.find((p) => p.id === event.target.value); if (!preset) return; width.value = preset.w; height.value = preset.h; $('#badge-photo').checked = preset.photo; $('#badge-accent').checked = Boolean(preset.accent); check(); }); [width, height].forEach((el) => el.addEventListener('input', () => { $('#badge-preset').value = ''; check(); })); $('#badge-photo').addEventListener('change', check); check(); } $('#new-site').addEventListener('click', () => { openModal('Add a site', field('Site name', 'name', ''), async (form) => { try { await api('/sites', { method: 'POST', body: { name: form.get('name') } }); toast('Site added. Set its badge options next.'); loadSites(); } catch (err) { toast(err.message, true); } }); }); function renderSiteFilter() { const select = $('#site-filter'); const scopedToOne = Boolean(me?.siteId); $('#site-switch').hidden = sites.length < 2 && !scopedToOne; select.innerHTML = (scopedToOne ? '' : '') + sites.map((s) => ``).join(''); if (scopedToOne) activeSiteId = String(me.siteId); select.value = activeSiteId; $('#csv-link').href = scoped('/admin/api/visits.csv'); } /* -------------------------------------------------------------- admins */ async function loadAdmins() { if (me.role !== 'owner') { $('#admins-table').innerHTML = '

Only an owner account can manage admins.

'; return; } const rows = await api('/users'); $('#admins-note').textContent = me.domainRule ? `New accounts must use an ${me.domainRule} address.` : 'Any email address can be used for an admin account.'; $('#admins-table').innerHTML = table( ['Email', 'Name', 'Role', 'Site', 'Two factor', 'Last sign in', ''], rows.map( (u) => ` ${esc(u.email)}${u.active ? '' : ' Disabled'} ${esc(u.name || '—')} ${esc(u.role)} ${u.siteId ? esc(siteName(u.siteId)) : 'All sites'} ${u.twoFactorOn ? 'On' : 'Not set up'} ${stamp(u.lastLoginAt)} ` ), 'No admin accounts.' ); $$('[data-edit-user]').forEach((btn) => btn.addEventListener('click', () => { const user = rows.find((u) => u.id === Number(btn.dataset.editUser)); openModal( `Edit ${user.email}`, `${field('Name', 'name', user.name)} `, async (form) => { try { await api(`/users/${user.id}`, { method: 'PATCH', body: { name: form.get('name'), role: form.get('role'), siteId: form.get('siteId') || null, active: form.has('active'), }, }); toast('Saved.'); loadAdmins(); } catch (err) { toast(err.message, true); } } ); }) ); $$('[data-reset-pw]').forEach((btn) => btn.addEventListener('click', async () => { if (!confirm('Reset this password? They will have to set a new one at next sign in.')) return; const { temporaryPassword } = await api(`/users/${btn.dataset.resetPw}/reset-password`, { method: 'POST', }); openModal( 'Temporary password', `

${esc(temporaryPassword)}

Give this to them in person or over the phone. They will be asked to change it as soon as they sign in.

`, null, { saveLabel: 'Done', hideCancel: true } ); }) ); $$('[data-reset-2fa]').forEach((btn) => btn.addEventListener('click', async () => { if (!confirm('Clear their two factor setup? They will enrol again at next sign in.')) return; await api(`/users/${btn.dataset.reset2fa}/reset-2fa`, { method: 'POST' }); toast('Two factor cleared.'); loadAdmins(); }) ); } $('#new-admin').addEventListener('click', () => { openModal( 'Invite an admin', `${field('Email address', 'email', '', 'email')} ${field('Name', 'name', '')} `, async (form) => { try { const created = await api('/users', { method: 'POST', body: { email: form.get('email'), name: form.get('name'), role: form.get('role'), siteId: form.get('siteId') || null, }, }); loadAdmins(); openModal( 'Account created', `

Temporary password for ${esc(created.email)}:

${esc(created.temporaryPassword)}

They will set their own password and enrol two factor at first sign in.

`, null, { saveLabel: 'Done', hideCancel: true } ); } catch (err) { toast(err.message, true); } } ); }); /* -------------------------------------------------------------- system */ async function loadSystem() { const s = await api(scoped('/status')); $('#system-body').innerHTML = `
Time zone
${esc(s.timezone)}
Sites
${s.siteCount}
Photo required
${s.requirePhoto ? 'Yes' : 'No'}
Photos kept for
${s.photoRetentionDays} days
Expiry warning
${s.expiryWarningDays} days before a WWCC or VIT lapses
Nightly auto sign out
${s.autoSignOutTime ? esc(s.autoSignOutTime) : 'Off'}
Two factor
${s.require2fa ? 'Required for every admin' : 'Optional'}
Admin email domain
${s.domainRule ? esc(s.domainRule) : 'Any address'}
On site now
${s.onSite}
Google Sheet
${ s.sheets.enabled ? `Mirroring who is on site to the "${esc(s.sheets.tab)}" tab${ s.sheets.stale ? ' waiting to retry' : '' }${s.sheets.lastError ? `
${esc(s.sheets.lastError)}` : ''}` : 'Turned off in the environment file.' }
${ s.sheets.enabled ? `
On the sheet
${ s.sheets.onSiteCount === null ? 'Not written yet' : `${s.sheets.onSiteCount} on site` }, last written ${stamp(s.sheets.lastOk)}
Service account
${ s.sheets.serviceAccount ? `${esc(s.sheets.serviceAccount)}
The spreadsheet must be shared with this address, with Editor access.` : 'No key file could be read' }
Spreadsheet
${ s.sheets.spreadsheetId ? `open the sheet` : 'SHEETS_SPREADSHEET_ID is not set' }
` : '' }

Certificate

${renderTls(s.tls)}`; $('#sheet-test').addEventListener('click', async () => { try { const r = await api('/sheets/test', { method: 'POST' }); toast(`Connected to "${r.title}".`); loadSystem(); } catch (err) { toast(err.message, true); } }); $('#sheet-resync').addEventListener('click', async () => { try { const r = await api('/sheets/resync', { method: 'POST' }); toast( r.skipped ? 'Sheet mirroring is off.' : `Sheet rewritten with ${r.rows} ${r.rows === 1 ? 'person' : 'people'} on site.` ); loadSystem(); } catch (err) { toast(err.message, true); } }); $('#renew-cert')?.addEventListener('click', async () => { try { const r = await api('/tls/renew', { method: 'POST', body: { newCa: false } }); toast( r.info.server ? `Certificate good until ${new Date(r.info.server.validTo).toLocaleDateString('en-AU')}.` : 'Certificate checked.' ); loadSystem(); } catch (err) { toast(err.message, true); } }); $('#new-ca')?.addEventListener('click', async () => { const warning = 'Create a brand new certificate authority?' + '\n\n' + 'Every kiosk device will show a warning until you install the new CA file on it. ' + 'Only do this if the old key may have leaked.'; if (!confirm(warning)) return; try { await api('/tls/renew', { method: 'POST', body: { newCa: true } }); toast('New authority created. Install it on every kiosk device.'); loadSystem(); } catch (err) { toast(err.message, true); } }); $('#photo-purge').addEventListener('click', async () => { if (!confirm('Delete photos older than the retention window? This cannot be undone.')) return; const r = await api('/photos/purge', { method: 'POST' }); toast(`${r.purged} photo(s) deleted.`); }); renderAccount(s); $$('.owner-only').forEach((el) => { el.hidden = me.role !== 'owner'; }); } function renderTls(tls) { if (!tls?.enabled) { return `

HTTPS is off, so the kiosk camera will only work on localhost. Set HTTPS_ENABLED=true in the environment file and restart.

`; } if (!tls.server) { return '

HTTPS is on but no certificate could be read.

'; } const soon = tls.server.daysLeft < 30; return `
Server certificate
Valid until ${new Date(tls.server.validTo).toLocaleDateString('en-AU')} ${tls.server.daysLeft} days
Valid for
${esc(tls.server.names.join(', '))}
Authority expires
${tls.ca ? new Date(tls.ca.validTo).toLocaleDateString('en-AU') : '—'} ${tls.ca ? `${tls.ca.daysLeft} days` : ''}
CA fingerprint
${esc(tls.ca?.fingerprint || '—')}

Install the CA file on each kiosk device once. The server certificate renews itself before it lapses, and devices that trust the authority keep working without being touched again.

Download the CA certificate
`; } function renderAccount(status) { $('#account-body').innerHTML = `
Signed in as
${esc(me.email)}
Role
${esc(me.role)}${me.siteId ? ` — ${esc(siteName(me.siteId))} only` : ''}
Two factor
${me.twoFactorOn ? 'On' : 'Not set up'}
${me.twoFactorOn ? status.require2fa ? '' : '' : ''}
`; $('#change-password').addEventListener('click', () => { openModal( 'Change your password', `${field('Current password', 'currentPassword', '', 'password')} ${field('New password', 'newPassword', '', 'password')}

At least 12 characters, with upper and lower case and a number.

`, async (form) => { try { await api('/account/password', { method: 'POST', body: { currentPassword: form.get('currentPassword'), newPassword: form.get('newPassword'), }, }); toast('Password changed.'); } catch (err) { toast(err.message, true); } } ); }); $('#enable-2fa')?.addEventListener('click', async () => { const { qr, secret } = await api('/account/2fa/start', { method: 'POST' }); openModal( 'Set up two factor', `

Scan this with your authenticator app, then enter the code it shows.

Two factor QR code

Or enter this key by hand: ${esc(secret)}

${field('6 digit code', 'code', '')}`, async (form) => { try { const r = await api('/account/2fa/enable', { method: 'POST', body: { code: form.get('code') } }); me.twoFactorOn = true; openModal( 'Recovery codes', `

Each of these works once if you lose your phone. Save them somewhere safe.

`, null, { saveLabel: 'Done', hideCancel: true } ); } catch (err) { toast(err.message, true); } }, { saveLabel: 'Turn on' } ); }); $('#disable-2fa')?.addEventListener('click', () => { openModal( 'Turn off two factor', `

Confirm with your password.

${field('Password', 'password', '', 'password')}`, async (form) => { try { await api('/account/2fa/disable', { method: 'POST', body: { password: form.get('password') } }); me.twoFactorOn = false; toast('Two factor turned off.'); loadSystem(); } catch (err) { toast(err.message, true); } }, { saveLabel: 'Turn off' } ); }); } /* ---------------------------------------------------------------- boot */ async function boot() { const session = await api('/session'); // The server redirects an unauthenticated /admin to the login page, so reaching // here without a session means it lapsed between the page load and this call. if (!session.admin || session.mustChangePassword) return toLogin(); me = { ...session.user, domainRule: session.domainRule }; $('#site-name').textContent = session.siteName; document.title = `Admin — ${session.siteName}`; $$('.owner-only').forEach((el) => { el.hidden = me.role !== 'owner'; }); sites = await api('/sites'); renderSiteFilter(); hosts = await api(scoped('/hosts')); await loadAlerts().catch(() => {}); loadOnsite(); } boot();