112 lines
4.9 KiB
JavaScript
112 lines
4.9 KiB
JavaScript
|
|
// web/view-packages.mjs -- the packages list: every package of the
|
||
|
|
// deployment, system and user, with its acts -- start (create a run:
|
||
|
|
// the naming form), fork (the copy-to-author act; the copy lands in
|
||
|
|
// the editor), edit (the owner's own user packages). The list is the
|
||
|
|
// launchpad (list -> action, developer 2026-11-19); a scaffold is
|
||
|
|
// authoring material, not a story -- its start refuses.
|
||
|
|
|
||
|
|
import { $, el } from './dom.mjs';
|
||
|
|
import { api } from './client.mjs';
|
||
|
|
import { state } from './state.mjs';
|
||
|
|
import { banner } from './chrome.mjs';
|
||
|
|
import { navigate } from './router.mjs';
|
||
|
|
|
||
|
|
/** @type {?{ display: string, name: string }} */ let pkg = null; // the picked package -- the view owns it
|
||
|
|
|
||
|
|
/** @param {?string} arg @param {number} seq @returns {Promise<void>} */
|
||
|
|
export async function enter(arg, seq) {
|
||
|
|
$('package-pick').hidden = false;
|
||
|
|
$('create-form').hidden = true;
|
||
|
|
const pkgs = await api('/packages');
|
||
|
|
if (seq !== state.seq) return;
|
||
|
|
const list = $('package-list');
|
||
|
|
list.textContent = '';
|
||
|
|
for (const p of pkgs) {
|
||
|
|
const card = el('div', 'card package-card');
|
||
|
|
card.appendChild(el('span', 'card-name', p.display));
|
||
|
|
const origin = p.origin === 'user' ? `user${p.ownerName ? `, by ${p.ownerName}` : ''}` : 'system';
|
||
|
|
card.appendChild(el('span', 'card-meta', `${p.name} \u00b7 ${origin} \u00b7 records ${p.records ? 'on' : 'off'}`));
|
||
|
|
if (!p.complete) card.appendChild(el('span', 'card-state attention', 'incomplete -- missing backdrop or character'));
|
||
|
|
if (p.scaffold) card.appendChild(el('span', 'card-state attention', 'scaffold -- fork it to author, then flip "scaffold" off in the editor'));
|
||
|
|
const actions = el('div', 'form-actions pkg-actions');
|
||
|
|
const start = /** @type {HTMLButtonElement} */ (el('button', 'primary', 'start'));
|
||
|
|
start.disabled = p.scaffold || !p.complete;
|
||
|
|
start.addEventListener('click', () => pickPackage(p));
|
||
|
|
actions.appendChild(start);
|
||
|
|
const fork = /** @type {HTMLButtonElement} */ (el('button', null, 'fork'));
|
||
|
|
fork.addEventListener('click', () => openForkForm(card, p));
|
||
|
|
actions.appendChild(fork);
|
||
|
|
if (p.origin === 'user' && p.owner === state.me?.id) {
|
||
|
|
const edit = /** @type {HTMLButtonElement} */ (el('button', null, 'edit'));
|
||
|
|
edit.addEventListener('click', () => navigate(`/packages/${p.name}`));
|
||
|
|
actions.appendChild(edit);
|
||
|
|
}
|
||
|
|
card.appendChild(actions);
|
||
|
|
list.appendChild(card);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param {{ display: string, name: string }} p */
|
||
|
|
async function pickPackage(p) {
|
||
|
|
pkg = p;
|
||
|
|
$('package-pick').hidden = true;
|
||
|
|
$('create-form').hidden = false;
|
||
|
|
$('create-title').textContent = `start -- ${p.display}`;
|
||
|
|
/** @type {HTMLInputElement} */ ($('create-name')).value = p.display;
|
||
|
|
const pcInp = /** @type {HTMLInputElement} */ ($('create-pc'));
|
||
|
|
pcInp.value = '';
|
||
|
|
pcInp.placeholder = 'rolling a name...';
|
||
|
|
pcInp.focus();
|
||
|
|
const roll = await api('/names').catch(() => null);
|
||
|
|
if (state.view !== 'packages' || $('create-form').hidden) return;
|
||
|
|
if (pcInp.value === '' && roll?.name) pcInp.value = roll.name;
|
||
|
|
}
|
||
|
|
|
||
|
|
// the fork form: revealed in the card, one decision -- the new
|
||
|
|
// package's display name. The fork lands in the editor (the
|
||
|
|
// copy-to-author act, developer 2026-11-19).
|
||
|
|
/** @param {HTMLElement} card @param {{ display: string, name: string }} p */
|
||
|
|
function openForkForm(card, p) {
|
||
|
|
const old = card.querySelector('.pkg-fork-form');
|
||
|
|
if (old) { old.remove(); return; } // a second click closes it
|
||
|
|
const form = el('form', 'pkg-fork-form');
|
||
|
|
const inp = /** @type {HTMLInputElement} */ (el('input'));
|
||
|
|
inp.placeholder = 'the new package\'s display name';
|
||
|
|
inp.autocomplete = 'off';
|
||
|
|
inp.value = `${p.display} fork`;
|
||
|
|
const go = /** @type {HTMLButtonElement} */ (el('button', 'primary', 'fork it'));
|
||
|
|
go.type = 'submit';
|
||
|
|
form.appendChild(inp);
|
||
|
|
form.appendChild(go);
|
||
|
|
form.addEventListener('submit', async (e) => {
|
||
|
|
e.preventDefault();
|
||
|
|
const name = inp.value.trim();
|
||
|
|
if (!name) return banner('the fork needs a display name');
|
||
|
|
go.disabled = true;
|
||
|
|
try {
|
||
|
|
const r = await api('/packages/fork', { method: 'POST', body: { from: p.name, name } });
|
||
|
|
navigate(`/packages/${r.package.id}`);
|
||
|
|
} catch (err) {
|
||
|
|
banner(err.message);
|
||
|
|
go.disabled = false;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
card.appendChild(form);
|
||
|
|
inp.focus();
|
||
|
|
}
|
||
|
|
|
||
|
|
$('create-back').addEventListener('click', () => { $('create-form').hidden = true; $('package-pick').hidden = false; });
|
||
|
|
$('create-form').addEventListener('submit', async (e) => {
|
||
|
|
e.preventDefault();
|
||
|
|
const name = /** @type {HTMLInputElement} */ ($('create-name')).value.trim();
|
||
|
|
const pc = /** @type {HTMLInputElement} */ ($('create-pc')).value.trim();
|
||
|
|
if (!name) return banner('the package needs a display name');
|
||
|
|
if (!pc) return banner('the main character needs a name');
|
||
|
|
try {
|
||
|
|
const r = await api('/runs', { method: 'POST', body: { package: pkg?.name, pc, name } });
|
||
|
|
navigate(`/runs/${r.run.id}/setup`);
|
||
|
|
} catch (err) {
|
||
|
|
banner(err.message);
|
||
|
|
}
|
||
|
|
});
|