aboutsummaryrefslogtreecommitdiff
path: root/static/globals.js
blob: fd576e2682e9357932afb375fa35e0faaf7c8907 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"use strict";

class VEventValue {
    constructor (type, value, parameters = {}) {
        this.type = type;
        this.value = value;
        this.parameters = parameters;
    }
}

/* maybe ... */
class VEventDuration extends VEventValue {
}

class VEvent {
    constructor (properties = {}, components = []) {
        this.properties = properties;
        this.components = components;
        this.registered = [];
    }

    getProperty (key) {
        let e = this.properties[key];
        if (! e) return e;
        return e.value;
    }

    setProperty (key, value) {
        let e = this.properties[key];
        if (! e) {
            let type = (valid_input_types[key.toUpperCase()] || ['unknown'])[0]
            if (typeof type === typeof []) type = type[0];
            e = this.properties[key] = new VEventValue(type, value);
        } else {
            e.value = value;
        }
        for (let el of this.registered) {
            /* TODO update correct fields, allow component to redraw themselves */
            el.redraw(this);
        }
    }

    register (htmlNode) {
        this.registered.push(htmlNode);
    }
}

function make_vevent_value (value_tag) {
    /* TODO parameters */
    return new VEventValue (value_tag.tagName, make_vevent_value_ (value_tag));
}

function make_vevent_value_ (value_tag) {
    /* RFC6321 3.6. */
    switch (value_tag.tagName) {
    case 'binary':
        /* Base64 to binary
           Seems to handle inline whitespace, which xCal standard reqires
           */
        return atob(value_tag.innerHTML)
        break;

    case 'boolean':
        switch (value_tag.innerHTML) {
        case 'true':  return true;
        case 'false': return false;
        default:
            console.warn(`Bad boolean ${value_tag.innerHTML}, defaulting with !!`)
            return !! value_tag.innerHTML;
        }
        break;

    case 'time':
    case 'date':
    case 'date-time':
        return parseDate(value_tag.innerHTML);
        break;

    case 'duration':
        /* TODO duration parser here 'P1D' */
        return value_tag.innerHTML;
        break;

    case 'float':
    case 'integer':
        return +value_tag.innerHTML;
        break;

    case 'period':
        /* TODO has sub components, meaning that a string wont do */
        let start = value_tag.getElementsByTagName('start')[0]
        parseDate(start.innerHTML);
        let other;
        if ((other = value_tag.getElementsByTagName('end')[0])) {
            return parseDate(other.innerHTML)
        } else if ((other = value_tag.getElementsByTagName('duration')[0])) {
            /* TODO parse duration */
            return other.innerHTML
        } else {
            console.warn('Invalid end to period, defaulting to 1H');
            return new Date(3600);
        }

    case 'recur':
        /* TODO parse */
        return "";

    case 'uc-offset':
        /* TODO parse */
        return "";

    default:
        console.warn(`Unknown type '${value_tag.tagName}', defaulting to string`)
    case 'cal-address':
    case 'uri':
    case 'text':
        return value_tag.innerHTML;
    }
}


/* xml dom object -> class VEvent */
function xml_to_vcal (xml) {
    /* xml MUST have a VEVENT (or equivalent) as its root */
    let properties = xml.getElementsByTagName('properties')[0];
    let components = xml.getElementsByTagName('components')[0];

    let property_map = {}
    if (properties) {
        for (var i = 0; i < properties.childElementCount; i++) {
            let tag = properties.childNodes[i];
            let parameters = {};
            let value = [];
            for (var j = 0; j < tag.childElementCount; j++) {
                let child = tag.childNodes[j];
                switch (tag.tagName) {
                case 'parameters':
                    parameters = /* handle parameters */ {};
                    break;

                    /* These can contain multiple value tags, per
                       RFC6321 3.4.1.1. */
                case 'categories':
                case 'resources':
                case 'freebusy':
                case 'exdate':
                case 'rdate':
                    value.push(make_vevent_value(child));
                    break;
                default:
                    value = make_vevent_value(child);
                }
            }
            property_map[tag.tagName] = value;
        }
    }

    let component_list = []
    if (components) {
        for (let child of components.childNodes) {
            component_list.push(xml_to_vcal(child))
        }
    }

    return new VEvent(property_map, component_list)
}

const vcal_objects = {};

class ComponentVEvent extends HTMLElement {
    constructor () {
        super ();
        this.template = document.getElementById(this.tagName);

        /* 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 */
    }

    redraw (data) {
        // update ourselves from template

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

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

        for (let el of body.getElementsByClassName("bind")) {
            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);
    }

}

class ComponentDescription extends ComponentVEvent {
    constructor () {
        super() ;
    }

}

class ComponentEdit extends ComponentVEvent {
    constructor () {
        super();

        this.firstTime = true;
    }

    connectedCallback() {

        /* Edit tab is rendered here. It's left blank server-side, since
           it only makes sense to have something here if we have javascript */
        this.redraw(vcal_objects[this.dataset.uid]);

        for (let el of this.getElementsByClassName("interactive")) {
            el.addEventListener('input', () => {
                vcal_objects[this.dataset.uid].setProperty(
                    el.dataset.property,
                    el.value)
            });
        }
    }

    redraw (data) {
        // update ourselves from template

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

        let body;
        if (this.firstTime) {
            body = this.template.content.cloneNode(true).firstElementChild;
        } else {
            body = this;
        }

        for (let el of body.getElementsByClassName("interactive")) {
            let p = el.dataset.property;
            let d;
            if ((d = data.getProperty(p))) {
                /*
                  https://stackoverflow.com/questions/57157830/how-can-i-specify-the-sequence-of-running-nested-web-components-constructors
                */
                window.setTimeout (() => {
                    /* NOTE Some specific types might require special formatting
                    here. But due to my custom components implementing custom
                    `.value' procedures, we might not need any special cases
                    here */
                    el.value = d;
                });
            }
        }

        if (this.firstTime) {
            this.replaceChildren(body);
            this.firstTime = false;
        }
    }

}

class ComponentBlock extends ComponentVEvent {
    constructor () {
        super();
    }
}

window.addEventListener('load', function () {

    // let json_objects_el = document.getElementById('json-objects');
    let div = document.getElementById('xcal-data');
    let vevents = div.firstElementChild.childNodes;

    for (let vevent of vevents) {
        let ev = xml_to_vcal(vevent);
        vcal_objects[ev.getProperty('uid')] = ev
    }

    /*
      - .popup
      - .block
      - .list
     */
    let vevent_els = document.getElementsByClassName('vevent')
    for (let el of vevent_els) {
        try {
            vcal_objects[el.dataset.uid].register(el);
        } catch {
            console.error("Invalid something, uid = ", el.dataset.uid,
                          "el = ", el
                         );
        }
    }

    customElements.define('vevent-description', ComponentDescription);
    customElements.define('vevent-edit', ComponentEdit);
    customElements.define('vevent-block', ComponentBlock);
})



class DateTimeInput extends HTMLElement {
    constructor () {
        super();
        this.innerHTML = '<input type="date" /><input type="time" />'
    }

    static get observedAttributes () {
        return [ 'dateonly' ]
    }

    attributeChangedCallback (name, from, to) {
        console.log(this, name, boolean(from), boolean(to));
        switch (name) {
        case 'dateonly':
            this.querySelector('[type="time"]').disabled = boolean(to)
            break;
        }
    }

    get dateonly () {
        return boolean(this.getAttribute('dateonly'));
    }

    set dateonly (bool) {
        this.setAttribute ('dateonly', bool);
    }

    get value () {

        let dt;
        let date = this.querySelector("[type='date']").value;
        if (boolean(this.getAttribute('dateonly'))) {
            dt = parseDate(date);
            dt.type = 'date';
        } else {
            let time = this.querySelector("[type='time']").value;
            dt = parseDate(date + 'T' + time)
            dt.type = 'date-time';
        }
        return dt;
    }

    set value (new_value) {
        let date, time;
        if (new_value instanceof Date) {
            date = new_value.format("~L~Y-~m-~d");
            time = new_value.format("~L~H:~M:~S");
        } else {
            [date, time] = new_value.split('T')
        }
        this.querySelector("[type='date']").value = date;
        this.querySelector("[type='time']").value = time;
    }

    addEventListener(type, proc) {
        if (type != 'input') throw "Only input supported";

        this.querySelector("[type='date']").addEventListener(type, proc);
        this.querySelector("[type='time']").addEventListener(type, proc);
    }
}

customElements.define('date-time-input', DateTimeInput)

function wholeday_checkbox (box) {
    box.closest('.timeinput')
        .getElementsByTagName('date-time-input')
        .forEach(el => el.dateonly = box.checked);
}