Compare commits
46 commits
no-more-pr
...
main
Author | SHA1 | Date | |
---|---|---|---|
b408f66f9d | |||
c1f61cfe9b | |||
becaf79690 | |||
68f54b341b | |||
5e3817c1a7 | |||
f0617522e3 | |||
7a39fbd418 | |||
8850e08f5d | |||
9a4e2b3ca5 | |||
9f5e6115e9 | |||
22bbbf0955 | |||
563462c1c5 | |||
cfae51c43f | |||
beaa76f996 | |||
3244cecc6a | |||
b4c70c0d16 | |||
079b91a6c1 | |||
aa3e1ab526 | |||
3fad4f8d6e | |||
93f8ba4226 | |||
764529f36b | |||
b2cbd4cc5d | |||
7aa1b483e1 | |||
72faa8901c | |||
da6d60f94c | |||
3671085acc | |||
bc8d0a3f92 | |||
93e1ac85cd | |||
c3b5666bd5 | |||
0ba211c054 | |||
b3c049a8fa | |||
5dde457d45 | |||
532cd614ce | |||
be021c4b16 | |||
26a48f23a5 | |||
30b28b8aba | |||
28bbdac90a | |||
940fc92856 | |||
93c9e5154f | |||
516473edeb | |||
7e1416a721 | |||
f451b1fbc3 | |||
bfd61c2e50 | |||
2be0658ed9 | |||
5534bc3942 | |||
3f9624fe91 |
18 changed files with 630 additions and 249 deletions
24
.forgejo/workflows/ci.yaml
Normal file
24
.forgejo/workflows/ci.yaml
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
name: Build & Test
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-run:
|
||||||
|
runs-on: docker
|
||||||
|
container:
|
||||||
|
image: rust
|
||||||
|
steps:
|
||||||
|
- name: Update package repos
|
||||||
|
run: apt update
|
||||||
|
- name: Install Node using apt
|
||||||
|
run: apt install nodejs -y
|
||||||
|
- name: Checkout repo
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Setup Deno
|
||||||
|
uses: https://github.com/denoland/setup-deno@v2
|
||||||
|
with:
|
||||||
|
deno-version: v2.x
|
||||||
|
- name: Build using Cargo
|
||||||
|
run: cargo build --verbose
|
||||||
|
- name: Run unit tests
|
||||||
|
run: cargo test --verbose
|
|
@ -136,7 +136,8 @@
|
||||||
],
|
],
|
||||||
"groups": [
|
"groups": [
|
||||||
"misskey-compliant",
|
"misskey-compliant",
|
||||||
"misskey-v13"
|
"misskey-v13",
|
||||||
|
"mastodon-compliant-api"
|
||||||
],
|
],
|
||||||
"forkOf": "misskey"
|
"forkOf": "misskey"
|
||||||
},
|
},
|
||||||
|
|
|
@ -98,3 +98,17 @@ impl<'r> FromParam<'r> for KnownInstanceSoftware<'r> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// If this test fails, known-software.json is invalid
|
||||||
|
#[test]
|
||||||
|
fn known_software_is_valid() {
|
||||||
|
assert!(!KNOWN_SOFTWARE.groups.is_empty());
|
||||||
|
assert!(!KNOWN_SOFTWARE.software.is_empty());
|
||||||
|
assert!(!KNOWN_SOFTWARE_NAMES.is_empty());
|
||||||
|
assert!(!KNOWN_SOFTWARE_NODEINFO_NAMES.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// This file handles the "Add an instance" dialog
|
// This file handles the "Add an instance" dialog
|
||||||
|
|
||||||
|
import { FormDialog, ONCE } from "./dialog.mjs";
|
||||||
import { findButtonOrFail, findFormOrFail, findInputOrFail } from "./dom.mjs";
|
import { findButtonOrFail, findFormOrFail, findInputOrFail } from "./dom.mjs";
|
||||||
|
|
||||||
export function parseHost(host: string): { host: string, secure: boolean } | null {
|
export function parseHost(host: string): { host: string, secure: boolean } | null {
|
||||||
|
@ -13,43 +14,67 @@ export function parseHost(host: string): { host: string, secure: boolean } | nul
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initializeAddInstanceDialog(
|
export type AddInstanceDialogData = {
|
||||||
dialog: HTMLDialogElement,
|
|
||||||
callback: (
|
|
||||||
host: string,
|
host: string,
|
||||||
secure: boolean,
|
secure: boolean,
|
||||||
autoQueryMetadata: boolean,
|
autoQueryMetadata: boolean,
|
||||||
) => void
|
};
|
||||||
): {
|
|
||||||
showAddInstanceDialog: () => void,
|
|
||||||
hideAddInstanceDialog: () => void,
|
|
||||||
} {
|
|
||||||
const showAddInstanceDialog = () => dialog.showModal();
|
|
||||||
const hideAddInstanceDialog = () => dialog.close();
|
|
||||||
|
|
||||||
const form = findFormOrFail(dialog, ".addInstanceForm");
|
export class AddInstanceDialog extends FormDialog {
|
||||||
const instanceHost = findInputOrFail(form, "#instanceHost");
|
protected instanceHost: HTMLInputElement;
|
||||||
const autoQueryMetadata = findInputOrFail(form, "#autoQueryMetadata");
|
protected autoQueryMetadata: HTMLInputElement;
|
||||||
const closeButton = findButtonOrFail(form, ".close");
|
protected closeButton: HTMLButtonElement;
|
||||||
|
|
||||||
instanceHost.addEventListener("input", e => {
|
constructor(dialog: HTMLDialogElement, initializeDOM: boolean = true) {
|
||||||
if (parseHost(instanceHost.value) === null)
|
super(dialog, findFormOrFail(dialog, ".addInstanceForm"));
|
||||||
instanceHost.setCustomValidity("Invalid instance hostname or URL");
|
|
||||||
else
|
|
||||||
instanceHost.setCustomValidity("");
|
|
||||||
});
|
|
||||||
|
|
||||||
form.addEventListener("submit", e => {
|
this.instanceHost = findInputOrFail(this.form, "#instanceHost");
|
||||||
// A sane browser doesn't allow for submitting the form if the above validation fails
|
this.autoQueryMetadata = findInputOrFail(this.form, "#autoQueryMetadata");
|
||||||
const { host, secure } = parseHost(instanceHost.value)!;
|
this.closeButton = findButtonOrFail(this.form, ".close");
|
||||||
callback(host, secure, autoQueryMetadata.checked);
|
|
||||||
form.reset();
|
|
||||||
});
|
|
||||||
|
|
||||||
closeButton.addEventListener("click", e => hideAddInstanceDialog());
|
if (initializeDOM) this.initializeDOM();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override initializeDOM() {
|
||||||
|
super.initializeDOM();
|
||||||
|
|
||||||
|
this.instanceHost.addEventListener("input", e => this.#getDataIfValid());
|
||||||
|
this.closeButton.addEventListener("click", e => this.close());
|
||||||
|
}
|
||||||
|
|
||||||
|
#getDataIfValid(): AddInstanceDialogData | null {
|
||||||
|
const parsedHost = parseHost(this.instanceHost.value);
|
||||||
|
if (parsedHost === null) {
|
||||||
|
this.instanceHost.setCustomValidity("Invalid instance hostname or URL");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
this.instanceHost.setCustomValidity("");
|
||||||
return {
|
return {
|
||||||
showAddInstanceDialog,
|
host: parsedHost.host,
|
||||||
hideAddInstanceDialog
|
secure: parsedHost.secure,
|
||||||
|
autoQueryMetadata: this.autoQueryMetadata.checked
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#handleSubmit(resolve: (data: AddInstanceDialogData) => void) {
|
||||||
|
this.form.addEventListener("submit", e => {
|
||||||
|
const data = this.#getDataIfValid();
|
||||||
|
if (data === null) {
|
||||||
|
// Prevent the user from submitting the form if it's invalid and let them try again
|
||||||
|
e.preventDefault();
|
||||||
|
this.#handleSubmit(resolve);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve(data);
|
||||||
|
this.close();
|
||||||
|
}, ONCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
async present(): Promise<AddInstanceDialogData> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.cancelOnceClosed(reject);
|
||||||
|
this.#handleSubmit(resolve);
|
||||||
|
this.open();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,69 +1,73 @@
|
||||||
import { initializeAddInstanceDialog } from "./add_an_instance.mjs";
|
import { AddInstanceDialog } from "./add_an_instance.mjs";
|
||||||
import { initializeInstanceDetailsDialog } from "./confirm_instance_details.mjs";
|
import { dialogDetailsToInstance, InstanceDetailsDialog, InstanceDetailsDialogData } from "./confirm_instance_details.mjs";
|
||||||
|
import { Dialog } from "./dialog.mjs";
|
||||||
import storageManager, { Instance } from "./storage_manager.mjs";
|
import storageManager, { Instance } from "./storage_manager.mjs";
|
||||||
|
|
||||||
export function initializeAddInstanceFlow(
|
export class AddInstanceFlow {
|
||||||
detailsDialog: HTMLDialogElement,
|
addDialog: AddInstanceDialog;
|
||||||
addDialog: HTMLDialogElement
|
spinnerDialog: Dialog;
|
||||||
): {
|
detailsDialog: InstanceDetailsDialog;
|
||||||
showAddInstanceDialog: () => void,
|
|
||||||
hideAddInstanceDialog: () => void
|
|
||||||
} {
|
|
||||||
const instanceDetailsDialogCallback = (
|
|
||||||
name: string,
|
|
||||||
host: string,
|
|
||||||
hostSecure: boolean,
|
|
||||||
software: string,
|
|
||||||
icon: string | null
|
|
||||||
) => {
|
|
||||||
const instance: Instance = {
|
|
||||||
name,
|
|
||||||
origin: `http${hostSecure ? "s" : ""}://${host}`,
|
|
||||||
software,
|
|
||||||
iconURL: icon ?? undefined
|
|
||||||
};
|
|
||||||
storageManager.storage.instances.push(instance);
|
|
||||||
storageManager.save();
|
|
||||||
console.log("Successfully added new instance:", instance);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
addDialog: AddInstanceDialog | HTMLDialogElement,
|
||||||
|
spinnerDialog: HTMLDialogElement,
|
||||||
|
detailsDialog: InstanceDetailsDialog | HTMLDialogElement,
|
||||||
|
) {
|
||||||
|
if (addDialog instanceof AddInstanceDialog)
|
||||||
|
this.addDialog = addDialog;
|
||||||
|
else
|
||||||
|
this.addDialog = new AddInstanceDialog(addDialog, true);
|
||||||
|
|
||||||
|
this.spinnerDialog = new Dialog(spinnerDialog);
|
||||||
|
|
||||||
|
if (detailsDialog instanceof InstanceDetailsDialog)
|
||||||
|
this.detailsDialog = detailsDialog;
|
||||||
|
else
|
||||||
|
this.detailsDialog = new InstanceDetailsDialog(detailsDialog, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async start(autoSave: boolean) {
|
||||||
const {
|
const {
|
||||||
showInstanceDetailsDialog,
|
autoQueryMetadata,
|
||||||
hideInstanceDetailsDialog,
|
host,
|
||||||
populateInstanceDetailsDialog
|
secure,
|
||||||
} = initializeInstanceDetailsDialog(detailsDialog, instanceDetailsDialogCallback);
|
} = await this.addDialog.present();
|
||||||
|
|
||||||
|
const detailsDialogData: InstanceDetailsDialogData = {
|
||||||
|
name: host,
|
||||||
|
host,
|
||||||
|
hostSecure: secure,
|
||||||
|
software: "",
|
||||||
|
iconURL: null,
|
||||||
|
preferredFor: []
|
||||||
|
};
|
||||||
|
|
||||||
const addInstanceDialogCallback = async (
|
|
||||||
host: string,
|
|
||||||
secure: boolean,
|
|
||||||
autoQueryMetadata: boolean,
|
|
||||||
) => {
|
|
||||||
try {
|
try {
|
||||||
if (!autoQueryMetadata) throw new Error("Don't");
|
if (!autoQueryMetadata) throw null; // Skip to catch block
|
||||||
|
|
||||||
|
this.spinnerDialog.open();
|
||||||
|
|
||||||
const { name, software, iconURL } =
|
const { name, software, iconURL } =
|
||||||
await fetch(`/api/instance_info/${secure}/${encodeURIComponent(host)}`)
|
await fetch(`/api/instance_info/${secure}/${encodeURIComponent(host)}`)
|
||||||
.then(r => r.json());
|
.then(r => r.json());
|
||||||
if (
|
if (
|
||||||
typeof name !== "string"
|
typeof name !== "string"
|
||||||
|| typeof software !== "string"
|
|| typeof software !== "string"
|
||||||
|| !(typeof iconURL === "string" || iconURL === null)
|
|| !(typeof iconURL === "string" || iconURL === null) // I guess TS is too stupid to understand this?
|
||||||
)
|
)
|
||||||
throw new Error("Invalid API response");
|
throw new Error("Invalid API response");
|
||||||
populateInstanceDetailsDialog(name, host, secure, software, iconURL as string | null);
|
|
||||||
} catch {
|
|
||||||
populateInstanceDetailsDialog(host, host, secure, "", null);
|
|
||||||
} finally {
|
|
||||||
showInstanceDetailsDialog();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const {
|
detailsDialogData.name = name;
|
||||||
showAddInstanceDialog,
|
detailsDialogData.software = software;
|
||||||
hideAddInstanceDialog
|
detailsDialogData.iconURL = iconURL as string | null;
|
||||||
} = initializeAddInstanceDialog(addDialog, addInstanceDialogCallback);
|
} catch { }
|
||||||
|
this.spinnerDialog.close();
|
||||||
|
|
||||||
return {
|
const finalData = await this.detailsDialog.present(detailsDialogData);
|
||||||
showAddInstanceDialog,
|
const instance = dialogDetailsToInstance(finalData, {});
|
||||||
hideAddInstanceDialog
|
|
||||||
};
|
storageManager.storage.instances.push(instance);
|
||||||
|
if (autoSave) storageManager.save();
|
||||||
|
console.log("Successfully added new instance:", instance);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,11 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>FeDirect</title>
|
<title>FeDirect</title>
|
||||||
<link rel="stylesheet" href="/static/main.css">
|
<link rel="stylesheet" href="/static/main.css">
|
||||||
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src *;">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<script type="module">
|
<script type="module" src="/static/config.mjs"></script>
|
||||||
Object.assign(globalThis, await import("/static/config.mjs"));
|
|
||||||
getMainDialog().show(); // Don't show until the page is ready
|
|
||||||
</script>
|
|
||||||
<div class="flex-vcenter">
|
<div class="flex-vcenter">
|
||||||
<dialog id="mainDialog" class="half-width half-height">
|
<dialog id="mainDialog" class="half-width half-height">
|
||||||
<header class="separator-bottom margin-large-bottom">
|
<header class="separator-bottom margin-large-bottom">
|
||||||
|
@ -27,7 +25,7 @@
|
||||||
<center class="half-width">
|
<center class="half-width">
|
||||||
<ol id="instanceList" class="align-start wfit-content"></ol>
|
<ol id="instanceList" class="align-start wfit-content"></ol>
|
||||||
<br>
|
<br>
|
||||||
<button onclick="showAddInstanceDialog()">Add an instance</button>
|
<button id="startAddInstanceFlow">Add an instance</button>
|
||||||
</center>
|
</center>
|
||||||
</div>
|
</div>
|
||||||
<div class="half-width align-self-start">
|
<div class="half-width align-self-start">
|
||||||
|
@ -52,9 +50,8 @@
|
||||||
<br>
|
<br>
|
||||||
<input id="autoQueryMetadata" type="checkbox" name="autoQueryMetadata" checked />
|
<input id="autoQueryMetadata" type="checkbox" name="autoQueryMetadata" checked />
|
||||||
<label for="autoQueryMetadata">
|
<label for="autoQueryMetadata">
|
||||||
<abbr title="This uses FeDirect's API to automatically try to detect instance metadata, such as the name, software, and an icon.
|
<abbr
|
||||||
We do this on the backend to avoid CORS problems.
|
title="This uses FeDirect's API to automatically try to detect instance metadata, such as the name, software, and an icon.">
|
||||||
We do not track or save any requests or data.">
|
|
||||||
Automatically query metadata
|
Automatically query metadata
|
||||||
</abbr>
|
</abbr>
|
||||||
</label>
|
</label>
|
||||||
|
@ -104,14 +101,15 @@ Unchecking this is not recommended, and this option only exists for exceptional
|
||||||
<br>
|
<br>
|
||||||
<label for="defaultsList">Default option for:</label><br>
|
<label for="defaultsList">Default option for:</label><br>
|
||||||
<select id="defaultsList" class="full-width" multiple>
|
<select id="defaultsList" class="full-width" multiple>
|
||||||
<option value="" disabled>(None, use the "Redirect always" button to set!)</option>
|
<option id="noDefaults" value="" disabled>(None, use the "Redirect always" button to set!)</option>
|
||||||
</select>
|
</select>
|
||||||
<button id="removeDefaults" disabled>Remove</button>
|
<button id="removeDefaults" type="button" disabled>Remove</button>
|
||||||
<br><br>
|
<br><br>
|
||||||
<button type="submit">OK</button>
|
<button type="submit">OK</button>
|
||||||
<button type="reset" class="close">Cancel</button>
|
<button type="reset" class="close">Cancel</button>
|
||||||
</form>
|
</form>
|
||||||
</dialog>
|
</dialog>
|
||||||
|
<dialog id="spinner"><span class="spinner"></span></dialog>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
|
@ -1,23 +1,35 @@
|
||||||
import { parseHost } from "./add_an_instance.mjs";
|
import { AddInstanceFlow } from "./add_instance_flow.mjs";
|
||||||
import { initializeAddInstanceFlow } from "./add_instance_flow.mjs";
|
import { dialogDetailsFromInstance, dialogDetailsToInstance, InstanceDetailsDialog } from "./confirm_instance_details.mjs";
|
||||||
import { initializeInstanceDetailsDialog } from "./confirm_instance_details.mjs";
|
|
||||||
import { findButtonOrFail, findDialogOrFail, findOlOrFail } from "./dom.mjs";
|
import { findButtonOrFail, findDialogOrFail, findOlOrFail } from "./dom.mjs";
|
||||||
import storageManager from "./storage_manager.mjs";
|
import storageManager, { Instance } from "./storage_manager.mjs";
|
||||||
|
|
||||||
let reordering = false;
|
let reordering = false;
|
||||||
|
let unsaved = false;
|
||||||
// Dragging code is a heavily modified version of https://stackoverflow.com/a/28962290
|
// Dragging code is a heavily modified version of https://stackoverflow.com/a/28962290
|
||||||
let elementBeingDragged: HTMLLIElement | undefined;
|
let elementBeingDragged: HTMLLIElement | undefined;
|
||||||
|
|
||||||
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
const mainDialog = findDialogOrFail(document.body, "#mainDialog");
|
||||||
|
const startAddInstanceFlowButton = findButtonOrFail(document.body, "#startAddInstanceFlow");
|
||||||
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
||||||
|
const spinnerDialog = findDialogOrFail(document.body, "#spinner");
|
||||||
|
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
||||||
const instanceList = findOlOrFail(document.body, "#instanceList");
|
const instanceList = findOlOrFail(document.body, "#instanceList");
|
||||||
const saveButton = findButtonOrFail(document.body, "#save");
|
const saveButton = findButtonOrFail(document.body, "#save");
|
||||||
const reorderButton = findButtonOrFail(document.body, "#reorder");
|
const reorderButton = findButtonOrFail(document.body, "#reorder");
|
||||||
|
const resetButton = findButtonOrFail(document.body, "#reset");
|
||||||
|
|
||||||
saveButton.addEventListener("click", e => {
|
let instanceDetailsDialog = new InstanceDetailsDialog(detailsDialog, true);
|
||||||
storageManager.save();
|
let addInstanceFlow = new AddInstanceFlow(addDialog, spinnerDialog, instanceDetailsDialog);
|
||||||
|
|
||||||
|
startAddInstanceFlowButton.addEventListener("click", e => {
|
||||||
|
addInstanceFlow.start(false).then(_ => {
|
||||||
|
updateInstanceList();
|
||||||
|
unsavedChanges();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
saveButton.addEventListener("click", e => saveChanges());
|
||||||
|
|
||||||
reorderButton.addEventListener("click", () => {
|
reorderButton.addEventListener("click", () => {
|
||||||
reordering = !reordering;
|
reordering = !reordering;
|
||||||
if (!reordering) applyReordering();
|
if (!reordering) applyReordering();
|
||||||
|
@ -25,22 +37,47 @@ reorderButton.addEventListener("click", () => {
|
||||||
reorderButton.innerText = reordering ? "Finish reordering" : "Reorder";
|
reorderButton.innerText = reordering ? "Finish reordering" : "Reorder";
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getMainDialog = () => findDialogOrFail(document.body, "#mainDialog");
|
resetButton.addEventListener("click", e => {
|
||||||
|
storageManager.reset();
|
||||||
const {
|
updateInstanceList();
|
||||||
showInstanceDetailsDialog,
|
unsavedChanges();
|
||||||
hideInstanceDetailsDialog,
|
});
|
||||||
populateInstanceDetailsDialog,
|
|
||||||
} = initializeInstanceDetailsDialog(detailsDialog, () => { });
|
|
||||||
|
|
||||||
export const {
|
|
||||||
showAddInstanceDialog,
|
|
||||||
hideAddInstanceDialog
|
|
||||||
} = initializeAddInstanceFlow(detailsDialog, addDialog);
|
|
||||||
|
|
||||||
updateInstanceList();
|
updateInstanceList();
|
||||||
storageManager.addSaveCallback(updateInstanceList);
|
storageManager.addSaveCallback(updateInstanceList);
|
||||||
|
|
||||||
|
mainDialog.show();
|
||||||
|
|
||||||
|
function saveChanges() {
|
||||||
|
storageManager.save();
|
||||||
|
unsaved = false;
|
||||||
|
saveButton.classList.remove("pulse-red");
|
||||||
|
}
|
||||||
|
|
||||||
|
function unsavedChanges() {
|
||||||
|
if (!unsaved) {
|
||||||
|
unsaved = true;
|
||||||
|
saveButton.classList.add("pulse-red");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function editInstance(instance: Instance) {
|
||||||
|
const data = dialogDetailsFromInstance(instance);
|
||||||
|
const newData = await instanceDetailsDialog.present(data);
|
||||||
|
dialogDetailsToInstance(newData, instance);
|
||||||
|
updateInstanceList();
|
||||||
|
unsavedChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteInstance(instance: Instance) {
|
||||||
|
storageManager.storage.instances.splice(
|
||||||
|
storageManager.storage.instances.indexOf(instance),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
updateInstanceList();
|
||||||
|
unsavedChanges();
|
||||||
|
}
|
||||||
|
|
||||||
function updateInstanceList() {
|
function updateInstanceList() {
|
||||||
instanceList.replaceChildren(); // Erase all child nodes
|
instanceList.replaceChildren(); // Erase all child nodes
|
||||||
instanceList.style.listStyleType = reordering ? "\"≡ \"" : "disc";
|
instanceList.style.listStyleType = reordering ? "\"≡ \"" : "disc";
|
||||||
|
@ -86,26 +123,11 @@ function updateInstanceList() {
|
||||||
const editLink = document.createElement("a");
|
const editLink = document.createElement("a");
|
||||||
editLink.innerText = `Edit`;
|
editLink.innerText = `Edit`;
|
||||||
editLink.href = "#";
|
editLink.href = "#";
|
||||||
editLink.addEventListener("click", e => {
|
editLink.addEventListener("click", e => editInstance(instance));
|
||||||
const host = parseHost(instance.origin)!;
|
|
||||||
populateInstanceDetailsDialog(
|
|
||||||
instance.name,
|
|
||||||
host.host,
|
|
||||||
host.secure,
|
|
||||||
instance.software,
|
|
||||||
instance.iconURL ?? null
|
|
||||||
);
|
|
||||||
showInstanceDetailsDialog();
|
|
||||||
});
|
|
||||||
const deleteLink = document.createElement("a");
|
const deleteLink = document.createElement("a");
|
||||||
deleteLink.innerText = `Delete`;
|
deleteLink.innerText = `Delete`;
|
||||||
deleteLink.href = "#";
|
deleteLink.href = "#";
|
||||||
deleteLink.addEventListener("click", e => {
|
deleteLink.addEventListener("click", e => deleteInstance(instance));
|
||||||
storageManager.storage.instances.splice(
|
|
||||||
storageManager.storage.instances.indexOf(instance)
|
|
||||||
);
|
|
||||||
updateInstanceList();
|
|
||||||
});
|
|
||||||
label.append(editLink, " ", deleteLink);
|
label.append(editLink, " ", deleteLink);
|
||||||
}
|
}
|
||||||
li.appendChild(label);
|
li.appendChild(label);
|
||||||
|
@ -130,4 +152,5 @@ function applyReordering() {
|
||||||
indices.push(parseInt(option));
|
indices.push(parseInt(option));
|
||||||
}
|
}
|
||||||
storageManager.storage.instances = indices.map(i => storageManager.storage.instances[i]);
|
storageManager.storage.instances = indices.map(i => storageManager.storage.instances[i]);
|
||||||
|
unsavedChanges();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,81 +1,185 @@
|
||||||
// This file handles the "Confirm instance details" dialog
|
// This file handles the "Confirm instance details" dialog
|
||||||
|
|
||||||
import { findButtonOrFail, findFormOrFail, findImageOrFail, findInputOrFail, findSelectOrFail } from "./dom.mjs";
|
import { parseHost } from "./add_an_instance.mjs";
|
||||||
|
import { FormDialog, ONCE } from "./dialog.mjs";
|
||||||
|
import { findButtonOrFail, findFormOrFail, findImageOrFail, findInputOrFail, findOptionOrFail, findSelectOrFail } from "./dom.mjs";
|
||||||
import knownSoftware from "./known_software.mjs";
|
import knownSoftware from "./known_software.mjs";
|
||||||
|
import { Instance } from "./storage_manager.mjs";
|
||||||
|
|
||||||
const blankImage = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
|
export function mergeHost(host: string, secure: boolean): string {
|
||||||
|
return `http${secure ? "s" : ""}://${host}`;
|
||||||
|
}
|
||||||
|
|
||||||
export function initializeInstanceDetailsDialog(
|
export type InstanceDetailsDialogData = {
|
||||||
dialog: HTMLDialogElement,
|
name: string,
|
||||||
callback: (
|
host: string,
|
||||||
instanceName: string,
|
hostSecure: boolean,
|
||||||
instanceHost: string,
|
software: string,
|
||||||
instanceHostSecure: boolean,
|
iconURL: string | null,
|
||||||
instanceSoftware: string,
|
preferredFor: string[],
|
||||||
instanceIcon: string | null
|
};
|
||||||
) => void
|
|
||||||
): {
|
|
||||||
showInstanceDetailsDialog: () => void,
|
|
||||||
hideInstanceDetailsDialog: () => void,
|
|
||||||
populateInstanceDetailsDialog: (
|
|
||||||
instanceNameValue: string,
|
|
||||||
instanceHostValue: string,
|
|
||||||
instanceHostSecureValue: boolean,
|
|
||||||
instanceSoftwareValue: string,
|
|
||||||
instanceIconValue: string | null
|
|
||||||
) => void
|
|
||||||
} {
|
|
||||||
const showInstanceDetailsDialog = () => dialog.showModal();
|
|
||||||
const hideInstanceDetailsDialog = () => dialog.close();
|
|
||||||
|
|
||||||
const form = findFormOrFail(dialog, ".instanceDetailsForm");
|
export function dialogDetailsFromInstance(instance: Instance): InstanceDetailsDialogData {
|
||||||
const instanceName = findInputOrFail(form, "#instanceName");
|
const host = parseHost(instance.origin)!;
|
||||||
const instanceHost = findInputOrFail(form, "#instanceHost");
|
return {
|
||||||
const instanceHostSecure = findInputOrFail(form, "#instanceHostSecure");
|
name: instance.name,
|
||||||
const instanceSoftware = findSelectOrFail(form, "#instanceSoftware");
|
host: host.host,
|
||||||
const instanceIcon = findImageOrFail(form, "#instanceIcon");
|
hostSecure: host.secure,
|
||||||
const closeButton = findButtonOrFail(form, ".close");
|
software: instance.software,
|
||||||
|
iconURL: instance.iconURL ?? null,
|
||||||
|
preferredFor: instance.preferredFor,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dialogDetailsToInstance(data: InstanceDetailsDialogData, instance: Partial<Instance>): Instance {
|
||||||
|
instance.name = data.name;
|
||||||
|
instance.origin = mergeHost(data.host, data.hostSecure);
|
||||||
|
instance.software = data.software;
|
||||||
|
instance.iconURL = data.iconURL ?? undefined;
|
||||||
|
instance.preferredFor = data.preferredFor;
|
||||||
|
return instance as Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class InstanceDetailsDialog extends FormDialog {
|
||||||
|
protected instanceName: HTMLInputElement;
|
||||||
|
protected instanceHost: HTMLInputElement;
|
||||||
|
protected instanceHostSecure: HTMLInputElement;
|
||||||
|
protected instanceSoftware: HTMLSelectElement;
|
||||||
|
protected instanceIcon: HTMLImageElement;
|
||||||
|
protected closeButton: HTMLButtonElement;
|
||||||
|
protected defaultsList?: {
|
||||||
|
list: HTMLSelectElement,
|
||||||
|
removeButton: HTMLButtonElement,
|
||||||
|
noDefaultsOption: HTMLOptionElement,
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(dialog: HTMLDialogElement, initializeDOM: boolean = true) {
|
||||||
|
super(dialog, findFormOrFail(dialog, ".instanceDetailsForm"));
|
||||||
|
|
||||||
|
this.instanceName = findInputOrFail(this.form, "#instanceName");
|
||||||
|
this.instanceHost = findInputOrFail(this.form, "#instanceHost");
|
||||||
|
this.instanceHostSecure = findInputOrFail(this.form, "#instanceHostSecure");
|
||||||
|
this.instanceSoftware = findSelectOrFail(this.form, "#instanceSoftware");
|
||||||
|
this.instanceIcon = findImageOrFail(this.form, "#instanceIcon");
|
||||||
|
this.closeButton = findButtonOrFail(this.form, ".close");
|
||||||
|
try {
|
||||||
|
this.defaultsList = {
|
||||||
|
list: findSelectOrFail(this.form, "#defaultsList"),
|
||||||
|
removeButton: findButtonOrFail(this.form, "#removeDefaults"),
|
||||||
|
noDefaultsOption: findOptionOrFail(this.form, "#noDefaults"),
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
this.defaultsList = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (initializeDOM) this.initializeDOM();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override initializeDOM() {
|
||||||
|
super.initializeDOM();
|
||||||
|
|
||||||
for (const [name, software] of Object.entries(knownSoftware.software)) {
|
for (const [name, software] of Object.entries(knownSoftware.software)) {
|
||||||
const option = new Option(software.name, name);
|
const option = new Option(software.name, name);
|
||||||
instanceSoftware.appendChild(option);
|
this.instanceSoftware.appendChild(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
instanceIcon.src = blankImage;
|
this.instanceIcon.hidden = true;
|
||||||
|
|
||||||
const populateInstanceDetailsDialog = (
|
this.closeButton.addEventListener("click", e => this.close());
|
||||||
instanceNameValue: string,
|
|
||||||
instanceHostValue: string,
|
|
||||||
instanceHostSecureValue: boolean,
|
|
||||||
instanceSoftwareValue: string,
|
|
||||||
instanceIconValue: string | null
|
|
||||||
) => {
|
|
||||||
instanceName.value = instanceNameValue;
|
|
||||||
instanceHost.value = instanceHostValue;
|
|
||||||
instanceHostSecure.checked = instanceHostSecureValue;
|
|
||||||
instanceSoftware.value = instanceSoftwareValue;
|
|
||||||
instanceIcon.src = instanceIconValue ?? blankImage;
|
|
||||||
};
|
|
||||||
|
|
||||||
form.addEventListener("submit", e => {
|
if (this.defaultsList) {
|
||||||
callback(
|
this.defaultsList.list.addEventListener("change", e => this.#handleListSelectionChange());
|
||||||
instanceName.value,
|
this.defaultsList.removeButton.addEventListener("click", e => this.#removeSelectedListOptions());
|
||||||
instanceHost.value,
|
}
|
||||||
instanceHostSecure.checked,
|
}
|
||||||
instanceSoftware.value,
|
|
||||||
instanceIcon.src
|
#getRemainingListOptions(): string[] {
|
||||||
);
|
if (!this.defaultsList) return [];
|
||||||
form.reset();
|
|
||||||
|
const items: string[] = [];
|
||||||
|
|
||||||
|
for (const option of this.defaultsList.list.options) {
|
||||||
|
if (option == this.defaultsList.noDefaultsOption) continue;
|
||||||
|
items.push(option.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
#removeSelectedListOptions() {
|
||||||
|
if (!this.defaultsList) return;
|
||||||
|
|
||||||
|
// Copy using spread because this breaks when the list changes mid-iteration
|
||||||
|
for (const option of [...this.defaultsList.list.selectedOptions]) {
|
||||||
|
option.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#handleListChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
#populateDefaultsList(items: string[]) {
|
||||||
|
if (!this.defaultsList) return;
|
||||||
|
|
||||||
|
while (this.defaultsList.list.children.length > 1)
|
||||||
|
this.defaultsList.list.removeChild(this.defaultsList.list.lastChild!);
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
const option = document.createElement("option");
|
||||||
|
option.value = item;
|
||||||
|
option.innerText = knownSoftware.software[item]?.name ?? knownSoftware.groups[item]?.name ?? item;
|
||||||
|
this.defaultsList.list.appendChild(option);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#handleListChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
#handleListChange() {
|
||||||
|
this.#handleListEmpty();
|
||||||
|
this.#handleListSelectionChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
#handleListSelectionChange() {
|
||||||
|
if (!this.defaultsList) return;
|
||||||
|
this.defaultsList.removeButton.disabled = this.defaultsList.list.selectedOptions.length === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#handleListEmpty() {
|
||||||
|
if (!this.defaultsList) return;
|
||||||
|
if (this.defaultsList.list.children.length == 1) {
|
||||||
|
this.defaultsList.noDefaultsOption.hidden = false;
|
||||||
|
} else {
|
||||||
|
this.defaultsList.noDefaultsOption.hidden = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#handleSubmit(data: InstanceDetailsDialogData, resolve: (data: InstanceDetailsDialogData) => void) {
|
||||||
|
this.form.addEventListener("submit", e => {
|
||||||
|
data.name = this.instanceName.value;
|
||||||
|
data.host = this.instanceHost.value;
|
||||||
|
data.hostSecure = this.instanceHostSecure.checked;
|
||||||
|
data.software = this.instanceSoftware.value;
|
||||||
|
data.preferredFor = this.#getRemainingListOptions();
|
||||||
|
|
||||||
|
resolve(data);
|
||||||
|
this.close();
|
||||||
|
}, ONCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
async present(data: InstanceDetailsDialogData): Promise<InstanceDetailsDialogData> {
|
||||||
|
this.instanceName.value = data.name;
|
||||||
|
this.instanceHost.value = data.host;
|
||||||
|
this.instanceHostSecure.checked = data.hostSecure;
|
||||||
|
this.instanceSoftware.value = data.software;
|
||||||
|
if (data.iconURL !== null) {
|
||||||
|
this.instanceIcon.src = data.iconURL;
|
||||||
|
this.instanceIcon.hidden = false;
|
||||||
|
} else this.instanceIcon.hidden = true;
|
||||||
|
this.#populateDefaultsList(data.preferredFor);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.cancelOnceClosed(reject);
|
||||||
|
this.#handleSubmit(data, resolve);
|
||||||
|
this.open();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
closeButton.addEventListener("click", e => {
|
|
||||||
instanceIcon.src = blankImage;
|
|
||||||
hideInstanceDetailsDialog();
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
showInstanceDetailsDialog,
|
|
||||||
hideInstanceDetailsDialog,
|
|
||||||
populateInstanceDetailsDialog
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
<p id="no-instance">You currently don't have any instances. You should add one!</p>
|
<p id="no-instance">You currently don't have any instances. You should add one!</p>
|
||||||
<form id="instanceSelectForm" class="align-start wfit-content"></form>
|
<form id="instanceSelectForm" class="align-start wfit-content"></form>
|
||||||
<br>
|
<br>
|
||||||
<button id="showAddInstanceDialog">Add an instance</button>
|
<button id="startAddInstanceFlow">Add an instance</button>
|
||||||
</center>
|
</center>
|
||||||
</div>
|
</div>
|
||||||
<div class="half-width align-self-start">
|
<div class="half-width align-self-start">
|
||||||
|
@ -54,9 +54,8 @@
|
||||||
<br>
|
<br>
|
||||||
<input id="autoQueryMetadata" type="checkbox" name="autoQueryMetadata" checked />
|
<input id="autoQueryMetadata" type="checkbox" name="autoQueryMetadata" checked />
|
||||||
<label for="autoQueryMetadata">
|
<label for="autoQueryMetadata">
|
||||||
<abbr title="This uses FeDirect's API to automatically try to detect instance metadata, such as the name, software, and an icon.
|
<abbr
|
||||||
We do this on the backend to avoid CORS problems.
|
title="This uses FeDirect's API to automatically try to detect instance metadata, such as the name, software, and an icon.">
|
||||||
We do not track or save any requests or data.">
|
|
||||||
Automatically query metadata
|
Automatically query metadata
|
||||||
</abbr>
|
</abbr>
|
||||||
</label>
|
</label>
|
||||||
|
@ -108,6 +107,7 @@ Unchecking this is not recommended, and this option only exists for exceptional
|
||||||
<button type="reset" class="close">Cancel</button>
|
<button type="reset" class="close">Cancel</button>
|
||||||
</form>
|
</form>
|
||||||
</dialog>
|
</dialog>
|
||||||
|
<dialog id="spinner"><span class="spinner"></span></dialog>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
|
@ -1,20 +1,37 @@
|
||||||
import { initializeAddInstanceFlow } from "./add_instance_flow.mjs";
|
import { AddInstanceFlow } from "./add_instance_flow.mjs";
|
||||||
import { findButtonOrFail, findDialogOrFail, findFormOrFail, findInputOrFail, findParagraphOrFail, findPreOrFail } from "./dom.mjs";
|
import { findButtonOrFail, findDialogOrFail, findFormOrFail, findInputOrFail, findParagraphOrFail, findPreOrFail } 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";
|
const RADIO_BUTTON_NAME = "instanceSelect";
|
||||||
|
|
||||||
|
let addInstanceFlow: AddInstanceFlow | undefined;
|
||||||
|
|
||||||
const mainDialog = findDialogOrFail(document.body, "#mainDialog");
|
const mainDialog = findDialogOrFail(document.body, "#mainDialog");
|
||||||
const showAddInstanceDialogButton = findButtonOrFail(document.body, "#showAddInstanceDialog");
|
const startAddInstanceFlowButton = findButtonOrFail(document.body, "#startAddInstanceFlow");
|
||||||
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
|
||||||
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
const addDialog = findDialogOrFail(document.body, "#addInstance");
|
||||||
|
const spinnerDialog = findDialogOrFail(document.body, "#spinner");
|
||||||
|
const detailsDialog = findDialogOrFail(document.body, "#instanceDetails");
|
||||||
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");
|
||||||
const pathText = findPreOrFail(document.body, "#path");
|
const pathText = findPreOrFail(document.body, "#path");
|
||||||
|
|
||||||
showAddInstanceDialogButton.addEventListener("click", e => showAddInstanceDialog());
|
// Don't bother initializing if we're performing autoredirect
|
||||||
|
if (!autoRedirect()) {
|
||||||
|
createInstanceSelectOptions();
|
||||||
|
storageManager.addSaveCallback(createInstanceSelectOptions);
|
||||||
|
updateNoInstanceHint();
|
||||||
|
storageManager.addSaveCallback(updateNoInstanceHint);
|
||||||
|
|
||||||
|
pathText.innerText = getTargetPath();
|
||||||
|
|
||||||
|
addInstanceFlow = new AddInstanceFlow(addDialog, spinnerDialog, detailsDialog);
|
||||||
|
|
||||||
|
mainDialog.show();
|
||||||
|
};
|
||||||
|
|
||||||
|
startAddInstanceFlowButton.addEventListener("click", e => addInstanceFlow?.start(true));
|
||||||
|
|
||||||
redirectButton.addEventListener("click", e => {
|
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
|
// Can be assumed to not fail because the button is disabled if there are no options and the first one is selected by default
|
||||||
|
@ -28,31 +45,9 @@ redirectAlwaysButton.addEventListener("click", e => {
|
||||||
redirect(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() {
|
function updateNoInstanceHint() {
|
||||||
findParagraphOrFail(document.body, "#no-instance").style.display =
|
findParagraphOrFail(document.body, "#no-instance").hidden =
|
||||||
storageManager.storage.instances.length > 0
|
storageManager.storage.instances.length > 0;
|
||||||
? "none"
|
|
||||||
: "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createInstanceSelectOptions() {
|
function createInstanceSelectOptions() {
|
||||||
|
@ -64,7 +59,7 @@ function createInstanceSelectOptions() {
|
||||||
radio.id = instance.origin;
|
radio.id = instance.origin;
|
||||||
radio.value = instance.origin;
|
radio.value = instance.origin;
|
||||||
radio.type = "radio";
|
radio.type = "radio";
|
||||||
radio.name = radioButtonName;
|
radio.name = RADIO_BUTTON_NAME;
|
||||||
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 + " ";
|
||||||
|
@ -111,7 +106,7 @@ function getTargetPath(): string {
|
||||||
|
|
||||||
function getSelectedOption(): string | null {
|
function getSelectedOption(): string | null {
|
||||||
try {
|
try {
|
||||||
return findInputOrFail(instanceSelectForm, `input[name="${radioButtonName}"]:checked`).value;
|
return findInputOrFail(instanceSelectForm, `input[name="${RADIO_BUTTON_NAME}"]:checked`).value;
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -130,7 +125,6 @@ function autoRedirect(): boolean {
|
||||||
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");
|
||||||
instance.preferredFor ??= [];
|
|
||||||
instance.preferredFor.push(getTargetSoftwareOrGroup());
|
instance.preferredFor.push(getTargetSoftwareOrGroup());
|
||||||
storageManager.save();
|
storageManager.save();
|
||||||
}
|
}
|
||||||
|
|
37
static/data_migration.mts
Normal file
37
static/data_migration.mts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import { LocalStorage } from "./storage_manager.mjs"
|
||||||
|
|
||||||
|
type InstanceV0 = {
|
||||||
|
name: string,
|
||||||
|
origin: string,
|
||||||
|
software: string,
|
||||||
|
iconURL?: string,
|
||||||
|
preferredFor?: string[],
|
||||||
|
}
|
||||||
|
type LocalStorageV0 = {
|
||||||
|
version: undefined,
|
||||||
|
instances: InstanceV0[],
|
||||||
|
}
|
||||||
|
|
||||||
|
type LocalStorageV1 = LocalStorage;
|
||||||
|
|
||||||
|
function migrate0to1(s: LocalStorageV0): LocalStorageV1 {
|
||||||
|
return {
|
||||||
|
version: 1,
|
||||||
|
instances: s.instances.map(i => ({
|
||||||
|
preferredFor: i.preferredFor ?? [],
|
||||||
|
...i
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
type AnyLocalStorage = LocalStorageV0 | LocalStorageV1;
|
||||||
|
|
||||||
|
export default function migrate(storage: AnyLocalStorage): LocalStorage {
|
||||||
|
switch (storage.version) {
|
||||||
|
case undefined:
|
||||||
|
storage = migrate0to1(storage);
|
||||||
|
case 1:
|
||||||
|
default:
|
||||||
|
return storage;
|
||||||
|
}
|
||||||
|
}
|
46
static/dialog.mts
Normal file
46
static/dialog.mts
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
export const ONCE = { once: true };
|
||||||
|
export const CANCELLED = Symbol("Cancelled");
|
||||||
|
|
||||||
|
export class Dialog {
|
||||||
|
protected dialog: HTMLDialogElement;
|
||||||
|
|
||||||
|
constructor(dialog: HTMLDialogElement) {
|
||||||
|
this.dialog = dialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function that should only be called once that has permanent effects on the DOM
|
||||||
|
*/
|
||||||
|
protected initializeDOM() { }
|
||||||
|
|
||||||
|
open() {
|
||||||
|
this.dialog.showModal();
|
||||||
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
this.dialog.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected cancelOnceClosed(reject: (reason?: any) => void) {
|
||||||
|
this.dialog.addEventListener("close", e => reject(CANCELLED), ONCE);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export class FormDialog extends Dialog {
|
||||||
|
protected form: HTMLFormElement;
|
||||||
|
|
||||||
|
constructor(dialog: HTMLDialogElement, form: HTMLFormElement) {
|
||||||
|
super(dialog);
|
||||||
|
this.form = form;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override initializeDOM() {
|
||||||
|
super.initializeDOM();
|
||||||
|
|
||||||
|
this.dialog.addEventListener("close", e => this.reset());
|
||||||
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.form.reset();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,13 @@
|
||||||
// I would've LOVED to use generics for this but unfortunately that's not possible.
|
// I would've LOVED to use generics for this but unfortunately that's not possible.
|
||||||
// Type safety, but at what cost... >~< thanks TypeScript
|
// Type safety, but at what cost... >~< thanks TypeScript
|
||||||
|
|
||||||
|
export function findOptionOrFail(on: Element, selector: string): HTMLOptionElement {
|
||||||
|
const element = on.querySelector(selector);
|
||||||
|
if (!(element instanceof HTMLOptionElement))
|
||||||
|
throw new Error(`${selector} isn't an option`);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
export function findOlOrFail(on: Element, selector: string): HTMLOListElement {
|
export function findOlOrFail(on: Element, selector: string): HTMLOListElement {
|
||||||
const element = on.querySelector(selector);
|
const element = on.querySelector(selector);
|
||||||
if (!(element instanceof HTMLOListElement))
|
if (!(element instanceof HTMLOListElement))
|
||||||
|
|
|
@ -2,7 +2,7 @@ export function resize(image: HTMLImageElement, width: number = 16, height: numb
|
||||||
const canvas = document.createElement('canvas');
|
const canvas = document.createElement('canvas');
|
||||||
canvas.width = width;
|
canvas.width = width;
|
||||||
canvas.height = height;
|
canvas.height = height;
|
||||||
canvas.style.display = "none";
|
canvas.hidden = true;
|
||||||
document.body.appendChild(canvas);
|
document.body.appendChild(canvas);
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
if (ctx === null) throw Error("Resize failed");
|
if (ctx === null) throw Error("Resize failed");
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
@import url("/static/spinner.css");
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
--red: #cb0b0b;
|
--red: #cb0b0b;
|
||||||
--blue: #2081c3;
|
--blue: #2081c3;
|
||||||
|
@ -183,3 +185,17 @@ abbr[title] {
|
||||||
.buttonPanel>* {
|
.buttonPanel>* {
|
||||||
margin-top: min(var(--xl), 6vh);
|
margin-top: min(var(--xl), 6vh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.pulse-red {
|
||||||
|
animation: 1s ease-in-out 0s infinite alternate both running pulse-red-anim;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes pulse-red-anim {
|
||||||
|
0% {
|
||||||
|
box-shadow: 0px 0px 0px var(--red);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
box-shadow: 0px 0px 20px var(--red);
|
||||||
|
}
|
||||||
|
}
|
78
static/spinner.css
Normal file
78
static/spinner.css
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/* Sourced and modified from https://cssloaders.github.io/ */
|
||||||
|
|
||||||
|
.spinner {
|
||||||
|
display: block;
|
||||||
|
animation: rotate 1s infinite;
|
||||||
|
height: 50px;
|
||||||
|
width: 50px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spinner:before,
|
||||||
|
.spinner:after {
|
||||||
|
border-radius: 50%;
|
||||||
|
content: "";
|
||||||
|
display: block;
|
||||||
|
height: 20px;
|
||||||
|
width: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spinner:before {
|
||||||
|
animation: ball1 1s infinite;
|
||||||
|
background-color: var(--red);
|
||||||
|
box-shadow: 30px 0 0 var(--blue);
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spinner:after {
|
||||||
|
animation: ball2 1s infinite;
|
||||||
|
background-color: var(--blue);
|
||||||
|
box-shadow: 30px 0 0 var(--red);
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes rotate {
|
||||||
|
0% {
|
||||||
|
transform: rotate(0deg) scale(0.8)
|
||||||
|
}
|
||||||
|
|
||||||
|
50% {
|
||||||
|
transform: rotate(360deg) scale(1.2)
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
transform: rotate(720deg) scale(0.8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes ball1 {
|
||||||
|
0% {
|
||||||
|
box-shadow: 30px 0 0 var(--blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
50% {
|
||||||
|
box-shadow: 0 0 0 var(--blue);
|
||||||
|
margin-bottom: 0;
|
||||||
|
transform: translate(15px, 15px);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
box-shadow: 30px 0 0 var(--blue);
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes ball2 {
|
||||||
|
0% {
|
||||||
|
box-shadow: 30px 0 0 var(--red);
|
||||||
|
}
|
||||||
|
|
||||||
|
50% {
|
||||||
|
box-shadow: 0 0 0 var(--red);
|
||||||
|
margin-top: -20px;
|
||||||
|
transform: translate(15px, 15px);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
box-shadow: 30px 0 0 var(--red);
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
import migrate from "./data_migration.mjs";
|
||||||
|
|
||||||
export type Instance = {
|
export type Instance = {
|
||||||
/**
|
/**
|
||||||
* The instance's (nick)name
|
* The instance's (nick)name
|
||||||
|
@ -21,18 +23,19 @@ export type Instance = {
|
||||||
software: string,
|
software: string,
|
||||||
/**
|
/**
|
||||||
* The instance's icon URL
|
* The instance's icon URL
|
||||||
*
|
* @example undefined
|
||||||
* Make sure to sanitize this! Could lead to XSS
|
* @example "https://void.lgbt/favicon.png"
|
||||||
*/
|
*/
|
||||||
iconURL?: string,
|
iconURL?: string,
|
||||||
/**
|
/**
|
||||||
* The list of software names and groups the user prefers to autoredirect to this instance
|
* The list of software names and groups the user prefers to autoredirect to this instance
|
||||||
* @example ["sharkey", "misskey-compliant"]
|
* @example ["sharkey", "misskey-compliant"]
|
||||||
*/
|
*/
|
||||||
preferredFor?: string[],
|
preferredFor: string[],
|
||||||
}
|
}
|
||||||
|
|
||||||
type LocalStorage = {
|
export type LocalStorage = {
|
||||||
|
version: number,
|
||||||
instances: Instance[],
|
instances: Instance[],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,12 +49,14 @@ export default new class StorageManager {
|
||||||
|
|
||||||
default(): LocalStorage {
|
default(): LocalStorage {
|
||||||
return {
|
return {
|
||||||
|
version: 1,
|
||||||
instances: []
|
instances: []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
load() {
|
load() {
|
||||||
this.storage = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default();
|
const data = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default();
|
||||||
|
this.storage = migrate(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
|
@ -62,4 +67,8 @@ export default new class StorageManager {
|
||||||
addSaveCallback(callback: () => void) {
|
addSaveCallback(callback: () => void) {
|
||||||
this.saveCallbacks.push(callback);
|
this.saveCallbacks.push(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.storage = this.default();
|
||||||
|
}
|
||||||
}();
|
}();
|
|
@ -4,6 +4,7 @@
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"checkJs": true,
|
"checkJs": true,
|
||||||
"strictNullChecks": true,
|
"strictNullChecks": true,
|
||||||
|
"noImplicitOverride": true,
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"static/**.mts",
|
"static/**.mts",
|
||||||
|
|
Loading…
Add table
Reference in a new issue