143 lines
5.5 KiB
TypeScript
143 lines
5.5 KiB
TypeScript
import { initializeAddInstanceFlow } from "./add_instance_flow.mjs";
|
|
import { findButtonOrFail, findDialogOrFail, findFormOrFail, findInputOrFail, findParagraphOrFail, findPreOrFail } from "./dom.mjs";
|
|
import knownSoftware from "./known_software.mjs";
|
|
import storageManager from "./storage_manager.mjs";
|
|
|
|
const radioButtonName = "instanceSelect";
|
|
|
|
const mainDialog = findDialogOrFail(document.body, "#mainDialog");
|
|
const showAddInstanceDialogButton = findButtonOrFail(document.body, "#showAddInstanceDialog");
|
|
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
|
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
|
const instanceSelectForm = findFormOrFail(document.body, "#instanceSelectForm");
|
|
const redirectButton = findButtonOrFail(document.body, "#redirect");
|
|
const redirectAlwaysButton = findButtonOrFail(document.body, "#redirectAlways");
|
|
const pathText = findPreOrFail(document.body, "#path");
|
|
|
|
showAddInstanceDialogButton.addEventListener("click", e => showAddInstanceDialog());
|
|
|
|
redirectButton.addEventListener("click", e => {
|
|
// Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default
|
|
redirect(getSelectedOption()!);
|
|
});
|
|
|
|
redirectAlwaysButton.addEventListener("click", e => {
|
|
// 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 = getSelectedOption()!;
|
|
setAutoRedirect(option);
|
|
redirect(option);
|
|
});
|
|
|
|
let showAddInstanceDialog = () => { };
|
|
let hideAddInstanceDialog = () => { };
|
|
|
|
// Don't bother initializing if we're performing autoredirect
|
|
if (!autoRedirect()) {
|
|
createInstanceSelectOptions();
|
|
storageManager.addSaveCallback(createInstanceSelectOptions);
|
|
updateNoInstanceHint();
|
|
storageManager.addSaveCallback(updateNoInstanceHint);
|
|
|
|
pathText.innerText = getTargetPath();
|
|
|
|
({
|
|
showAddInstanceDialog,
|
|
hideAddInstanceDialog
|
|
} = initializeAddInstanceFlow(detailsDialog, addDialog));
|
|
|
|
mainDialog.show();
|
|
};
|
|
|
|
function updateNoInstanceHint() {
|
|
findParagraphOrFail(document.body, "#no-instance").style.display =
|
|
storageManager.storage.instances.length > 0
|
|
? "none"
|
|
: "";
|
|
}
|
|
|
|
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;
|
|
radio.value = instance.origin;
|
|
radio.type = "radio";
|
|
radio.name = radioButtonName;
|
|
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 medium-height";
|
|
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");
|
|
if (firstInput) firstInput.checked = true;
|
|
setRedirectButtonState(firstInput !== null);
|
|
}
|
|
|
|
function setRedirectButtonState(enabled: boolean) {
|
|
redirectButton.disabled = !enabled;
|
|
redirectAlwaysButton.disabled = !enabled;
|
|
}
|
|
|
|
function getTargetSoftwareOrGroup(): string {
|
|
const currentURL = URL.parse(location.href)!;
|
|
const target = currentURL.pathname.match(/\/+([^\/]*)\/?/)?.[1];
|
|
if (target == null) throw new Error("Crossroad was served on an invalid path (likely a backend routing mistake)");
|
|
const softwareName = Object.entries(knownSoftware.software).find(([name, software]) => software.aliases.includes(target))?.[0];
|
|
if (softwareName) return softwareName;
|
|
const groupName = Object.entries(knownSoftware.groups).find(([name, group]) => group.aliases.includes(target))?.[0];
|
|
if (groupName) return groupName;
|
|
throw new Error("Could not identify target software or group");
|
|
}
|
|
|
|
function getTargetPath(): string {
|
|
const currentURL = URL.parse(location.href)!;
|
|
return currentURL.pathname.replace(/\/+[^\/]*\/?/, "/");
|
|
}
|
|
|
|
function getSelectedOption(): string | null {
|
|
try {
|
|
return findInputOrFail(instanceSelectForm, `input[name="${radioButtonName}"]:checked`).value;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function autoRedirect(): boolean {
|
|
const targetSoftware = getTargetSoftwareOrGroup();
|
|
const preferredFor = storageManager.storage.instances.find(instance => instance.preferredFor?.includes(targetSoftware));
|
|
if (preferredFor) {
|
|
redirect(preferredFor.origin);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function setAutoRedirect(option: string) {
|
|
const instance = storageManager.storage.instances.find(e => e.origin === option);
|
|
if (!instance) throw new Error("Invalid argument");
|
|
instance.preferredFor ??= [];
|
|
instance.preferredFor.push(getTargetSoftwareOrGroup());
|
|
storageManager.save();
|
|
}
|
|
|
|
function redirect(to: string) {
|
|
const url = URL.parse(to);
|
|
if (url === null) throw new Error("Couldn't parse destination");
|
|
url.pathname = getTargetPath();
|
|
location.href = url.toString();
|
|
}
|