Files
easy-ocpp/static/htmx.min.js
T
ManuelandClaude Fable 5 e5dab3e08c easy-occp v0.2.0 – OCPP-Wallbox-Management mit Live-Messwerten
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>
2026-08-18 23:15:12 +02:00

66 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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);
});
})();