aboutsummaryrefslogtreecommitdiff
path: root/static/server_connect.js
blob: 6c4e44960629b980f5b32fd0236c2191676c3d90 (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

async function remove_event (element) {
    let uid = element.querySelector("icalendar uid text").innerHTML;

    let data = new URLSearchParams();
    data.append('uid', uid);

    let response = await fetch ( '/remove', {
        method: 'POST',
        body: data
    });

    console.log(response);
    toggle_popup("popup" + element.id);

    if (response.status < 200 || response.status >= 300) {
        let body = await response.text();
        alert(`HTTP error ${response.status}\n${body}`)
    } else {
        element.remove();
    }
}

async function create_event (event) {

    // let xml = event.getElementsByTagName("icalendar")[0].outerHTML
    let calendar = event.properties.calendar;

    console.log(calendar/*, xml*/);

    let data = new URLSearchParams();
    data.append("cal", calendar);
    // data.append("data", xml);

    for (let prop of event.properties.ical_properties) {
        let v = event.properties[prop];
        if (v !== undefined) {
            [prop, {}, /*type*/, v];
            /* TODO , here */

            let type = 'text';
            let value;

            if (v instanceof Array) {
            } else if (v instanceof Date) {
                if (v.isWholeDay) {
                    type = 'date';
                    value = v.format("~Y-~m-~d");
                } else {
                    type = 'date-time';
                    /* TODO TZ */
                    value = v.format("~Y-~m~dT~H:~M:~S");
                }
            } else if (v === true || v === false) {
                type = 'boolean';
                value = v;
            } else if (typeof(v) == 'number') {
                /* TODO float or integer */
                type = 'integer';
                value = v;
            }
            /* TODO period */
            /* TODO recur */
            else {
                /* text types */
            }

        }
    }


    let jcal =
        ['vcalendar',
         ['vevent',
          [
              ['summary', {}, 'text', 'Example summary'],
          ],
          []
         ]
        ];

    let doc = jcal_to_xcal(jcal);
    console.log(doc);

    console.log(event.properties);

    return;

    let response = await fetch ( '/insert', {
        method: 'POST',
        body: data
    });

    console.log(response);
    if (response.status < 200 || response.status >= 300) {
        let body = await response.text();
        alert(`HTTP error ${response.status}\n${body}`)
        return;
    }

    let body = await response.text();

    /* server is assumed to return an XML document on the form
       <properties>
       **xcal property** ...
       </properties>
       parse that, and update our own vevent with the data.
    */

    let parser = new DOMParser();
    let properties = parser
        .parseFromString(body, 'text/xml')
        .children[0];

    let child;
    while ((child = properties.firstChild)) {
        let target = event.querySelector(
            "vevent properties " + child.tagName);
        if (target) {
            target.replaceWith(child);
        } else {
            event.querySelector("vevent properties")
                .appendChild(child);
        }
    }

    event.classList.remove("generated");
    toggle_popup("popup" + event.id);
}