// The frame of the page: the top bar, the inputs pane, the status bar, the
// menus, and the dialogs. Every function returns HTML for one region, from the
// state alone; `app.js` puts it in the document and handles the events. The
// detail pane is in `detail.js`.
import { LANGUAGES, language, show, t } from './i18n.js';
import {
VERSION,
allItems,
allOutputs,
counts,
fullPath,
hasSelection,
isDone,
isNonTarget,
outputBusy,
state,
targets,
} from './state.js';
import { MAX_LIMIT_MIB, MIN_LIMIT_MIB, formatClock } from './model.js';
import { dot, esc, groupName, modeLabel, rowState, shownPath, stepText, tag, typeTag } from './ui.js';
export { esc, groupName } from './ui.js';
export { detail, logText } from './detail.js';
// ---------- inputs ----------
const selectable = (id, selected) =>
`data-act="select" data-id="${id}" tabindex="0" role="option" aria-selected="${selected}"`;
const isSelected = (id) => state.selected === id || state.multi.includes(id);
const caret = (id, open) =>
`${open ? '▼' : '▶'}`;
function rowHtml(item, withCaret) {
const row = rowState(item);
const selected = isSelected(item.id);
const open = !state.collapsed.has(item.id);
const label = `${esc(row.label)}`;
const extra = row.extra ? `·${esc(row.extra)}` : '';
const time = row.time ? `${esc(row.time)}` : '';
return (
`
`
);
}
/** The parts of the inputs pane that stay put while its list changes. */
export function inputsFrame() {
const search = esc(t('inputs.search'));
return (
`
${esc(t('inputs.title'))}` +
`
` +
`` +
`
` +
``
);
}
/** The filter chips. There are none until an input has a result. */
export function chips() {
const items = allItems();
if (!items.some(isDone)) return '';
const count = counts(items);
return ['all', 'ok', 'suspect', 'failed', 'skipped']
.map((key) => {
const on = state.filter === key;
return (
``
);
})
.join('');
}
/**
* The rows of the inputs list, each `{ key, html }` with one element of HTML,
* so the page can replace only the rows that changed.
*/
export function listRows() {
const needle = state.search.trim().toLowerCase();
const filtering = state.filter !== 'all' || needle !== '';
// A filter chip hides the inputs that finished some other way; an input
// that has not run stays.
const visible = (item) =>
!(state.filter !== 'all' && isDone(item) && item.state !== state.filter) &&
(matches(item.name, needle) || (item.entries ?? []).some((entry) => matches(entry.name, needle)));
const rows = [];
for (const group of state.groups) {
const items = group.items.filter(visible);
if (filtering && items.length === 0 && !group.scanning) continue;
rows.push({ key: group.id, html: groupHead(group, items.length) });
if (!state.collapsed.has(group.id)) for (const item of items) rows.push(...itemRows(item, needle));
}
if (rows.length === 0) rows.push({ key: 'none', html: `
${esc(t('inputs.none'))}
` });
return rows;
}
/** The inputs list as one piece of HTML. */
export const list = () => listRows().map((row) => row.html).join('');
// ---------- top bar and status bar ----------
/** Where outputs go, as the output chip and the settings name it. */
const outputLabel = () =>
state.out.mode === 'folder' ? shownPath(`${state.out.dir.name}/unpack`) : t('web.out_downloads');
/** True while nothing can be run: a run or a scan is in progress. */
const busy = () => outputBusy() || state.scans.length > 0;
export function topbar() {
const count = targets().length;
const label = hasSelection() ? t('TbUnpackN', { n: count }) : t('tb.unpack.all');
const idle = !busy() && count > 0;
return (
`
` +
`Senbei${VERSION}
` +
`` +
`` +
`` +
`` +
`` +
`` +
``
);
}
/** The status line. The Cancel button beside it is the page's own. */
export function status() {
const run = state.run;
if (run) {
const total = Math.max(run.total, run.done);
const percent = total === 0 ? 0 : Math.round((run.done / total) * 100);
const current = run.current
? `${esc(run.current.name)}` +
`${run.step ? `${esc(stepText(run.step))}` : ''}` +
`${formatClock(performance.now() - run.started)}`
: '';
return (
`●${esc(t('tb.unpack'))}` +
`
`;
return `${choices}${pick}`;
}
/**
* The menu of a row. `kind` is `item`, `entry`, or `group`; `runnable` says
* the row can be unpacked, and `busy` that a run is in progress.
*/
export function rowMenu(id, { kind, runnable, busy }) {
const item = (act, label, disabled, shortcut = '') =>
``;
const sep = '';
const remove = item('remove', t('menu.remove'), busy, kind === 'item' ? 'Del' : '');
if (kind === 'group') return remove;
const top = item('run-item', t('tb.unpack'), busy || !runnable) + sep + item('copy-path', t('menu.copy_path'), false);
return kind === 'entry' ? top : top + sep + remove;
}
// ---------- dialogs ----------
function languageSelect(id) {
const options = LANGUAGES.map(
([code, name]) => ``,
);
return ``;
}
/** The legal notice. Until it is accepted it is the only thing to act on. */
export function legal(accepted) {
// The four paragraphs are the catalog's own HTML: they carry .
const text = ['legal.p1', 'legal.p2', 'legal.p3', 'legal.p4'].map((key) => `
${t(key)}
`);
const close = accepted
? ``
: ``;
return (
`
${esc(t('legal.title'))}
` +
`${languageSelect('legal-language')}
` +
`
${text.join('')}
${close}
`
);
}
/** A settings row: its label, its control, and an optional help line. */
function field(id, label, control, help) {
const name = id ? `` : `${esc(label)}`;
const helpId = id ? ` id="${id}-help"` : '';
return (
`