web: split app.js into view modules
Pure move, behaviour unchanged: app.js is now the shell (boot, state, client wiring, connection/banner, navigation and routes, render dispatch, header). Views live under src/views with shared DOM and format helpers. client.js stays DOM-free; no circular imports; every server-derived string still uses textContent.
This commit is contained in:
@@ -0,0 +1,405 @@
|
||||
// The Templates gallery and detail, including the create-from-template dialog
|
||||
// and its validate-then-submit flow. Pure wording/model helpers live in
|
||||
// templates.js; this module owns the DOM and the page's template state.
|
||||
|
||||
import { h, kvMono } from "../dom.js";
|
||||
import { describeError } from "../format.js";
|
||||
import {
|
||||
CREATE_ACTION_LABEL,
|
||||
CREATE_NAME_LABEL,
|
||||
CREATE_NOTE,
|
||||
createConfirmLabel,
|
||||
createDismissLabel,
|
||||
createFormTitle,
|
||||
createFormValid,
|
||||
createSuccessMessage,
|
||||
createdPipelineName,
|
||||
effectiveStageType,
|
||||
templateCard,
|
||||
templateDetailName,
|
||||
templateGalleryName,
|
||||
templateGraphNote,
|
||||
templateStageCountLabel,
|
||||
templateStageSummary,
|
||||
TEMPLATE_UNPARSED_SOURCE_NOTE,
|
||||
} from "../templates.js";
|
||||
|
||||
export function createTemplatesView(rt) {
|
||||
const { state, repo } = rt;
|
||||
const renderContent = (...args) => rt.renderContent(...args);
|
||||
const pushRoute = (...args) => rt.pushRoute(...args);
|
||||
const setError = (...args) => rt.setError(...args);
|
||||
const refreshPipelines = (...args) => rt.views.pipelines.refreshPipelines(...args);
|
||||
|
||||
/**
|
||||
* Reset the template detail/create state that belongs to a selection. Shared by
|
||||
* selecting a template and opening the gallery from Settings, so the two lists of
|
||||
* fields can never drift.
|
||||
*/
|
||||
function resetTemplateCreateState() {
|
||||
state.templateTab = "summary";
|
||||
// A different template reseeds the create dialog, so a name typed for one
|
||||
// template can never be submitted against another.
|
||||
state.templateCreateOpen = false;
|
||||
state.templateCreateName = "";
|
||||
state.templateCreateSending = false;
|
||||
state.templateCreateError = "";
|
||||
state.templateCreateCreated = null;
|
||||
}
|
||||
|
||||
/** Clear the selection and everything that belongs to it. */
|
||||
function resetTemplateSelection() {
|
||||
state.selectedTemplateKey = null;
|
||||
resetTemplateCreateState();
|
||||
}
|
||||
|
||||
/** The cached parsed graph for a template key, or null when it did not parse. */
|
||||
function templateGraphFor(key) {
|
||||
return state.templateGraphs.has(key) ? state.templateGraphs.get(key) : null;
|
||||
}
|
||||
|
||||
function renderTemplateGallery() {
|
||||
const pane = h("div", { class: "list gallery" });
|
||||
pane.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "pane-head" },
|
||||
h("h2", { text: "Templates" }),
|
||||
h("button", { class: "ghost", text: "Refresh templates", onclick: () => loadTemplates(true) }),
|
||||
),
|
||||
);
|
||||
if (state.templateLoading && !state.templates.length) {
|
||||
pane.append(h("div", { class: "muted pad", text: "Loading templates…" }));
|
||||
return pane;
|
||||
}
|
||||
if (!state.templates.length) {
|
||||
pane.append(h("div", { class: "muted pad", text: "No templates are available on this server." }));
|
||||
return pane;
|
||||
}
|
||||
const cards = h("div", { class: "cards" });
|
||||
for (const entry of state.templates) {
|
||||
const card = templateCard(entry, templateGraphFor(entry.key));
|
||||
cards.append(
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
class: `card${state.selectedTemplateKey === entry.key ? " selected" : ""}`,
|
||||
onclick: () => selectTemplate(entry.key),
|
||||
},
|
||||
h("span", { class: "card-name", text: templateGalleryName(card.name) }),
|
||||
h("span", { class: "card-desc", text: card.description }),
|
||||
h("span", { class: "card-meta", text: templateStageCountLabel(card.stageCount) }),
|
||||
),
|
||||
);
|
||||
}
|
||||
pane.append(cards);
|
||||
return pane;
|
||||
}
|
||||
|
||||
function renderTemplateDetail() {
|
||||
const pane = h("div", { class: "detail" });
|
||||
const entry = state.templates.find((t) => t.key === state.selectedTemplateKey);
|
||||
if (!entry) {
|
||||
pane.append(h("div", { class: "muted pad", text: "Select a template." }));
|
||||
return pane;
|
||||
}
|
||||
const graph = templateGraphFor(entry.key);
|
||||
pane.append(renderTemplateDetailActions());
|
||||
pane.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "tabstrip" },
|
||||
templateTabButton("summary", "Summary"),
|
||||
templateTabButton("graph", "Graph"),
|
||||
templateTabButton("source", "Source"),
|
||||
),
|
||||
);
|
||||
if (state.templateTab === "graph") pane.append(renderTemplateGraphTab(graph));
|
||||
else if (state.templateTab === "source") pane.append(renderTemplateSourceTab(entry, graph));
|
||||
else pane.append(renderTemplateSummaryTab(entry, graph));
|
||||
if (state.templateCreateOpen) pane.append(renderTemplateCreateDialog(entry));
|
||||
return pane;
|
||||
}
|
||||
|
||||
/**
|
||||
* The template detail's action row: the one "Create pipeline from this
|
||||
* template" action (P5), matching the app's top-bar add action. It opens the
|
||||
* dialog; it never submits on its own.
|
||||
*/
|
||||
function renderTemplateDetailActions() {
|
||||
return h(
|
||||
"div",
|
||||
{ class: "detail-actions" },
|
||||
h("button", {
|
||||
class: "ghost template-create-open",
|
||||
type: "button",
|
||||
text: CREATE_ACTION_LABEL,
|
||||
onclick: () => openTemplateCreateDialog(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/** Open the create dialog with a fresh, empty name; nothing is sent until Create. */
|
||||
function openTemplateCreateDialog() {
|
||||
state.templateCreateOpen = true;
|
||||
state.templateCreateName = "";
|
||||
state.templateCreateSending = false;
|
||||
state.templateCreateError = "";
|
||||
state.templateCreateCreated = null;
|
||||
renderContent();
|
||||
document.querySelector(".template-create-name")?.focus();
|
||||
}
|
||||
|
||||
/** Dismiss the create dialog (never while a validate+submit is in flight). */
|
||||
function closeTemplateCreateDialog() {
|
||||
if (state.templateCreateSending) return;
|
||||
state.templateCreateOpen = false;
|
||||
state.templateCreateName = "";
|
||||
state.templateCreateError = "";
|
||||
state.templateCreateCreated = null;
|
||||
renderContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* The create dialog (P5): the title, the one required name field, the note, the
|
||||
* server's refusal text or the success line, and Create/Cancel (Cancel reads
|
||||
* Close after a success). Create is disabled while the name is blank or while
|
||||
* sending; the field locks while sending and after a success. Every server
|
||||
* string is set with textContent.
|
||||
*/
|
||||
function renderTemplateCreateDialog(entry) {
|
||||
const title = createFormTitle(entry);
|
||||
const overlay = h("div", {
|
||||
class: "template-create-dialog",
|
||||
role: "dialog",
|
||||
"aria-modal": "true",
|
||||
"aria-label": title,
|
||||
});
|
||||
overlay.addEventListener("click", (event) => {
|
||||
if (event.target === overlay) closeTemplateCreateDialog();
|
||||
});
|
||||
const panel = h("div", { class: "template-create-panel" });
|
||||
panel.append(h("h2", { class: "template-create-title", text: title }));
|
||||
|
||||
const field = h("label", { class: "template-create-field" });
|
||||
field.append(h("span", { class: "template-create-label", text: CREATE_NAME_LABEL }));
|
||||
field.append(
|
||||
h("input", {
|
||||
class: "template-create-name",
|
||||
type: "text",
|
||||
value: state.templateCreateName,
|
||||
spellcheck: "false",
|
||||
autocomplete: "off",
|
||||
disabled: state.templateCreateSending || state.templateCreateCreated != null,
|
||||
oninput: (event) => {
|
||||
// Update in place (no re-render) so the field keeps focus and its caret;
|
||||
// only the confirm button's enabled state depends on the name.
|
||||
state.templateCreateName = event.target.value;
|
||||
const confirm = overlay.querySelector(".template-create-confirm");
|
||||
if (confirm) {
|
||||
confirm.disabled = !createFormValid(state.templateCreateName, {
|
||||
sending: state.templateCreateSending,
|
||||
created: state.templateCreateCreated,
|
||||
});
|
||||
}
|
||||
},
|
||||
}),
|
||||
);
|
||||
panel.append(field);
|
||||
panel.append(h("p", { class: "template-create-note muted", text: CREATE_NOTE }));
|
||||
if (state.templateCreateError) {
|
||||
panel.append(h("div", { class: "template-create-error bad", text: state.templateCreateError }));
|
||||
}
|
||||
if (state.templateCreateCreated != null) {
|
||||
panel.append(
|
||||
h("div", {
|
||||
class: "template-create-success",
|
||||
text: createSuccessMessage(state.templateCreateCreated),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const actions = h("div", { class: "template-create-actions" });
|
||||
actions.append(
|
||||
h("button", {
|
||||
class: "ghost template-create-cancel",
|
||||
type: "button",
|
||||
text: createDismissLabel(state.templateCreateCreated),
|
||||
disabled: state.templateCreateSending,
|
||||
onclick: closeTemplateCreateDialog,
|
||||
}),
|
||||
);
|
||||
actions.append(
|
||||
h("button", {
|
||||
class: "template-create-confirm",
|
||||
type: "button",
|
||||
text: createConfirmLabel(state.templateCreateSending),
|
||||
disabled: !createFormValid(state.templateCreateName, {
|
||||
sending: state.templateCreateSending,
|
||||
created: state.templateCreateCreated,
|
||||
}),
|
||||
onclick: () => createFromTemplate(entry),
|
||||
}),
|
||||
);
|
||||
panel.append(actions);
|
||||
overlay.append(panel);
|
||||
return overlay;
|
||||
}
|
||||
|
||||
/**
|
||||
* The P5 submit flow, exactly the app's: validate the template's source first
|
||||
* and, when the server refuses to parse it, show its own error in the dialog and
|
||||
* send nothing; otherwise submit the source create-only with the chosen name. On
|
||||
* success remember the created name, refresh the pipeline list so the new row is
|
||||
* visible and leave the dialog open (Cancel now reads Close); on failure show
|
||||
* the server's error without closing.
|
||||
*/
|
||||
async function createFromTemplate(entry) {
|
||||
if (
|
||||
!createFormValid(state.templateCreateName, {
|
||||
sending: state.templateCreateSending,
|
||||
created: state.templateCreateCreated,
|
||||
})
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const name = state.templateCreateName;
|
||||
state.templateCreateSending = true;
|
||||
state.templateCreateError = "";
|
||||
renderContent();
|
||||
try {
|
||||
await repo.validate(entry.source);
|
||||
} catch (error) {
|
||||
state.templateCreateSending = false;
|
||||
state.templateCreateError = describeError(error);
|
||||
renderContent();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const detail = await repo.submit(name, entry.source);
|
||||
state.templateCreateSending = false;
|
||||
state.templateCreateCreated = createdPipelineName(detail, name);
|
||||
await refreshPipelines();
|
||||
} catch (error) {
|
||||
state.templateCreateSending = false;
|
||||
state.templateCreateError = describeError(error);
|
||||
}
|
||||
renderContent();
|
||||
}
|
||||
|
||||
function templateTabButton(tab, label) {
|
||||
return h("button", {
|
||||
class: `minitab${state.templateTab === tab ? " active" : ""}`,
|
||||
text: label,
|
||||
onclick: () => {
|
||||
state.templateTab = tab;
|
||||
renderContent();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function renderTemplateSummaryTab(entry, graph) {
|
||||
const wrap = h("div", { class: "tabbody" });
|
||||
wrap.append(h("h2", { text: templateDetailName(entry) }));
|
||||
if ((entry.description ?? "").trim()) {
|
||||
wrap.append(h("p", { class: "body", text: entry.description }));
|
||||
}
|
||||
wrap.append(h("div", { class: "kv" }, kvMono("Key", entry.key)));
|
||||
wrap.append(
|
||||
h("div", { class: "meta-line", text: templateStageCountLabel(graph?.stages?.length ?? null) }),
|
||||
);
|
||||
if (entry.scheduled) wrap.append(h("div", { class: "meta-line", text: "Includes a schedule" }));
|
||||
|
||||
const summary = templateStageSummary(graph);
|
||||
if (summary.note) {
|
||||
wrap.append(h("div", { class: "muted pad", text: summary.note }));
|
||||
return wrap;
|
||||
}
|
||||
wrap.append(h("h3", { text: "Stages" }));
|
||||
const ul = h("ul", { class: "stages" });
|
||||
for (const stage of summary.stages) {
|
||||
ul.append(
|
||||
h(
|
||||
"li",
|
||||
{},
|
||||
h("code", { text: stage.key }),
|
||||
h("span", { class: "chip", text: effectiveStageType(stage.type) }),
|
||||
),
|
||||
);
|
||||
}
|
||||
wrap.append(ul);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
function renderTemplateGraphTab(graph) {
|
||||
const wrap = h("div", { class: "tabbody" });
|
||||
wrap.append(h("p", { class: "muted", text: templateGraphNote(graph) }));
|
||||
return wrap;
|
||||
}
|
||||
|
||||
function renderTemplateSourceTab(entry, graph) {
|
||||
const wrap = h("div", { class: "tabbody" });
|
||||
if (graph == null) {
|
||||
wrap.append(h("p", { class: "muted", text: TEMPLATE_UNPARSED_SOURCE_NOTE }));
|
||||
}
|
||||
wrap.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "codeblock" },
|
||||
h("div", { class: "codeblock-label", text: "Definition source" }),
|
||||
h("pre", { class: "codeblock-body", text: entry.source ?? "" }),
|
||||
),
|
||||
);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the template catalogue, then validates each template's source once and
|
||||
* caches the parsed graph (null when it did not parse) so a card's stage count
|
||||
* and the detail's stage list read the same result and can never disagree.
|
||||
*/
|
||||
async function loadTemplates(force = false) {
|
||||
state.templateLoading = true;
|
||||
state.error = "";
|
||||
renderContent();
|
||||
try {
|
||||
state.templates = await repo.listTemplates();
|
||||
for (const entry of state.templates) {
|
||||
if (force || !state.templateGraphs.has(entry.key)) {
|
||||
state.templateGraphs.set(entry.key, await validateTemplate(entry));
|
||||
}
|
||||
}
|
||||
state.templateLoading = false;
|
||||
renderContent();
|
||||
} catch (err) {
|
||||
state.templateLoading = false;
|
||||
setError(err);
|
||||
}
|
||||
}
|
||||
|
||||
/** The parsed graph for a template, or null when the server cannot parse its source. */
|
||||
async function validateTemplate(entry) {
|
||||
try {
|
||||
return await repo.validate(entry.source);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function selectTemplate(key) {
|
||||
if (key) pushRoute({ kind: "template", key });
|
||||
else pushRoute({ kind: "templates" });
|
||||
state.selectedTemplateKey = key || null;
|
||||
resetTemplateCreateState();
|
||||
renderContent();
|
||||
}
|
||||
|
||||
return {
|
||||
resetTemplateCreateState,
|
||||
resetTemplateSelection,
|
||||
renderTemplateGallery,
|
||||
renderTemplateDetail,
|
||||
loadTemplates,
|
||||
selectTemplate,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user