Section 31 point 7: build the key fieldsets from key_count

This commit is contained in:
ai-bot committed 2026-10-13 07:42:50 +00:00
1 parent 763613b46c
commit c3ac7c6c2d
1 file changed
+131 -5
+131 -5
View File
@@ -3,9 +3,16 @@
const form = document.querySelector("#setup-form"); const form = document.querySelector("#setup-form");
const saveButton = document.querySelector("#save-setup"); const saveButton = document.querySelector("#save-setup");
const status = document.querySelector("#setup-status"); const status = document.querySelector("#setup-status");
const keyFields = document.querySelector("#key-fields");
const thresholdInput = document.querySelector("#threshold");
const keyCountInput = document.querySelector("#key-count");
// The app always configures exactly three key slots (section 23.2). // The number of key slots the page falls back to when nothing is stored yet,
// and the bounds the field enforces. The stored value decides the real count
// (section 31 point 7).
const KEY_COUNT = 3; const KEY_COUNT = 3;
const MIN_KEY_COUNT = 2;
const MAX_KEY_COUNT = 15;
function slot(number) { function slot(number) {
return { return {
@@ -15,8 +22,109 @@ function slot(number) {
}; };
} }
function currentKeyCount() {
const value = Number.parseInt(keyCountInput.value, 10);
if (Number.isNaN(value)) {
return KEY_COUNT;
}
return Math.min(MAX_KEY_COUNT, Math.max(MIN_KEY_COUNT, value));
}
function buildFieldset(number) {
const fieldset = document.createElement("fieldset");
fieldset.className = "check-card";
const legend = document.createElement("legend");
legend.textContent = `Key ${number}`;
fieldset.append(legend);
const grid = document.createElement("div");
grid.className = "field-grid";
const nameLabel = document.createElement("label");
nameLabel.className = "field";
const nameSpan = document.createElement("span");
nameSpan.textContent = "Name";
const nameInput = document.createElement("input");
nameInput.name = `key_${number}_name`;
nameInput.type = "text";
nameInput.maxLength = 60;
nameInput.autocomplete = "off";
nameInput.required = true;
nameLabel.append(nameSpan, nameInput);
const typeLabel = document.createElement("label");
typeLabel.className = "field";
const typeSpan = document.createElement("span");
typeSpan.textContent = "Type";
const typeSelect = document.createElement("select");
typeSelect.name = `key_${number}_type`;
typeSelect.required = true;
for (const [value, text] of [["", "Select type"], ["hardware", "Hardware"], ["cloud", "Cloud"]]) {
const option = document.createElement("option");
option.value = value;
option.textContent = text;
typeSelect.append(option);
}
typeLabel.append(typeSpan, typeSelect);
grid.append(nameLabel, typeLabel);
const pathLabel = document.createElement("label");
pathLabel.className = "field";
const pathSpan = document.createElement("span");
pathSpan.textContent = "Address path";
const pathInput = document.createElement("input");
pathInput.name = `key_${number}_address_path`;
pathInput.type = "text";
pathInput.maxLength = 200;
pathInput.autocomplete = "off";
pathInput.required = true;
pathLabel.append(pathSpan, pathInput);
fieldset.append(grid, pathLabel);
return fieldset;
}
function rebuildKeyFields() {
// Whatever the user already typed is carried over, so changing the number of
// key slots never throws away a key that is filled in (section 31 point 7).
const existing = {};
for (let number = 1; number <= MAX_KEY_COUNT; number += 1) {
const fields = slot(number);
if (!fields.name) {
continue;
}
existing[number] = {
name: fields.name.value,
type: fields.type.value,
addressPath: fields.addressPath.value,
};
}
const count = currentKeyCount();
const fieldsets = [];
for (let number = 1; number <= count; number += 1) {
fieldsets.push(buildFieldset(number));
}
keyFields.replaceChildren(...fieldsets);
for (const [number, values] of Object.entries(existing)) {
if (Number(number) > count) {
continue;
}
const fields = slot(Number(number));
fields.name.value = values.name;
fields.type.value = values.type;
fields.addressPath.value = values.addressPath;
}
}
function fillSlot(number, key) { function fillSlot(number, key) {
const fields = slot(number); const fields = slot(number);
if (!fields.name) {
return;
}
fields.name.value = key.name; fields.name.value = key.name;
fields.type.value = key.type; fields.type.value = key.type;
fields.addressPath.value = key.address_path || ""; fields.addressPath.value = key.address_path || "";
@@ -32,7 +140,8 @@ function lockForm() {
function collectSetup() { function collectSetup() {
const entries = []; const entries = [];
for (let number = 1; number <= KEY_COUNT; number += 1) { const count = currentKeyCount();
for (let number = 1; number <= count; number += 1) {
const fields = slot(number); const fields = slot(number);
entries.push({ entries.push({
key_number: number, key_number: number,
@@ -41,7 +150,11 @@ function collectSetup() {
address_path: fields.addressPath.value.trim(), address_path: fields.addressPath.value.trim(),
}); });
} }
return { keys: entries }; return {
threshold: Number.parseInt(thresholdInput.value, 10),
key_count: count,
keys: entries,
};
} }
function showLockedState() { function showLockedState() {
@@ -57,18 +170,26 @@ function showLockedState() {
} }
async function loadSetup() { async function loadSetup() {
rebuildKeyFields();
try { try {
const response = await fetch("/api/setup", { headers: { Accept: "application/json" } }); const response = await fetch("/api/setup", { headers: { Accept: "application/json" } });
if (!response.ok) { if (!response.ok) {
throw new Error("The key setup is unavailable."); throw new Error("The key setup is unavailable.");
} }
const setup = await response.json(); const setup = await response.json();
if (typeof setup.threshold === "number") {
thresholdInput.value = String(setup.threshold);
}
if (typeof setup.key_count === "number") {
keyCountInput.value = String(setup.key_count);
}
// The stored key count decides how many fieldsets the page shows.
rebuildKeyFields();
if (!setup.configured) { if (!setup.configured) {
status.textContent = ""; status.textContent = "";
return; return;
} }
// The stored keys arrive in key order, so the slot number is their index. (setup.keys || []).forEach((key) => fillSlot(key.key_number, key));
(setup.keys || []).forEach((key, index) => fillSlot(index + 1, key));
lockForm(); lockForm();
showLockedState(); showLockedState();
} catch { } catch {
@@ -102,5 +223,10 @@ async function saveSetup(event) {
} }
} }
keyCountInput.addEventListener("change", () => {
if (!keyCountInput.disabled) {
rebuildKeyFields();
}
});
form.addEventListener("submit", saveSetup); form.addEventListener("submit", saveSetup);
loadSetup(); loadSetup();