// Visualizzatore (mockup 02, spec §4.6): covers the whole window; arrows and arrow keys move through the files of the // series and then to the next entry; Esc closes; videos start muted. The side panel shows the data, the files and the // provenance, and the actions: «Mostra nella cartella» (desktop) or «Scarica» (browser), «Apri su Instagram» (in // Alberto's own browser, never in Febe's), «Elimina dalla libreria». import { api, withCarried } from '../core/api.js'; import { caps, serverNow } from '../core/context.js'; import { h, icon, fill } from '../core/dom.js'; import { t, dayName, capitalize, bytes, duration, number, ago, shortDay } from '../core/text.js'; import { avatar, label, showError, toast } from '../core/ui.js'; import { confirmDelete } from './library.js'; const KIND_ICON = { post: 'image', story: 'circle-dashed', highlight: 'star', tagged: 'square-user', profile: 'circle-user' }; /** * Opens the viewer on a tile. source: { position(id), neighbor(id, delta), reveal(id) } (the mosaic, or a plain list). * origin: «Libreria» or the account name. onDeleted(ids): called after a deletion. */ export function openViewer({ tile, source, origin, onDeleted }) { const opener = document.activeElement; let entry = null; let fileIndex = 0; let zoomed = false; let token = 0; const where = h('span.dove'); const closeButton = h('button.tondino', { type: 'button', 'aria-label': t('vis.chiudi'), title: t('vis.chiudi'), onclick: close }, icon('x')); const zoomButton = h('button.tondino', { type: 'button', 'aria-label': t('vis.ingrandisci'), title: t('vis.ingrandisci'), onclick: () => setZoom(!zoomed) }, icon('zoom-in')); const fullButton = h('button.tondino', { type: 'button', 'aria-label': t('vis.schermoIntero'), title: t('vis.schermoIntero'), onclick: () => (document.fullscreenElement ? document.exitFullscreen() : box.requestFullscreen && box.requestFullscreen().catch(() => {})), }, icon('maximize')); const prev = h('button.freccia.sx', { type: 'button', 'aria-label': t('vis.precedente'), onclick: () => step(-1) }, icon('chevron-left')); const next = h('button.freccia.dx', { type: 'button', 'aria-label': t('vis.successivo'), onclick: () => step(1) }, icon('chevron-right')); const stage = h('div.scena'); const strip = h('div.serie', { role: 'group', 'aria-label': t('vis.serie') }); const panelTop = h('div.alto'); const panelBottom = h('div.basso'); const box = h('div.vis', { role: 'dialog', 'aria-modal': 'true', 'aria-label': t('vis.etichetta') }, h('div.palco', h('div.palco-testa', closeButton, where, h('div.destra', zoomButton, fullButton)), h('div', { style: { position: 'relative', flex: '1', minHeight: '0', display: 'flex' } }, prev, stage, next), strip), h('aside.scheda', panelTop, panelBottom)); function onKey(e) { if (document.querySelector('.velo.finestra, .menu')) return; // a dialog or a menu over the viewer has the keys if (e.target.closest && e.target.closest('input,textarea,select,video')) { if (e.key !== 'Escape') return; } if (e.key === 'Escape') { if (document.fullscreenElement) return; e.preventDefault(); close(); } else if (e.key === 'ArrowLeft') { e.preventDefault(); step(-1); } else if (e.key === 'ArrowRight') { e.preventDefault(); step(1); } else if (e.key === 'Tab') { const items = [...box.querySelectorAll('button:not([disabled]),a[href],video')].filter((el) => el.offsetParent !== null); if (!items.length) return; if (e.shiftKey && document.activeElement === items[0]) { items[items.length - 1].focus(); e.preventDefault(); } else if (!e.shiftKey && document.activeElement === items[items.length - 1]) { items[0].focus(); e.preventDefault(); } } } function close() { document.removeEventListener('keydown', onKey, true); for (const v of box.querySelectorAll('video')) v.pause(); if (document.fullscreenElement) document.exitFullscreen().catch(() => {}); box.remove(); document.body.style.overflow = ''; if (entry && source && source.reveal) source.reveal(entry.id); if (opener && document.contains(opener)) opener.focus(); } function setZoom(on) { zoomed = on; stage.classList.toggle('ingrandita', on); zoomButton.setAttribute('aria-pressed', String(on)); } async function load(id, index = 0) { const mine = ++token; try { const data = await api(`/api/entries/${id}`); if (mine !== token) return; entry = data; fileIndex = index < 0 ? entry.media.length - 1 : Math.min(index, entry.media.length - 1); draw(); } catch (error) { showError(error); } } async function step(delta) { if (!entry) return; const target = fileIndex + delta; if (target >= 0 && target < entry.media.length) { fileIndex = target; drawMedia(); drawStrip(); drawPanel(); return; } if (!source || !source.neighbor) return; const other = await source.neighbor(entry.id, delta); if (other) await load(other.id, delta > 0 ? 0 : -1); } function draw() { drawWhere(); drawMedia(); drawStrip(); drawPanel(); } function drawWhere() { if (source && source.position) { const p = source.position(entry.id); where.textContent = p.index ? t('vis.dove', { origine: origin, n: number(p.index), totale: number(p.total) }) : origin; } else where.textContent = origin; } function drawMedia() { setZoom(false); for (const v of stage.querySelectorAll('video')) v.pause(); const media = entry.media[fileIndex]; if (!media) { fill(stage, h('div.mancante', icon('info', 'l'), h('p', t('vis.eliminato')))); return; } const thumb = media.thumb ? `/api/thumbnails/${media.thumb}` : null; if (!media.available) { fill(stage, h('div.mancante', thumb ? h('img', { src: thumb, alt: '' }) : icon('image', 'l'), h('p', t(media.kind === 'video' ? 'vis.videoMancante' : 'vis.mancante')))); return; } const src = `/api/media/${media.id}/file`; if (media.kind === 'video') { fill(stage, h('video', { src, poster: thumb, controls: true, autoplay: true, muted: true, playsinline: true, preload: 'metadata', 'aria-label': t('libreria.tipiTessera.' + entry.kind) })); const video = stage.querySelector('video'); video.muted = true; // spec §10.3: videos start without sound } else { const img = h('img', { src, alt: entry.caption ? entry.caption.slice(0, 140) : `@${entry.account.username}` }); img.addEventListener('click', () => setZoom(!zoomed)); fill(stage, img); } prev.disabled = false; next.disabled = false; } function drawStrip() { if (entry.media.length < 2) { strip.hidden = true; strip.replaceChildren(); return; } strip.hidden = false; fill(strip, entry.media.map((m, i) => h(`button${i === fileIndex ? '.qui' : ''}`, { type: 'button', 'aria-label': t('vis.fileN', { n: i + 1, totale: entry.media.length }), 'aria-current': i === fileIndex ? 'true' : null, onclick: () => { fileIndex = i; drawMedia(); drawStrip(); drawPanel(); }, }, m.thumb ? h('img', { src: `/api/thumbnails/${m.thumb}`, alt: '', loading: 'lazy' }) : null, m.kind === 'video' ? icon('play') : null))); } function row(term, value) { return value == null || value === '' ? null : [h('dt', term), h('dd', value)]; } function drawPanel() { const a = entry.account; const media = entry.media[fileIndex]; const today = caps().today; const kindText = t(`libreria.tipiTessera.${entry.kind}`); const mediaText = media ? t(media.kind === 'video' ? 'comune.video' : 'comune.foto') : ''; const typeLabel = entry.media.length > 1 ? t('vis.tipoFile', { tipo: kindText, media: mediaText, n: fileIndex + 1, totale: entry.media.length }) : t('vis.tipoSingolo', { tipo: kindText, media: mediaText }); const published = `${capitalize(dayName(entry.publishedDay))} ${entry.publishedDay.slice(0, 4)}, ${entry.publishedTime}`; const downloaded = entry.downloaded ? `${shortDay(entry.downloaded.day)} ${entry.downloaded.day.slice(0, 4)}, ${entry.downloaded.clock}` : null; fill(panelTop, h('div.chi-vis', avatar(a.username, 'm', a.avatar), h('div', h('b', `@${a.username}`), a.displayName ? h('span', a.displayName) : null), a.isFollowed ? label(t('vis.seguito'), 'accento', 'user-check') : null), h('div.etichette', label(typeLabel, '', KIND_ICON[entry.kind] || 'image'), a.isPrivate ? label(t('vis.privato'), '', 'lock') : null), h('div.quando', h('b', published), h('span', t('vis.pubblicato', { quando: ago(entry.publishedAt, serverNow(), today, entry.publishedDay) }))), h('section', h('h3', t('vis.didascalia')), entry.caption ? h('p.didascalia', entry.caption) : h('p.didascalia.muto', t('vis.senzaDidascalia'))), h('section', h('h3', t('vis.file')), h('dl', media ? row(t('vis.nome'), media.fileName) : null, media && media.width && media.height ? row(t('vis.dimensioni'), t('vis.pixel', { l: number(media.width), a: number(media.height) })) : null, media && media.bytes ? row(t('vis.peso'), bytes(media.bytes)) : null, media && media.duration ? row(t('vis.durata'), duration(media.duration)) : null, media ? row(t('vis.cartella'), media.folder) : null, row(t('vis.codice'), entry.code || entry.instagramId), entry.author ? row(t('vis.autore'), `@${entry.author}`) : null, entry.media.length > 1 ? row(t('vis.posizione'), t('vis.posizioneDi', { n: fileIndex + 1, totale: entry.media.length })) : null, entry.highlights.length ? row(t('vis.raccolte'), entry.highlights.join(', ')) : null)), h('section', h('h3', t('vis.provenienza')), h('dl', row(t('vis.origine'), t(entry.provenance === 'imported' ? 'vis.importato' : 'vis.scaricato')), row(t('vis.quando'), downloaded)))); const c = caps(); const actions = []; if (media && media.available) { actions.push(c.showInFolder ? h('button.btn', { type: 'button', onclick: () => showInFolder(media.id) }, icon('folder-open'), t('vis.mostra')) : h('a.btn', { href: `/api/media/${media.id}/file?download=1`, download: media.fileName || '' }, icon('download'), t('vis.scarica'))); } if (entry.instagramUrl) { actions.push(c.openUrlNative ? h('button.btn', { type: 'button', onclick: () => openNative(entry.id) }, icon('external-link'), t('vis.apri')) : h('a.btn', { href: entry.instagramUrl, target: '_blank', rel: 'noopener noreferrer' }, icon('external-link'), t('vis.apri'))); } if (entry.state !== 'deleted') { actions.push(h('button.btn.nudo.pericolo', { type: 'button', onclick: remove }, icon('trash-2'), t('vis.elimina'))); } fill(panelBottom, actions); } async function showInFolder(mediaId) { try { const result = await api('/api/media/show-in-folder', { method: 'POST', body: { mediaId } }); toast(result.message); } catch (error) { showError(error); } } async function openNative(id) { try { await api(`/api/entries/${id}/open-on-instagram`, { method: 'POST' }); } catch (error) { showError(error); } } async function remove() { const id = entry.id; const following = source && source.neighbor ? (await source.neighbor(id, 1)) || (await source.neighbor(id, -1)) : null; const removed = await confirmDelete([id]); if (!removed) return; if (onDeleted) onDeleted(removed); if (following) await load(following.id, 0); else close(); } document.addEventListener('keydown', onKey, true); document.body.append(box); closeButton.focus(); load(tile.id, 0); void withCarried; }