// The manager's "מקצועות" tab (add-subjects-and-worksheets): every subject with its lessons. A manager or a developer creates a // subject, renames it, changes its icon, deletes an empty one and orders its lessons. Math is fixed: it holds the content file's // lessons, which keep their order; lessons added to math come after them. import { api } from "./api.js"; import { esc } from "./math.js"; const STATE = { published: "פורסם", draft: "טיוטה", drafting: "בהכנה", failed: "נכשל" }; const stateOf = l => l.status === "drafting" ? "drafting" : l.status === "failed" ? "failed" : l.published ? "published" : "draft"; const icons = (list, chosen, name) => `
${list.map((icon, i) => ``).join("")}
`; function lessonList(subject) { const fixed = subject.fixed ? `
  • ${subject.file_lessons.length} שיעורים מקובץ התוכן (הסדר שלהם קבוע)
  • ` : ""; const rows = subject.lessons.map((l, i) => `
  • ${esc(l.name || "שיעור בלי שם")} ${STATE[stateOf(l)]} ${l.skills} מיומנויות
  • `).join(""); return ``; } const UPLOAD_TYPES = ".pdf,application/pdf,image/jpeg,image/png,image/webp,.docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document"; function uploadForm(data) { return `

    דף עבודה חדש

    PDF אחד (עד 20 עמודים), עד 10 תמונות, או מסמך Word אחד, עד 15MB. ה־AI מכין ממנו טיוטה של שיעור, ואתם בודקים ומתקנים לפני הפרסום.

    `; } export async function view() { const data = await api("/api/subjects"); const warning = data.content_error ? `` : ""; return `${warning}${uploadForm(data)}

    מקצוע חדש

    ${icons(data.icons, null, "sb-new-icon")}

    ${data.subjects.map(s => `

    ${esc(s.name)}

    ${s.fixed ? "" : ``}
    ${s.fixed ? "" : ``} ${lessonList(s)}
    `).join("")}
    `; } /** Send one file as the request body (the server takes a PDF, an image or a Word document by its content type). */ async function sendFile(lessonId, file) { const { token } = await import("./session.js"); const kind = file.type || (file.name.toLowerCase().endsWith(".docx") ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : ""); const response = await fetch(`/api/worksheets/${lessonId}/files`, { method: "POST", body: file, headers: { Authorization: "Bearer " + await token(), "Content-Type": kind } }); if (!response.ok) { let detail = ""; try { detail = (await response.json()).detail; } catch { /* no body */ } throw new Error(typeof detail === "string" && detail ? detail : "ההעלאה נכשלה"); } } function wireUpload(openLesson) { const form = document.getElementById("worksheet-form"), pick = document.getElementById("ws-subject"); pick.addEventListener("change", () => { document.getElementById("ws-new").hidden = pick.value !== "new"; }); form.addEventListener("submit", async event => { event.preventDefault(); const error = document.getElementById("ws-error"), progress = document.getElementById("ws-progress"), files = [...document.getElementById("ws-files").files]; error.hidden = true; const body = pick.value === "new" ? { new_subject: document.getElementById("ws-new-name").value, icon: form.querySelector("input[name=ws-icon]:checked")?.value } : { subject_id: pick.value }; try { progress.textContent = "מתחילים…"; const lesson = await api("/api/worksheets", { method: "POST", body }); for (const [i, file] of files.entries()) { progress.textContent = `מעלים ${i + 1} מתוך ${files.length}…`; await sendFile(lesson.id, file); } progress.textContent = "מבקשים טיוטה…"; await api(`/api/worksheets/${lesson.id}/draft`, { method: "POST" }); openLesson(lesson.id); } catch (e) { progress.textContent = ""; error.textContent = e.message; error.hidden = false; } }); } export function wire(go, say, openLesson) { const done = (message, error = "") => { say(message, error); go("subjects"); }; wireUpload(openLesson); document.getElementById("new-subject").addEventListener("submit", async event => { event.preventDefault(); const icon = document.querySelector("#new-subject input[name=sb-new-icon]:checked")?.value; try { const made = await api("/api/subjects", { method: "POST", body: { name: document.getElementById("sb-name").value, icon } }); done(`המקצוע "${made.name}" נוצר.`); } catch (e) { done("", e.message); } }); document.getElementById("subjects").addEventListener("click", async event => { const card = event.target.closest("[data-subject]"); if (!card) return; const id = card.dataset.subject, form = card.querySelector(".sb-rename"); const edit = event.target.closest("[data-open-lesson]")?.dataset.openLesson; if (edit) { openLesson(Number(edit)); return; } if (event.target.closest("[data-rename]")) { form.hidden = false; form.querySelector("input").focus(); return; } if (event.target.closest("[data-cancel]")) { form.hidden = true; return; } if (event.target.closest("[data-delete]")) { if (!confirm(`למחוק את המקצוע "${card.querySelector("h3").textContent.trim()}"?`)) return; try { await api(`/api/subjects/${encodeURIComponent(id)}`, { method: "DELETE" }); done("המקצוע נמחק."); } catch (e) { done("", e.message); } return; } const move = event.target.closest("[data-move]"); if (move) { const order = [...card.querySelectorAll("[data-lesson]")].map(li => Number(li.dataset.lesson)); const from = order.indexOf(Number(move.closest("[data-lesson]").dataset.lesson)), to = from + Number(move.dataset.move); [order[from], order[to]] = [order[to], order[from]]; try { await api(`/api/subjects/${encodeURIComponent(id)}/order`, { method: "PUT", body: { lessons: order } }); done("הסדר נשמר."); } catch (e) { done("", e.message); } } }); document.querySelectorAll("#subjects .sb-rename").forEach(form => form.addEventListener("submit", async event => { event.preventDefault(); const id = form.closest("[data-subject]").dataset.subject; const body = { name: form.querySelector("input[type=text], input:not([type])").value, icon: form.querySelector("input[type=radio]:checked")?.value }; try { await api(`/api/subjects/${encodeURIComponent(id)}`, { method: "PUT", body }); done("המקצוע עודכן."); } catch (e) { done("", e.message); } })); }