Whether platform checkers may check private addresses, including the host's Docker socket, is now a platform setting; unset, it stays as before: allowed self-hosted, forbidden in the cloud edition. Each of an organization's checkers has its own switch, on by default, settable at creation and toggled in the checker list. Both apply from the checker's next poll. Organizations choose in their settings which of the platform's check types their members are offered; existing monitors keep running, and if the platform later switches off everything an organization kept, it is offered the platform's types again rather than none. Forms marked data-autosave are sent when a field changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
364 lines
14 KiB
JavaScript
364 lines
14 KiB
JavaScript
(() => {
|
|
const meta = document.querySelector('meta[name="csrf-token"]');
|
|
const api = axios.create({ baseURL: "/api/v1", withCredentials: true });
|
|
api.interceptors.request.use((cfg) => {
|
|
if (meta?.content) cfg.headers["X-CSRF-Token"] = meta.content;
|
|
return cfg;
|
|
});
|
|
window.api = api;
|
|
|
|
const message = (err) =>
|
|
err.response?.data?.message ?? document.body.dataset.msgNetwork;
|
|
|
|
const showError = (form, text) => {
|
|
const el = form.querySelector("[data-error]");
|
|
if (!el) return;
|
|
el.textContent = text;
|
|
el.classList.toggle("hidden", !text);
|
|
};
|
|
|
|
// Inputs inside a hidden [data-step] are skipped. Checkboxes become
|
|
// booleans, or arrays of values with [data-multi]; [data-list] inputs are
|
|
// split on commas, [data-number] parsed and empty [data-nullable] sent as null.
|
|
// [data-headers] textareas become objects from "Name: value" lines.
|
|
// Dotted names such as "config.keyword" build nested objects.
|
|
const serialize = (form) => {
|
|
const out = {};
|
|
// Numeric segments build arrays: "config.assertions.0.path".
|
|
const set = (name, value) => {
|
|
const path = name.split(".");
|
|
let obj = out;
|
|
path.slice(0, -1).forEach((key, i) => {
|
|
obj = obj[key] ??= /^\d+$/.test(path[i + 1]) ? [] : {};
|
|
});
|
|
obj[path.at(-1)] = value;
|
|
};
|
|
const get = (name) => name.split(".").reduce((o, k) => o?.[k], out);
|
|
for (const el of form.elements) {
|
|
if (!el.name || el.type === "fieldset" || el.closest("[data-step]")?.hidden) continue;
|
|
// :disabled also covers fields inside a disabled fieldset, such as
|
|
// the settings of other monitor types.
|
|
const disabled = el.matches(":disabled");
|
|
if (el.dataset.multi !== undefined) {
|
|
if (get(el.name) === undefined) set(el.name, []);
|
|
if (el.checked && !disabled) get(el.name).push(el.value);
|
|
continue;
|
|
}
|
|
if (disabled) continue;
|
|
// "<resource_type>:<id>" limits an assignment to one resource.
|
|
if (el.dataset.scope !== undefined) {
|
|
const [type, id] = el.value.split(":");
|
|
if (id) {
|
|
set("resource_type", type);
|
|
set("resource_id", id);
|
|
}
|
|
continue;
|
|
}
|
|
if (el.type === "checkbox") set(el.name, el.checked);
|
|
else if (el.dataset.number !== undefined) set(el.name, Number(el.value));
|
|
else if (el.dataset.nullable !== undefined) set(el.name, el.value || null);
|
|
else if (el.dataset.headers !== undefined)
|
|
set(el.name, Object.fromEntries(
|
|
el.value.split("\n").map((l) => l.split(":")).filter((p) => p.length > 1 && p[0].trim())
|
|
.map(([k, ...v]) => [k.trim(), v.join(":").trim()])));
|
|
else if (el.dataset.list !== undefined)
|
|
set(el.name, el.value.split(",").map((v) => v.trim()).filter(Boolean));
|
|
else set(el.name, el.value);
|
|
}
|
|
// Forms with data-rows="name" send the checked [data-row] elements as an
|
|
// array of objects built from their [data-field] inputs.
|
|
if (form.dataset.rows) {
|
|
out[form.dataset.rows] = [...form.querySelectorAll("[data-row]")]
|
|
.filter((row) => row.querySelector('[data-field="include"]')?.checked)
|
|
.map((row) => {
|
|
const obj = {};
|
|
for (const f of row.querySelectorAll("[data-field]")) {
|
|
if (f.dataset.field !== "include") obj[f.dataset.field] = f.value;
|
|
}
|
|
return obj;
|
|
});
|
|
}
|
|
return out;
|
|
};
|
|
|
|
document.addEventListener("submit", async (ev) => {
|
|
const form = ev.target.closest("form[data-api]");
|
|
if (!form) return;
|
|
ev.preventDefault();
|
|
if (form.dataset.confirm && !confirm(form.dataset.confirm)) return;
|
|
const button = form.querySelector("button[type=submit]");
|
|
const body = serialize(form);
|
|
const totpStep = form.querySelector('[data-step="totp"]');
|
|
const url = totpStep && !totpStep.hidden ? `${form.dataset.api}/totp` : form.dataset.api;
|
|
|
|
showError(form, "");
|
|
button && (button.disabled = true);
|
|
try {
|
|
const res = await api.request({ method: form.dataset.method || "post", url, data: body });
|
|
if (form.dataset.showCodes !== undefined && res.data?.recovery_codes) {
|
|
const out = form.querySelector("[data-recovery-output]");
|
|
const list = out.querySelector("[data-recovery-list]");
|
|
list.replaceChildren(
|
|
...res.data.recovery_codes.map((c) => Object.assign(document.createElement("li"), { textContent: c })),
|
|
);
|
|
for (const el of form.elements) el.disabled = true;
|
|
out.hidden = false;
|
|
return;
|
|
}
|
|
const shown = res.data?.url || res.data?.token || res.data?.signing_secret;
|
|
if (form.dataset.showLink !== undefined && shown) {
|
|
const out = form.querySelector("[data-link-output]");
|
|
const input = out.querySelector("[data-link]");
|
|
input.value = shown;
|
|
out.hidden = false;
|
|
input.select();
|
|
return;
|
|
}
|
|
if (res.data?.status === "totp_required" && totpStep) {
|
|
form.querySelector('[data-step="password"]').hidden = true;
|
|
totpStep.hidden = false;
|
|
totpStep.querySelector("input")?.focus();
|
|
return;
|
|
}
|
|
let next = form.dataset.next || location.href;
|
|
if (form.dataset.nextFrom) next += res.data?.[form.dataset.nextFrom] ?? "";
|
|
location.assign(next);
|
|
} catch (err) {
|
|
showError(form, message(err));
|
|
} finally {
|
|
button && (button.disabled = false);
|
|
}
|
|
});
|
|
|
|
document.addEventListener("click", async (ev) => {
|
|
const locale = ev.target.closest("[data-locale]");
|
|
if (locale) {
|
|
await api.post("/locale", { locale: locale.dataset.locale }).catch(() => {});
|
|
location.reload();
|
|
return;
|
|
}
|
|
const del = ev.target.closest("[data-delete]");
|
|
if (del) {
|
|
if (del.dataset.confirm && !confirm(del.dataset.confirm)) return;
|
|
try {
|
|
await api.delete(del.dataset.delete);
|
|
if (del.dataset.next) location.assign(del.dataset.next);
|
|
else location.reload();
|
|
} catch (err) {
|
|
alert(message(err));
|
|
}
|
|
return;
|
|
}
|
|
const post = ev.target.closest("[data-post]");
|
|
if (post) {
|
|
post.disabled = true;
|
|
try {
|
|
await api.post(post.dataset.post);
|
|
alert(post.dataset.ok);
|
|
if (post.dataset.reload !== undefined) location.reload();
|
|
} catch (err) {
|
|
alert(message(err));
|
|
} finally {
|
|
post.disabled = false;
|
|
}
|
|
return;
|
|
}
|
|
if (ev.target.closest("[data-logout]")) {
|
|
await api.post("/auth/logout").catch(() => {});
|
|
location.assign("/login");
|
|
}
|
|
if (ev.target.closest("[data-totp-start]")) {
|
|
const root = ev.target.closest("[data-totp-setup]");
|
|
try {
|
|
const { data } = await api.post("/me/totp/setup");
|
|
root.querySelector("[data-totp-qr]").src = data.qr;
|
|
root.querySelector("[data-totp-secret]").textContent = data.secret;
|
|
root.querySelector("[data-totp-panel]").hidden = false;
|
|
ev.target.hidden = true;
|
|
} catch (err) {
|
|
alert(message(err));
|
|
}
|
|
}
|
|
});
|
|
|
|
// Only the settings of the selected check type are shown and submitted.
|
|
document.addEventListener("change", (ev) => {
|
|
const sel = ev.target.closest("[data-type-select]");
|
|
if (!sel) return;
|
|
for (const fs of sel.form.querySelectorAll("fieldset[data-type]")) {
|
|
const active = fs.dataset.type === sel.value;
|
|
fs.hidden = !active;
|
|
fs.disabled = !active;
|
|
}
|
|
// [data-hide-for] holds fields a type does without, e.g. heartbeats
|
|
// have neither target nor regions.
|
|
for (const el of sel.form.querySelectorAll("[data-hide-for]")) {
|
|
const hide = el.dataset.hideFor.split(" ").includes(sel.value);
|
|
el.hidden = hide;
|
|
if (el.tagName === "FIELDSET") el.disabled = hide;
|
|
for (const input of el.querySelectorAll("input[required], input[data-required]")) {
|
|
input.required = !hide;
|
|
input.dataset.required = "";
|
|
}
|
|
}
|
|
});
|
|
|
|
// Forms with [data-autosave] are sent as soon as a field changes, such
|
|
// as a single switch in a list.
|
|
document.addEventListener("change", (ev) => {
|
|
const form = ev.target.closest("form[data-autosave]");
|
|
if (form) form.requestSubmit();
|
|
});
|
|
|
|
document.addEventListener("change", async (ev) => {
|
|
const sel = ev.target.closest("[data-switch-org]");
|
|
if (!sel) return;
|
|
await api.post("/me/org", { org_id: sel.value });
|
|
location.reload();
|
|
});
|
|
})();
|
|
|
|
// Time zone fields start with the browser's zone.
|
|
for (const el of document.querySelectorAll("input[data-tz-default]")) {
|
|
if (!el.value) el.value = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
|
}
|
|
|
|
// Markdown editors: toolbar, shortcuts, list continuation, character count
|
|
// and a preview rendered by the server, so it matches what is published.
|
|
(() => {
|
|
// insertText keeps the browser's undo history, setRangeText is the
|
|
// fallback where it is unsupported.
|
|
const replace = (ta, start, end, text, selStart, selEnd) => {
|
|
ta.focus();
|
|
ta.setSelectionRange(start, end);
|
|
if (!document.execCommand("insertText", false, text)) {
|
|
ta.setRangeText(text, start, end, "end");
|
|
}
|
|
ta.setSelectionRange(start + selStart, start + selEnd);
|
|
ta.dispatchEvent(new Event("input", { bubbles: true }));
|
|
};
|
|
|
|
const wrap = (ta, mark, placeholder) => {
|
|
const { selectionStart: s, selectionEnd: e, value } = ta;
|
|
const sel = value.slice(s, e);
|
|
if (value.slice(s - mark.length, s) === mark && value.slice(e, e + mark.length) === mark) {
|
|
replace(ta, s - mark.length, e + mark.length, sel, 0, sel.length);
|
|
return;
|
|
}
|
|
const inner = sel || placeholder;
|
|
replace(ta, s, e, mark + inner + mark, mark.length, mark.length + inner.length);
|
|
};
|
|
|
|
// Prefixes every selected line, or removes the prefix when all have it.
|
|
const prefixLines = (ta, prefixFor, pattern) => {
|
|
const { selectionStart: s, selectionEnd: e, value } = ta;
|
|
const start = value.lastIndexOf("\n", s - 1) + 1;
|
|
const nl = value.indexOf("\n", e);
|
|
const end = nl === -1 ? value.length : nl;
|
|
const lines = value.slice(start, end).split("\n");
|
|
const all = lines.every((l) => pattern.test(l));
|
|
const out = lines.map((l, i) => (all ? l.replace(pattern, "") : prefixFor(i) + l)).join("\n");
|
|
replace(ta, start, end, out, 0, out.length);
|
|
};
|
|
|
|
const block = (ta, text, selFrom, selTo) => {
|
|
const { selectionStart: s, selectionEnd: e, value } = ta;
|
|
const before = s > 0 && value[s - 1] !== "\n" ? "\n\n" : "";
|
|
replace(ta, s, e, before + text, before.length + selFrom, before.length + selTo);
|
|
};
|
|
|
|
const actions = {
|
|
bold: (ta) => wrap(ta, "**", "text"),
|
|
italic: (ta) => wrap(ta, "_", "text"),
|
|
strike: (ta) => wrap(ta, "~~", "text"),
|
|
code: (ta) => wrap(ta, "`", "code"),
|
|
heading: (ta) => prefixLines(ta, () => "## ", /^#{1,6} /),
|
|
ul: (ta) => prefixLines(ta, () => "- ", /^[-*+] /),
|
|
ol: (ta) => prefixLines(ta, (i) => `${i + 1}. `, /^\d+\. /),
|
|
quote: (ta) => prefixLines(ta, () => "> ", /^> ?/),
|
|
link: (ta) => {
|
|
const { selectionStart: s, selectionEnd: e, value } = ta;
|
|
const text = value.slice(s, e) || "text";
|
|
const url = "https://";
|
|
replace(ta, s, e, `[${text}](${url})`, text.length + 3, text.length + 3 + url.length);
|
|
},
|
|
codeblock: (ta) => {
|
|
const sel = ta.value.slice(ta.selectionStart, ta.selectionEnd) || "code";
|
|
block(ta, "```\n" + sel + "\n```\n", 4, 4 + sel.length);
|
|
},
|
|
table: (ta) => block(ta, "| Column | Column |\n| --- | --- |\n| Cell | Cell |\n", 2, 8),
|
|
hr: (ta) => block(ta, "---\n", 4, 4),
|
|
};
|
|
const shortcuts = { b: "bold", i: "italic", k: "link" };
|
|
|
|
// Enter on a list item continues the list; on an empty item it ends it.
|
|
const continueList = (ta, ev) => {
|
|
const { selectionStart: s, selectionEnd: e, value } = ta;
|
|
if (s !== e) return;
|
|
const start = value.lastIndexOf("\n", s - 1) + 1;
|
|
const line = value.slice(start, s);
|
|
const m = line.match(/^(\s*)([-*+]|(\d+)\.|>)( +)(.*)$/);
|
|
if (!m || value.slice(s, value.indexOf("\n", s) === -1 ? value.length : value.indexOf("\n", s)).trim()) return;
|
|
ev.preventDefault();
|
|
if (!m[5].trim()) {
|
|
replace(ta, start, s, "", 0, 0);
|
|
return;
|
|
}
|
|
const marker = m[3] ? `${Number(m[3]) + 1}.` : m[2];
|
|
const next = "\n" + m[1] + marker + m[4];
|
|
replace(ta, s, s, next, next.length, next.length);
|
|
};
|
|
|
|
for (const ed of document.querySelectorAll("[data-md-editor]")) {
|
|
const ta = ed.querySelector("textarea");
|
|
const preview = ed.querySelector("[data-md-preview]");
|
|
const count = ed.querySelector("[data-md-count]");
|
|
const available = new Set([...ed.querySelectorAll("[data-md-action]")].map((b) => b.dataset.mdAction));
|
|
|
|
const updateCount = () => {
|
|
count.textContent = `${ta.value.length.toLocaleString()} / ${Number(ta.maxLength).toLocaleString()}`;
|
|
};
|
|
updateCount();
|
|
ta.addEventListener("input", updateCount);
|
|
|
|
ed.querySelector("[data-md-toolbar]").addEventListener("click", (ev) => {
|
|
const b = ev.target.closest("[data-md-action]");
|
|
if (b && !ta.hidden) actions[b.dataset.mdAction]?.(ta);
|
|
});
|
|
ta.addEventListener("keydown", (ev) => {
|
|
if ((ev.ctrlKey || ev.metaKey) && !ev.altKey && shortcuts[ev.key.toLowerCase()]) {
|
|
const name = shortcuts[ev.key.toLowerCase()];
|
|
if (!available.has(name)) return;
|
|
ev.preventDefault();
|
|
actions[name](ta);
|
|
} else if (ev.key === "Enter" && !ev.shiftKey && !ev.isComposing) {
|
|
continueList(ta, ev);
|
|
}
|
|
});
|
|
|
|
const show = async (tab) => {
|
|
for (const t of ed.querySelectorAll("[data-md-tab]")) t.setAttribute("aria-selected", String(t.dataset.mdTab === tab));
|
|
const previewing = tab === "preview";
|
|
ta.hidden = previewing;
|
|
preview.hidden = !previewing;
|
|
for (const b of ed.querySelectorAll("[data-md-action]")) b.disabled = previewing;
|
|
if (!previewing) {
|
|
ta.focus();
|
|
return;
|
|
}
|
|
if (!ta.value.trim()) {
|
|
preview.textContent = preview.dataset.empty;
|
|
return;
|
|
}
|
|
try {
|
|
const res = await window.api.post("/markdown/preview", { markdown: ta.value });
|
|
// The server renders without raw HTML or unsafe links.
|
|
preview.innerHTML = res.data.html;
|
|
} catch (err) {
|
|
preview.textContent = err.response?.data?.message ?? document.body.dataset.msgNetwork;
|
|
}
|
|
};
|
|
for (const t of ed.querySelectorAll("[data-md-tab]")) t.addEventListener("click", () => show(t.dataset.mdTab));
|
|
}
|
|
})();
|