// Stepped full-page create flow: Basics → Source → Outputs → Review. import { el, clear, append, toast, setText, withBusy, } from "../util.js"; import { api } from "../api.js"; import { refresh, canEdit } from "../store.js"; import { variantEditor, outputEditor } from "../components.js"; const STEPS = ["Basics", "Source", "Outputs", "Review"]; export function streamCreate(root) { if (!canEdit()) { root.append(el("div.card", el("h2", { text: "Not available" }), el("p.hint", { text: "Operators and admins can create streams." }), )); return; } let step = 0; let apps = []; const name = el("input", { name: "name", required: true, placeholder: "city-news", maxlength: "64" }); const title = el("input", { name: "title", placeholder: "City News" }); const appSel = el("select", { onchange: fillChannels }, el("option", { value: "", text: "(none)" })); const chanSel = el("select"); const tags = el("input", { placeholder: "council, sports" }); const onDemand = el("input", { type: "checkbox" }); const enabled = el("input", { type: "checkbox", checked: true }); const isPublic = el("input", { type: "checkbox", checked: true }); const mpd = el("input", { placeholder: "https://…/manifest.mpd" }); const key = el("textarea", { rows: "3", placeholder: "KID:KEY per line — leave empty for clear streams", spellcheck: "false" }); const proxy = el("input", { placeholder: "socks5://127.0.0.1:1080 (optional)" }); const headers = el("input", { value: "{}" }); const idle = el("input", { type: "number", value: "300", min: "30", max: "86400" }); const drm = el("select", el("option", { value: "auto", text: "Auto — infer from the key" }), el("option", { value: "yes", text: "Encrypted — keys required" }), el("option", { value: "no", text: "Clear — no keys needed" }), ); const rewriter = el("select", el("option", { value: "", text: "Inherit from module / none" }), ); const videoSel = el("input", { placeholder: "for=best (blank = module default)" }); const audioSel = el("input", { placeholder: "for=best (blank = module default)" }); const variants = variantEditor(null); const outputs = outputEditor(null); const stepNav = el("div.wizard-steps"); const panel = el("div"); const msg = el("p.msg"); const backBtn = el("button.ghost", { type: "button", text: "Back", onclick: () => go(step - 1) }); const nextBtn = el("button", { type: "button", text: "Next", onclick: () => go(step + 1) }); const skipBtn = el("button.ghost", { type: "button", text: "Use defaults → Review", onclick: () => go(3), }); const createBtn = el("button", { type: "button", text: "Create stream", onclick: (ev) => submit(ev.currentTarget) }); const cancelBtn = el("button.ghost", { type: "button", text: "Cancel", onclick: () => { location.hash = "#/streams"; }, }); const footLeft = el("div.row"); const footRight = el("div.row"); root.append(el("div.card", el("div.card-head", el("h2", { text: "Add stream" }), el("span.spacer"), el("a", { href: "#/streams", text: "Back to list" }), ), stepNav, panel, msg, el("div.wizard-foot", footLeft, el("span.spacer"), footRight), )); function fillChannels() { clear(chanSel); const a = apps.find((x) => x.name === appSel.value); if (!a || !a.channels || !a.channels.length) { chanSel.append(el("option", { value: "", text: a ? "(module declares no channels)" : "(none)" })); return; } append(chanSel, a.channels.map((c) => el("option", { value: c.id, text: c.label && c.label !== c.id ? `${c.id} — ${c.label}` : c.id }))); } function body() { return { name: name.value.trim(), title: title.value.trim(), app: appSel.value, channel: chanSel.value, mpd: mpd.value.trim(), key: key.value.trim(), proxy: proxy.value.trim(), headers_json: headers.value.trim() || "{}", tags: tags.value, drm_mode: drm.value, rewriter: rewriter.value, video_select: videoSel.value.trim(), audio_select: audioSel.value.trim(), enabled: enabled.checked, on_demand: onDemand.checked, public: isPublic.checked, idle_timeout_s: parseInt(idle.value, 10) || 300, variants: variants.readValue(), outputs: outputs.readValue(), }; } function validateStep(i) { setText(msg, ""); msg.classList.remove("err"); if (i === 0) { if (!name.value.trim()) { setText(msg, "Name is required."); msg.classList.add("err"); return false; } } if (i === 1) { try { JSON.parse(headers.value || "{}"); } catch { setText(msg, "Headers must be valid JSON."); msg.classList.add("err"); return false; } } return true; } function go(i) { if (i > step && !validateStep(step)) return; step = Math.max(0, Math.min(STEPS.length - 1, i)); paint(); } function paintSteps() { clear(stepNav); append(stepNav, STEPS.map((label, i) => el(`button.step${i === step ? ".on" : ""}${i < step ? ".done" : ""}`, { type: "button", text: `${i + 1}. ${label}`, onclick: () => { if (i <= step || validateStep(step)) go(i); }, }))); } function paintPanel() { clear(panel); if (step === 0) { panel.append(el("div.grid", el("label", el("span.lbl", { text: "Name (URL slug)" }), name), el("label", el("span.lbl", { text: "Title" }), title), el("label", el("span.lbl", { text: "App module" }), appSel), el("label", el("span.lbl", { text: "Channel" }), chanSel), el("label", el("span.lbl", { text: "Tags" }), tags), el("label.inline-check", enabled, el("span", { text: "Enabled (required for on-demand wake)" })), el("label.inline-check", onDemand, el("span", { text: "Start on demand, stop when idle" })), el("label.inline-check", isPublic, el("span", { text: "Publicly fetchable" })), )); } else if (step === 1) { panel.append(el("div.grid", el("label.span2", el("span.lbl", { text: "Manifest URL" }), mpd), el("label.span2", el("span.lbl", { text: "Keys" }), key), el("label", el("span.lbl", { text: "Origin proxy" }), proxy), el("label", el("span.lbl", { text: "Headers JSON" }), headers), el("label", el("span.lbl", { text: "Encryption" }), drm), el("label", el("span.lbl", { text: "Rewriter" }), rewriter), el("label", el("span.lbl", { text: "Video select" }), videoSel), el("label", el("span.lbl", { text: "Audio select" }), audioSel), el("label", el("span.lbl", { text: "Idle timeout (s)" }), idle), )); } else if (step === 2) { panel.append(el("div.stack", variants, outputs)); } else { const b = body(); panel.append(el("div.stack", el("p.hint", { text: "Confirm and create. You can edit everything later on the stream page." }), el("div.card", { style: { boxShadow: "none" } }, row("Name", b.name), row("Title", b.title || "—"), row("Module", b.app ? `${b.app} / ${b.channel || "—"}` : "—"), row("Manifest", b.mpd || "(from module / capture)"), row("Encryption", b.drm_mode), row("On-demand", b.on_demand ? "yes" : "no"), row("Public", b.public ? "yes" : "no"), row("Variants", String((b.variants || []).filter((v) => v.enabled).length) + " enabled"), row("Outputs", String((b.outputs || []).filter((o) => o.enabled).length) + " enabled"), ), )); } } function row(k, v) { return el("div.row", { style: { justifyContent: "space-between", padding: "4px 0" } }, el("span.muted", { text: k }), el("span.mono", { text: v }), ); } function paintFoot() { clear(footLeft); clear(footRight); footLeft.append(cancelBtn); if (step === 0 && (appSel.value || name.value)) footLeft.append(skipBtn); if (step > 0) footRight.append(backBtn); if (step < STEPS.length - 1) footRight.append(nextBtn); else footRight.append(createBtn); } function paint() { paintSteps(); paintPanel(); paintFoot(); if (step === 0) name.focus(); } async function submit(btn) { if (!validateStep(0) || !validateStep(1)) { go(0); return; } await withBusy(btn, async () => { try { const st = await api.createStream(body()); toast(`Created ${st.name}`); await refresh("create"); location.hash = `#/stream/${st.id}`; } catch (err) { setText(msg, err.message); msg.classList.add("err"); } }); } (async () => { try { const d = await api.apps(); apps = d.apps || []; clear(appSel); appSel.append(el("option", { value: "", text: "(none)" })); append(appSel, apps.map((a) => el("option", { value: a.name, text: a.name }))); fillChannels(); for (const n of d.rewriters || []) { rewriter.append(el("option", { value: n, text: n })); } } catch { // Module list is optional. } paint(); })(); paint(); }