aboutsummaryrefslogtreecommitdiff
path: root/client-web/source/helper.ts
blob: 4075a0f847a4ce9579f41c7b2ede3a60e324895f (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
/// <reference lib="dom" />

const elem = (s: string) => document.createElement(s)

interface Opts { class?: string[] | string, id?: string, src?: string, onclick?: () => void }

function apply_opts(e: HTMLElement, o: Opts | undefined) {
    if (!o) return
    if (o.id) e.id = o.id
    if (o.onclick) e.onclick = o.onclick
    if (typeof o?.class == "string") e.classList.add(o.class)
    if (typeof o?.class == "object") e.classList.add(...o.class)
}
const elem_with_content = (s: string) => (c: string, opts?: Opts) => {
    const e = elem(s)
    apply_opts(e, opts)
    e.textContent = c
    return e
}
const elem_with_children = (s: string) => (opts?: Opts, ...cs: (HTMLElement | string)[]) => {
    const e = elem(s)
    apply_opts(e, opts)
    for (const c of cs) {
        e.append(c)
    }
    return e
}

export const ep = elem_with_content("p")
export const eh1 = elem_with_content("h1")
export const eh2 = elem_with_content("h2")
export const eh3 = elem_with_content("h3")
export const eh4 = elem_with_content("h4")
export const eh5 = elem_with_content("h5")
export const eh6 = elem_with_content("h6")
export const ediv = elem_with_children("div")
export const espan = elem_with_content("span")
export const elabel = elem_with_content("label")
export const ebutton = elem_with_content("button")
export const ebr = () => document.createElement("br")

export const OVERLAYS = ediv({ class: "overlays" })

export class OverlayUi {
    _shown = false
    constructor(public el: HTMLElement, initial = false) {
        this.shown = initial
    }
    get shown() { return this._shown }
    set shown(v: boolean) {
        if (v && !this._shown) OVERLAYS.append(this.el)
        if (!v && this._shown) OVERLAYS.removeChild(this.el)
        this._shown = v
    }
}

export function image_view(url: string, opts?: Opts): HTMLElement {
    const img = document.createElement("img")
    apply_opts(img, opts)
    img.src = url
    img.alt = `Image (click to open)`
    img.addEventListener("click", () => {
        window.open(url, "_blank", "noreferrer=true,noopener=true,popup=true")
    })
    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 })
}