Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
440f78ce9e |
No files matched your search
@@ -4,7 +4,7 @@
|
||||
import { test, expect } from '../harness/fixtures';
|
||||
import type { RecordInput } from '../harness/ui';
|
||||
import { up } from '../harness/api';
|
||||
import { addAddress, checkZone, createZone } from '../harness/personas';
|
||||
import { addAddress, address, checkZone, createZone } from '../harness/personas';
|
||||
|
||||
test.use({ group: 'stupid' });
|
||||
|
||||
@@ -70,3 +70,83 @@ test('operator mistakes preserve DNS, user records and delegations', async ({ wo
|
||||
expect((await api.get(zonePath)).status).toBe(404);
|
||||
expect(await world.db.records(zone)).toEqual([]);
|
||||
});
|
||||
|
||||
test('records page keeps its view in the URL and protects typed input', async ({ world }) => {
|
||||
const admin = await world.bootstrapAdmin();
|
||||
const api = await world.apiAs(admin);
|
||||
const ui = await world.uiAs(admin);
|
||||
const zone = world.zoneName();
|
||||
const zonePath = await createZone(api, zone);
|
||||
await addAddress(api, zonePath, 'zeta', '192.0.2.40');
|
||||
await addAddress(api, zonePath, 'alpha', '192.0.2.41');
|
||||
await addAddress(api, zonePath, 'www', '192.0.2.42');
|
||||
await addAddress(api, zonePath, 'www2', '192.0.2.44');
|
||||
const mixed = await api.post(`${zonePath}/records`, address('www', '192.0.2.43', 600));
|
||||
expect(mixed.status, mixed.text).toBe(201);
|
||||
await ui.openZone(zone);
|
||||
const page = ui.page;
|
||||
const names = async () => (await ui.rows()).map((row) => row.name);
|
||||
|
||||
const nameHeader = page.locator('#records-table th[data-sort=name]');
|
||||
await nameHeader.getByRole('button').click();
|
||||
await expect(nameHeader).toHaveAttribute('aria-sort', 'ascending');
|
||||
expect(await names()).toEqual(['@', '@', 'alpha', 'www', 'www', 'www2', 'zeta']);
|
||||
await nameHeader.getByRole('button').click();
|
||||
await expect(nameHeader).toHaveAttribute('aria-sort', 'descending');
|
||||
expect(await names()).toEqual(['zeta', 'www2', 'www', 'www', 'alpha', '@', '@']);
|
||||
await page.locator('#filter-text').fill('alpha');
|
||||
await expect(page).toHaveURL(/[?&]q=alpha(&|$)/);
|
||||
await expect(page).toHaveURL(/[?&]sort=-name(&|$)/);
|
||||
await page.reload();
|
||||
await ui.settle();
|
||||
await expect(page.locator('#filter-text')).toHaveValue('alpha');
|
||||
await expect(nameHeader).toHaveAttribute('aria-sort', 'descending');
|
||||
expect(await names()).toEqual(['alpha']);
|
||||
|
||||
await page.getByRole('button', { name: 'Show www A in the records table' }).first().click();
|
||||
await expect(page.locator('#filter-text')).toHaveValue(`www.${zone}`);
|
||||
await expect(page.locator('#filter-type')).toHaveValue('A');
|
||||
await expect(page.locator('#filter-text')).toBeFocused();
|
||||
expect(await names()).toEqual(['www', 'www']);
|
||||
await expect(page).toHaveURL(/[?&]name=www\./);
|
||||
await page.locator('#filter-text').fill('');
|
||||
await page.locator('#filter-type').selectOption('');
|
||||
|
||||
const dialog = page.locator('#record-dialog');
|
||||
await page.click('#record-add');
|
||||
await page.fill('#rec-name', 'unsaved');
|
||||
await page.mouse.click(4, 4);
|
||||
await expect(dialog, 'a backdrop click keeps an edited form open').toBeVisible();
|
||||
await page.keyboard.press('Escape');
|
||||
await expect(dialog).toBeHidden();
|
||||
await page.click('#record-add');
|
||||
await page.mouse.click(4, 4);
|
||||
await expect(dialog, 'a backdrop click closes an untouched form').toBeHidden();
|
||||
|
||||
await page.click('#record-add');
|
||||
await page.fill('#rec-name', 'multi');
|
||||
await page.fill('#rec-f-ip', '192.0.2.50');
|
||||
const first = page.waitForResponse((res) => res.url().endsWith(`/api/v1${zonePath}/records`) && res.request().method() === 'POST');
|
||||
await page.click('#rec-submit-another');
|
||||
expect((await first).status()).toBe(201);
|
||||
await expect(dialog).toBeVisible();
|
||||
await expect(page.locator('#rec-name')).toHaveValue('multi');
|
||||
await expect(page.locator('#rec-f-ip')).toHaveValue('');
|
||||
await expect(page.locator('#rec-f-ip')).toBeFocused();
|
||||
await expect(page.locator('#rec-status')).toContainText('Record added.');
|
||||
await page.fill('#rec-f-ip', '192.0.2.51');
|
||||
const second = page.waitForResponse((res) => res.url().endsWith(`/api/v1${zonePath}/records`) && res.request().method() === 'POST');
|
||||
await page.click('#rec-submit');
|
||||
expect((await second).status()).toBe(201);
|
||||
await expect(dialog).toBeHidden();
|
||||
await expect(page.locator('#records-body tr.row-flash')).toHaveCount(1);
|
||||
await expect(page.locator('#records-body tr.row-flash th')).toHaveText('multi');
|
||||
await ui.settle();
|
||||
expect((await world.db.records(zone)).filter((row) => row.name === 'multi').length).toBe(2);
|
||||
|
||||
await page.click('#audit-link');
|
||||
await ui.settle();
|
||||
await expect(page.locator('#af-zone')).toHaveValue(zone);
|
||||
await expect(page.locator('#af-kind')).toHaveValue('change');
|
||||
await expect(page.locator('#audit-body tr:not(.detail-row)').first()).toContainText(zone);
|
||||
});
|
||||
@@ -283,6 +283,16 @@ pre.code {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.btn[aria-busy="true"] {
|
||||
cursor: progress;
|
||||
}
|
||||
|
||||
.btn-link {
|
||||
border-color: transparent;
|
||||
color: var(--accent);
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: var(--accent);
|
||||
border-color: var(--accent);
|
||||
@@ -488,6 +498,41 @@ input[type="radio"] {
|
||||
width: 2rem;
|
||||
}
|
||||
|
||||
.th-sort {
|
||||
background: none;
|
||||
border: 0;
|
||||
color: inherit;
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
font-weight: 600;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.th-sort::after {
|
||||
color: var(--muted);
|
||||
content: "\2195";
|
||||
content: "\2195" / "";
|
||||
display: inline-block;
|
||||
margin-left: 0.25rem;
|
||||
width: 1ch;
|
||||
}
|
||||
|
||||
[aria-sort="ascending"] .th-sort::after {
|
||||
color: var(--text);
|
||||
content: "\2191";
|
||||
content: "\2191" / "";
|
||||
}
|
||||
|
||||
[aria-sort="descending"] .th-sort::after {
|
||||
color: var(--text);
|
||||
content: "\2193";
|
||||
content: "\2193" / "";
|
||||
}
|
||||
|
||||
.th-sort:hover {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.table .value code {
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
@@ -508,6 +553,16 @@ input[type="radio"] {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.table tbody tr.row-flash {
|
||||
animation: row-flash 2s ease-out;
|
||||
}
|
||||
|
||||
@keyframes row-flash {
|
||||
from {
|
||||
background: var(--info-bg);
|
||||
}
|
||||
}
|
||||
|
||||
.detail-row > td {
|
||||
background: var(--stripe);
|
||||
}
|
||||
@@ -660,6 +715,24 @@ input[type="radio"] {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.dialog-status {
|
||||
align-self: center;
|
||||
flex: 1 1 100%;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.dialog-status:empty {
|
||||
border: 0;
|
||||
clip: rect(0 0 0 0);
|
||||
height: 1px;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
padding: 0;
|
||||
position: absolute;
|
||||
white-space: nowrap;
|
||||
width: 1px;
|
||||
}
|
||||
|
||||
.dialog-actions {
|
||||
border-top: 1px solid var(--border);
|
||||
display: flex;
|
||||
|
||||
+29
-3
@@ -1,10 +1,10 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, api, busy, formatTime, h, handleError, icon } from "./common.js";
|
||||
import { $, api, busy, formatTime, h, handleError, icon, readQuery, writeQuery } from "./common.js";
|
||||
import { FIELD_SPECS } from "./records.js";
|
||||
|
||||
const state = { nextBeforeId: null, count: 0, filters: {}, counter: 0 };
|
||||
const state = { nextBeforeId: null, count: 0, filters: {}, counter: 0, generation: 0 };
|
||||
const SOA_FIELDS = [
|
||||
{ key: "ns", label: "Primary name server" },
|
||||
{ key: "MBox", label: "Hostmaster" },
|
||||
@@ -211,6 +211,12 @@ async function load(append) {
|
||||
const status = $("#audit-status");
|
||||
const query = { ...state.filters };
|
||||
if (append && state.nextBeforeId !== null) query.before_id = state.nextBeforeId;
|
||||
if (append && state.nextBeforeId === null) return;
|
||||
if (!append) {
|
||||
state.generation += 1;
|
||||
$("#audit-more").hidden = true;
|
||||
}
|
||||
const generation = state.generation;
|
||||
if (!append) {
|
||||
body.replaceChildren();
|
||||
state.count = 0;
|
||||
@@ -218,6 +224,7 @@ async function load(append) {
|
||||
}
|
||||
try {
|
||||
const { data } = await api("GET", "/audit", { query });
|
||||
if (generation !== state.generation) return;
|
||||
const entries = data.entries || [];
|
||||
for (const entry of entries) body.append(...entryRows(entry));
|
||||
state.count += entries.length;
|
||||
@@ -225,15 +232,30 @@ async function load(append) {
|
||||
status.textContent = state.count === 0 ? "No audit rows match." : state.count + " rows shown.";
|
||||
$("#audit-more").hidden = state.nextBeforeId === null;
|
||||
} catch (err) {
|
||||
if (generation !== state.generation) return;
|
||||
status.textContent = "The audit could not be loaded.";
|
||||
handleError(err);
|
||||
}
|
||||
}
|
||||
|
||||
const FILTERS = ["zone", "actor", "operation", "kind", "outcome"];
|
||||
|
||||
function prefill() {
|
||||
const form = $("#audit-filters");
|
||||
const query = readQuery();
|
||||
for (const name of FILTERS) {
|
||||
const value = query.get(name);
|
||||
if (!value) continue;
|
||||
const control = form.elements[name];
|
||||
if (control.tagName === "SELECT" && ![...control.options].some((item) => item.value === value)) continue;
|
||||
control.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
function readFilters() {
|
||||
const form = $("#audit-filters");
|
||||
const filters = {};
|
||||
for (const name of ["zone", "actor", "operation", "kind", "outcome"]) {
|
||||
for (const name of FILTERS) {
|
||||
const value = form.elements[name].value.trim();
|
||||
if (value) filters[name] = value;
|
||||
}
|
||||
@@ -244,14 +266,18 @@ $("#audit-filters").addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
state.filters = readFilters();
|
||||
state.nextBeforeId = null;
|
||||
writeQuery(state.filters);
|
||||
load(false);
|
||||
});
|
||||
$("#audit-filters").addEventListener("reset", () => {
|
||||
setTimeout(() => {
|
||||
state.filters = {};
|
||||
state.nextBeforeId = null;
|
||||
writeQuery({});
|
||||
load(false);
|
||||
}, 0);
|
||||
});
|
||||
$("#audit-more").addEventListener("click", (event) => busy(event.target, () => load(true)));
|
||||
prefill();
|
||||
state.filters = readFilters();
|
||||
load(false);
|
||||
+37
-4
@@ -266,11 +266,17 @@ export function handleError(err, options = {}) {
|
||||
}
|
||||
|
||||
export async function busy(button, task) {
|
||||
if (button) button.disabled = true;
|
||||
if (button) {
|
||||
button.disabled = true;
|
||||
button.setAttribute("aria-busy", "true");
|
||||
}
|
||||
try {
|
||||
return await task();
|
||||
} finally {
|
||||
if (button) button.disabled = false;
|
||||
if (button) {
|
||||
button.disabled = false;
|
||||
button.removeAttribute("aria-busy");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,7 +284,7 @@ const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]):not([t
|
||||
|
||||
function trapFocus(dialog, event) {
|
||||
if (event.key !== "Tab") return;
|
||||
const items = [...dialog.querySelectorAll(FOCUSABLE)].filter((el) => !el.hidden && el.offsetParent !== null);
|
||||
const items = [...dialog.querySelectorAll(FOCUSABLE)].filter((el) => !el.hidden && el.offsetParent !== null && !el.closest("[inert]"));
|
||||
if (items.length === 0) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
@@ -298,8 +304,11 @@ export function setupDialog(dialog) {
|
||||
if (dialog.dataset.ready) return;
|
||||
dialog.dataset.ready = "1";
|
||||
dialog.addEventListener("keydown", (event) => trapFocus(dialog, event));
|
||||
dialog.addEventListener("input", () => {
|
||||
dialog.dataset.dirty = "1";
|
||||
});
|
||||
dialog.addEventListener("click", (event) => {
|
||||
if (event.target === dialog) dialog.close();
|
||||
if (event.target === dialog && !dialog.dataset.dirty) dialog.close();
|
||||
});
|
||||
for (const button of dialog.querySelectorAll("[data-close]")) {
|
||||
button.addEventListener("click", () => dialog.close());
|
||||
@@ -314,6 +323,7 @@ export function openDialog(dialog) {
|
||||
setupDialog(dialog);
|
||||
const form = dialog.querySelector("form");
|
||||
if (form) clearErrors(form);
|
||||
delete dialog.dataset.dirty;
|
||||
dialog.showModal();
|
||||
const first = dialog.querySelector("input:not([type=hidden]):not([disabled]), select:not([disabled]), textarea");
|
||||
if (first) first.focus();
|
||||
@@ -389,6 +399,29 @@ export function zoneLink(name) {
|
||||
return h("a", { href: "/zones/" + encodeURIComponent(name), text: name });
|
||||
}
|
||||
|
||||
export function readQuery() {
|
||||
return new URLSearchParams(location.search);
|
||||
}
|
||||
|
||||
let queryRetry = null;
|
||||
|
||||
export function writeQuery(values) {
|
||||
clearTimeout(queryRetry);
|
||||
queryRetry = null;
|
||||
const params = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(values)) {
|
||||
if (value !== undefined && value !== null && value !== "") params.set(key, String(value));
|
||||
}
|
||||
const text = params.toString();
|
||||
const url = location.pathname + (text ? "?" + text : "") + location.hash;
|
||||
if (url === location.pathname + location.search + location.hash) return;
|
||||
try {
|
||||
history.replaceState(history.state, "", url);
|
||||
} catch (err) {
|
||||
queryRetry = setTimeout(() => writeQuery(values), 1000);
|
||||
}
|
||||
}
|
||||
|
||||
export function bindPasswordForm(form, submit) {
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
@@ -76,8 +76,8 @@ export function displayName(record) {
|
||||
|
||||
export function matchesFilter(record, type, text) {
|
||||
if (type && record.type !== type) return false;
|
||||
if (!text) return true;
|
||||
const needle = text.toLowerCase();
|
||||
const needle = text.trim().toLowerCase();
|
||||
if (!needle) return true;
|
||||
const haystack = [displayName(record), record.fqdn || "", record.type, presentValue(record)].join(" ").toLowerCase();
|
||||
return haystack.includes(needle);
|
||||
}
|
||||
|
||||
+176
-15
@@ -1,23 +1,47 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, api, appData, busy, clear, clearErrors, confirmDialog, flash, formatTTL, handleError, h, icon, openDialog, option, parseNumber, plural, setupDialogs, showFieldErrors, showFlash, toast, zonePath } from "./common.js";
|
||||
import { $, api, appData, busy, clear, clearErrors, confirmDialog, flash, formatTTL, handleError, h, icon, openDialog, option, parseNumber, plural, readQuery, setupDialogs, showFieldErrors, showFlash, toast, writeQuery, zonePath } from "./common.js";
|
||||
import { EDITABLE_TYPES, FIELD_SPECS, displayName, matchesFilter, presentValue, previewName, stripDot } from "./records.js";
|
||||
|
||||
const PAGE_SIZE = 100;
|
||||
const { zone: zoneParam, isAdmin } = appData();
|
||||
const base = zonePath(zoneParam);
|
||||
|
||||
const query = readQuery();
|
||||
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
||||
|
||||
function parseSort(text) {
|
||||
const match = /^(-?)(name|type|ttl|value)$/.exec(text || "");
|
||||
return match ? { key: match[2], dir: match[1] ? -1 : 1 } : null;
|
||||
}
|
||||
|
||||
const owner = (query.get("name") || "").toLowerCase();
|
||||
|
||||
const state = {
|
||||
summary: null,
|
||||
records: [],
|
||||
problems: [],
|
||||
type: "",
|
||||
text: "",
|
||||
type: query.get("type") || "",
|
||||
text: owner || query.get("q") || "",
|
||||
owner,
|
||||
sort: parseSort(query.get("sort")),
|
||||
page: 0,
|
||||
pageRows: [],
|
||||
selected: new Set(),
|
||||
};
|
||||
|
||||
const COMPARE = {
|
||||
name: (a, b) => collator.compare(a.name, b.name),
|
||||
type: (a, b) => collator.compare(a.type, b.type),
|
||||
ttl: (a, b) => (a.ttl || 360) - (b.ttl || 360),
|
||||
value: (a, b) => collator.compare(displayValue(a), displayValue(b)),
|
||||
};
|
||||
|
||||
function syncUrl() {
|
||||
writeQuery({ type: state.type, q: state.owner ? "" : state.text.trim(), name: state.owner, sort: state.sort ? (state.sort.dir < 0 ? "-" : "") + state.sort.key : "" });
|
||||
}
|
||||
|
||||
function zoneName() {
|
||||
return state.summary ? state.summary.name : zoneParam;
|
||||
}
|
||||
@@ -40,6 +64,10 @@ function isBroken(record) {
|
||||
return record.data === null || record.data === undefined || hasErrors(record);
|
||||
}
|
||||
|
||||
function displayValue(record) {
|
||||
return isBroken(record) ? String(record.raw ?? "") : presentValue(record);
|
||||
}
|
||||
|
||||
function problemBadge(problem) {
|
||||
return h("li", { class: "problem problem-" + problem.severity },
|
||||
h("span", { class: "badge badge-" + (problem.severity === "error" ? "error" : "warning"), text: problem.severity }),
|
||||
@@ -81,6 +109,20 @@ function renderSoa() {
|
||||
managed.hidden = !dynamic;
|
||||
}
|
||||
|
||||
function showRecords(name, type) {
|
||||
const apex = zoneName().endsWith(".") ? zoneName() : zoneName() + ".";
|
||||
state.type = type || "";
|
||||
state.owner = ((name === "" ? "" : name + ".") + apex).toLowerCase();
|
||||
state.text = state.owner;
|
||||
state.page = 0;
|
||||
typeOptions();
|
||||
$("#filter-text").value = state.text;
|
||||
syncUrl();
|
||||
renderRecords();
|
||||
$("#records-title").scrollIntoView({ block: "start" });
|
||||
$("#filter-text").focus();
|
||||
}
|
||||
|
||||
function renderProblems() {
|
||||
const panel = $("#problems-panel");
|
||||
const list = $("#problems-list");
|
||||
@@ -88,9 +130,13 @@ function renderProblems() {
|
||||
panel.hidden = state.problems.length === 0;
|
||||
$("#problems-title").textContent = "Problems (" + state.problems.length + ")";
|
||||
for (const problem of state.problems) {
|
||||
const where = problem.name === null || problem.name === undefined ? "zone" : (problem.name === "" ? "@" : problem.name) + (problem.type ? " " + problem.type : "");
|
||||
const located = problem.name !== null && problem.name !== undefined;
|
||||
const where = located ? (problem.name === "" ? "@" : problem.name) + (problem.type ? " " + problem.type : "") : "zone";
|
||||
const item = problemBadge(problem);
|
||||
item.append(" ", h("span", { class: "muted", text: "(" + where + ")" }));
|
||||
if (located) {
|
||||
item.append(" ", h("button", { type: "button", class: "btn btn-small btn-link", "aria-label": "Show " + where + " in the records table", text: "Show", onclick: () => showRecords(problem.name, problem.type) }));
|
||||
}
|
||||
list.append(item);
|
||||
}
|
||||
}
|
||||
@@ -105,7 +151,24 @@ function typeOptions() {
|
||||
}
|
||||
|
||||
function visibleRecords() {
|
||||
return state.records.filter((record) => matchesFilter(record, state.type, state.text));
|
||||
const rows = state.owner
|
||||
? state.records.filter((record) => (!state.type || record.type === state.type) && (record.fqdn || "").toLowerCase() === state.owner)
|
||||
: state.records.filter((record) => matchesFilter(record, state.type, state.text));
|
||||
if (!state.sort) return rows;
|
||||
const { key, dir } = state.sort;
|
||||
if (key === "ttl") {
|
||||
const missing = (record) => !Number.isInteger(record.ttl);
|
||||
return rows.sort((a, b) => missing(a) - missing(b) || (missing(a) ? 0 : dir * COMPARE.ttl(a, b)));
|
||||
}
|
||||
return rows.sort((a, b) => dir * COMPARE[key](a, b));
|
||||
}
|
||||
|
||||
function renderSortHeaders() {
|
||||
for (const th of document.querySelectorAll("#records-table th[data-sort]")) {
|
||||
const active = state.sort && state.sort.key === th.dataset.sort;
|
||||
if (active) th.setAttribute("aria-sort", state.sort.dir > 0 ? "ascending" : "descending");
|
||||
else th.removeAttribute("aria-sort");
|
||||
}
|
||||
}
|
||||
|
||||
function problemsCell(record) {
|
||||
@@ -120,8 +183,10 @@ function renderRecords() {
|
||||
const pages = Math.max(1, Math.ceil(all.length / PAGE_SIZE));
|
||||
if (state.page >= pages) state.page = pages - 1;
|
||||
const rows = all.slice(state.page * PAGE_SIZE, (state.page + 1) * PAGE_SIZE);
|
||||
state.pageRows = rows;
|
||||
clear(body);
|
||||
for (const record of rows) body.append(recordRow(record));
|
||||
renderSortHeaders();
|
||||
const status = $("#records-status");
|
||||
if (state.records.length === 0) status.textContent = "This zone has no records.";
|
||||
else if (all.length === 0) status.textContent = "No record matches the filter.";
|
||||
@@ -152,14 +217,14 @@ function recordRow(record) {
|
||||
const hasError = hasErrors(record);
|
||||
const hasWarning = (record.problems || []).length > 0;
|
||||
const value = h("td", { class: "value" },
|
||||
h("code", { class: broken ? "mono raw" : "mono", text: broken ? (record.raw ?? "") : presentValue(record) }),
|
||||
h("code", { class: broken ? "mono raw" : "mono", text: displayValue(record) }),
|
||||
(record.problems || []).length > 0 ? problemsCell(record) : null);
|
||||
const edit = h("button", { type: "button", class: "btn btn-small", "aria-label": "Edit " + label, onclick: () => (isSoa ? openSoa(record) : openRecord(record)) }, icon("edit"), " Edit");
|
||||
const actions = h("td", { class: "actions-cell" }, edit);
|
||||
if (!isSoa || soaRows().length > 1) {
|
||||
actions.append(" ", h("button", { type: "button", class: "btn btn-small btn-danger", "aria-label": "Delete " + label, onclick: () => deleteRecord(record) }, icon("trash"), " Delete"));
|
||||
}
|
||||
return h("tr", { class: hasError ? "row-error" : hasWarning ? "row-warning" : null },
|
||||
return h("tr", { class: hasError ? "row-error" : hasWarning ? "row-warning" : null, "data-id": record.id },
|
||||
h("td", { class: "check" }, checkbox),
|
||||
h("th", { scope: "row", title: record.fqdn || "" }, h("span", { class: "mono", text: displayName(record) })),
|
||||
h("td", { text: record.type }),
|
||||
@@ -168,8 +233,22 @@ function recordRow(record) {
|
||||
actions);
|
||||
}
|
||||
|
||||
function pageRows() {
|
||||
return visibleRecords().slice(state.page * PAGE_SIZE, (state.page + 1) * PAGE_SIZE);
|
||||
function highlight(id) {
|
||||
if (id === undefined || id === null || !state.records.some((record) => record.id === id)) return;
|
||||
const index = visibleRecords().findIndex((record) => record.id === id);
|
||||
if (index < 0) {
|
||||
toast("The saved record is hidden by the current filter.");
|
||||
return;
|
||||
}
|
||||
const page = Math.floor(index / PAGE_SIZE);
|
||||
if (page !== state.page) {
|
||||
state.page = page;
|
||||
renderRecords();
|
||||
}
|
||||
const row = [...$("#records-body").rows].find((tr) => tr.dataset.id === String(id));
|
||||
if (!row) return;
|
||||
row.classList.add("row-flash");
|
||||
row.scrollIntoView({ block: "nearest" });
|
||||
}
|
||||
|
||||
function updateSelection() {
|
||||
@@ -178,16 +257,21 @@ function updateSelection() {
|
||||
const button = $("#bulk-delete");
|
||||
button.disabled = state.selected.size === 0;
|
||||
button.textContent = state.selected.size === 0 ? "Delete selected" : "Delete selected (" + state.selected.size + ")";
|
||||
const selectable = pageRows().filter((record) => record.type !== "SOA");
|
||||
const selectable = state.pageRows.filter((record) => record.type !== "SOA");
|
||||
$("#select-all").checked = selectable.length > 0 && selectable.every((record) => state.selected.has(record.id));
|
||||
}
|
||||
|
||||
let loadGeneration = 0;
|
||||
|
||||
async function load() {
|
||||
loadGeneration += 1;
|
||||
const generation = loadGeneration;
|
||||
const results = await Promise.allSettled([
|
||||
api("GET", base),
|
||||
api("GET", base + "/records"),
|
||||
api("GET", base + "/check"),
|
||||
]);
|
||||
if (generation !== loadGeneration) return;
|
||||
const [summary, records, check] = results;
|
||||
if (summary.status === "rejected") {
|
||||
$("#soa-status").textContent = "The zone could not be loaded.";
|
||||
@@ -203,6 +287,7 @@ async function load() {
|
||||
renderSoa();
|
||||
renderProblems();
|
||||
typeOptions();
|
||||
if (records.status === "fulfilled") syncUrl();
|
||||
renderRecords();
|
||||
if (isAdmin) loadUsers();
|
||||
}
|
||||
@@ -288,6 +373,9 @@ function reportWarnings(message, data) {
|
||||
const recordForm = {
|
||||
editing: null,
|
||||
ttlTouched: false,
|
||||
another: false,
|
||||
pending: 0,
|
||||
session: 0,
|
||||
};
|
||||
|
||||
function fieldId(key) {
|
||||
@@ -351,6 +439,13 @@ function applyRrsetTtl(result) {
|
||||
|
||||
function openRecord(record) {
|
||||
const form = $("#record-form");
|
||||
recordForm.session += 1;
|
||||
form.querySelector(".dialog-body").inert = false;
|
||||
$("#rec-status").textContent = "";
|
||||
for (const button of [$("#rec-submit"), $("#rec-submit-another")]) {
|
||||
button.disabled = false;
|
||||
button.removeAttribute("aria-busy");
|
||||
}
|
||||
const broken = Boolean(record) && isBroken(record);
|
||||
const known = Boolean(record) && EDITABLE_TYPES.includes(record.type);
|
||||
recordForm.editing = record || null;
|
||||
@@ -368,6 +463,7 @@ function openRecord(record) {
|
||||
form.elements.ttl.value = String(ttl);
|
||||
buildTypeFields(select.value, record && !broken ? record.data : {});
|
||||
$("#rec-submit").textContent = record ? "Save changes" : "Add record";
|
||||
$("#rec-submit-another").hidden = Boolean(record);
|
||||
updatePreview();
|
||||
openDialog($("#record-dialog"));
|
||||
}
|
||||
@@ -394,27 +490,77 @@ function setupRecordForm() {
|
||||
buildTypeFields(form.elements.type.value, readTypeValues(form));
|
||||
if (!recordForm.ttlTouched && !recordForm.editing) updatePreview();
|
||||
});
|
||||
const anotherButton = $("#rec-submit-another");
|
||||
anotherButton.addEventListener("click", () => {
|
||||
recordForm.another = true;
|
||||
form.requestSubmit();
|
||||
});
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
const another = recordForm.another && !recordForm.editing;
|
||||
recordForm.another = false;
|
||||
if (recordForm.pending === recordForm.session) return;
|
||||
clearErrors(form);
|
||||
if (!form.elements.type.value) {
|
||||
showFieldErrors(form, { type: "Select a type." });
|
||||
return;
|
||||
}
|
||||
const editing = recordForm.editing;
|
||||
await busy(form.querySelector("button[type=submit]"), async () => {
|
||||
const session = recordForm.session;
|
||||
const fields = form.querySelector(".dialog-body");
|
||||
const submitButton = $("#rec-submit");
|
||||
const clicked = another ? anotherButton : submitButton;
|
||||
const current = () => session === recordForm.session && dialog.open;
|
||||
let refocus = false;
|
||||
recordForm.pending = session;
|
||||
fields.inert = true;
|
||||
$("#rec-status").textContent = "";
|
||||
submitButton.disabled = true;
|
||||
anotherButton.disabled = true;
|
||||
clicked.setAttribute("aria-busy", "true");
|
||||
try {
|
||||
const body = recordBody(form);
|
||||
const result = editing
|
||||
? await api("PUT", base + "/records/" + editing.id, { body, ifMatch: editing.etag })
|
||||
: await api("POST", base + "/records", { body });
|
||||
dialog.close();
|
||||
const keepOpen = another && current();
|
||||
if (keepOpen) {
|
||||
const warnings = result.data && Array.isArray(result.data.warnings) ? result.data.warnings : [];
|
||||
await load();
|
||||
if (current()) {
|
||||
fields.inert = false;
|
||||
$("#rec-status").textContent = ["Record added.", ...warnings.map((warning) => "Warning: " + warning.message)].join(" ");
|
||||
buildTypeFields(form.elements.type.value, {});
|
||||
delete dialog.dataset.dirty;
|
||||
const first = $("#rec-fields").querySelector("input, select, textarea");
|
||||
if (first) first.focus();
|
||||
} else {
|
||||
reportWarnings("Record added.", result.data);
|
||||
}
|
||||
} else {
|
||||
if (current()) dialog.close();
|
||||
reportWarnings(editing ? "Record saved." : "Record added.", result.data);
|
||||
await load();
|
||||
if (!dialog.open) highlight(result.data && result.data.record ? result.data.record.id : null);
|
||||
}
|
||||
} catch (err) {
|
||||
if (!current()) {
|
||||
handleError(err, { reload: load });
|
||||
} else {
|
||||
fields.inert = false;
|
||||
handleError(err, { form, reload: () => { dialog.close(); return load(); } });
|
||||
refocus = true;
|
||||
}
|
||||
} finally {
|
||||
if (recordForm.pending === session) recordForm.pending = 0;
|
||||
if (session === recordForm.session) {
|
||||
submitButton.disabled = false;
|
||||
anotherButton.disabled = false;
|
||||
clicked.removeAttribute("aria-busy");
|
||||
fields.inert = false;
|
||||
if (refocus && dialog.open) (form.querySelector('[aria-invalid="true"]:not(:disabled)') || clicked).focus();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -448,16 +594,30 @@ async function bulkDelete() {
|
||||
}
|
||||
|
||||
function setupTable() {
|
||||
const search = $("#filter-text");
|
||||
search.value = state.text;
|
||||
$("#filter-type").addEventListener("change", (event) => {
|
||||
state.type = event.target.value;
|
||||
state.page = 0;
|
||||
syncUrl();
|
||||
renderRecords();
|
||||
});
|
||||
$("#filter-text").addEventListener("input", (event) => {
|
||||
search.addEventListener("input", (event) => {
|
||||
state.text = event.target.value;
|
||||
state.owner = "";
|
||||
state.page = 0;
|
||||
renderRecords();
|
||||
syncUrl();
|
||||
});
|
||||
for (const th of document.querySelectorAll("#records-table th[data-sort]")) {
|
||||
th.querySelector("button").addEventListener("click", () => {
|
||||
const key = th.dataset.sort;
|
||||
state.sort = { key, dir: state.sort && state.sort.key === key ? -state.sort.dir : 1 };
|
||||
state.page = 0;
|
||||
syncUrl();
|
||||
renderRecords();
|
||||
});
|
||||
}
|
||||
$("#page-prev").addEventListener("click", () => {
|
||||
state.page -= 1;
|
||||
renderRecords();
|
||||
@@ -467,7 +627,7 @@ function setupTable() {
|
||||
renderRecords();
|
||||
});
|
||||
$("#select-all").addEventListener("change", (event) => {
|
||||
for (const record of pageRows()) {
|
||||
for (const record of state.pageRows) {
|
||||
if (record.type === "SOA") continue;
|
||||
if (event.target.checked) state.selected.add(record.id);
|
||||
else state.selected.delete(record.id);
|
||||
@@ -558,6 +718,7 @@ const exportLink = $("#export-link");
|
||||
exportLink.href = "/api/v1" + base + "/export";
|
||||
exportLink.setAttribute("download", "");
|
||||
$("#import-link").href = "/zones/" + encodeURIComponent(zoneParam) + "/import";
|
||||
$("#audit-link").href = "/audit?zone=" + encodeURIComponent(zoneParam) + "&kind=change";
|
||||
setupSoa();
|
||||
setupRecordForm();
|
||||
setupTable();
|
||||
|
||||
+6
-3
@@ -1,10 +1,10 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, api, appData, busy, clear, clearErrors, handleError, h, openDialog, parseNumber, plural, setupDialogs, showFlash, toast, zoneLink } from "./common.js";
|
||||
import { $, api, appData, busy, clear, clearErrors, handleError, h, openDialog, parseNumber, plural, readQuery, setupDialogs, showFlash, toast, writeQuery, zoneLink } from "./common.js";
|
||||
|
||||
const { isAdmin } = appData();
|
||||
const state = { zones: [], counts: null, countsFailed: false, search: "" };
|
||||
const state = { zones: [], counts: null, countsFailed: false, search: readQuery().get("q") || "" };
|
||||
|
||||
function problemCell(zone) {
|
||||
const cell = h("td");
|
||||
@@ -123,9 +123,12 @@ function setupCreate() {
|
||||
|
||||
setupDialogs();
|
||||
showFlash();
|
||||
$("#zone-search").addEventListener("input", (event) => {
|
||||
const search = $("#zone-search");
|
||||
search.value = state.search;
|
||||
search.addEventListener("input", (event) => {
|
||||
state.search = event.target.value;
|
||||
render();
|
||||
writeQuery({ q: state.search.trim() });
|
||||
});
|
||||
if (isAdmin) setupCreate();
|
||||
load();
|
||||
@@ -8,6 +8,7 @@
|
||||
<div class="actions">
|
||||
<a class="btn" id="export-link" href="/">Export zone file</a>
|
||||
<a class="btn" id="import-link" href="/">Import zone file</a>
|
||||
<a class="btn" id="audit-link" href="/audit">Audit history</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -51,7 +52,11 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" class="check"><input type="checkbox" id="select-all" aria-label="Select all records on this page"></th>
|
||||
<th scope="col">Name</th><th scope="col">Type</th><th scope="col">TTL</th><th scope="col">Value</th><th scope="col">Actions</th>
|
||||
<th scope="col" data-sort="name"><button type="button" class="th-sort">Name</button></th>
|
||||
<th scope="col" data-sort="type"><button type="button" class="th-sort">Type</button></th>
|
||||
<th scope="col" data-sort="ttl"><button type="button" class="th-sort">TTL</button></th>
|
||||
<th scope="col" data-sort="value"><button type="button" class="th-sort">Value</button></th>
|
||||
<th scope="col">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="records-body"></tbody>
|
||||
|
||||
@@ -29,7 +29,9 @@
|
||||
<div id="rec-fields"></div>
|
||||
</div>
|
||||
<div class="dialog-actions">
|
||||
<p class="hint dialog-status" id="rec-status" role="status"></p>
|
||||
<button type="button" class="btn" data-close>Cancel</button>
|
||||
<button type="button" class="btn" id="rec-submit-another" hidden>Save and add another</button>
|
||||
<button type="submit" class="btn btn-primary" id="rec-submit">Save record</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
Reference in new issue
Block a user