// 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 ( `
` + `${withCaret ? caret(item.id, open) : ''}${dot(row.dot)}` + `${esc(item.name)}${tag(typeTag(item))}` + `${label}${extra}${time}
` ); } const matches = (name, needle) => needle === '' || name.toLowerCase().includes(needle); /** An input's rows: its own, then the entries of a package under it. */ function itemRows(item, needle) { const entries = (item.entries ?? []).filter(isDone); // A search lists every entry it matches; otherwise only the targets show. const shown = entries.filter((entry) => (needle ? matches(entry.name, needle) : !isNonTarget(entry))); const others = needle ? 0 : entries.filter(isNonTarget).length; const rows = [{ key: item.id, html: rowHtml(item, shown.length > 0 || others > 0) }]; if (state.collapsed.has(item.id)) return rows; for (const entry of shown) rows.push({ key: entry.id, html: rowHtml(entry, false) }); if (others) { rows.push({ key: `others:${item.id}`, html: `
` + `${esc(t('EntriesOthers', { n: others }))}
`, }); } return rows; } function groupHead(group, shown) { const selected = state.selected === group.id; const folder = group.kind === 'folder'; const path = folder ? shownPath(`${group.name}/`) : ''; let sub = ''; if (group.scanning) { sub = `${esc(t('GroupScanning', { n: group.found }))}`; } else if (folder) { sub = `${esc(group.lastRun ? `${modeLabel()} · ${path}` : path)}`; } return ( `
` + `${caret(group.id, !state.collapsed.has(group.id))}` + `${esc(groupName(group))}` + `${group.scanning ? group.found : shown}${sub}
` ); } /** 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'))}` + `
` + `
${run.done} / ${total}${current}` + `` + `${run.stopping ? `${esc(t('run.stopping'))}` : ''}` ); } if (state.scans.length) { const group = state.scans[0].group; return `●${esc(t('ScanStatus', { p: groupName(group), n: group.found }))}`; } const notice = state.notice ? `${esc(show(state.notice))}` : ''; return `●${esc(t('status.ready'))}${notice}`; } // ---------- menus ---------- function choice(act, on, label, sub) { return ( `` ); } /** The output menu. `canPick` says this browser can write to a folder. */ export function outMenu(canPick) { const folder = state.out.dir; const choices = choice('out-downloads', state.out.mode === 'downloads', t('web.out_downloads')) + (folder ? choice('out-folder', state.out.mode === 'folder', t('settings.output_recent'), shownPath(`${folder.name}/unpack`)) : ''); const pick = canPick ? `` + `

${esc(t('web.out_folder_help'))}

` : `

${esc(t('web.out_unsupported'))}

`; 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 ( `` + `` ); } /** 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 ( `
${name}
${control}
` + `${help ? `

${esc(help)}

` : ''}
` ); } function settingsPane(canPick) { if (state.settingsTab === 'memory') { const mib = Math.round(state.limitBytes / (1024 * 1024)); return field( 'set-limit', t('web.limit'), `MiB`, t('web.limit_help'), ); } if (state.settingsTab === 'about') { return ( `
Senbei
` + `
${esc(t('AboutVersion', { v: VERSION }))}

${esc(t('about.desc'))}

` + field(null, t('about.license'), 'AGPL-3.0-only') + field( null, t('about.source'), 'github.com/Momoko-Ayase/Senbei', ) + `
` ); } const option = (value, label, on) => ``; const folder = state.out.dir; const output = ``; const outputHelp = t(canPick ? 'web.out_folder_help' : 'web.out_unsupported'); return ( field('set-language', t('settings.language'), languageSelect('set-language')) + field('set-output', t('settings.output'), output, outputHelp) ); } /** The settings dialog. `canPick` says this browser can write to a folder. */ export function settings(canPick) { const nav = ['general', 'memory', 'about'].map((key) => { const on = state.settingsTab === key; return ``; }); return ( `` + `
` + `
${settingsPane(canPick)}
` + `` ); }