aboutsummaryrefslogtreecommitdiff
path: root/client-web/source/helper.ts
blob: 70b7c28bfc949a43a72e5977a2fbed49d9974abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
    This file is part of keks-meet (https://codeberg.org/metamuffin/keks-meet)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
/// <reference lib="dom" />

import { PO } from "./locale/mod.ts";
import { PREFS } from "./preferences/mod.ts";

interface Opts<E> {
    class?: string[] | string,
    id?: string,
    src?: string,
    for?: string,
    type?: string,
    href?: string,
    alt?: string,
    onclick?: (e: E) => void,
    onchange?: (e: E) => void,
    onkeydown?: (e: E, ev: KeyboardEvent) => void,
    onkeyup?: (e: E, ev: KeyboardEvent) => void,
    role?: "dialog" | "separator" | "switch" | "button" | "log" | "group" | "toolbar",
    aria_label?: string
    aria_live?: "polite" | "assertive" | "off",
    aria_modal?: boolean
    aria_popup?: "menu"
    icon?: string,
    hidden?: boolean,
}

function apply_opts<E extends HTMLElement>(el: E, o: Opts<E>) {
    if (o.id) el.id = o.id
    if (o.onclick) el.onclick = () => o.onclick!(el)
    if (o.onchange) el.onchange = () => o.onchange!(el)
    if (o.onkeydown) el.onkeydown = ev => o.onkeydown!(el, ev)
    if (o.onkeyup) el.onkeyup = ev => o.onkeyup!(el, ev)
    if (o.for) (el as unknown as HTMLLabelElement).htmlFor = o.for
    if (o.type && el instanceof HTMLInputElement) el.type = o.type
    if (o.href && el instanceof HTMLAnchorElement) el.href = o.href;
    if (o.alt !== undefined && el instanceof HTMLImageElement) el.alt = o.alt;
    if (typeof o?.class == "string") el.classList.add(o.class)
    if (typeof o?.class == "object") el.classList.add(...o.class)
    if (o.role) el.role = o.role;
    if (o.aria_modal) el.ariaModal = "true"
    if (o.aria_popup) el.ariaHasPopup = o.aria_popup
    if (o.aria_label) el.ariaLabel = o.aria_label
    if (o.aria_live) el.ariaLive = o.aria_live
    if (o.src && el instanceof HTMLImageElement) el.src = o.src;
    if (o.hidden) el.hidden = o.hidden;
    if (o.icon) {
        el.prepend(e("img", { src: `/assets/icons/${o.icon}.svg`, alt: "", class: "icon" }))
    }
}

export function e<K extends keyof HTMLElementTagNameMap>(name: K, opts: Opts<HTMLElementTagNameMap[K]>, ...children: (HTMLElement | string)[]): HTMLElementTagNameMap[K] {
    const el = document.createElement(name)
    apply_opts(el, opts)
    for (const c of children) el.append(c);
    return el
}

export function image_view(url: string, opts?: Opts<HTMLElement>): HTMLElement {
    const img = document.createElement("img")
    apply_opts(img, opts ?? {})
    img.src = url
    img.alt = PO.image_alt
    img.addEventListener("click", () => {
        globalThis.open(url, "_blank", `noreferrer=true,noopener=true,popup=${PREFS.image_view_popup}`)
    })
    return img
}

export function notify(body: string, author?: string) {
    if (document.hasFocus()) return
    if (Notification.permission != "granted") return
    if (author)
        new Notification(`keks-meet: ${author}`, { body })
    else
        new Notification(`keks-meet`, { body })
}

export function sleep(delay: number) { return new Promise(r => setTimeout(r, delay)) }

export function display_filesize(n: number): string {
    if (n > 1000000000000) return (n / 1000000000000).toFixed(1) + "TB"
    if (n > 1000000000) return (n / 1000000000).toFixed(1) + "GB"
    if (n > 1000000) return (n / 1000000).toFixed(1) + "MB"
    if (n > 1000) return (n / 1000).toFixed(1) + "kB"
    return n.toString() + "B"
}

export class EventEmitter<E> {
    private handlers: Set<(e: E) => unknown> = new Set()
    public dispatch(e: E) { this.handlers.forEach(h => h(e)) }
    public add_listener(listener: (e: E) => unknown) { this.handlers.add(listener) }
    public remove_listener(listener: (e: E) => unknown) { this.handlers.delete(listener) }
}

export function array_swap<T>(arr: T[], a: number, b: number) {
    const temp = arr[a]
    arr[a] = arr[b]
    arr[b] = temp
}