From c60a60422f69e29628b6c946a15be271e90015aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 10 Nov 2021 00:47:10 +0100 Subject: Basic event modification works again. --- static/globals.ts | 10 ++++++++++ static/jcal.ts | 2 +- static/server_connect.ts | 15 ++++++++++----- static/types.ts | 6 +++++- static/vevent.ts | 15 +++++++++------ 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/static/globals.ts b/static/globals.ts index daf5a2f7..5187d007 100644 --- a/static/globals.ts +++ b/static/globals.ts @@ -8,6 +8,7 @@ import { close_popup, toggle_popup } from './popup' import { VEvent, xml_to_vcal } from './vevent' import { bind_popup_control } from './dragable' import { uid, parseDate, gensym, to_local, boolean, makeElement } from './lib' +import { create_event } from './server_connect' const vcal_objects: Map = new Map; (window as any).vcal_objects = vcal_objects; @@ -188,6 +189,15 @@ class ComponentEdit extends ComponentVEvent { el.value) }); } + + let submit = this.querySelector('form') as HTMLFormElement + submit.addEventListener('submit', (e) => { + console.log(submit, e); + create_event(vcal_objects.get(this.uid)!); + + e.preventDefault(); + return false; + }); } redraw(data: VEvent) { diff --git a/static/jcal.ts b/static/jcal.ts index 327a560d..59ee93b4 100644 --- a/static/jcal.ts +++ b/static/jcal.ts @@ -82,7 +82,7 @@ function jcal_property_to_xcal_property( let el = doc.createElementNS(xcal, key); - for (let v of asList(params[key])) { + for (let v of asList(params.get(key))) { let text = doc.createElementNS(xcal, 'text'); text.textContent = '' + v; el.appendChild(text); diff --git a/static/server_connect.ts b/static/server_connect.ts index 96f6e872..594e46de 100644 --- a/static/server_connect.ts +++ b/static/server_connect.ts @@ -1,3 +1,5 @@ +export { create_event } + import { jcal_to_xcal } from './jcal' import { VEvent } from './vevent' @@ -49,7 +51,11 @@ async function remove_event(element: Element): void { async function create_event(event: VEvent) { // let xml = event.getElementsByTagName("icalendar")[0].outerHTML - let calendar = event.getProperty('x-hnh-calendar-name'); + let calendar = event._calendar; + if (!calendar) { + console.error("Can't create event without calendar") + return; + } console.log('calendar=', calendar/*, xml*/); @@ -57,20 +63,19 @@ async function create_event(event: VEvent) { data.append("cal", calendar); // data.append("data", xml); - console.log(event); + // console.log(event); let jcal = event.to_jcal(); + // console.log(jcal); let doc: Document = jcal_to_xcal(jcal); - console.log(doc); + // console.log(doc); let str = doc.documentElement.outerHTML; console.log(str); data.append("data", str); // console.log(event.properties); - // return; - let response = await fetch('/insert', { method: 'POST', body: data diff --git a/static/types.ts b/static/types.ts index 1cf77f51..6d1331c7 100644 --- a/static/types.ts +++ b/static/types.ts @@ -181,6 +181,10 @@ let valid_input_types: Map = type tagname = 'vevent' | string -type JCalProperty = [string, any, ical_type, any[]] +/* TODO is this type correct? + What really are valid values for any? Does that depend on ical_type? Why is the tail a list? + What really is the type for the parameter map? +*/ +type JCalProperty = [string, Map, ical_type, any[]] type JCal = [tagname, JCalProperty[], JCal[]] diff --git a/static/vevent.ts b/static/vevent.ts index c9068106..d8ef58ce 100644 --- a/static/vevent.ts +++ b/static/vevent.ts @@ -1,4 +1,4 @@ -import { ical_type, valid_input_types, JCal } from './types' +import { ical_type, valid_input_types, JCal, JCalProperty } from './types' import { uid, parseDate } from './lib' export { VEvent, xml_to_vcal } @@ -141,11 +141,14 @@ class VEvent { } to_jcal(): JCal { - let out_properties = [] - for (let [key, value] of Object.entries(this.properties)) { - let sub = value.to_jcal(); - sub.unshift(key) - out_properties.push(sub); + let out_properties: JCalProperty[] = [] + console.log(this.properties); + for (let [key, value] of this.properties) { + let prop: JCalProperty = [ + key.toLowerCase(), + ...value.to_jcal(), + ] + out_properties.push(prop); } return ['vevent', out_properties, [/* alarms go here*/]] } -- cgit v1.2.3