mirror of
https://github.com/hilman2/easy-ocpp.git
synced 2026-10-20 08:46:49 +02:00
Erstveröffentlichung: OCPP 1.6J Central System mit Web-UI (axum, askama, htmx, SQLite). Chip-/Mitarbeiterverwaltung, Remote-Start/Stop, Statistik, Live-Anzeige von kWh/Leistung/SoC während der Ladung inkl. automatischer MeterValues-Konfiguration der Wallboxen. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
66 lines
2.3 KiB
JavaScript
66 lines
2.3 KiB
JavaScript
// Minimaler htmx-Fallback. Unterstützt ausschließlich hx-get mit
|
||
// hx-trigger="load delay:Ns" – einmalig, ersetzt das Element (Chip-Enrollment)
|
||
// hx-trigger="every Ns" – Polling, ersetzt den Inhalt (Live-Ansichten)
|
||
// Für vollen htmx-Funktionsumfang die offizielle Datei ersetzen:
|
||
// https://unpkg.com/htmx.org@1.9.12
|
||
(function() {
|
||
function parseMs(str, re) {
|
||
var m = re.exec(str || "");
|
||
if (!m) return null;
|
||
var n = parseInt(m[1], 10);
|
||
return m[2] === "s" ? n * 1000 : n;
|
||
}
|
||
function fetchInto(el, url, swapInner, onDone) {
|
||
fetch(url, { credentials: "same-origin" })
|
||
.then(function(r) {
|
||
// Session abgelaufen / Fehler: nichts swappen, Polling beenden.
|
||
if (!r.ok || r.redirected) {
|
||
if (onDone) onDone(false);
|
||
return null;
|
||
}
|
||
return r.text();
|
||
})
|
||
.then(function(html) {
|
||
if (html == null) return;
|
||
if (swapInner) {
|
||
el.innerHTML = html;
|
||
} else {
|
||
var doc = new DOMParser().parseFromString(html, "text/html");
|
||
var replacement = doc.querySelector("[hx-get='" + url + "']") ||
|
||
doc.querySelector(".card");
|
||
if (replacement) {
|
||
el.replaceWith(replacement);
|
||
apply(replacement);
|
||
}
|
||
}
|
||
if (onDone) onDone(true);
|
||
})
|
||
.catch(function(e) {
|
||
// Netzwerkfehler sind meist transient (Server-Neustart) – weiter pollen.
|
||
console.error("htmx-shim fetch error", e);
|
||
if (onDone) onDone(true);
|
||
});
|
||
}
|
||
function apply(el) {
|
||
var url = el.getAttribute("hx-get");
|
||
var trigger = el.getAttribute("hx-trigger") || "";
|
||
if (!url) return;
|
||
var swapInner = (el.getAttribute("hx-swap") || "") === "innerHTML";
|
||
var every = parseMs(trigger, /every\s+(\d+)(m?s)/);
|
||
if (every !== null) {
|
||
var timer = setInterval(function() {
|
||
fetchInto(el, url, swapInner, function(keepGoing) {
|
||
if (!keepGoing) clearInterval(timer);
|
||
});
|
||
}, every);
|
||
return;
|
||
}
|
||
if (trigger.indexOf("load") === -1) return;
|
||
var delay = parseMs(trigger, /delay:(\d+)(m?s)?/) || 0;
|
||
setTimeout(function() { fetchInto(el, url, swapInner); }, delay);
|
||
}
|
||
document.addEventListener("DOMContentLoaded", function() {
|
||
document.querySelectorAll("[hx-get]").forEach(apply);
|
||
});
|
||
})();
|