// Video fermi (spec §4.10, §10.3, tappa E4): what the viewer says of an analysed video, the confirmation of «Cerca i // video fermi» of a selection (from the dialogs of mockup 13) and the following of its job. The analysis is only ever // asked for: «È un video fermo?» in the viewer, or «Cerca i video fermi» for a selection. The job works on the server, one // video at a time, never during an engine session; its progress and «Interrompi» are in Dashboard → Esportazione, and // the page that started it says when it ends. import { api } from '../core/api.js'; import { h, icon, fill } from '../core/dom.js'; import { t } from '../core/text.js'; import { dialog, showError, toast } from '../core/ui.js'; /** How often a job is asked how far it is (milliseconds). */ const FOLLOW = 1500; /** Where the progress of the job is. */ const PROGRESS_PAGE = '/dashboard/esportazione'; /** The library filtered by «Video fermi» (§16 domanda 40). */ export const STILL_LIBRARY = '/libreria?tipo=fermi'; /** A video that can be analysed: in the library, readable, and with frames Febe can take. */ export function canAnalyse(media) { return Boolean(media && media.kind === 'video' && media.available && !media.damaged && media.framesAvailable !== false); } /** The words of an analysis in the panel «File» of the viewer. */ export function stillText(still) { const codes = String(still.note || '').split(' ').map((code) => code.split(':')[0]).filter(Boolean); if (still.outcome === 'still') { return t('esportazione.fermi.fermo') + (codes.includes('SmallMotion') ? t('esportazione.fermi.piccolaParte') : '') + (still.frameLabel ? t('esportazione.fermi.istante', { istante: still.frameLabel }) : ''); } if (still.outcome === 'moving') return t('esportazione.fermi.inMovimento'); const reasons = t('esportazione.fermi.motivi'); const reason = codes.map((code) => reasons[code]).find(Boolean); return reason ? t('esportazione.fermi.nonAnalizzabilePerche', { motivo: reason }) : t('esportazione.fermi.nonAnalizzabile'); } // ------------------------------------------------------------------------------------------------- the job let following = 0; let atWork = false; /** * Reads the job «Cerca i video fermi». While it works it is followed wherever the page goes, and its end is said once * («Analizzati 28 video: 3 fermi, 24 in movimento, 1 non analizzabile»). */ export async function readStillVideos(options = {}) { const job = await api('/api/still-videos/progress', options); noteJob(job); return job; } function noteJob(job) { const ended = atWork && !job.running; atWork = job.running; clearTimeout(following); if (job.running) following = setTimeout(follow, FOLLOW); if (ended && job.total) sayEnd(job); } async function follow() { try { await readStillVideos(); } catch { // Febe does not answer for a moment: asked again, at a slower pace. following = setTimeout(follow, FOLLOW * 4); } } /** What a job that is over found. */ export function endText(job) { if (!job.stopped) return `${job.summary}.`; return job.done ? t('esportazione.fermi.interrotta', { riepilogo: job.summary }) : t('esportazione.fermi.interrottaSubito'); } function sayEnd(job) { toast(endText(job), '', job.still ? { label: t('esportazione.fermi.mostra'), href: STILL_LIBRARY } : null); } /** «Interrompi»: the video at work is left, the outcomes already found stay. */ export async function stopStillVideos() { const job = await api('/api/still-videos/stop', { method: 'POST' }); noteJob(job); return job; } // ------------------------------------------------------------------------------------------------- the confirmation /** * «Cerca i video fermi» of a selection ({ mediaIds } or { entryIds }): how many videos there are to analyse, how many * are already, the photos that are skipped, and the button that starts the job. Resolves when the dialog closes. */ export function stillVideosDialog(selection) { return dialog({ title: t('esportazione.fermi.cercaTitolo'), className: 'cerca-fermi', build: (close) => { const summary = h('div.riepilogo-fermi', { 'aria-live': 'polite' }, h('p.aiuto', t('esportazione.fermi.conto'))); const start = h('button.btn.primo', { type: 'button', disabled: true, onclick: () => go() }); const cancel = h('button.btn.nudo', { type: 'button', autofocus: true, onclick: () => close(undefined) }, t('comune.annulla')); let plan = null; let closed = false; let busy = false; function draw() { fill(start, icon('search'), t('esportazione.fermi.analizza', { n: plan ? plan.toAnalyse : 0 })); start.disabled = busy || !plan || !plan.toAnalyse; } async function go() { if (busy || !plan) return; busy = true; draw(); try { const job = await api('/api/still-videos/start', { method: 'POST', body: selection }); const follow = { label: t('esportazione.copia.vediAvanzamento'), href: PROGRESS_PAGE }; if (job.added) toast(t('esportazione.fermi.avviata', { n: job.added }), '', follow); else if (job.running) toast(t('esportazione.fermi.giaInCoda'), '', follow); // all of them were queued already else toast(t('esportazione.fermi.niente')); noteJob(job); close(job); } catch (error) { busy = false; draw(); showError(error); } } async function count() { try { plan = await api('/api/still-videos/plan', { method: 'POST', body: selection }); } catch (error) { if (!closed) fill(summary, h('div.avviso.rosso', icon('octagon-alert', 's'), h('span', error.message))); return; } if (closed) return; fill(summary, h('p.riepilogo-copia', h('b', plan.summary)), plan.toAnalyse ? null : h('p.aiuto', t(plan.videos ? 'esportazione.fermi.niente' : 'esportazione.fermi.nienteVideo')), plan.toAnalyse && plan.waiting ? h('div.avviso.ambra', icon('hourglass', 's'), h('span', plan.waiting)) : null); draw(); } draw(); count(); return { body: h('div.contenuto', summary, h('p.aiuto', t('esportazione.fermi.cercaAiuto')), h('p.aiuto', t('esportazione.fermi.cercaRiapertura'))), footer: h('footer', h('div.destra', cancel, start)), onClose: () => { closed = true; }, }; }, }); }