FeDirect/static/crossroad.mts

72 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-01-14 15:58:30 +01:00
import { initializeAddInstanceFlow } from "./add_instance_flow.mjs";
2025-01-14 18:25:02 +01:00
import { findButtonOrFail, findDialogOrFail, findFormOrFail, findInputOrFail } from "./dom.mjs";
2025-01-14 17:41:14 +01:00
import knownSoftware from "./known_software.mjs";
import storageManager from "./storage_manager.mjs";
2025-01-14 18:25:02 +01:00
const radioButtonName = "instanceSelect";
export function getMainDialog(): HTMLDialogElement {
return document.getElementById('mainDialog') as HTMLDialogElement;
}
2025-01-14 15:58:30 +01:00
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
const addDialog = findDialogOrFail(document.body, "#addInstance");
2025-01-14 17:41:14 +01:00
const instanceSelectForm = findFormOrFail(document.body, "#instanceSelectForm");
2025-01-14 18:25:02 +01:00
const redirectButton = findButtonOrFail(document.body, "#redirect");
2025-01-14 10:49:44 +01:00
export const {
showAddInstanceDialog,
hideAddInstanceDialog
2025-01-14 15:58:30 +01:00
} = initializeAddInstanceFlow(detailsDialog, addDialog);
2025-01-14 17:41:14 +01:00
function createInstanceSelectOptions() {
instanceSelectForm.replaceChildren(); // Erase all child nodes
for (const instance of storageManager.storage.instances) {
const div = document.createElement("div");
div.setAttribute("x-option", instance.origin);
const radio = document.createElement("input");
radio.id = instance.origin;
2025-01-14 18:25:02 +01:00
radio.value = instance.origin;
2025-01-14 17:41:14 +01:00
radio.type = "radio";
2025-01-14 18:25:02 +01:00
radio.name = radioButtonName;
2025-01-14 17:41:14 +01:00
const label = document.createElement("label");
label.htmlFor = instance.origin;
label.innerText = instance.name + " ";
if (instance.iconURL) {
const img = new Image();
img.src = instance.iconURL;
img.alt = `${instance.name} icon`;
img.className = "inlineIcon";
label.append(img, " ");
}
const small = document.createElement("small");
const softwareName = knownSoftware.software[instance.software].name;
small.innerText = `(${softwareName})`;
label.appendChild(small);
div.appendChild(radio);
div.appendChild(label);
instanceSelectForm.appendChild(div);
}
const firstInput = instanceSelectForm.querySelector("input");
2025-01-14 18:25:02 +01:00
if (firstInput) {
firstInput.checked = true;
redirectButton.disabled = false;
} else {
redirectButton.disabled = true;
}
2025-01-14 17:41:14 +01:00
}
createInstanceSelectOptions();
storageManager.addSaveCallback(createInstanceSelectOptions);
2025-01-14 18:25:02 +01:00
function redirect() {
// Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default
const option = findInputOrFail(instanceSelectForm, `input[name="${radioButtonName}"]:checked`).value;
const url = URL.parse(option)!;
const currentURL = URL.parse(location.href)!;
url.pathname = currentURL.pathname.replace(/\/.*?\//, "/");
location.href = url.toString();
}
redirectButton.addEventListener("click", e => redirect());