// Collapsible sections for the console.
//
// Every titled block in the two sidebars (Jobs, Drawings, Evidence, Wall twin, Drone twin, Operator, Areas, Work order, Approve, ...) becomes an accordion
// card: click the title to fold it. Blocks other scripts add later (twin.js, dronetwin.js, live.js, zones.js, prompt.js) are picked up as they appear.
// Nothing is rebuilt, only re-parented, so every id and event listener the other scripts rely on keeps working.
//
// Also: fold-all / open-all per sidebar, buttons that hide a whole sidebar (focus view), and the toolbar's brush group folded behind "Edit wall".
// Open/closed state is remembered per browser (localStorage, wrapped: it can throw in private windows).
"use strict";
(() => {
const OPEN_BY_DEFAULT = new Set(["jobs", "measurement confidence", "operator", "areas", "work order", "approve"]);
const store = {
get(k) { try { return localStorage.getItem(k); } catch (e) { return null; } },
set(k, v) { try { localStorage.setItem(k, v); } catch (e) { /* private window: state just isn't remembered */ } },
};
const titleOf = (h) => (h.firstChild && h.firstChild.nodeType === 3 ? h.firstChild.textContent : h.textContent).trim();
function chevron() {
const s = document.createElement("span");
s.className = "acc-chev"; s.setAttribute("aria-hidden", "true");
return s;
}
function setOpen(card, open, remember = true) {
card.classList.toggle("collapsed", !open);
const head = card.querySelector(":scope > .acc-head");
if (head) head.setAttribute("aria-expanded", open ? "true" : "false");
const body = card.querySelector(":scope > .acc-body");
if (body) { body.inert = !open; body.setAttribute("aria-hidden", open ? "false" : "true"); }
if (remember && card.dataset.accKey) store.set(card.dataset.accKey, open ? "1" : "0");
}
function makeCard(card, headEl, h2, side, bodyNodes) {
const title = titleOf(h2), key = `pv-acc:${side}:${title.toLowerCase()}`;
card.classList.add("acc"); card.dataset.accKey = key; card.dataset.accTitle = title.toLowerCase();
headEl.classList.add("acc-head");
headEl.setAttribute("role", "button"); headEl.tabIndex = 0;
headEl.appendChild(chevron());
const body = document.createElement("div"), inner = document.createElement("div");
body.className = "acc-body"; inner.className = "acc-inner";
body.appendChild(inner);
bodyNodes.forEach((n) => inner.appendChild(n));
card.appendChild(headEl); card.appendChild(body);
const saved = store.get(key);
setOpen(card, saved === null ? OPEN_BY_DEFAULT.has(title.toLowerCase()) : saved === "1", false);
const toggle = (e) => {
if (e.target.closest("a, input, select, textarea, label, button:not(.acc-head)")) return; // controls inside a header (counts, links) keep working
setOpen(card, card.classList.contains("collapsed"));
};
headEl.addEventListener("click", toggle);
headEl.addEventListener("keydown", (e) => { if ((e.key === "Enter" || e.key === " ") && e.target === headEl) { e.preventDefault(); toggle(e); } });
}
function scan(aside, side) {
const kids = Array.from(aside.children);
let group = null;
const flush = () => {
if (!group) return;
const { h2, nodes } = group;
const card = document.createElement("div");
h2.parentNode.insertBefore(card, h2);
makeCard(card, h2, h2, side, nodes);
group = null;
};
for (const el of kids) {
if (el.classList.contains("acc") || el.id === "acc-tools") { flush(); continue; }
if (el.matches("h2.section")) { flush(); group = { h2: el, nodes: [] }; continue; }
const own = el.querySelector("h2.section");
if (own && !el.closest("dialog")) { // a panel with its own title (evidence, twins, live, zones...)
flush();
let head = own; while (head.parentElement && head.parentElement !== el) head = head.parentElement;
const rest = Array.from(el.childNodes).filter((n) => n !== head);
makeCard(el, head, own, side, rest);
continue;
}
if (group) group.nodes.push(el);
}
flush();
}
function sidebarTools(aside, side) {
if (aside.querySelector(":scope > #acc-tools")) return;
const bar = document.createElement("div");
bar.id = "acc-tools"; bar.className = "acc-tools";
bar.innerHTML = ``;
bar.addEventListener("click", (e) => {
const b = e.target.closest(".acc-all"); if (!b) return;
aside.querySelectorAll(":scope > .acc").forEach((c) => setOpen(c, b.dataset.open === "1"));
});
aside.insertBefore(bar, aside.firstChild);
}
function focusButtons() {
const top = document.querySelector(".top"), model = document.getElementById("model");
if (!top || document.getElementById("view-toggles")) return;
const wrap = document.createElement("div");
wrap.id = "view-toggles"; wrap.className = "view-toggles";
wrap.innerHTML = `
`;
top.insertBefore(wrap, model);
const apply = (side, shown) => {
document.body.classList.toggle(`hide-${side}`, !shown);
const b = wrap.querySelector(`[data-side="${side}"]`); if (b) b.setAttribute("aria-pressed", shown ? "true" : "false");
};
wrap.addEventListener("click", (e) => {
const b = e.target.closest(".vt"); if (!b) return;
const shown = b.getAttribute("aria-pressed") !== "true";
apply(b.dataset.side, shown); store.set(`pv-side:${b.dataset.side}`, shown ? "1" : "0");
});
["jobs", "panel"].forEach((s) => { if (store.get(`pv-side:${s}`) === "0") apply(s, false); });
}
// Layers stay visible; the brush group and the zone bar (shapes, keep-out/paint-only, fix detection, undo) fold behind one button.
function toolbarFold() {
const tb = document.querySelector(".toolbar");
const parts = tb ? Array.from(tb.querySelectorAll(":scope > .group.tools, :scope > .group.zonebar")) : [];
if (!parts.length) return;
let btn = document.getElementById("tools-toggle");
const wasOpen = btn ? btn.getAttribute("aria-expanded") === "true" : false;
if (!btn) {
btn = document.createElement("button");
btn.id = "tools-toggle"; btn.type = "button"; btn.className = "ghost tools-toggle"; btn.setAttribute("aria-expanded", "false");
btn.textContent = "Edit wall";
parts[0].parentNode.insertBefore(btn, parts[0]);
btn.addEventListener("click", () => set(btn.getAttribute("aria-expanded") !== "true"));
}
const set = (open) => { document.querySelectorAll(".toolbar > .group.tools, .toolbar > .group.zonebar").forEach((g) => { g.hidden = !open; }); btn.setAttribute("aria-expanded", open ? "true" : "false"); btn.classList.toggle("on", open); };
set(wasOpen);
// picking a brush or zone shape from elsewhere (keyboard shortcut, tests) must not leave an invisible active tool
if (!tb.dataset.foldWatch) {
tb.dataset.foldWatch = "1";
new MutationObserver(() => {
const g = tb.querySelector(".group.tools [data-tool].on"), z = tb.querySelector('.group.zonebar [data-zt][aria-pressed="true"]');
if (((g && g.dataset.tool !== "none") || z) && btn.getAttribute("aria-expanded") !== "true") set(true);
}).observe(tb, { subtree: true, attributes: true, attributeFilter: ["class", "aria-pressed"] });
}
}
let pending = 0;
function run() {
pending = 0;
[[".jobs", "jobs"], [".panel", "panel"]].forEach(([sel, side]) => {
const a = document.querySelector(`aside${sel}`); if (!a) return;
scan(a, side); sidebarTools(a, side);
});
focusButtons(); toolbarFold();
}
const schedule = () => { if (!pending) pending = setTimeout(run, 60); };
function init() {
run();
document.querySelectorAll("aside.jobs, aside.panel").forEach((a) => new MutationObserver(schedule).observe(a, { childList: true }));
window.addEventListener("load", schedule);
setTimeout(schedule, 800); // scripts that build their panel after fetching
}
window.PVCollapse = { run, setOpen };
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => setTimeout(init, 0)); else setTimeout(init, 0);
})();