aboutsummaryrefslogtreecommitdiff
path: root/static/components/vevent.ts
blob: 23eba0a910ea38514c2077eebe84839d85364ba3 (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
export { ComponentVEvent }

import { vcal_objects } from '../globals'
import { VEvent } from '../vevent'

/* Root component for all events which content is closely linked to a
@code{VEvent} object

Lacks an accompaning tag, and shouldn't be directly instanciated.
*/
class ComponentVEvent extends HTMLElement {

    template: HTMLTemplateElement
    uid: string

    constructor(uid?: string) {
        super();
        this.template = document.getElementById(this.tagName) as HTMLTemplateElement;

        let real_uid;
        if (this.dataset.uid) uid = this.dataset.uid;
        if (uid) real_uid = uid;

        if (!real_uid) {
            console.warn(this.outerHTML);
            throw `UID required`
        }

        this.uid = real_uid;
        this.dataset.uid = uid;

        vcal_objects.get(this.uid)?.register(this);

        /* We DON'T have a redraw here in the general case, since the
           HTML rendered server-side should be fine enough for us.
           Those that need a direct rerendering (such as the edit tabs)
           should take care of that some other way */
    }

    connectedCallback() {
        let uid = this.dataset.uid
        if (uid) {
            let v = vcal_objects.get(uid)
            if (v) this.redraw(v);
        }
    }

    redraw(data: VEvent) {
        // update ourselves from template

        if (!this.template) {
            throw "Something";
        }

        let body = (this.template.content.cloneNode(true) as DocumentFragment).firstElementChild!;

        for (let el of body.getElementsByClassName("bind")) {
            if (!(el instanceof HTMLElement)) continue;
            let p = el.dataset.property!;
            let d, fmt;
            if ((d = data.getProperty(p))) {
                if ((fmt = el.dataset.fmt)) {
                    el.innerHTML = d.format(fmt);
                } else {
                    el.innerHTML = d;
                }
            }
        }

        this.replaceChildren(body);
    }

}