Clean up a bit
This commit is contained in:
parent
e66b399961
commit
3a09f6c1f0
1 changed files with 34 additions and 25 deletions
|
@ -5,20 +5,42 @@ import storageManager from "./storage_manager.mjs";
|
||||||
|
|
||||||
const radioButtonName = "instanceSelect";
|
const radioButtonName = "instanceSelect";
|
||||||
|
|
||||||
export function getMainDialog(): HTMLDialogElement {
|
|
||||||
return document.getElementById('mainDialog') as HTMLDialogElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
||||||
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
||||||
const instanceSelectForm = findFormOrFail(document.body, "#instanceSelectForm");
|
const instanceSelectForm = findFormOrFail(document.body, "#instanceSelectForm");
|
||||||
const redirectButton = findButtonOrFail(document.body, "#redirect");
|
const redirectButton = findButtonOrFail(document.body, "#redirect");
|
||||||
const redirectAlwaysButton = findButtonOrFail(document.body, "#redirectAlways");
|
const redirectAlwaysButton = findButtonOrFail(document.body, "#redirectAlways");
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
export const getMainDialog = () => findDialogOrFail(document.body, "#mainDialog");
|
||||||
|
|
||||||
export const {
|
export const {
|
||||||
showAddInstanceDialog,
|
showAddInstanceDialog,
|
||||||
hideAddInstanceDialog
|
hideAddInstanceDialog
|
||||||
} = initializeAddInstanceFlow(detailsDialog, addDialog);
|
} = ((): {
|
||||||
|
showAddInstanceDialog: () => void,
|
||||||
|
hideAddInstanceDialog: () => void
|
||||||
|
} => {
|
||||||
|
// Don't bother initializing if we're performing autoredirect
|
||||||
|
if (autoRedirect()) return {
|
||||||
|
showAddInstanceDialog: () => { },
|
||||||
|
hideAddInstanceDialog: () => { }
|
||||||
|
}
|
||||||
|
createInstanceSelectOptions();
|
||||||
|
storageManager.addSaveCallback(createInstanceSelectOptions);
|
||||||
|
return initializeAddInstanceFlow(detailsDialog, addDialog)
|
||||||
|
})();
|
||||||
|
|
||||||
function createInstanceSelectOptions() {
|
function createInstanceSelectOptions() {
|
||||||
instanceSelectForm.replaceChildren(); // Erase all child nodes
|
instanceSelectForm.replaceChildren(); // Erase all child nodes
|
||||||
|
@ -53,15 +75,12 @@ function createInstanceSelectOptions() {
|
||||||
setRedirectButtonState(firstInput !== null);
|
setRedirectButtonState(firstInput !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
createInstanceSelectOptions();
|
|
||||||
storageManager.addSaveCallback(createInstanceSelectOptions);
|
|
||||||
|
|
||||||
function setRedirectButtonState(enabled: boolean) {
|
function setRedirectButtonState(enabled: boolean) {
|
||||||
redirectButton.disabled = !enabled;
|
redirectButton.disabled = !enabled;
|
||||||
redirectAlwaysButton.disabled = !enabled;
|
redirectAlwaysButton.disabled = !enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTargetSoftwareOrGroup(): string {
|
function getTargetSoftwareOrGroup(): string {
|
||||||
const currentURL = URL.parse(location.href)!;
|
const currentURL = URL.parse(location.href)!;
|
||||||
const target = currentURL.pathname.match(/\/+([^\/]*)\/?/)?.[1];
|
const target = currentURL.pathname.match(/\/+([^\/]*)\/?/)?.[1];
|
||||||
if (target == null) throw new Error("Crossroad was served on an invalid path (likely a backend routing mistake)");
|
if (target == null) throw new Error("Crossroad was served on an invalid path (likely a backend routing mistake)");
|
||||||
|
@ -85,14 +104,16 @@ function getSelectedOption(): string | null {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function autoRedirect() {
|
function autoRedirect(): boolean {
|
||||||
const targetSoftware = getTargetSoftwareOrGroup();
|
const targetSoftware = getTargetSoftwareOrGroup();
|
||||||
const preferredFor = storageManager.storage.instances.find(instance => instance.preferredFor?.includes(targetSoftware));
|
const preferredFor = storageManager.storage.instances.find(instance => instance.preferredFor?.includes(targetSoftware));
|
||||||
if (preferredFor) redirect(preferredFor.origin);
|
if (preferredFor) {
|
||||||
|
redirect(preferredFor.origin);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
autoRedirect();
|
|
||||||
|
|
||||||
function setAutoRedirect(option: string) {
|
function setAutoRedirect(option: string) {
|
||||||
const instance = storageManager.storage.instances.find(e => e.origin === option);
|
const instance = storageManager.storage.instances.find(e => e.origin === option);
|
||||||
if (!instance) throw new Error("Invalid argument");
|
if (!instance) throw new Error("Invalid argument");
|
||||||
|
@ -101,21 +122,9 @@ function setAutoRedirect(option: string) {
|
||||||
storageManager.save();
|
storageManager.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
function redirect(to: string) {
|
function redirect(to: string) {
|
||||||
const url = URL.parse(to);
|
const url = URL.parse(to);
|
||||||
if (url === null) throw new Error("Couldn't parse destination");
|
if (url === null) throw new Error("Couldn't parse destination");
|
||||||
url.pathname = getTargetPath();
|
url.pathname = getTargetPath();
|
||||||
location.href = url.toString();
|
location.href = url.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
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()!);
|
|
||||||
});
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue