Stage 3-5 server (military, expansion, social), web client for stages 1-2, ops/deploy
Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances, roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map, alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js. Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push, bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
parent
b56b13880e
commit
249818a7aa
111 changed files with 17472 additions and 13 deletions
132
public/js/ui.js
Normal file
132
public/js/ui.js
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
'use strict';
|
||||
/*
|
||||
* Small DOM helpers shared by the shell and the screens (window.UI). Everything is built with createElement and
|
||||
* textContent: player-controlled strings (POP names, usernames) never go through innerHTML. No inline handlers
|
||||
* (CSP): events are attached with addEventListener.
|
||||
*/
|
||||
(function () {
|
||||
const SVG_NS = 'http://www.w3.org/2000/svg';
|
||||
const XLINK = 'http://www.w3.org/1999/xlink';
|
||||
|
||||
// h('button', { class: 'btn', onclick: fn, 'aria-label': 'x', dataset: { k: 1 } }, 'text', childNode, [more])
|
||||
function h(tag, props, ...kids) {
|
||||
const el = document.createElement(tag);
|
||||
if (props) {
|
||||
for (const k of Object.keys(props)) {
|
||||
const v = props[k];
|
||||
if (v === undefined || v === null || v === false) continue;
|
||||
if (k === 'class') el.className = v;
|
||||
else if (k === 'text') el.textContent = v;
|
||||
else if (k === 'dataset') Object.assign(el.dataset, v);
|
||||
else if (k === 'style') { for (const s of Object.keys(v)) el.style.setProperty(s, v[s]); }
|
||||
else if (k.startsWith('on') && typeof v === 'function') el.addEventListener(k.slice(2), v);
|
||||
else if (v === true) el.setAttribute(k, '');
|
||||
else el.setAttribute(k, String(v));
|
||||
}
|
||||
}
|
||||
append(el, kids);
|
||||
return el;
|
||||
}
|
||||
|
||||
function append(el, kids) {
|
||||
for (const c of kids) {
|
||||
if (c === null || c === undefined || c === false) continue;
|
||||
if (Array.isArray(c)) append(el, c);
|
||||
else el.appendChild(c instanceof Node ? c : document.createTextNode(String(c)));
|
||||
}
|
||||
return el;
|
||||
}
|
||||
|
||||
function clear(el) { while (el.firstChild) el.removeChild(el.firstChild); return el; }
|
||||
|
||||
// <svg class="ic"><use href="icons/sprite.svg#i-name"></use></svg>
|
||||
function icon(name, cls) {
|
||||
const svg = document.createElementNS(SVG_NS, 'svg');
|
||||
svg.setAttribute('class', 'ic' + (cls ? ' ' + cls : ''));
|
||||
svg.setAttribute('aria-hidden', 'true');
|
||||
svg.setAttribute('focusable', 'false');
|
||||
const use = document.createElementNS(SVG_NS, 'use');
|
||||
use.setAttribute('href', 'icons/sprite.svg#i-' + name);
|
||||
use.setAttributeNS(XLINK, 'xlink:href', 'icons/sprite.svg#i-' + name);
|
||||
svg.appendChild(use);
|
||||
return svg;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ toasts
|
||||
function toast(msg, kind) {
|
||||
const root = document.getElementById('toasts');
|
||||
if (!root) return;
|
||||
const el = h('div', { class: 'toast ' + (kind || 'info'), text: msg });
|
||||
root.appendChild(el);
|
||||
while (root.children.length > 3) root.removeChild(root.firstChild);
|
||||
setTimeout(() => { el.classList.add('out'); setTimeout(() => el.remove(), 250); }, kind === 'error' ? 4500 : 2600);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ sheet (bottom sheet on phones, dialog on desktop)
|
||||
// openSheet({ title, icon, body: Node|Node[], onClose }) -> { el, body, close(), setTitle() }
|
||||
let current = null;
|
||||
|
||||
function openSheet(opts) {
|
||||
if (current) current.close(true);
|
||||
const root = document.getElementById('sheet-root');
|
||||
const prevFocus = document.activeElement;
|
||||
const titleId = 'sheet-title';
|
||||
const body = h('div', { class: 'sheet-body' });
|
||||
const titleEl = h('h2', { id: titleId, class: 'sheet-title', text: opts.title || '' });
|
||||
const closeBtn = h('button', { class: 'icon-btn', type: 'button', 'aria-label': opts.closeLabel || 'Close' }, icon('x'));
|
||||
const panel = h('div', { class: 'sheet', role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': titleId, tabindex: '-1' },
|
||||
h('div', { class: 'sheet-grab', 'aria-hidden': 'true' }),
|
||||
h('div', { class: 'sheet-head' }, opts.icon ? h('span', { class: 'sheet-icon ' + (opts.iconClass || '') }, icon(opts.icon)) : null, titleEl, closeBtn),
|
||||
body);
|
||||
const backdrop = h('div', { class: 'backdrop' });
|
||||
const wrap = h('div', { class: 'sheet-wrap' }, backdrop, panel);
|
||||
append(body, [].concat(opts.body || []));
|
||||
root.appendChild(wrap);
|
||||
requestAnimationFrame(() => wrap.classList.add('open'));
|
||||
|
||||
let closed = false;
|
||||
const api = {
|
||||
el: panel, body,
|
||||
setTitle(s) { titleEl.textContent = s; },
|
||||
close(silent) {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
if (current === api) current = null;
|
||||
document.removeEventListener('keydown', onKey, true);
|
||||
wrap.classList.remove('open');
|
||||
setTimeout(() => wrap.remove(), 200);
|
||||
if (!silent && prevFocus && prevFocus.focus && document.contains(prevFocus)) { try { prevFocus.focus({ preventScroll: true }); } catch (e) { /* gone */ } }
|
||||
if (opts.onClose) opts.onClose();
|
||||
},
|
||||
};
|
||||
function onKey(e) {
|
||||
if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); api.close(); return; }
|
||||
if (e.key !== 'Tab') return;
|
||||
const f = panel.querySelectorAll('button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])');
|
||||
if (!f.length) return;
|
||||
const first = f[0], last = f[f.length - 1];
|
||||
if (e.shiftKey && (document.activeElement === first || document.activeElement === panel)) { e.preventDefault(); last.focus(); }
|
||||
else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); }
|
||||
}
|
||||
document.addEventListener('keydown', onKey, true);
|
||||
closeBtn.addEventListener('click', () => api.close());
|
||||
// a sheet opened on finger lift (map tap) must not be closed by the compatibility click that follows the lift
|
||||
const openedAt = Date.now();
|
||||
backdrop.addEventListener('click', () => { if (Date.now() - openedAt > 400) api.close(); });
|
||||
setTimeout(() => { try { panel.focus({ preventScroll: true }); } catch (e) { /* ignore */ } }, 0);
|
||||
current = api;
|
||||
return api;
|
||||
}
|
||||
|
||||
function closeSheet() { if (current) current.close(); }
|
||||
const sheetOpen = () => !!current;
|
||||
|
||||
// Cheap safe storage for per-viewer conveniences (language, last screen). Never for anything that must persist.
|
||||
const safeStore = {
|
||||
get(k) { try { return window.localStorage.getItem(k); } catch (e) { return null; } },
|
||||
set(k, v) { try { window.localStorage.setItem(k, v); } catch (e) { /* private window */ } },
|
||||
del(k) { try { window.localStorage.removeItem(k); } catch (e) { /* ignore */ } },
|
||||
};
|
||||
|
||||
window.UI = { h, append, clear, icon, toast, openSheet, closeSheet, sheetOpen, safeStore };
|
||||
}());
|
||||
Loading…
Add table
Add a link
Reference in a new issue