85 lines
2.4 KiB
JavaScript
85 lines
2.4 KiB
JavaScript
// cockpit/fester-module/targets.js
|
|
|
|
// State for available targets
|
|
let targets = [];
|
|
|
|
// Fetch current targets from backend
|
|
async function fetchTargets() {
|
|
try {
|
|
const res = await fetch("/api/targets");
|
|
targets = await res.json();
|
|
renderTargets();
|
|
} catch (err) {
|
|
console.error("Failed to fetch targets:", err);
|
|
}
|
|
}
|
|
|
|
// Render target list in the sidebar
|
|
function renderTargets() {
|
|
let sidebar = document.getElementById("sidebar");
|
|
let panel = document.getElementById("targets-panel");
|
|
|
|
if(!panel) {
|
|
panel = document.createElement("div");
|
|
panel.id = "targets-panel";
|
|
panel.className = "panel";
|
|
sidebar.appendChild(panel);
|
|
}
|
|
|
|
panel.innerHTML = "<h2>Targets</h2>";
|
|
|
|
targets.forEach(target => {
|
|
const container = document.createElement("div");
|
|
container.style.marginBottom = "5px";
|
|
|
|
const checkbox = document.createElement("input");
|
|
checkbox.type = "checkbox";
|
|
checkbox.id = `target-${target.name}`;
|
|
checkbox.checked = target.enabled;
|
|
checkbox.addEventListener("change", () => toggleTarget(target.name, checkbox.checked));
|
|
|
|
const label = document.createElement("label");
|
|
label.htmlFor = checkbox.id;
|
|
label.innerText = target.name;
|
|
|
|
container.appendChild(checkbox);
|
|
container.appendChild(label);
|
|
panel.appendChild(container);
|
|
});
|
|
}
|
|
|
|
// Enable/disable target via backend
|
|
async function toggleTarget(targetName, enabled) {
|
|
try {
|
|
const res = await fetch(`/api/targets/${targetName}`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ enabled })
|
|
});
|
|
if (!res.ok) {
|
|
console.error("Failed to update target", targetName);
|
|
} else {
|
|
console.log(`Target ${targetName} set to ${enabled}`);
|
|
}
|
|
} catch (err) {
|
|
console.error("Error toggling target:", err);
|
|
}
|
|
}
|
|
|
|
// WebSocket for live target state updates
|
|
const wsTargets = new WebSocket("ws://localhost:8080/ws-targets");
|
|
|
|
wsTargets.onmessage = (msg) => {
|
|
const event = JSON.parse(msg.data);
|
|
if (event.type === "target-update") {
|
|
const t = targets.find(t => t.name === event.data.name);
|
|
if (t) {
|
|
t.enabled = event.data.enabled;
|
|
renderTargets();
|
|
}
|
|
}
|
|
};
|
|
|
|
// Initial fetch
|
|
fetchTargets();
|