summaryrefslogtreecommitdiff
path: root/client-web/source/preferences/mod.ts
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2022-09-10 00:56:25 +0200
committermetamuffin <metamuffin@disroot.org>2022-09-10 00:56:25 +0200
commit429dc2d5375abf8ca9c3861bdc4bdff52a31b0e4 (patch)
tree2fa8201ebdf45237a9ec90429bd18b5d6cdd9944 /client-web/source/preferences/mod.ts
parent95041256f86745832df42423e889d50d2cff35e7 (diff)
downloadkeks-meet-429dc2d5375abf8ca9c3861bdc4bdff52a31b0e4.tar
keks-meet-429dc2d5375abf8ca9c3861bdc4bdff52a31b0e4.tar.bz2
keks-meet-429dc2d5375abf8ca9c3861bdc4bdff52a31b0e4.tar.zst
overlay rework + settings
Diffstat (limited to 'client-web/source/preferences/mod.ts')
-rw-r--r--client-web/source/preferences/mod.ts87
1 files changed, 87 insertions, 0 deletions
diff --git a/client-web/source/preferences/mod.ts b/client-web/source/preferences/mod.ts
new file mode 100644
index 0000000..5b33924
--- /dev/null
+++ b/client-web/source/preferences/mod.ts
@@ -0,0 +1,87 @@
+import { PREF_DECLS } from "./decl.ts";
+
+
+export interface PrefDecl<T> {
+ default?: T,
+ type: T,
+ description?: string,
+ possible_values?: T[]
+ optional?: boolean,
+}
+
+type Type = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
+type TypeMapper = { "string": string, "number": number, "boolean": boolean }
+
+type PrefMap<T extends { [key: string]: { type: unknown } }> = { [Key in keyof T]: T[Key]["type"] }
+type Optional<T extends { [key: string]: unknown }> = { [Key in keyof T]?: T[Key] }
+export const { prefs: PREFS, explicit: PREFS_EXPLICIT } = register_prefs(PREF_DECLS)
+export const on_pref_change_handlers: ((key: keyof typeof PREFS) => void)[] = []
+
+export function register_prefs<T extends Record<string, PrefDecl<unknown>>>(ds: T): { prefs: PrefMap<T>, explicit: Optional<PrefMap<T>> } {
+ const prefs: PrefMap<T> = {} as PrefMap<T>
+ const explicit: Optional<PrefMap<T>> = {}
+ for (const key in ds) {
+ const d = ds[key];
+ const type = typeof d.type;
+ let value = get_param(type, key)
+ if (value !== undefined) explicit[key] = value
+ value ??= d.default;
+ if (d.possible_values) if (!d.possible_values.includes(value)) value = d.default
+ prefs[key] = value
+ }
+ return { prefs, explicit }
+}
+
+export function change_pref<T extends keyof typeof PREFS>(key: T, value: typeof PREFS[T]) {
+ PREFS[key] = value
+ if ((PREF_DECLS as Record<string, PrefDecl<unknown>>)[key].default != value)
+ PREFS_EXPLICIT[key] = value
+ else delete PREFS_EXPLICIT[key]
+ window.location.hash = "#" + generate_section()
+}
+export function generate_section(): string {
+ const section = []
+ for (const key in PREFS_EXPLICIT) {
+ section.push(encodeURIComponent(key) + "=" + encodeURIComponent(param_to_string(
+ PREFS_EXPLICIT[key as unknown as keyof typeof PREFS_EXPLICIT]
+ )))
+ }
+ return load_params().rname + "?" + section.join("&")
+}
+
+export function load_params(): { raw_params: { [key: string]: string }, rname: string } {
+ const raw_params: Record<string, string> = {}
+ const [rname, param_str] = window.location.hash.substring(1).split("?")
+ if (!param_str) return { rname, raw_params: {} }
+ for (const kv of param_str.split("&")) {
+ const [key, value] = kv.split("=")
+ if (key == "prototype") continue
+ raw_params[decodeURIComponent(key)] = decodeURIComponent(value)
+ }
+ return { raw_params, rname }
+}
+
+function param_to_string<T>(p: T): string {
+ if (typeof p == "string") return p
+ else if (typeof p == "boolean") return JSON.stringify(p)
+ else if (typeof p == "number") return JSON.stringify(p)
+ throw new Error("impossible");
+}
+
+function get_param<T>(ty: string, key: string): T | undefined {
+ const v = load_params().raw_params[key]
+ if (v == undefined) return undefined
+ if (ty == "string") return v as unknown as T
+ else if (ty == "number") {
+ const n = parseInt(v)
+ if (!Number.isNaN(n)) return n as unknown as T
+ console.warn("invalid number parameter");
+ } else if (ty == "boolean") {
+ if (v == "0" || v == "false" || v == "no") return false as unknown as T
+ if (v == "1" || v == "true" || v == "yes") return true as unknown as T
+ console.warn("invalid boolean parameter");
+ } else {
+ throw new Error("invalid param type");
+ }
+ return undefined
+}