elorasmus/public/js/ui.js
agent 0951f34a9d Add mobile-first web UI
Compare (duel with row-by-row comparison and thumb-reach pick bar), Ranking,
Triage (keep/drop pass, problem schools first), school detail with sources and
notes, Settings. Picks go to a localStorage outbox first and sync in the
background, so nothing is lost on a flaky connection. Light/dark themes,
installable to the home screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gXkMmmRRKLSnY6vQN3
2026-11-12 20:55:35 +00:00

99 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Tiny UI helpers. `html` is a tagged template that escapes every interpolated value unless it
// was produced by `html` itself (or wrapped in `raw`). Research text comes from the web, so
// escaping by default is the line of defence against markup injection.
const ESC = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
class Safe {
constructor(s) { this.s = s; }
toString() { return this.s; }
}
export const raw = (s) => new Safe(String(s));
export const esc = (v) => String(v ?? '').replace(/[&<>"']/g, (c) => ESC[c]);
function piece(v) {
if (v == null || v === false) return '';
if (v instanceof Safe) return v.s;
if (Array.isArray(v)) return v.map(piece).join('');
return esc(v);
}
export function html(strings, ...values) {
let out = strings[0];
for (let i = 0; i < values.length; i++) out += piece(values[i]) + strings[i + 1];
return new Safe(out);
}
/** Only http(s) links survive; anything else (javascript:, data:) becomes "#". */
export function safeUrl(u) {
try {
const url = new URL(u);
return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : '#';
} catch {
return '#';
}
}
export const extLink = (url, label) =>
html`<a href="${safeUrl(url)}" target="_blank" rel="noopener noreferrer">${label}</a>`;
/** Score bar 0-10 (no inline styles: CSP forbids them, so widths are classes). */
export function bar(v, tone = '') {
if (v == null) return html`<span class="muted">–</span>`;
const w = Math.max(0, Math.min(10, Math.round(v)));
return html`<span class="bar ${tone}"><i class="w${w}"></i></span><b class="num">${Math.round(v * 10) / 10}</b>`;
}
let toastTimer;
/** Show a short message; `action` = { label, run } adds a button (e.g. Undo). */
export function toast(message, action) {
const el = document.getElementById('toast');
el.replaceChildren();
const span = document.createElement('span');
span.textContent = message;
el.append(span);
if (action) {
const btn = document.createElement('button');
btn.type = 'button';
btn.textContent = action.label;
btn.addEventListener('click', () => { el.classList.remove('show'); action.run(); }, { once: true });
el.append(btn);
}
el.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => el.classList.remove('show'), action ? 5000 : 2600);
}
/** Random id that works without a secure context (crypto.randomUUID needs HTTPS). */
export function newId() {
const bytes = crypto.getRandomValues(new Uint8Array(16));
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
/** localStorage that never throws (private mode, blocked storage). */
export const local = {
get(key, fallback) {
try {
const v = localStorage.getItem(`elorasmus.${key}`);
return v == null ? fallback : JSON.parse(v);
} catch {
return fallback;
}
},
set(key, value) {
try {
localStorage.setItem(`elorasmus.${key}`, JSON.stringify(value));
} catch { /* storage unavailable: state just won't survive a reload */ }
},
};
export const ICONS = {
duel: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 4 3 8l4 4M3 8h11M17 20l4-4-4-4m4 4H10" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>'),
rank: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 20V10m6 10V4m6 16v-7m4 7H2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'),
triage: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16M7 12h10m-7 6h4" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'),
settings: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="3" fill="none" stroke="currentColor" stroke-width="2"/><path d="M12 2v3m0 14v3M2 12h3m14 0h3M4.9 4.9l2.1 2.1m10 10 2.1 2.1m0-14.2L17 7m-10 10-2.1 2.1" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'),
back: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M15 5l-7 7 7 7" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg>'),
undo: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 7 4 12l5 5M4 12h11a5 5 0 0 1 0 10h-2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>'),
skip: raw('<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m6 5 7 7-7 7m7-14 7 7-7 7" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>'),
};