Thunderbird-MailExtension zum Ablegen von E-Mails in DocuWare (.eml + PDF + Anhänge), dynamische Indexfelder aus dem Store-Dialog, Auswahllisten, Identity-Service-Login. Self-distribution-Updates über updates.json auf eigenem Gitea.
123 lines
3.8 KiB
JavaScript
123 lines
3.8 KiB
JavaScript
// Logik der Einstellungsseite.
|
||
|
||
const $ = (id) => document.getElementById(id);
|
||
const FIELDS = ["serverUrl", "organization", "username", "password"];
|
||
const CHECKS = ["storeEml", "storePdf", "storeAttachments", "tagOnSuccess"];
|
||
|
||
function setStatus(msg, kind) {
|
||
const el = $("status");
|
||
el.textContent = msg;
|
||
el.className = kind || "";
|
||
}
|
||
|
||
async function load() {
|
||
const s = await Settings.get();
|
||
FIELDS.forEach((f) => ($(f).value = s[f] || ""));
|
||
CHECKS.forEach((c) => ($(c).checked = !!s[c]));
|
||
// Schrankliste, falls vorher schon getestet (im Storage gecached)
|
||
const cached = (await browser.storage.local.get("cabinets")).cabinets || [];
|
||
fillCabinets(cached, s.defaultCabinetId);
|
||
}
|
||
|
||
function fillCabinets(cabinets, selectedId) {
|
||
const sel = $("defaultCabinet");
|
||
sel.innerHTML = '<option value="">— keiner —</option>';
|
||
const addGroup = (label, list) => {
|
||
if (!list.length) return;
|
||
const g = document.createElement("optgroup");
|
||
g.label = label;
|
||
list.forEach((c) => {
|
||
const o = document.createElement("option");
|
||
o.value = c.id;
|
||
o.textContent = c.name;
|
||
if (String(c.id) === String(selectedId)) o.selected = true;
|
||
g.appendChild(o);
|
||
});
|
||
sel.appendChild(g);
|
||
};
|
||
addGroup("Archive", cabinets.filter((c) => !c.isBasket));
|
||
addGroup("Briefkörbe", cabinets.filter((c) => c.isBasket));
|
||
}
|
||
|
||
function collect() {
|
||
const partial = {};
|
||
FIELDS.forEach((f) => (partial[f] = $(f).value.trim()));
|
||
CHECKS.forEach((c) => (partial[c] = $(c).checked));
|
||
partial.defaultCabinetId = $("defaultCabinet").value;
|
||
return partial;
|
||
}
|
||
|
||
$("save").addEventListener("click", async () => {
|
||
await Settings.set(collect());
|
||
setStatus("Gespeichert.", "ok");
|
||
});
|
||
|
||
$("test").addEventListener("click", async () => {
|
||
setStatus("Verbinde …");
|
||
try {
|
||
const s = await Settings.set(collect()); // erst speichern, dann testen
|
||
await Auth.logon(s);
|
||
const cabinets = await DocuWare.listCabinets(s);
|
||
await browser.storage.local.set({ cabinets });
|
||
fillCabinets(cabinets, s.defaultCabinetId);
|
||
const arch = cabinets.filter((c) => !c.isBasket).length;
|
||
const bk = cabinets.filter((c) => c.isBasket).length;
|
||
setStatus(`Verbindung ok – ${arch} Archive, ${bk} Briefkörbe gefunden.`, "ok");
|
||
} catch (e) {
|
||
setStatus(`Fehler: ${e.message}`, "err");
|
||
}
|
||
});
|
||
|
||
$("exportBtn").addEventListener("click", async () => {
|
||
const s = await Settings.get();
|
||
const blob = new Blob([JSON.stringify(s, null, 2)], { type: "application/json" });
|
||
const url = URL.createObjectURL(blob);
|
||
const a = document.createElement("a");
|
||
a.href = url;
|
||
a.download = "docuware-ablage-einstellungen.json";
|
||
document.body.appendChild(a);
|
||
a.click();
|
||
a.remove();
|
||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||
setStatus("Einstellungen exportiert.", "ok");
|
||
});
|
||
|
||
$("importBtn").addEventListener("click", () => $("importFile").click());
|
||
|
||
$("importFile").addEventListener("change", async (e) => {
|
||
const file = e.target.files && e.target.files[0];
|
||
if (!file) return;
|
||
try {
|
||
const text = await file.text();
|
||
const data = JSON.parse(text);
|
||
await Settings.set(data);
|
||
await load();
|
||
setStatus("Einstellungen importiert.", "ok");
|
||
} catch (err) {
|
||
setStatus(`Import fehlgeschlagen: ${err.message}`, "err");
|
||
} finally {
|
||
e.target.value = "";
|
||
}
|
||
});
|
||
|
||
$("diagBtn").addEventListener("click", async () => {
|
||
const cabinetId = $("defaultCabinet").value;
|
||
const out = $("diag");
|
||
out.style.display = "block";
|
||
if (!cabinetId) {
|
||
out.value = "Bitte oben zuerst ein Standard-Ziel (Archiv) wählen.";
|
||
return;
|
||
}
|
||
out.value = "Analysiere …";
|
||
try {
|
||
const s = await Settings.set(collect());
|
||
await Auth.logon(s);
|
||
const dump = await DocuWare.diagnoseStoreDialog(s, cabinetId);
|
||
out.value = JSON.stringify(dump, null, 2);
|
||
} catch (e) {
|
||
out.value = `Fehler: ${e.message}`;
|
||
}
|
||
});
|
||
|
||
load();
|