/* Configuration controls share the application's existing form styles. */
(() => {
const esc = value => String(value ?? '').replace(/[&<>"']/g, c => ({'&':'&', '<':'<', '>':'>', '"':'"', "'":'''}[c]));
const labels = {'self-signed':'Selbstsigniert', acme:'ACME', ca:'Smallstep CA', files:'Zertifikat und Schlüssel hochladen'};
function fieldMarkup(field, groups = [], bootstrap = false) {
const {key, kind, label, value, configured} = field;
const id = 'setting-' + key;
let input;
if (kind === 'bool') return ` ${esc(label)} `;
if (kind === 'group') {
const options = [...groups];
if (value && !options.some(group => group.sid === value)) options.push({sid:value, name:value});
input = `${options.map(group => `${esc(group.name)} `).join('')} `;
} else if (kind === 'choice') input = `${field.choices.map(option => `${esc(labels[option] || option)} `).join('')} `;
else if (kind.includes('pem')) input = ``;
else input = ` `;
const hint = field.summary ? `Aktuelles Ziel: ${esc(field.summary)} ` : key === 'BACKUP_DESTINATION' ? 'Unterstützt rsync, SFTP, SMB und WebDAV. Zugangsdaten in der Adresse bitte URL-kodieren. ' : '';
input = input.replace(/^<(input|select|textarea)\b/, `<$1 aria-label="${esc(label)}"`);
const clear = configured && ['secret','secretpem'].includes(kind) ? ` Gespeicherten Wert löschen ` : '';
return `
${esc(label)}${input}${hint} ${clear}
`;
}
function readValues(form, fields) {
const values = {};
for (const field of fields) {
const input = form.elements.namedItem(field.key);
if (!input || input.disabled) continue;
if (form.elements.namedItem('clear-' + field.key)?.checked) { values[field.key] = ''; continue; }
if (['secret','secreturl','secretpem'].includes(field.kind) && !input.value) continue;
values[field.key] = field.kind === 'bool' ? input.checked : field.kind === 'int' ? Number(input.value) : input.value;
}
return values;
}
function tlsVisibility(form) {
const mode = form.elements.namedItem('WEB_TLS_MODE');
if (!mode) return;
const update = () => form.querySelectorAll('[data-setting]').forEach(label => {
const key = label.dataset.setting;
const active = key.startsWith('ACME_') ? mode.value === 'acme' : key.startsWith('TLS_CA_') ? mode.value === 'ca' : ['WEB_TLS_CERT_PEM','WEB_TLS_KEY_PEM'].includes(key) ? mode.value === 'files' : true;
label.hidden = !active;
label.querySelectorAll('input,textarea,select').forEach(input => { input.disabled = !active; });
});
mode.addEventListener('change', update); update();
}
async function recoveryDialog(api, configuration, refreshed) {
const result = await api('/api/settings/backup-key/generate', {method:'POST', body:'{}'});
const oldKey = configuration.encryption.keyId || configuration.encryption.legacy;
const dialog = document.createElement('dialog');
dialog.className = 'access-dialog settings-key-dialog';
dialog.setAttribute('aria-labelledby', 'recovery-title');
dialog.innerHTML = `Wiederherstellungsphrase notieren Diese Phrase wird nur jetzt angezeigt. Sie entschlüsselt die vollständige Sicherung, einschließlich Zugangsdaten und Serverzustand. Bewahren Sie sie getrennt vom Server auf.
${result.phrase.split(' ').map(word => `${esc(word)} `).join('')} Schlüsselkennung: ${esc(result.keyId)}
`;
// The phrase stays in this dialog only; it is never stored in browser storage.
result.phrase = '';
document.body.append(dialog);
dialog.addEventListener('close', () => { dialog.replaceChildren(); dialog.remove(); }, {once:true});
dialog.querySelector('[data-close]').onclick = () => dialog.close();
dialog.querySelector('form').onsubmit = async event => {
event.preventDefault(); const form = event.currentTarget, button = form.querySelector('[type=submit]'); button.disabled = true;
try {
await api('/api/settings/backup-key/activate', {method:'POST', body:JSON.stringify({nonce:result.nonce, writtenDown:form.elements.written.checked, acknowledgeOldBackups:oldKey ? form.elements.old.checked : false})});
dialog.close(); await refreshed();
} catch (error) { const target = form.querySelector('[role=alert]'); target.textContent = error.message; target.hidden = false; button.disabled = false; }
};
dialog.showModal();
}
async function mount(host, {api, notice, section = '', initialSection = '', onDirty = () => {}}) {
const data = await api('/api/settings' + (section ? '?section=' + section : ''));
if (!host.isConnected) return;
let selected = section || initialSection || data.sections[0].id;
const render = async () => {
const current = data.sections.find(item => item.id === selected);
let groups = [];
if (selected === 'groups') groups = (await api('/api/settings/groups')).groups;
if (!host.isConnected) return;
host.innerHTML = `${!section ? `${data.sections.map(item => `${esc(item.label)} `).join('')}
` : ''}`;
const form = host.querySelector('form'); tlsVisibility(form);
form.addEventListener('input', () => onDirty(true));
host.querySelectorAll('[data-section]').forEach(button => { button.onclick = async () => {
if (form.dataset.dirty === 'true' && !confirm('Ungespeicherte Änderungen verwerfen?')) return;
onDirty(false); selected = button.dataset.section; await render();
}; });
form.addEventListener('input', () => { form.dataset.dirty = 'true'; });
if (selected === 'backup') host.querySelector('[data-generate]').onclick = async () => {
try {
const draft = form.dataset.dirty === 'true' ? readValues(form, current.fields) : null;
const clearDestination = form.elements.clearDestination?.checked;
await recoveryDialog(api, data, async () => {
onDirty(false);
await mount(host, {api, notice, section, initialSection:selected, onDirty});
if (draft) {
draft.BACKUP_ENCRYPTION_ENABLED = true;
const next = host.querySelector('form');
for (const [key, value] of Object.entries(draft)) {
const input = next.elements.namedItem(key);
if (input) { if (input.type === 'checkbox') input.checked = value; else input.value = value; }
}
if (next.elements.clearDestination) next.elements.clearDestination.checked = clearDestination;
next.dataset.dirty = 'true'; onDirty(true);
}
notice('Verschlüsselungsschlüssel aktiviert.');
});
} catch (error) { notice(error.message); }
};
form.onsubmit = async event => {
event.preventDefault(); const button = form.querySelector('[type=submit]'); button.disabled = true;
const errorBox = form.querySelector('[role=alert]'); errorBox.hidden = true;
try {
const values = readValues(form, current.fields);
if (form.elements.clearDestination?.checked) { values.BACKUP_DESTINATION = ''; values.BACKUP_AUTO_ENABLED = false; }
const saved = await api('/api/settings', {method:'POST', body:JSON.stringify({revision:data.revision, values})});
onDirty(false); form.dataset.dirty = 'false';
Object.assign(data, saved);
form.querySelector('[role=status]').textContent = saved.restartRequired ? 'Gespeichert. Dienste starten neu; anschließend die Seite neu laden.' : 'Gespeichert.';
form.querySelectorAll('input[type=password],textarea[name=WEB_TLS_KEY_PEM]').forEach(input => { input.value = ''; });
} catch (error) { errorBox.textContent = error.message; errorBox.hidden = false; }
finally { button.disabled = false; }
};
};
await render();
}
window.ConfigurationUI = {mount, fieldMarkup, readValues, tlsVisibility};
})();