chore(ui): add module file and main stub
- chore(ui): add module file and main stub - feat(ui): add layout, pages and partials - feat(ui): add stylesheet and scripts - feat(ui): add page renderer and static file handler - test(ui): cover rendering, escaping and static serving
This commit is contained in:
1 parent
7089a6f408
commit
5ac8f98f10
26 files changed
+3921
No files matched your search
@@ -0,0 +1,146 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, ApiError, api, appData, busy, clear, clearErrors, flash, formatTTL, h, handleError, plural, showFieldErrors, toast, zonePath } from "./common.js";
|
||||
|
||||
const { zone } = appData();
|
||||
const base = zonePath(zone);
|
||||
const state = { fingerprint: null, mode: "merge", content: "" };
|
||||
|
||||
function entryTable(title, entries, withOld) {
|
||||
if (!entries || entries.length === 0) return null;
|
||||
const head = ["Name", "Type", "TTL", "Value"];
|
||||
if (withOld) head.push("Old TTL");
|
||||
return h("div", { class: "table-wrap" },
|
||||
h("table", { class: "table" },
|
||||
h("caption", { text: title + " (" + entries.length + ")" }),
|
||||
h("thead", null, h("tr", null, head.map((label) => h("th", { scope: "col", text: label })))),
|
||||
h("tbody", null, entries.map((entry) => h("tr", null,
|
||||
h("th", { scope: "row", class: "mono", text: entry.name === "" ? "@" : entry.name }),
|
||||
h("td", { text: entry.type }),
|
||||
h("td", { class: "num", text: formatTTL(entry.ttl) }),
|
||||
h("td", { class: "mono", text: entry.value }),
|
||||
withOld ? h("td", { class: "num", text: formatTTL(entry.old_ttl) }) : null)))));
|
||||
}
|
||||
|
||||
function skippedList(skipped) {
|
||||
if (!skipped || skipped.length === 0) return null;
|
||||
return h("div", null,
|
||||
h("h3", { text: "Skipped (" + skipped.length + ")" }),
|
||||
h("ul", { class: "list" }, skipped.map((item) => h("li", null,
|
||||
item.line ? "Line " + item.line + ": " : "",
|
||||
h("code", { class: "mono", text: item.text || "" }),
|
||||
" ",
|
||||
h("span", { class: "muted", text: item.reason })))));
|
||||
}
|
||||
|
||||
function showErrors(list) {
|
||||
const box = $("#import-errors");
|
||||
clear(box);
|
||||
box.hidden = !list || list.length === 0;
|
||||
if (box.hidden) return;
|
||||
box.append(h("div", { class: "alert alert-error", role: "alert" },
|
||||
h("p", { text: "The file cannot be imported:" }),
|
||||
h("ul", { class: "list" }, list.map((item) => h("li", { text: (item.line ? "Line " + item.line + ": " : "") + item.message })))));
|
||||
}
|
||||
|
||||
function showPreview(data) {
|
||||
const result = $("#import-result");
|
||||
result.hidden = false;
|
||||
showErrors([]);
|
||||
const summary = $("#import-summary");
|
||||
clear(summary);
|
||||
const added = data.added || [];
|
||||
const updated = data.updated || [];
|
||||
const deleted = data.deleted || [];
|
||||
const skipped = data.skipped || [];
|
||||
summary.append(h("p", { text: plural(added.length, "addition", "additions") + ", " + plural(updated.length, "update", "updates") + ", " + plural(deleted.length, "deletion", "deletions") + ", " + (data.unchanged || 0) + " unchanged, " + plural(skipped.length, "skipped entry", "skipped entries") + "." }));
|
||||
if (data.soa) {
|
||||
summary.append(h("p", { text: "The SOA record is replaced: " + data.soa.ns + " " + data.soa.mbox + " refresh " + data.soa.refresh + " retry " + data.soa.retry + " expire " + data.soa.expire + " minimum " + data.soa.minttl + " TTL " + formatTTL(data.soa.ttl) + "." }));
|
||||
}
|
||||
for (const warning of data.warnings || []) {
|
||||
summary.append(h("p", { class: "alert alert-warn", text: (warning.line ? "Line " + warning.line + ": " : "") + warning.message }));
|
||||
}
|
||||
const tables = $("#import-tables");
|
||||
clear(tables);
|
||||
for (const node of [entryTable("Additions", added, false), entryTable("Updates", updated, true), entryTable("Deletions", deleted, false), skippedList(skipped)]) {
|
||||
if (node) tables.append(node);
|
||||
}
|
||||
state.fingerprint = data.fingerprint;
|
||||
const total = added.length + updated.length + deleted.length + (data.soa ? 1 : 0);
|
||||
$("#import-apply").hidden = total === 0;
|
||||
if (total === 0) summary.append(h("p", { class: "muted", text: "Nothing to apply." }));
|
||||
}
|
||||
|
||||
function currentInput() {
|
||||
const form = $("#import-form");
|
||||
return { mode: form.elements.mode.value, content: form.elements.content.value };
|
||||
}
|
||||
|
||||
async function preview(event) {
|
||||
event.preventDefault();
|
||||
const form = $("#import-form");
|
||||
clearErrors(form);
|
||||
const input = currentInput();
|
||||
if (input.content.trim() === "") {
|
||||
showFieldErrors(form, { content: "Paste a zone file or load one." });
|
||||
return;
|
||||
}
|
||||
await busy($("#import-preview"), async () => {
|
||||
try {
|
||||
const { data } = await api("POST", base + "/import/preview", { body: input });
|
||||
state.mode = input.mode;
|
||||
state.content = input.content;
|
||||
showPreview(data);
|
||||
} catch (err) {
|
||||
$("#import-apply").hidden = true;
|
||||
state.fingerprint = null;
|
||||
if (err instanceof ApiError && err.status === 422 && Array.isArray(err.body && err.body.errors)) {
|
||||
$("#import-result").hidden = false;
|
||||
clear($("#import-summary"));
|
||||
clear($("#import-tables"));
|
||||
showErrors(err.body.errors);
|
||||
return;
|
||||
}
|
||||
handleError(err, { form });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function apply() {
|
||||
if (!state.fingerprint) return;
|
||||
await busy($("#import-apply"), async () => {
|
||||
try {
|
||||
const { data } = await api("POST", base + "/import/apply", { body: { mode: state.mode, content: state.content, fingerprint: state.fingerprint } });
|
||||
flash("Import applied: " + plural(data.added || 0, "addition", "additions") + ", " + plural(data.updated || 0, "update", "updates") + ", " + plural(data.deleted || 0, "deletion", "deletions") + ".");
|
||||
location.assign("/zones/" + encodeURIComponent(zone));
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status === 409) {
|
||||
$("#import-apply").hidden = true;
|
||||
state.fingerprint = null;
|
||||
toast("The zone changed since the preview. Run the preview again.", { kind: "error" });
|
||||
return;
|
||||
}
|
||||
handleError(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$("#zone-link").href = "/zones/" + encodeURIComponent(zone);
|
||||
$("#import-form").addEventListener("submit", preview);
|
||||
$("#import-apply").addEventListener("click", apply);
|
||||
$("#import-form").addEventListener("input", () => {
|
||||
$("#import-apply").hidden = true;
|
||||
state.fingerprint = null;
|
||||
});
|
||||
$("#import-file").addEventListener("change", async (event) => {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
if (file.size > 2 * 1024 * 1024) {
|
||||
toast("The file is larger than 2 MiB.", { kind: "error" });
|
||||
return;
|
||||
}
|
||||
$("#import-content").value = await file.text();
|
||||
$("#import-apply").hidden = true;
|
||||
state.fingerprint = null;
|
||||
});
|
||||
Reference in new issue
Block a user