aboutsummaryrefslogtreecommitdiff
path: root/static/vcal.js
blob: f86d53de070eb983e23e4bd2bc7094bc39dce7d1 (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
/*
  Properties are icalendar properties.

  p['name'] to get and set value (also updates any connected slots)

  p['_value_name'] for raw value
  p['_slot_name'] for connected slots, Vector of pairs, where the
                  car should be a reference to the slot, and the
                  cdr a procedure which takes a slot and a value
                  and binds the value to the slot.
 */
class VComponent {

    constructor(el, wide_event=false) {
        el.properties = this;
        this.html_element = el;

        /*
          List of field listeners, which are all notified whenever
          the listened for field is updated.
          - keys are field names
          - values MUST be a pair of
            + a javascript object to update
            + a prodecude taking that object, and the new value
         */
        this._slots = {}

        /* VCalParameter objects */
        this._values = {}

        /*
          All properties on this object which are part of the vcomponent.
          Ideally simply looping through all javascript fields would be nice,
          but we only want to export some.

          Popuplated by iCalendars built in types per default, and extended
         */
        this.ical_properties = new Set();

        let popup = popup_from_event(el);
        // let children = el.getElementsByTagName("properties")[0].children;

        /* actual component (not popup) */
        /*
          for (let e of el.querySelectorAll(".bind")) {
          }
        */

        /* bind_recur */

        /* primary display tab */

        let p;
        let lst = [...popup.querySelectorAll(".bind"),
                   ...el.querySelectorAll('.bind')];
        for (let e of lst) {
            // if (e.classList.contains('summary')) {
            //     console.log(e, e.closest('[data-bindby]'));
            // }
            if ((p = e.closest('[data-bindby]'))) {
                // console.log(p);
                // console.log(p.dataset.bindby);
                eval(p.dataset.bindby)(el, e);
            } else {
                // if (e.classList.contains('summary')) {
                //     /* TODO transfer data from backend to frontend in a better manner */
                //     console.log (this.get_callback_list(e.dataset.property));
                // }
                let f = (s, v) => {
                    console.log(s, v);
                    s.innerText = v.format(s.dataset && s.dataset.fmt);
                };
                this.get_callback_list(e.dataset.property).push([e, f]);
                // if (e.classList.contains('summary')) {
                //     console.log (this.get_callback_list(e.dataset.property));
                // }
                // console.log("registreing", e, e.dataset.property, this);
            }
        }

        /* checkbox for whole day */

        /* Popuplate default types, see types.js for property_names */
        for (let property of property_names) {
            this.ical_properties.add(property)
            // console.log("prop", property)

            this.create_property(property);
        }

        /* icalendar properties */
        for (let child of el.querySelector("vevent > properties").children) {
            /* child ≡ <dtstart><date-time>...</date-time></dtstart> */

            let field = child.tagName;
            // // let lst = get_property(el, field);
            // let lst = this.get(field);

            this.ical_properties.add(field)

            /* Bind vcomponent fields for this event */
            for (let s of el.querySelectorAll(`${field} > :not(parameters)`)) {
                /* s ≡ <date-time>...</date-time> */

                /* Binds value from XML-tree to javascript object
                   [parsedate]

                   TODO capture xcal type here, to enable us to output it to jcal later.
                */
                let parsedValue;
                let type = s.tagName.toLowerCase();
                switch (type) {
                case 'float':
                case 'integer':
                    parsedValue = Number(s.textContent);
                    break;

                case 'date-time':
                case 'date':
                    parsedValue = parseDate(s.textContent);
                    break;

                    /* TODO */
                case 'duration':
                    let start = s.getElementsByTagName('start');
                    let end = s.getElementsByTagName('end, duration');
                    if (end.tagName === 'period') {
                        parsePeriod(end.textContent);
                    }
                    break;
                    /* TODO */
                case 'period':
                    parsedValue = parsePeriod(s.textContent);
                    break;
                    /* TODO */
                case 'utc-offset':
                    break;

                case 'recur':
                    parsedValue = recur_xml_to_rrule(s);
                    break;

                case 'boolean':
                    switch (s.textContent) {
                    case 'true':  parsedValue = true; break;
                    case 'false': parsedValue = false; break;
                    default: throw "Value error"
                    }
                    break;


                case 'binary':
                    /* Binary is going to be BASE64 decoded, allowing us to ignore
                       it and handle it as a string for the time being */
                case 'cal-address':
                case 'text':
                case 'uri':
                    parsedValue = s.textContent;
                    // parsedValue.type = type;
                    break;

                default:
                    parsedValue = s.textContent;
                }


                // this['_value_rrule'] = new VCalParameter(type, parsedValue);
                // console.log("set", field, type, parsedValue);
                this._values[field] = new VCalParameter(type, parsedValue);
                if (! this._slots[field]) {
                    this._slots[field] = [];
                }
            }
        }

        /* set up graphical display changes */
        let container = el.closest(".event-container");
        if (container === null) {
            console.log("No enclosing event container for", el);
            return;
        }
        let start = parseDate(container.dataset.start);
        let end = parseDate(container.dataset.end);

        if (this.dtstart) {
            /* [parsedate] */
            // el.properties.dtstart = parseDate(el.properties.dtstart);
            this.get_callback_list('dtstart').push(
                [el.style, (s, v) => {
                    console.log(v);
                    s[wide_event?'left':'top'] = 100 * (to_local(v) - start)/(end - start) + "%";
                } ]);
        }


        if (this.dtend) {
            // el.properties.dtend = parseDate(el.properties.dtend);
            this.get_callback_list('dtend').push(
                // TODO right and bottom only works if used from the start. However,
                // events from the backend instead use top/left and width/height.
                // Normalize so all use the same, or find a way to convert between.
                [el.style,
                 (s, v) => s[wide_event?'right':'bottom'] = 100 * (1 - (to_local(v)-start)/(end-start)) + "%"]);
        }


        /* ---------- Calendar ------------------------------ */

        if (! el.dataset.calendar) {
            el.dataset.calendar = "Unknown";
        }

        let calprop = this.get_callback_list('calendar');
        this.create_property('calendar');
        this._values['calendar'] =
            new VCalParameter('INVALID', el.dataset.calendar);

        const rplcs = (s, v) => {
            let [_, calclass] = s.classList.find(/^CAL_/);
            s.classList.replace(calclass, "CAL_" + v);
        }

        calprop.push([popup, rplcs]);
        calprop.push([el, rplcs]);
        calprop.push([el, (s, v) => s.dataset.calendar = v]);



        /* ---------- /Calendar ------------------------------ */
    }


    /*
      Returns the _value_ slot of given field in event, creating it if needed .
      el - the event to work on
      field - name of the field
      default_value - default value when creating
      bind_to_ical - should this property be added to the icalendar subtree?
    */
    get_callback_list(field) {
        // let el = this.html_element;
        if (! this._slots[field]) {
            this._slots[field] = [];
        }

        // console.log("get", field);
        return this._slots[field];
    }

    to_jcal() {
        let properties = [];

        /* ??? */
        // for (let prop of event.properties.ical_properties) {
        for (let prop of this.ical_properties) {
            // console.log(prop);
            let v = this[prop];
            if (v !== undefined) {
                let sub = v.to_jcal();
                sub.unshift(prop);
                properties.push(sub);
            }
        }

        return ['vevent', properties, [/* alarms go here */]]
    }

    create_property(property_name) {
        Object.defineProperty(
            this, property_name,
            {
                /* TODO there is an assymetry here with .value needing to be called for
                   get:ed stuff, but set MUST be an unwrapped item.
                   Fix this.
                   */
                get: function() {
                    return this._values[property_name];
                },
                set: function (value) {
                    console.log("set", property_name, value);
                    this._values[property_name].value = value;
                    console.log(this._slots[property_name].length,
                                this._slots[property_name]);
                    /* TODO validate type */
                    /* See valid_input_types and all_types */
                    for (let [slot,updater] of this._slots[property_name]) {
                        // console.log(updater, slot);
                        updater(slot, value);
                    }
                },
            });
    }

}



/* "Body" of a vcomponent field.
   For example, given the JCal
   ["dtstamp", {}, "date-time", "2006-02-06T00:11:21Z"],
   this class would have
   VCalParameter {
       type = "date-time",
       properties = {},
       _value = new Date(2006,1,6,0,11,21)
    }
    And returns [{}, "date-time", "2006-02-06T00:11:21Z"]
    when serialized
   */
class VCalParameter {
    constructor (type, value, properties={}) {
        this.type = type;
        this._value = value;
        this.properties = properties;
    }

    get value() {
        return this._value;
    }

    set value(v) {
        this._value = v;
    }

    to_jcal() {
        let value;
        let v = this._value;
        switch (this.type) {
        case 'binary':
            /* TOOD */
            break;
        case 'date-time':
            value = v.format("~Y-~m-~dT~H:~M:~S");
            // TODO TZ
            break;
        case 'date':
            value = v.format("~Y-~m-~d");
            break;
        case 'duration':
            /* TODO */
            break;
        case 'period':
            /* TODO */
            break;
        case 'utc-offset':
            /* TODO */
            break;
        case 'recur':
            value = v.asJcal();
            break;

        case 'float':
        case 'integer':
        case 'text':
        case 'uri':
        case 'cal-address':
        case 'boolean':
            value = v;
        }
        return [this.properties, this.type, value];
    }
}