(function () { 'use strict'; function ready(callback) { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { window.setTimeout(callback, 0); }); } else { window.setTimeout(callback, 0); } } ready(function () { var section = document.getElementById('nc-connector'); if (!section) return; var pwgTokenInput = section.querySelector('input[name="pwg_token"]'); var pwgToken = pwgTokenInput ? pwgTokenInput.value : ''; var modeKey = 'bratonienNcWizardMode'; function escapeHtml(value) { return String(value == null ? '' : value).replace(/[&<>"']/g, function (c) { return {'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]; }); } function setMode(value) { try { sessionStorage.setItem(modeKey, value); } catch (e) {} } function postForm(label, tool, id, mode) { var form = document.createElement('form'); form.method = 'post'; form.style.display = 'inline'; form.dataset.ncV2Action = tool; form.innerHTML = '' + '' + ''; if (mode) form.addEventListener('submit', function () { setMode(mode); }, true); return form; } function loadConnection(id) { return fetch('plugins/bratonien_tools/nc-connector-edit-data.php?connection_id='+encodeURIComponent(id)+'&_='+Date.now(), { credentials:'same-origin', cache:'no-store', headers:{'Accept':'application/json'} }).then(function (response) { return response.json().then(function (data) { if (!response.ok) throw new Error(data.error || ('HTTP '+response.status)); return data; }); }); } function dialog() { var node = document.getElementById('bratonien-nc-connection-edit-v2'); if (node) return node; node = document.createElement('dialog'); node.id = 'bratonien-nc-connection-edit-v2'; node.className = 'bratonien-edit-dialog'; node.innerHTML = '
' + '
' + '

Verbindung bearbeiten

Alle Daten dieser Verbindung werden hier gepflegt. Eine Migration ist ein eigener Vorgang.

' + '
' + '
'; document.body.appendChild(node); node.querySelector('[data-edit-v2-close]').addEventListener('click', function () { node.close(); }); node.addEventListener('click', function (event) { if (event.target === node) node.close(); }); return node; } function storageRow(storage) { storage = storage || {}; return '
' + '' + '' + '' + '
'; } function clearValidation(form) { [].slice.call(form.querySelectorAll('[aria-invalid="true"]')).forEach(function (field) { field.removeAttribute('aria-invalid'); field.style.borderColor = ''; field.style.boxShadow = ''; }); [].slice.call(form.querySelectorAll('[data-edit-v2-field-error]')).forEach(function (node) { node.remove(); }); var box = form.querySelector('[data-edit-v2-error-summary]'); if (box) box.remove(); } function fieldNodes(form, fieldName) { return [].slice.call(form.querySelectorAll('[name="'+fieldName.replace(/"/g, '\\"')+'"]')); } function showValidation(form, data) { clearValidation(form); var message = (data && data.message) ? data.message : 'Die Verbindung konnte nicht gespeichert werden.'; var summary = document.createElement('div'); summary.dataset.editV2ErrorSummary = '1'; summary.className = 'bratonien-main-cache__warning'; summary.style.margin = '0 0 1rem'; summary.style.padding = '.75rem'; summary.style.border = '1px solid currentColor'; summary.innerHTML = 'Speichern fehlgeschlagen: '+escapeHtml(message); form.insertBefore(summary, form.firstChild); var first = null; var fields = data && Array.isArray(data.fields) ? data.fields : []; fields.forEach(function (fieldName) { fieldNodes(form, fieldName).forEach(function (field) { field.setAttribute('aria-invalid', 'true'); field.style.borderColor = '#d65a5a'; field.style.boxShadow = '0 0 0 1px #d65a5a'; if (!first) first = field; var note = document.createElement('span'); note.dataset.editV2FieldError = '1'; note.className = 'bratonien-main-cache__warning'; note.style.display = 'block'; note.style.marginTop = '.25rem'; note.textContent = message; field.insertAdjacentElement('afterend', note); }); }); if (first) { first.scrollIntoView({block:'center', behavior:'smooth'}); window.setTimeout(function () { first.focus(); }, 150); } else { summary.scrollIntoView({block:'center', behavior:'smooth'}); } } function submitEditor(form, editDialog) { clearValidation(form); if (!form.reportValidity()) return; var submit = form.querySelector('button[type="submit"]'); if (submit) submit.disabled = true; var body = new FormData(form); fetch('plugins/bratonien_tools/nc-connector-edit-save.php', { method:'POST', credentials:'same-origin', cache:'no-store', headers:{'Accept':'application/json'}, body:body }).then(function (response) { return response.json().then(function (data) { if (!response.ok || !data.ok) { showValidation(form, data); return; } editDialog.close(); window.location.reload(); }); }).catch(function (error) { showValidation(form, {message:error.message || String(error), fields:[]}); }).finally(function () { if (submit) submit.disabled = false; }); } function openEditor(id) { var editDialog = dialog(); var content = editDialog.querySelector('[data-edit-v2-content]'); content.innerHTML = '

Verbindung wird geladen …

'; if (typeof editDialog.showModal === 'function') editDialog.showModal(); else editDialog.setAttribute('open', 'open'); loadConnection(id).then(function (data) { if (data.adapter !== 'local') throw new Error('Diese Ansicht ist nur für die bestehende Legacy-Verbindung vorgesehen.'); var legacy = data.legacy || {}; var webdav = data.webdav || {}; var storages = Array.isArray(legacy.storages) ? legacy.storages : []; var rows = storages.map(storageRow).join('') || storageRow({}); var migrationText = webdav.migration_ready ? 'Bereit. Nextcloud-Zugang und verbindungseigener Piwigo-API-Key sind vollständig gespeichert.' : 'Noch nicht bereit. Es fehlen: '+escapeHtml((webdav.migration_missing || []).join(', ') || 'WebDAV-Zugangsdaten')+'.'; content.innerHTML = '
' + '' + '' + '
Verbindung
' + '' + '
' + '
Nextcloud / WebDAV
' + '

Diese Angaben werden für den neuen WebDAV-Weg benötigt und gehören zu genau dieser Verbindung.

' + '
' + '' + '' + '' + '
' + '
Piwigo API dieser Verbindung
' + '

Der API-Key wird verbindungseigen gespeichert und beim Speichern geprüft.

' + '
' + '' + '' + '
' + '

WebDAV-Migration: '+migrationText+'

' + '
Bestehender Legacy-Weg
' + '' + '' + '' + '' + '' + '
' + '
Speicherorte
'+rows+'
' + '' + '
Erweiterte Legacy-Einstellungen
' + '' + '' + '' + '' + '' + '' + '
' + '
' + '
'; var form = content.querySelector('[data-edit-v2-form]'); form.querySelector('[data-edit-v2-cancel]').addEventListener('click', function () { editDialog.close(); }); form.querySelector('[data-add-storage]').addEventListener('click', function () { form.querySelector('[data-storage-list]').insertAdjacentHTML('beforeend', storageRow({})); }); form.addEventListener('click', function (event) { var remove = event.target.closest('[data-remove-storage]'); if (!remove) return; var row = remove.closest('[data-storage-row]'); if (row) row.remove(); }); form.addEventListener('submit', function (event) { event.preventDefault(); event.stopPropagation(); submitEditor(form, editDialog); }); }).catch(function (error) { content.innerHTML = '

Bearbeiten nicht möglich: '+escapeHtml(error.message || String(error))+'

'; }); } function rebuildLocalActions(deleteForm, id, connectionData) { var actions = deleteForm.parentElement; if (!actions) return; [].slice.call(actions.querySelectorAll('form')).forEach(function (form) { var migrate = form.querySelector('button[value="nc_connector_migrate_start"]'); var editStart = form.querySelector('button[value="nc_connector_edit_start"]'); if (migrate || editStart) form.remove(); }); [].slice.call(actions.children).forEach(function (child) { if (child.tagName === 'BUTTON' && (child.textContent || '').trim() === 'Bearbeiten') child.remove(); if (child.dataset && child.dataset.ncMigrationInfo) child.remove(); }); var edit = document.createElement('button'); edit.type = 'button'; edit.className = 'buttonLike'; edit.textContent = 'Bearbeiten'; edit.addEventListener('click', function () { openEditor(id); }); actions.insertBefore(edit, deleteForm); var info = document.createElement('span'); info.dataset.ncMigrationInfo = '1'; info.className = 'bratonien-base-note'; info.textContent = 'WebDAV-Migration wird geprüft …'; actions.insertBefore(info, deleteForm); var dataPromise = connectionData ? Promise.resolve(connectionData) : loadConnection(id); dataPromise.then(function (data) { var webdav = data.webdav || {}; if (webdav.migration_ready) { info.remove(); actions.insertBefore(postForm('Auf WebDAV migrieren', 'nc_connector_migrate_start', id, 'migrate'), deleteForm); } else { var missing = (webdav.migration_missing || []).join(', '); info.textContent = 'WebDAV-Migration noch nicht bereit: '+(missing || 'Zugangsdaten fehlen')+'. Zuerst „Bearbeiten“.'; } }).catch(function (error) { info.textContent = 'WebDAV-Migration kann nicht geprüft werden: '+(error.message || String(error)); }); } function rebuildRemoteActions(deleteForm, id) { var actions = deleteForm.parentElement; if (!actions) return; [].slice.call(actions.querySelectorAll('form')).forEach(function (form) { var oldEdit = form.querySelector('button[value="nc_connector_edit_start"]'); var oldMigrate = form.querySelector('button[value="nc_connector_migrate_start"]'); if (oldEdit || oldMigrate) form.remove(); }); [].slice.call(actions.children).forEach(function (child) { if (child.tagName === 'BUTTON' && (child.textContent || '').trim() === 'Bearbeiten') child.remove(); if (child.dataset && child.dataset.ncMigrationInfo) child.remove(); }); actions.insertBefore(postForm('Bearbeiten', 'nc_connector_edit_start', id, 'edit'), deleteForm); } [].slice.call(section.querySelectorAll('button[value="nc_connector_edit_start"]')).forEach(function (button) { var form = button.closest('form'); if (form) form.remove(); }); [].slice.call(section.querySelectorAll('button[value="nc_connector_delete"]')).forEach(function (deleteButton) { var deleteForm = deleteButton.closest('form'); if (!deleteForm) return; var idInput = deleteForm.querySelector('input[name="connection_id"]'); if (!idInput) return; var id = idInput.value; loadConnection(id).then(function (data) { if (data.adapter === 'local') rebuildLocalActions(deleteForm, id, data); else rebuildRemoteActions(deleteForm, id); }).catch(function (error) { var actions = deleteForm.parentElement; if (!actions) return; var info = document.createElement('span'); info.className = 'bratonien-main-cache__warning'; info.textContent = 'Verbindungstyp konnte nicht geladen werden: '+(error.message || String(error)); actions.insertBefore(info, deleteForm); }); }); }); })();