const $ = (s) => document.querySelector(s); const JOB = new URLSearchParams(location.search).get("job"); let state = { settings: {}, steps: [] }; let pickerStep = null; const imgURL = (n) => n && n.startsWith("up_") ? `/api/jobs/${JOB}/uploads/${n}` : `/api/jobs/${JOB}/frames/${n}`; const esc = (s) => String(s == null ? "" : s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); // ---------------------------------------------------------------- init async function init() { if (!JOB) { $("#doc").innerHTML = "
No job specified.
"; return; } const r = await fetch(`/api/jobs/${JOB}/result`); if (!r.ok) { $("#doc").innerHTML = "
Result not found.
"; return; } buildState(await r.json()); fillSettings(); renderDoc(); } function buildState(d) { state.raw = d; const today = new Date().toLocaleDateString("de-DE"); state.settings = { product_name: d.product.name || "", model: d.product.model || "", id_number: d.product.id_number || "", station_title: (d.stations[0] && d.stations[0].station_title) || "Station 1: Final Assembly", bilingual: false, include_goal: true, include_narration: false, include_part_ids: true, date: today, drawn_by: "AI Documentation Engine", }; state.steps = []; for (const st of d.stations) { for (const s of st.steps) { state.steps.push({ include: true, number: s.step_number, title: s.title || "", goal: s.goal || "", bullets: (s.instructions || []).map((i) => i.text), image: s.frame_image, narration_de: (s.narration && s.narration.original_text) || "", narration_en: (s.narration && s.narration.english_text) || "", parts: (s.components || []) .filter((c) => c.part_id || (c.part_match && c.part_match.confident)) .map((c) => ({ name: c.name, part_no: c.part_id || (c.part_match && c.part_match.part_no) })), }); } } } // ---------------------------------------------------------------- settings panel function fillSettings() { $("#s-name").value = state.settings.product_name; $("#s-model").value = state.settings.model; $("#s-id").value = state.settings.id_number; $("#s-station").value = state.settings.station_title; const bind = (id, key, render) => $(id).addEventListener("input", (e) => { state.settings[key] = e.target.value; if (render) render(); }); bind("#s-name", "product_name"); bind("#s-model", "model"); bind("#s-id", "id_number"); bind("#s-station", "station_title", () => { const h = $("#station-h"); if (h) h.textContent = state.settings.station_title; }); const tog = (id, key) => $(id).addEventListener("change", (e) => (state.settings[key] = e.target.checked)); tog("#t-goal", "include_goal"); tog("#t-parts", "include_part_ids"); tog("#t-narr", "include_narration"); tog("#t-biling", "bilingual"); } // ---------------------------------------------------------------- document render function renderDoc() { const s = state.settings; $("#doc").innerHTML = `
${esc(s.product_name || "Assembly Instruction")}
Montageanweisung · ${esc(s.model || "")} ${s.id_number ? "· ID " + esc(s.id_number) : ""}
${esc(s.station_title)}
${state.steps.map((st, i) => stepHTML(st, i)).join("")}`; } function stepHTML(st, i) { const bullets = st.bullets.map((b, bi) => `
›
`).join(""); const parts = st.parts.length ? `
${st.parts.map((p) => `${esc(p.name)} ${esc(p.part_no)}`).join("")}
` : ""; return `
${st.number}
${bullets} ${parts}
click to change / upload image
`; } function reStep(i) { const el = document.getElementById(`estep-${i}`); if (el) el.outerHTML = stepHTML(state.steps[i], i); } // ---------------------------------------------------------------- events (delegated) $("#doc").addEventListener("input", (e) => { const el = e.target, i = el.dataset.idx, f = el.dataset.field; if (i == null) return; const st = state.steps[i]; if (f === "title") st.title = el.value; else if (f === "goal") st.goal = el.value; else if (f === "bullet") st.bullets[el.dataset.b] = el.value; }); $("#doc").addEventListener("change", (e) => { if (e.target.dataset.act === "inc") { const i = e.target.dataset.idx; state.steps[i].include = e.target.checked; document.getElementById(`estep-${i}`).classList.toggle("off", !e.target.checked); } }); $("#doc").addEventListener("click", (e) => { const b = e.target.closest("[data-act]"); if (!b) return; const i = b.dataset.idx, act = b.dataset.act; if (act === "addbul") { state.steps[i].bullets.push(""); reStep(i); } else if (act === "delbul") { state.steps[i].bullets.splice(b.dataset.b, 1); reStep(i); } else if (act === "img") openPicker(+i); }); // ---------------------------------------------------------------- image picker async function openPicker(i) { pickerStep = i; $("#picker").classList.remove("hidden"); const box = $("#picker-opts"); box.innerHTML = "Extracting frames…"; const cur = state.steps[i].image; const r = await fetch(`/api/jobs/${JOB}/frame_options?step=${state.steps[i].number}&count=6`); const { options } = await r.json(); const all = [{ name: cur, url: imgURL(cur), t: "current" }, ...options]; box.innerHTML = all.map((o) => `
${o.t === "current" ? "current" : "@ " + o.t + "s"}
`).join(""); box.querySelectorAll(".opt").forEach((el) => el.addEventListener("click", () => { setImage(i, el.dataset.name); closePicker(); })); } function setImage(i, name) { state.steps[i].image = name; const el = document.querySelector(`#estep-${i} .imgwrap img`); if (el) el.src = imgURL(name); } function closePicker() { $("#picker").classList.add("hidden"); } $("#picker-close").addEventListener("click", closePicker); $("#picker").addEventListener("click", (e) => { if (e.target.id === "picker") closePicker(); }); $("#picker-upload").addEventListener("change", async (e) => { if (!e.target.files.length || pickerStep == null) return; const fd = new FormData(); fd.append("image", e.target.files[0]); const r = await fetch(`/api/jobs/${JOB}/images`, { method: "POST", body: fd }); const { name } = await r.json(); setImage(pickerStep, name); closePicker(); }); // ---------------------------------------------------------------- export $("#export").addEventListener("click", async () => { const btn = $("#export"); btn.disabled = true; btn.textContent = "Building…"; try { const model = { settings: state.settings, steps: state.steps }; const r = await fetch(`/api/jobs/${JOB}/export`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(model), }); if (!r.ok) throw new Error("export failed"); const blob = await r.blob(); const a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = (state.settings.model || "assembly_instruction").replace(/\s+/g, "_") + ".docx"; a.click(); URL.revokeObjectURL(a.href); toast("Word document downloaded ✓"); } catch (err) { toast("Export failed: " + err.message); } btn.disabled = false; btn.textContent = "⬇ Export to Word (.docx)"; }); $("#dl-json").addEventListener("click", () => { const blob = new Blob([JSON.stringify(state.raw, null, 2)], { type: "application/json" }); const a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = (state.settings.model || "assembly") + ".json"; a.click(); URL.revokeObjectURL(a.href); }); function toast(msg) { let t = $(".toast"); if (!t) { t = document.createElement("div"); t.className = "toast"; document.body.appendChild(t); } t.textContent = msg; t.classList.add("show"); setTimeout(() => t.classList.remove("show"), 2600); } init();