diff --git a/js/nc_connector_edit_v2.js b/js/nc_connector_edit_v2.js new file mode 100644 index 0000000..59966ac --- /dev/null +++ b/js/nc_connector_edit_v2.js @@ -0,0 +1,205 @@ +(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 = '
Alle Daten dieser Verbindung werden hier gepflegt. Eine Migration ist ein eigener Vorgang.
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 = ''; + + 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(); + }); + }).catch(function (error) { + content.innerHTML = 'Bearbeiten nicht möglich: '+escapeHtml(error.message || String(error))+'
'; + }); + } + + function rebuildLocalActions(deleteForm, id) { + 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"]'); + if (migrate) 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); + + loadConnection(id).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)); + }); + } + + [].slice.call(section.querySelectorAll('button[value="nc_connector_delete"]')).forEach(function (deleteButton) { + var deleteForm = deleteButton.closest('form'); + var card = deleteButton.closest('details'); + if (!deleteForm || !card) return; + var idInput = deleteForm.querySelector('input[name="connection_id"]'); + if (!idInput) return; + var text = card.textContent || ''; + var isLocal = text.indexOf('bestehende Legacy-Konfiguration') !== -1; + if (isLocal) rebuildLocalActions(deleteForm, idInput.value); + }); + }); +})();