Naive redirect implementation

This commit is contained in:
CenTdemeern1 2025-01-14 18:25:02 +01:00 committed by git.gay
parent af41b8e10b
commit 6861a549ac
2 changed files with 26 additions and 4 deletions

View file

@ -28,7 +28,9 @@
<br> <br>
<button onclick="showAddInstanceDialog()">Add an instance</button> <button onclick="showAddInstanceDialog()">Add an instance</button>
</div> </div>
<div class="half-width"></div> <div class="half-width">
<button id="redirect">Redirect</button>
</div>
</div> </div>
</dialog> </dialog>
</div> </div>

View file

@ -1,8 +1,10 @@
import { initializeAddInstanceFlow } from "./add_instance_flow.mjs"; import { initializeAddInstanceFlow } from "./add_instance_flow.mjs";
import { findDialogOrFail, findFormOrFail } from "./dom.mjs"; import { findButtonOrFail, findDialogOrFail, findFormOrFail, findInputOrFail } from "./dom.mjs";
import knownSoftware from "./known_software.mjs"; import knownSoftware from "./known_software.mjs";
import storageManager from "./storage_manager.mjs"; import storageManager from "./storage_manager.mjs";
const radioButtonName = "instanceSelect";
export function getMainDialog(): HTMLDialogElement { export function getMainDialog(): HTMLDialogElement {
return document.getElementById('mainDialog') as HTMLDialogElement; return document.getElementById('mainDialog') as HTMLDialogElement;
} }
@ -10,6 +12,7 @@ export function getMainDialog(): 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");
export const { export const {
showAddInstanceDialog, showAddInstanceDialog,
@ -23,8 +26,9 @@ function createInstanceSelectOptions() {
div.setAttribute("x-option", instance.origin); div.setAttribute("x-option", instance.origin);
const radio = document.createElement("input"); const radio = document.createElement("input");
radio.id = instance.origin; radio.id = instance.origin;
radio.value = instance.origin;
radio.type = "radio"; radio.type = "radio";
radio.name = "instanceSelect"; radio.name = radioButtonName;
const label = document.createElement("label"); const label = document.createElement("label");
label.htmlFor = instance.origin; label.htmlFor = instance.origin;
label.innerText = instance.name + " "; label.innerText = instance.name + " ";
@ -44,8 +48,24 @@ function createInstanceSelectOptions() {
instanceSelectForm.appendChild(div); instanceSelectForm.appendChild(div);
} }
const firstInput = instanceSelectForm.querySelector("input"); const firstInput = instanceSelectForm.querySelector("input");
if (firstInput) firstInput.checked = true; if (firstInput) {
firstInput.checked = true;
redirectButton.disabled = false;
} else {
redirectButton.disabled = true;
}
} }
createInstanceSelectOptions(); createInstanceSelectOptions();
storageManager.addSaveCallback(createInstanceSelectOptions); storageManager.addSaveCallback(createInstanceSelectOptions);
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());