2025-01-13 06:59:58 +01:00
|
|
|
// This file handles the "Add an instance" dialog
|
|
|
|
|
2025-01-14 14:52:31 +01:00
|
|
|
import { findButtonOrFail, findFormOrFail, findInputOrFail } from "./dom.mjs";
|
|
|
|
|
2025-01-13 06:59:58 +01:00
|
|
|
export function parseHost(host: string): { host: string, secure: boolean } | null {
|
|
|
|
let parsedInstance = URL.parse(host);
|
|
|
|
parsedInstance ??= URL.parse("https://" + host);
|
|
|
|
if (!parsedInstance?.host) return null;
|
2025-01-14 13:10:29 +01:00
|
|
|
if (!/https?:/.test(parsedInstance.protocol)) return null;
|
2025-01-13 06:59:58 +01:00
|
|
|
return {
|
|
|
|
host: parsedInstance.host,
|
|
|
|
secure: parsedInstance.protocol === "https:"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2025-01-14 10:49:44 +01:00
|
|
|
export function initializeAddInstanceDialog(
|
|
|
|
dialog: HTMLDialogElement,
|
|
|
|
callback: (
|
|
|
|
host: string,
|
|
|
|
secure: boolean,
|
|
|
|
autoQueryMetadata: boolean,
|
|
|
|
) => void
|
|
|
|
): {
|
2025-01-13 06:59:58 +01:00
|
|
|
showAddInstanceDialog: () => void,
|
|
|
|
hideAddInstanceDialog: () => void,
|
|
|
|
} {
|
|
|
|
const showAddInstanceDialog = () => dialog.showModal();
|
|
|
|
const hideAddInstanceDialog = () => dialog.close();
|
|
|
|
|
2025-01-14 14:52:31 +01:00
|
|
|
const form = findFormOrFail(dialog, ".addInstanceForm");
|
|
|
|
const instanceHost = findInputOrFail(form, "#instanceHost");
|
|
|
|
const autoQueryMetadata = findInputOrFail(form, "#autoQueryMetadata");
|
|
|
|
const closeButton = findButtonOrFail(form, ".close");
|
2025-01-13 06:59:58 +01:00
|
|
|
|
|
|
|
instanceHost.addEventListener("input", e => {
|
|
|
|
if (parseHost(instanceHost.value) === null)
|
|
|
|
instanceHost.setCustomValidity("Invalid instance hostname or URL");
|
|
|
|
else
|
|
|
|
instanceHost.setCustomValidity("");
|
|
|
|
});
|
|
|
|
|
2025-01-14 10:49:44 +01:00
|
|
|
form.addEventListener("submit", e => {
|
2025-01-13 06:59:58 +01:00
|
|
|
// A sane browser doesn't allow for submitting the form if the above validation fails
|
|
|
|
const { host, secure } = parseHost(instanceHost.value)!;
|
2025-01-14 10:49:44 +01:00
|
|
|
callback(host, secure, autoQueryMetadata.checked);
|
2025-01-13 06:59:58 +01:00
|
|
|
form.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
closeButton.addEventListener("click", e => hideAddInstanceDialog());
|
|
|
|
|
|
|
|
return {
|
|
|
|
showAddInstanceDialog,
|
|
|
|
hideAddInstanceDialog
|
|
|
|
};
|
|
|
|
}
|