// Impostazioni, Generali (mockup 08, spec §4.7, §10.5, §12): the Libreria and Dati folders with «Cambia…» (desktop: // native picker; Docker: read-only with the note), free and used space; the two naming schemes with the live «Risultato» // tree, the placeholders and the errors; «Imposta la data del file a quella di pubblicazione»; theme and language; // the «Avvio» block on Windows only. import { api } from '../core/api.js'; import { h, icon, fill, later } from '../core/dom.js'; import { t, bytes } from '../core/text.js'; import { saveTheme, themeChoice } from '../core/theme.js'; import { checkbox, confirmDialog, label, segmented, showError, toast, toggle } from '../core/ui.js'; import { guardChanges, heading } from './settings.js'; export default async function general(page, box) { let data = await api('/api/settings/general', { signal: page.signal }); let saved = { naming: data.namingScheme, highlight: data.highlightScheme, setFileTime: data.setFileTime }; const form = { ...saved }; const dirty = () => form.naming !== saved.naming || form.highlight !== saved.highlight || form.setFileTime !== saved.setFileTime; guardChanges(page, dirty); // ------------------------------------------------------------------------------------------- folders const folders = h('div'); function drawFolders() { const row = (title, help, folder, side, onChange, canChange) => h('div.cartella-impostazioni', h('b.titolo-campo', title), h('p.aiuto', help), h('div.riga-cartella', h('div.campo.sola', { title: folder.path }, icon('folder', 's'), h('span.val.dati', folder.path)), canChange ? h('button.btn', { type: 'button', onclick: onChange }, t('impostazioni.generali.cambia')) : null, h('span.spazio.dati', side))); const free = data.library.freeBytes != null ? t('impostazioni.generali.liberi', { n: bytes(data.library.freeBytes) }) : ''; const used = data.data.usedBytes != null ? t('impostazioni.generali.usati', { n: bytes(data.data.usedBytes) }) : data.data.measuring ? t('impostazioni.generali.misura') : ''; fill(folders, row(t('impostazioni.generali.libreria'), t('impostazioni.generali.libreriaAiuto'), data.library, free, changeLibrary, !data.foldersReadOnly && data.canPickFolder), row(t('impostazioni.generali.dati'), t('impostazioni.generali.datiAiuto'), data.data, used, changeData, data.canChangeDataFolder), data.foldersReadOnly ? h('div.avviso', { style: { marginTop: '12px' } }, icon('info', 's'), h('span', t('impostazioni.generali.dockerNota'))) : null); } async function pick(which) { try { const result = await api('/api/settings/pick-folder', { method: 'POST', body: { which } }); return result.path || null; } catch (error) { showError(error); return null; } } async function changeLibrary() { const path = await pick('library'); if (!path) return; const ok = await confirmDialog({ title: t('impostazioni.generali.nuovaLibreria'), text: t('impostazioni.generali.nuovaLibreriaTesto', { percorso: path }), buttons: [ { label: t('comune.annulla'), value: false, kind: 'nudo' }, { label: t('impostazioni.generali.usaCartella'), value: true, kind: 'primo', autofocus: true }, ], }); if (!ok) return; await put({ libraryFolder: path }); } async function changeData() { const path = await pick('data'); if (!path) return; const ok = await confirmDialog({ title: t('impostazioni.generali.nuoviDati'), text: t('impostazioni.generali.nuoviDatiTesto', { percorso: path }), buttons: [ { label: t('comune.annulla'), value: false, kind: 'nudo' }, { label: t('impostazioni.generali.usaCartella'), value: true, kind: 'primo', autofocus: true }, ], }); if (!ok) return; try { const result = await api('/api/settings/data-folder', { method: 'POST', body: { path } }); toast(result.message); } catch (error) { showError(error); } } /** Saves with the stored naming values plus the given changes (folders and switches act at once). */ async function put(changes, { withForm = false } = {}) { const body = { libraryFolder: null, namingScheme: withForm ? form.naming : saved.naming, highlightScheme: withForm ? form.highlight : saved.highlight, setFileTime: withForm ? form.setFileTime : saved.setFileTime, autostart: null, keepInTray: null, ...changes, }; try { data = await api('/api/settings/general', { method: 'PUT', body }); saved = { naming: data.namingScheme, highlight: data.highlightScheme, setFileTime: data.setFileTime }; if (withForm) Object.assign(form, saved); drawFolders(); drawSaveBar(); toast(t('impostazioni.salvate')); return true; } catch (error) { showError(error); return false; } } // ------------------------------------------------------------------------------------------- naming const naming = h('input.dati', { type: 'text', value: form.naming, id: 'schema', spellcheck: 'false', autocomplete: 'off' }); const highlight = h('input.dati', { type: 'text', value: form.highlight, id: 'schema-evidenza', spellcheck: 'false', autocomplete: 'off' }); const namingField = h('label.campo', naming); const highlightField = h('label.campo', highlight); const problems = h('div.problemi-schema', { 'aria-live': 'polite' }); const tree = h('div.albero.pannello', { 'aria-label': t('impostazioni.generali.risultato') }); const setTime = checkbox({ checked: form.setFileTime, label: t('impostazioni.generali.dataFile'), onchange: (v) => { form.setFileTime = v; drawSaveBar(); } }); function drawTree(preview) { const errors = preview.errors || []; namingField.classList.toggle('errore', errors.some((e) => e.startsWith('Schema:'))); highlightField.classList.toggle('errore', errors.some((e) => !e.startsWith('Schema:'))); fill(problems, errors.map((e) => h('p.errore-testo', icon('octagon-alert', 'xs'), ' ', e)), (preview.warnings || []).map((w) => h('p.avviso-testo', icon('triangle-alert', 'xs'), ' ', w))); fill(tree, h('div.ramo.radice', icon('folder', 's'), h('b', preview.accountFolder)), preview.folders.map((f) => [ f.path.length ? h('div.ramo.livello-1', icon('folder', 's'), h('span', f.path.join(' \\ '))) : null, f.files.map((file) => h(`div.ramo.${f.path.length ? 'livello-2' : 'livello-1'}`, icon(file.kind === 'video' ? 'clapperboard' : 'image', 's'), h('span.dati', file.name))), ])); } const refresh = later(async () => { try { drawTree(await api('/api/settings/naming-preview', { method: 'POST', body: { namingScheme: form.naming, highlightScheme: form.highlight }, signal: page.signal })); } catch (error) { if (error.name !== 'AbortError') showError(error); } }, 150); page.onLeave(() => refresh.cancel()); naming.addEventListener('input', () => { form.naming = naming.value; drawSaveBar(); refresh(); }); highlight.addEventListener('input', () => { form.highlight = highlight.value; drawSaveBar(); refresh(); }); // ------------------------------------------------------------------------------------------- save bar function drawSaveBar() { if (!dirty()) { page.saveBar(null); return; } page.saveBar(h('div', { style: { display: 'contents' } }, h('span.nota', t('impostazioni.modifiche')), h('div.destra', h('button.btn', { type: 'button', onclick: discard }, t('impostazioni.annulla')), h('button.btn.primo', { type: 'button', onclick: () => put({}, { withForm: true }) }, icon('check'), t('impostazioni.salva'))))); } function discard() { Object.assign(form, saved); naming.value = form.naming; highlight.value = form.highlight; setTime.input.checked = form.setFileTime; drawSaveBar(); refresh(); } // ------------------------------------------------------------------------------------------- appearance and start const theme = segmented({ label: t('impostazioni.generali.tema'), value: themeChoice(), options: [ { value: 'chiaro', label: t('impostazioni.generali.temi.chiaro'), icon: 'sun' }, { value: 'scuro', label: t('impostazioni.generali.temi.scuro'), icon: 'moon' }, { value: 'sistema', label: t('impostazioni.generali.temi.sistema'), icon: 'monitor' }, ], onchange: async (v) => { try { await saveTheme(v); } catch (error) { showError(error); } }, }); const language = h('label.campo.scelta.lingua', h('select', { id: 'lingua', 'aria-describedby': 'lingua-nota' }, h('option', { value: 'it', selected: true }, t('impostazioni.generali.italiano'))), icon('chevron-down', 's')); const start = data.autostart == null ? null : h('div.sezione', h('div.testa-sez', h('h2.sez', t('impostazioni.generali.avvio'), ' ', label(t('impostazioni.generali.soloWindows')))), h('div.leve', toggle({ checked: data.autostart, label: t('impostazioni.generali.accensione'), onchange: (v) => put({ autostart: v }) }), toggle({ checked: data.keepInTray, label: t('impostazioni.generali.area'), onchange: (v) => put({ keepInTray: v }) }))); drawFolders(); drawTree(data.preview); fill(box, h('div.sezione', heading(t('impostazioni.generali.cartelle')), folders), h('div.sezione', heading(t('impostazioni.generali.nomi'), t('impostazioni.generali.nomiAiuto')), h('div.nomi-file', h('div.colonna-nomi', h('label.etichetta', { for: 'schema' }, t('impostazioni.generali.schema')), namingField, h('label.etichetta', { for: 'schema-evidenza' }, t('impostazioni.generali.schemaEvidenza')), highlightField, h('p.aiuto.segnaposto', t('impostazioni.generali.segnaposto', { elenco: data.placeholders.join(', ') })), problems, h('div.data-file', setTime)), h('div.colonna-albero', h('span.etichetta', t('impostazioni.generali.risultato')), tree))), h('div.sezione', heading(t('impostazioni.generali.aspetto')), h('div.aspetto', h('div', h('span.etichetta', t('impostazioni.generali.tema')), theme), h('div', h('label.etichetta', { for: 'lingua' }, t('impostazioni.generali.lingua')), language, h('p.aiuto.piccolo', { id: 'lingua-nota' }, t('impostazioni.generali.linguaNota'))))), start); // The used size of the Data folder is measured in the background: ask again once. if (data.data.measuring && data.data.usedBytes == null) { const timer = setTimeout(async () => { try { const again = await api('/api/settings/general', { signal: page.signal }); data = { ...data, data: again.data, library: again.library }; drawFolders(); } catch { // keep «calcolo…» } }, 1000); page.onLeave(() => clearTimeout(timer)); } }