Image Segmentation
PyTorch
English
painting-vision-robotics-kit
semantic-segmentation
robotics
edge-ai
construction-ai
autonomous-painting
wall-painting-robot
paint-coverage-estimation
building-facade
drywall
skirting-detection
window-detection
lidar
depth-validation
segformer
deeplabv3
Eval Results (legacy)
Upload console/app.js with huggingface_hub
Browse files- console/app.js +420 -0
console/app.js
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Operator console front end: no framework, talks to console_server.py.
|
| 2 |
+
"use strict";
|
| 3 |
+
|
| 4 |
+
const $ = (sel) => document.querySelector(sel);
|
| 5 |
+
const STATUS = {
|
| 6 |
+
ready_to_paint: ["Ready to paint", "The gate passes. Check the green area, then approve."],
|
| 7 |
+
prep_required: ["Prep first", "Damage must be scraped/treated and primed, then the wall rescanned before the coat."],
|
| 8 |
+
touch_up_only: ["Touch-up only", "The wall is signed off; only small unconfirmed patches remain."],
|
| 9 |
+
complete: ["Complete", "The wall is signed off. Nothing to paint."],
|
| 10 |
+
blocked: ["Blocked", "The safety gate stops this job. Fix or confirm the reasons below."],
|
| 11 |
+
approved: ["Approved", "This work order is frozen and recorded."],
|
| 12 |
+
};
|
| 13 |
+
const RELEVANT = { not_wallpaper: /wallpaper/i, depth_waived: /depth/i, uncertainty_reviewed: /uncertain|re-observ/i,
|
| 14 |
+
colour_checked: /colour|ΔE00|tin/i };
|
| 15 |
+
|
| 16 |
+
let health = null;
|
| 17 |
+
let job = null;
|
| 18 |
+
let tool = "none";
|
| 19 |
+
let dirty = false;
|
| 20 |
+
let drawing = false;
|
| 21 |
+
const editCanvas = $("#edits");
|
| 22 |
+
const ectx = editCanvas.getContext("2d");
|
| 23 |
+
|
| 24 |
+
function esc(s) {
|
| 25 |
+
return String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
| 26 |
+
}
|
| 27 |
+
function sqft(mm2) {
|
| 28 |
+
const ft = mm2 / 92903.04;
|
| 29 |
+
return ft < 0.1 ? "< 0.1 sq ft" : `${fmt(ft, ft < 10 ? 1 : 0)} sq ft`;
|
| 30 |
+
}
|
| 31 |
+
function fmt(n, digits = 0) {
|
| 32 |
+
return n == null ? "—" : Number(n).toLocaleString(undefined, { maximumFractionDigits: digits });
|
| 33 |
+
}
|
| 34 |
+
async function api(path, options = {}) {
|
| 35 |
+
const res = await fetch(path, options);
|
| 36 |
+
const body = await res.json().catch(() => ({}));
|
| 37 |
+
if (!res.ok) throw new Error(body.error || `${res.status} ${res.statusText}`);
|
| 38 |
+
return body;
|
| 39 |
+
}
|
| 40 |
+
function store(key, value) {
|
| 41 |
+
try { value === undefined ? localStorage.getItem(key) : localStorage.setItem(key, value); } catch (_) { /* private mode */ }
|
| 42 |
+
}
|
| 43 |
+
function recall(key) {
|
| 44 |
+
try { return localStorage.getItem(key) || ""; } catch (_) { return ""; }
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// ------------------------------------------------------------------ boot
|
| 48 |
+
async function boot() {
|
| 49 |
+
$("#operator").value = recall("pv.operator");
|
| 50 |
+
$("#operator").addEventListener("change", (e) => store("pv.operator", e.target.value.trim()));
|
| 51 |
+
try {
|
| 52 |
+
health = await api("/api/health");
|
| 53 |
+
const m = health.model || {};
|
| 54 |
+
$("#model").textContent = `${m.checkpoint || "model"} · ${m.arch || ""} · ${m.device || ""}`;
|
| 55 |
+
if ((m.warnings || []).length) {
|
| 56 |
+
$("#banner").hidden = false;
|
| 57 |
+
$("#banner").textContent = "Model not fully calibrated: " + m.warnings.join(" · ");
|
| 58 |
+
}
|
| 59 |
+
} catch (err) {
|
| 60 |
+
$("#model").textContent = "console offline: " + err.message;
|
| 61 |
+
}
|
| 62 |
+
renderConfirmations();
|
| 63 |
+
await refreshJobs();
|
| 64 |
+
const id = new URLSearchParams(location.hash.slice(1)).get("job");
|
| 65 |
+
if (id) openJob(id);
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
async function refreshJobs() {
|
| 69 |
+
const jobs = await api("/api/jobs").catch(() => []);
|
| 70 |
+
const list = $("#job-list");
|
| 71 |
+
list.innerHTML = "";
|
| 72 |
+
for (const j of jobs) {
|
| 73 |
+
const li = document.createElement("li");
|
| 74 |
+
const b = document.createElement("button");
|
| 75 |
+
const status = j.approved ? "approved" : j.status;
|
| 76 |
+
b.innerHTML = `<span class="name">${esc(j.wall_id || j.filename || j.id)}</span>
|
| 77 |
+
<span class="small muted">${esc(new Date(j.created).toLocaleString())}</span>
|
| 78 |
+
<span class="pill ${esc(status)}">${esc((STATUS[status] || [status])[0])}</span>`;
|
| 79 |
+
if (job && job.id === j.id) b.classList.add("active");
|
| 80 |
+
b.addEventListener("click", () => openJob(j.id));
|
| 81 |
+
li.appendChild(b);
|
| 82 |
+
list.appendChild(li);
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// ------------------------------------------------------------------ upload
|
| 87 |
+
async function upload(file) {
|
| 88 |
+
if (!file) return;
|
| 89 |
+
const status = $("#upload-status");
|
| 90 |
+
status.textContent = `Analysing ${file.name}… (about 10–20 s on CPU)`;
|
| 91 |
+
try {
|
| 92 |
+
const q = new URLSearchParams({ filename: file.name, wall_id: $("#wall-id").value.trim() });
|
| 93 |
+
const created = await api(`/api/jobs?${q}`, { method: "POST", body: file, headers: { "Content-Type": file.type || "application/octet-stream" } });
|
| 94 |
+
status.textContent = "";
|
| 95 |
+
$("#wall-id").value = "";
|
| 96 |
+
showJob(created);
|
| 97 |
+
refreshJobs();
|
| 98 |
+
} catch (err) {
|
| 99 |
+
status.textContent = "Upload failed: " + err.message;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
$("#file").addEventListener("change", (e) => upload(e.target.files[0]));
|
| 103 |
+
const drop = $("#drop");
|
| 104 |
+
["dragenter", "dragover"].forEach((t) => drop.addEventListener(t, (e) => { e.preventDefault(); drop.classList.add("over"); }));
|
| 105 |
+
["dragleave", "drop"].forEach((t) => drop.addEventListener(t, (e) => { e.preventDefault(); drop.classList.remove("over"); }));
|
| 106 |
+
drop.addEventListener("drop", (e) => upload(e.dataTransfer.files[0]));
|
| 107 |
+
|
| 108 |
+
// ------------------------------------------------------------------ job view
|
| 109 |
+
async function openJob(id) {
|
| 110 |
+
try { showJob(await api(`/api/jobs/${encodeURIComponent(id)}`)); } catch (err) { alert(err.message); }
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
function showJob(j) {
|
| 114 |
+
job = j;
|
| 115 |
+
history.replaceState(null, "", `#job=${j.id}`);
|
| 116 |
+
const v = `?v=${encodeURIComponent(j.updated || j.created)}`;
|
| 117 |
+
$("#empty").hidden = true;
|
| 118 |
+
$("#stage").hidden = false;
|
| 119 |
+
const photo = $("#photo");
|
| 120 |
+
photo.src = `/api/jobs/${j.id}/photo.jpg`;
|
| 121 |
+
for (const name of ["paint", "keepout", "uncertain", "touchup"]) {
|
| 122 |
+
document.querySelector(`img.layer[data-layer="${name}"]`).src = `/api/jobs/${j.id}/layer_${name}.png${v}`;
|
| 123 |
+
}
|
| 124 |
+
editCanvas.width = j.width;
|
| 125 |
+
editCanvas.height = j.height;
|
| 126 |
+
ectx.clearRect(0, 0, j.width, j.height);
|
| 127 |
+
if (j.has_edits) {
|
| 128 |
+
const img = new Image();
|
| 129 |
+
img.onload = () => ectx.drawImage(img, 0, 0);
|
| 130 |
+
img.src = `/api/jobs/${j.id}/layer_edits.png${v}`;
|
| 131 |
+
}
|
| 132 |
+
setDirty(false);
|
| 133 |
+
renderVector();
|
| 134 |
+
renderPanel();
|
| 135 |
+
applyLayerToggles();
|
| 136 |
+
document.querySelectorAll(".job-list button").forEach((b) => b.classList.remove("active"));
|
| 137 |
+
refreshJobs();
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
function svgEl(name, attrs) {
|
| 141 |
+
const el = document.createElementNS("http://www.w3.org/2000/svg", name);
|
| 142 |
+
for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);
|
| 143 |
+
return el;
|
| 144 |
+
}
|
| 145 |
+
const pts = (poly) => (poly || []).map(([x, y]) => `${x},${y}`).join(" ");
|
| 146 |
+
|
| 147 |
+
function renderVector() {
|
| 148 |
+
const svg = $("#vector");
|
| 149 |
+
svg.innerHTML = "";
|
| 150 |
+
svg.setAttribute("viewBox", `0 0 ${job.width} ${job.height}`);
|
| 151 |
+
svg.setAttribute("preserveAspectRatio", "none");
|
| 152 |
+
const damage = svgEl("g", { "data-layer": "damage" });
|
| 153 |
+
(job.regions || []).forEach((r, i) => {
|
| 154 |
+
if (r.prep_zone_xy && r.prep_zone_xy.length > 2) damage.appendChild(svgEl("polygon", { points: pts(r.prep_zone_xy), class: "prep-zone" }));
|
| 155 |
+
if (r.outline_xy && r.outline_xy.length > 2) damage.appendChild(svgEl("polygon", { points: pts(r.outline_xy), class: "damage", "data-region": i }));
|
| 156 |
+
});
|
| 157 |
+
svg.appendChild(damage);
|
| 158 |
+
const paths = svgEl("g", { "data-layer": "paths" });
|
| 159 |
+
const lines = (strokes, cls) => (strokes || []).forEach(([[x0, y0], [x1, y1]]) =>
|
| 160 |
+
paths.appendChild(svgEl("line", { x1: x0, y1: y0, x2: x1, y2: y1, class: cls })));
|
| 161 |
+
for (const step of (job.work_order || {}).steps || []) {
|
| 162 |
+
if (step.step === "paint" && step.plan) {
|
| 163 |
+
lines(step.plan.plan.strokes, "roller");
|
| 164 |
+
lines(step.plan.plan.trim.strokes, "trim");
|
| 165 |
+
}
|
| 166 |
+
if (step.step === "prep") step.tasks.forEach((t) => lines(t.strokes_px, "prep-path"));
|
| 167 |
+
if (step.step === "touch_up") step.tasks.forEach((t) => lines(t.strokes_px, "touch-path"));
|
| 168 |
+
}
|
| 169 |
+
svg.appendChild(paths);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
function renderPanel() {
|
| 173 |
+
const order = job.work_order || {};
|
| 174 |
+
const status = job.approved ? "approved" : order.status;
|
| 175 |
+
const [title, text] = STATUS[status] || [status || "—", ""];
|
| 176 |
+
const decision = $("#decision");
|
| 177 |
+
decision.className = `decision ${status}`;
|
| 178 |
+
decision.innerHTML = `<h3>${esc(title)}</h3><p>${esc(text)}</p>`;
|
| 179 |
+
|
| 180 |
+
const reasons = (order.gate || {}).reasons || [];
|
| 181 |
+
const warnings = order.warnings || [];
|
| 182 |
+
$("#reasons").innerHTML = reasons.length || warnings.length
|
| 183 |
+
? `<ul class="reasons">${reasons.map((r) => `<li class="bad">${esc(r)}</li>`).join("")}${warnings.map((w) => `<li class="warn">${esc(w)}</li>`).join("")}</ul>`
|
| 184 |
+
: "";
|
| 185 |
+
|
| 186 |
+
// Confirmations: check the ones already signed; highlight the ones a reason asks for.
|
| 187 |
+
const signed = new Set((order.confirmations || []).map((c) => c.name));
|
| 188 |
+
document.querySelectorAll("#confirmations input").forEach((box) => {
|
| 189 |
+
box.checked = signed.has(box.value);
|
| 190 |
+
const label = box.closest("label");
|
| 191 |
+
label.classList.toggle("relevant", reasons.some((r) => (RELEVANT[box.value] || /$^/).test(r)));
|
| 192 |
+
box.disabled = !!job.approved;
|
| 193 |
+
});
|
| 194 |
+
|
| 195 |
+
const s = order.summary || {};
|
| 196 |
+
const scale = order.scale || {};
|
| 197 |
+
const areaFt = s.paint_area_ft2 != null ? `${fmt(s.paint_area_ft2, 1)} sq ft` : `${fmt(s.paint_area_px)} px`;
|
| 198 |
+
const m = (job.measurements || {}).faces || [];
|
| 199 |
+
// What colour is on the wall (and does it match the spec), and what equipment is on site.
|
| 200 |
+
const colour = job.paint_color || {};
|
| 201 |
+
const objects = job.site_objects || {};
|
| 202 |
+
const coat = colour.dominant;
|
| 203 |
+
const spec = colour.reference;
|
| 204 |
+
const coatCell = coat
|
| 205 |
+
? `<span class="swatch" style="background:${esc(coat.hex)}"></span> ${esc(coat.name)}
|
| 206 |
+
<span class="muted">${esc(coat.hex)}</span>${spec ? ` · spec
|
| 207 |
+
<b class="${spec.verdict === "match" ? "" : "warn"}">${esc(spec.verdict)}</b>
|
| 208 |
+
<span class="muted">ΔE00 ${fmt(spec.delta_e, 1)}</span>` : ""}`
|
| 209 |
+
: "not reported";
|
| 210 |
+
const seen = (objects.present || []).map((n) => n.replace(/_/g, " "));
|
| 211 |
+
const equipmentCell = objects.status
|
| 212 |
+
? `${seen.length ? esc(seen.join(", ")) : "none seen"}${(objects.missing_tools || []).length
|
| 213 |
+
? ` <span class="warn">· missing ${esc(objects.missing_tools.join(", ").replace(/_/g, " "))}</span>` : ""}`
|
| 214 |
+
: "not reported";
|
| 215 |
+
$("#areas").innerHTML = `
|
| 216 |
+
<dt>Paint area</dt><dd>${areaFt}</dd>
|
| 217 |
+
<dt>Coat colour</dt><dd class="small">${coatCell}</dd>
|
| 218 |
+
<dt>Equipment seen</dt><dd class="small">${equipmentCell}</dd>
|
| 219 |
+
<dt>Prep regions</dt><dd>${fmt(s.prep_regions)}</dd>
|
| 220 |
+
<dt>Touch-up patches</dt><dd>${fmt(s.touch_up_patches)}</dd>
|
| 221 |
+
<dt>Wall faces measured</dt><dd>${fmt(m.length)}</dd>
|
| 222 |
+
<dt>Scale</dt><dd class="small">${scale.mm_per_px ? `${fmt(scale.mm_per_px, 2)} mm/px` : "none"}</dd>
|
| 223 |
+
${scale.source ? `<dd class="small muted full">${esc(scale.source.replace(/_/g, " "))}</dd>` : ""}`;
|
| 224 |
+
|
| 225 |
+
const steps = $("#steps");
|
| 226 |
+
steps.innerHTML = "";
|
| 227 |
+
for (const step of order.steps || []) {
|
| 228 |
+
const li = document.createElement("li");
|
| 229 |
+
let inner = `<span class="step-name">${esc(step.step.replace("_", " "))}</span>`;
|
| 230 |
+
if (step.skipped) inner += ` <span class="small muted">— ${esc(step.skipped)}</span>`;
|
| 231 |
+
if (step.provisional) inner += ` <span class="small warn">provisional until rescan</span>`;
|
| 232 |
+
if (step.hold_minutes) inner += ` <span class="small muted">· hold ${fmt(step.hold_minutes)} min</span>`;
|
| 233 |
+
if (step.step === "rescan") inner += `<div class="small muted">${esc(step.reason)}</div>`;
|
| 234 |
+
if (step.plan) {
|
| 235 |
+
const p = step.plan.plan;
|
| 236 |
+
inner += `<div class="small muted">${fmt(p.passes)} roller passes · ${fmt(p.trim.segments)} trim strokes</div>`;
|
| 237 |
+
}
|
| 238 |
+
if (step.step === "setup") {
|
| 239 |
+
// What has to be on site before any paint: tools the plan needs, colour check, clutter.
|
| 240 |
+
inner += "<ul>" + step.tasks.map((t) => `<li class="task"><b>${esc(String(t.task).replace(/-/g, " "))}</b>
|
| 241 |
+
<div class="muted">${esc(t.detail || "")}</div></li>`).join("") + "</ul>";
|
| 242 |
+
} else if (step.step === "spot_prime") {
|
| 243 |
+
// One line per primer: the zones are the prep zones above.
|
| 244 |
+
const groups = {};
|
| 245 |
+
step.tasks.forEach((t) => {
|
| 246 |
+
const g = (groups[t.primer] = groups[t.primer] || { n: 0, mm2: 0, px: 0 });
|
| 247 |
+
g.n += 1; g.mm2 += t.zone_area_mm2 || 0; g.px += t.zone_area_px || 0;
|
| 248 |
+
});
|
| 249 |
+
inner += "<ul>" + Object.entries(groups).map(([primer, g]) => `<li class="task"><b>${esc(primer)}</b>
|
| 250 |
+
<div class="muted">${g.n} zone${g.n > 1 ? "s" : ""} · ${g.mm2 ? sqft(g.mm2) : fmt(g.px) + " px"}</div></li>`).join("") + "</ul>";
|
| 251 |
+
} else if (step.tasks) {
|
| 252 |
+
const card = (t, i) => `<li class="task" data-task="${i}" data-step="${esc(step.step)}">
|
| 253 |
+
<b>${esc(t.type ? t.type.replace("_", " ") : t.task)}</b>
|
| 254 |
+
${t.method ? `<div>${esc(t.method)}</div>` : ""}
|
| 255 |
+
<div class="muted">${t.zone_area_mm2 || t.area_mm2 ? sqft(t.zone_area_mm2 || t.area_mm2) : fmt(t.zone_area_px ?? t.area_px) + " px"}
|
| 256 |
+
${t.confidence ? ` · conf ${fmt(t.confidence.mean, 2)}` : ""}</div></li>`;
|
| 257 |
+
// Largest first for display (the order itself stays in travel order).
|
| 258 |
+
const ranked = step.tasks.map((t, i) => [t, i]).sort((a, b) => (b[0].zone_area_px ?? b[0].area_px) - (a[0].zone_area_px ?? a[0].area_px));
|
| 259 |
+
const head = ranked.slice(0, 3).map(([t, i]) => card(t, i)).join("");
|
| 260 |
+
const rest = ranked.slice(3).map(([t, i]) => card(t, i)).join("");
|
| 261 |
+
inner += `<ul>${head}</ul>` + (rest ? `<details class="more"><summary class="small">Show ${ranked.length - 3} more</summary><ul>${rest}</ul></details>` : "");
|
| 262 |
+
}
|
| 263 |
+
li.innerHTML = inner;
|
| 264 |
+
steps.appendChild(li);
|
| 265 |
+
}
|
| 266 |
+
// Hovering a prep/prime task highlights its damage region (tasks are in travel order; match by centre).
|
| 267 |
+
steps.querySelectorAll(".task").forEach((el) => {
|
| 268 |
+
const step = (order.steps || []).find((s) => s.step === el.dataset.step);
|
| 269 |
+
const task = step && step.tasks[+el.dataset.task];
|
| 270 |
+
const region = task && regionFor(task);
|
| 271 |
+
el.addEventListener("mouseenter", () => highlight(region, true));
|
| 272 |
+
el.addEventListener("mouseleave", () => highlight(region, false));
|
| 273 |
+
});
|
| 274 |
+
|
| 275 |
+
const locked = !!job.approved;
|
| 276 |
+
$("#replan").disabled = locked;
|
| 277 |
+
$("#approve").disabled = locked || order.status === "blocked";
|
| 278 |
+
$("#approve").title = order.status === "blocked" ? "The safety gate blocks this order" : "";
|
| 279 |
+
$("#download").hidden = false;
|
| 280 |
+
$("#download").href = `/api/jobs/${job.id}/work_order.json?download=1`;
|
| 281 |
+
$("#approved").innerHTML = locked
|
| 282 |
+
? `Approved by <b>${esc(job.approved.by)}</b> at ${esc(new Date(job.approved.at).toLocaleString())}<br><span class="muted">registry ${esc(job.approved.registry_hash.slice(0, 16))}…</span>`
|
| 283 |
+
: "";
|
| 284 |
+
document.querySelectorAll("[data-tool]").forEach((b) => (b.disabled = locked && b.dataset.tool !== "none"));
|
| 285 |
+
$("#clear-edits").disabled = locked;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
function regionFor(task) {
|
| 289 |
+
const poly = JSON.stringify(task.zone_polygon_px || null);
|
| 290 |
+
return (job.regions || []).findIndex((r) => JSON.stringify(r.prep_zone_xy || null) === poly);
|
| 291 |
+
}
|
| 292 |
+
function highlight(index, on) {
|
| 293 |
+
if (index == null || index < 0) return;
|
| 294 |
+
const el = document.querySelector(`#vector [data-region="${index}"]`);
|
| 295 |
+
if (el) el.classList.toggle("hl", on);
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
function renderConfirmations() {
|
| 299 |
+
const box = $("#confirmations");
|
| 300 |
+
box.innerHTML = "";
|
| 301 |
+
for (const [name, meaning] of Object.entries((health && health.confirmations) || {})) {
|
| 302 |
+
const label = document.createElement("label");
|
| 303 |
+
label.innerHTML = `<input type="checkbox" value="${esc(name)}"><span>${esc(meaning)}</span>`;
|
| 304 |
+
label.querySelector("input").addEventListener("change", () => setDirty(true));
|
| 305 |
+
box.appendChild(label);
|
| 306 |
+
}
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
// ------------------------------------------------------------------ layers
|
| 310 |
+
function applyLayerToggles() {
|
| 311 |
+
const opacity = $("#opacity").value / 100;
|
| 312 |
+
document.querySelectorAll("#layer-toggles input").forEach((box) => {
|
| 313 |
+
const name = box.dataset.layer;
|
| 314 |
+
document.querySelectorAll(`[data-layer="${name}"]:not(input)`).forEach((el) => {
|
| 315 |
+
if (el instanceof SVGElement) el.style.display = box.checked ? "" : "none"; // .hidden is HTML-only
|
| 316 |
+
else el.hidden = !box.checked;
|
| 317 |
+
if (el.tagName === "IMG") el.style.opacity = opacity;
|
| 318 |
+
});
|
| 319 |
+
});
|
| 320 |
+
}
|
| 321 |
+
document.querySelectorAll("#layer-toggles input").forEach((b) => b.addEventListener("change", applyLayerToggles));
|
| 322 |
+
$("#opacity").addEventListener("input", applyLayerToggles);
|
| 323 |
+
|
| 324 |
+
// ------------------------------------------------------------------ brush
|
| 325 |
+
document.querySelectorAll("[data-tool]").forEach((b) => b.addEventListener("click", () => {
|
| 326 |
+
tool = b.dataset.tool;
|
| 327 |
+
document.querySelectorAll("[data-tool]").forEach((x) => {
|
| 328 |
+
x.classList.toggle("on", x === b);
|
| 329 |
+
x.setAttribute("aria-pressed", String(x === b));
|
| 330 |
+
});
|
| 331 |
+
$("#stage").classList.toggle("drawing", tool !== "none");
|
| 332 |
+
if (tool !== "none") {
|
| 333 |
+
const edits = document.querySelector('#layer-toggles input[data-layer="edits"]');
|
| 334 |
+
edits.checked = true;
|
| 335 |
+
applyLayerToggles();
|
| 336 |
+
}
|
| 337 |
+
}));
|
| 338 |
+
|
| 339 |
+
function toImage(e) {
|
| 340 |
+
const r = editCanvas.getBoundingClientRect();
|
| 341 |
+
return { x: (e.clientX - r.left) * (editCanvas.width / r.width), y: (e.clientY - r.top) * (editCanvas.height / r.height), scale: editCanvas.width / r.width };
|
| 342 |
+
}
|
| 343 |
+
function dab(e) {
|
| 344 |
+
if (!editCanvas.getBoundingClientRect().width || !$("#photo").complete) return; // photo not laid out yet
|
| 345 |
+
const { x, y, scale } = toImage(e);
|
| 346 |
+
const radius = ($("#brush").value / 2) * scale;
|
| 347 |
+
ectx.globalCompositeOperation = tool === "erase" ? "destination-out" : "source-over";
|
| 348 |
+
ectx.fillStyle = tool === "add" ? "rgb(0,255,0)" : "rgb(255,0,0)";
|
| 349 |
+
ectx.beginPath();
|
| 350 |
+
ectx.arc(x, y, radius, 0, Math.PI * 2);
|
| 351 |
+
ectx.fill();
|
| 352 |
+
setDirty(true);
|
| 353 |
+
}
|
| 354 |
+
function moveCursor(e) {
|
| 355 |
+
const cursor = $("#cursor");
|
| 356 |
+
const stage = $("#stage").getBoundingClientRect();
|
| 357 |
+
cursor.hidden = tool === "none";
|
| 358 |
+
const size = $("#brush").value;
|
| 359 |
+
cursor.style.width = cursor.style.height = `${size}px`;
|
| 360 |
+
cursor.style.left = `${e.clientX - stage.left}px`;
|
| 361 |
+
cursor.style.top = `${e.clientY - stage.top}px`;
|
| 362 |
+
}
|
| 363 |
+
editCanvas.addEventListener("pointerdown", (e) => {
|
| 364 |
+
if (tool === "none" || (job && job.approved)) return;
|
| 365 |
+
drawing = true;
|
| 366 |
+
editCanvas.setPointerCapture(e.pointerId);
|
| 367 |
+
dab(e);
|
| 368 |
+
});
|
| 369 |
+
editCanvas.addEventListener("pointermove", (e) => { moveCursor(e); if (drawing) dab(e); });
|
| 370 |
+
editCanvas.addEventListener("pointerup", () => (drawing = false));
|
| 371 |
+
editCanvas.addEventListener("pointerleave", () => ($("#cursor").hidden = true));
|
| 372 |
+
$("#clear-edits").addEventListener("click", () => { ectx.clearRect(0, 0, editCanvas.width, editCanvas.height); setDirty(true); });
|
| 373 |
+
|
| 374 |
+
function setDirty(on) {
|
| 375 |
+
dirty = on;
|
| 376 |
+
$("#dirty").hidden = !on;
|
| 377 |
+
}
|
| 378 |
+
function editsPayload() {
|
| 379 |
+
const data = ectx.getImageData(0, 0, editCanvas.width, editCanvas.height).data;
|
| 380 |
+
for (let i = 3; i < data.length; i += 4) if (data[i]) return editCanvas.toDataURL("image/png");
|
| 381 |
+
return "";
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
// ------------------------------------------------------------------ actions
|
| 385 |
+
$("#replan").addEventListener("click", async () => {
|
| 386 |
+
if (!job) return;
|
| 387 |
+
const confirmations = [...document.querySelectorAll("#confirmations input:checked")].map((b) => b.value);
|
| 388 |
+
const btn = $("#replan");
|
| 389 |
+
btn.disabled = true;
|
| 390 |
+
btn.textContent = "Planning…";
|
| 391 |
+
try {
|
| 392 |
+
showJob(await api(`/api/jobs/${job.id}/plan`, {
|
| 393 |
+
method: "POST", headers: { "Content-Type": "application/json" },
|
| 394 |
+
body: JSON.stringify({ edits_png: editsPayload(), confirmations, operator: $("#operator").value.trim() }),
|
| 395 |
+
}));
|
| 396 |
+
} catch (err) {
|
| 397 |
+
alert(err.message);
|
| 398 |
+
} finally {
|
| 399 |
+
btn.textContent = "Re-plan";
|
| 400 |
+
btn.disabled = !!(job && job.approved);
|
| 401 |
+
}
|
| 402 |
+
});
|
| 403 |
+
|
| 404 |
+
$("#approve").addEventListener("click", async () => {
|
| 405 |
+
if (!job) return;
|
| 406 |
+
if (dirty) { alert("Re-plan first: your edits or confirmations are not in this work order yet."); return; }
|
| 407 |
+
const operator = $("#operator").value.trim();
|
| 408 |
+
if (!operator) { $("#operator").focus(); alert("Enter your name to approve."); return; }
|
| 409 |
+
if (!confirm(`Approve this work order as ${operator}? It will be frozen and recorded.`)) return;
|
| 410 |
+
try {
|
| 411 |
+
showJob(await api(`/api/jobs/${job.id}/approve`, {
|
| 412 |
+
method: "POST", headers: { "Content-Type": "application/json" },
|
| 413 |
+
body: JSON.stringify({ operator, note: $("#note").value }),
|
| 414 |
+
}));
|
| 415 |
+
} catch (err) {
|
| 416 |
+
alert(err.message);
|
| 417 |
+
}
|
| 418 |
+
});
|
| 419 |
+
|
| 420 |
+
boot();
|