From 7949fcdc683d07689bad5da5d20bfa3eeb5a6a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 5 Sep 2023 01:25:00 +0200 Subject: Move frontend code to subdirectories, to simplify command line flags. --- static/ts/server_connect.ts | 133 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 static/ts/server_connect.ts (limited to 'static/ts/server_connect.ts') diff --git a/static/ts/server_connect.ts b/static/ts/server_connect.ts new file mode 100644 index 00000000..29f5bab2 --- /dev/null +++ b/static/ts/server_connect.ts @@ -0,0 +1,133 @@ +export { create_event, remove_event } + +import { jcal_to_xcal } from './jcal' +import { VEvent } from './vevent' +import { uid } from './types' +import { vcal_objects } from './globals' +import { PopupElement } from './components/popup-element' + +async function remove_event(uid: uid) { + let element = vcal_objects.get(uid); + if (!element) { + console.error(`No VEvent with that uid = '${uid}', giving up`) + return; + } + + let data = new URLSearchParams(); + data.append('uid', uid); + + let response = await fetch('/remove', { + method: 'POST', + body: data + }); + + console.log(response); + // toggle_popup(popup_from_event(element)); + + if (response.status < 200 || response.status >= 300) { + let body = await response.text(); + alert(`HTTP error ${response.status}\n${body}`) + } else { + /* Remove all HTML components which belong to this vevent */ + for (let component of element.registered) { + component.remove(); + } + /* remove the vevent from our global store, + hopefully also freeing it for garbace collection */ + vcal_objects.delete(uid); + } +} + +// function event_to_jcal(event) { +// /* encapsulate event in a shim calendar, to ensure that +// we always send correct stuff */ +// return ['vcalendar', +// [ +// /* +// 'prodid' and 'version' are technically both required (RFC 5545, +// 3.6 Calendar Components). +// */ +// ], +// [ +// /* vtimezone goes here */ +// event.properties.to_jcal() +// ] +// ]; +// } + +async function create_event(event: VEvent) { + + // let xml = event.getElementsByTagName("icalendar")[0].outerHTML + let calendar = event.calendar; + if (!calendar) { + console.error("Can't create event without calendar") + return; + } + + console.log('calendar =', atob(calendar)/*, xml*/); + + let data = new URLSearchParams(); + data.append("cal", calendar); + // data.append("data", xml); + + // console.log(event); + + let jcal = event.to_jcal(); + // console.log(jcal); + + let doc: Document = jcal_to_xcal(jcal); + // console.log(doc); + let str = doc.documentElement.outerHTML; + console.log(str); + data.append("data", str); + + // console.log(event.properties); + + 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; + } + + /* response from here on is good */ + + // let body = await response.text(); + + /* server is assumed to return an XML document on the form + + **xcal property** ... + + parse that, and update our own vevent with the data. + */ + + // let parser = new DOMParser(); + // let return_properties = parser + // .parseFromString(body, 'text/xml') + // .children[0]; + + // let child; + // while ((child = return_properties.firstChild)) { + // let target = event.querySelector( + // "vevent properties " + child.tagName); + // if (target) { + // target.replaceWith(child); + // } else { + // event.querySelector("vevent properties") + // .appendChild(child); + // } + // } + + for (let r of event.registered) { + r.classList.remove('generated'); + if (r instanceof PopupElement) { + console.log(r); + r.removeAttribute('visible'); + } + } +} -- cgit v1.2.3 From f653a01328be3b8be6af35c0c96867623765ca5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 5 Sep 2023 11:41:46 +0200 Subject: Move JS documentation into the JS-code. Texinfo was a bad match for how TypeScript is structured. This also allows generation of jsdoc pages, which can be nice. Another large win is that this opens up for the texinfo pages to replace the Guile heading with different subheadings, including - external library - internal library - C library - ... --- static/ts/server_connect.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'static/ts/server_connect.ts') diff --git a/static/ts/server_connect.ts b/static/ts/server_connect.ts index 29f5bab2..ad4bc9eb 100644 --- a/static/ts/server_connect.ts +++ b/static/ts/server_connect.ts @@ -1,3 +1,9 @@ +/** + * Procedures for interfacing with the backend server. + * + * @module + */ + export { create_event, remove_event } import { jcal_to_xcal } from './jcal' @@ -6,6 +12,15 @@ import { uid } from './types' import { vcal_objects } from './globals' import { PopupElement } from './components/popup-element' +/** + * Requests that the server permanently remove the event with the given + * unique id from its persistant storage. + * + * If the server responds with a success also delete it from our local + * store (`vcal_objects`). + * + * // TODO link to our backend flow here +*/ async function remove_event(uid: uid) { let element = vcal_objects.get(uid); if (!element) { @@ -55,6 +70,13 @@ async function remove_event(uid: uid) { // ]; // } +/** + * Packs up the given event and sends it to the server to either be + * created, or simply be updated in the persistant database. + + * Also does some minor updates registered components, to show that the + * event is actually created. +*/ async function create_event(event: VEvent) { // let xml = event.getElementsByTagName("icalendar")[0].outerHTML -- cgit v1.2.3