aboutsummaryrefslogtreecommitdiff
path: root/static/components/vevent.ts
blob: abf0f4c0df061caa97dd68e61702b63cca263e1e (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
105
106
107
108
109
110
111
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.
*/
abstract class ComponentVEvent extends HTMLElement {

    template: HTMLTemplateElement | null
    uid: string
    instance: string | null

    constructor(uid?: string, instance?: string) {
        super();
        this.template = document.getElementById(this.tagName.toLowerCase()) as HTMLTemplateElement | null

        let real_uid, real_instance;

        if (uid) {
            // console.log('Got UID directly');
            real_uid = uid;
        } else {
            /* I know that this case is redundant, it's here if we don't want to
               look up the tree later */
            if (this.dataset.uid) {
                // console.log('Had UID as direct attribute');
                real_uid = this.dataset.uid;
            } else {
                /* TODO when is this case relevant? */
                let el = this.closest('[data-uid]')
                if (el) {
                    // console.log('Found UID higher up in the tree');
                    real_uid = (el as HTMLElement).dataset.uid
                } else {
                    throw "No parent with [data-uid] set"
                }
            }
        }

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

        // console.log(real_uid);
        this.uid = real_uid;
        this.dataset.uid = real_uid;

        if (instance) {
            real_instance = instance
        } else {
            if (this.dataset.instance) {
                real_instance = this.dataset.instance
            } else {
                let el = this.closest('[data-instance]')
                if (el) {
                    real_instance = (el as HTMLElement).dataset.instance
                    if (real_instance === undefined) {
                        real_instance = null;
                    }
                } else {
                    real_instance = null
                }
            }
        }
        this.instance = real_instance;
        if (real_instance) {
            this.dataset.instance = real_instance;
        }

        /* TODO */
        /* Here, choose different rules depending on if we are repeating or not */

        vcal_objects.get(this.getKey())?.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 */
    }

    getKey(): string {
        let uid = this.dataset.uid
        // let instance_id = ev.getProperty('dtstart')!.format('~Y-~m-~dT~H:~M:~S')
        let instance = this.dataset.instance
        if (!uid) throw new Error("UID missing");
        let key = uid;
        /* NOTE proper composite keys would be nice, since this can collide with
           a regular key. However, javascript's Map doesn't support lists (or
           object) as keys by default. */
        if (instance) key += '---' + instance;
        return key;
    }

    /* This return different object for different instances */
    getData(): VEvent | undefined {
        return vcal_objects.get(this.getKey())
    }

    connectedCallback() {
        let v = this.getData();
        if (v) this.redraw(v);
    }

    abstract redraw(data: VEvent): void

}