From c6c65f9e8273a5bc1b2ac1155d66003d2b98591c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 4 Oct 2021 17:40:59 +0200 Subject: {.js => .ts} on relavant files. --- static/jcal.ts | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 static/jcal.ts (limited to 'static/jcal.ts') diff --git a/static/jcal.ts b/static/jcal.ts new file mode 100644 index 00000000..003294d1 --- /dev/null +++ b/static/jcal.ts @@ -0,0 +1,174 @@ +function jcal_type_to_xcal(doc, type, value) { + let el = doc.createElementNS(xcal, type); + switch (type) { + case 'boolean': + el.textContent = value ? "true" : "false"; + break; + + case 'float': + case 'integer': + el.textContent = '' + value; + break; + + case 'period': + let [start, end] = value; + let startEl = doc.createElementNS(xcal, 'start'); + startEl.textContent = start; + let endEL; + if (end.find('P')) { + endEl = doc.createElementNS(xcal, 'duration'); + } else { + endEl = doc.createElementNS(xcal, 'end'); + } + endEl.textContent = end; + el.appendChild(startEl); + el.appendChild(endEl); + break; + + case 'recur': + for (var key in value) { + if (! value.hasOwnProperty(key)) continue; + let e = doc.createElementNS(xcal, key); + e.textContent = value[key]; + el.appendChild(e); + } + break; + + case 'date': + case 'time': + case 'date-time': + + case 'duration': + + case 'binary': + case 'text': + case 'uri': + case 'cal-address': + case 'utc-offset': + el.textContent = value; + break; + + default: + /* TODO error */ + } + return el; +} + +function jcal_property_to_xcal_property(doc, jcal) { + let [propertyName, params, type, ...values] = jcal; + + let tag = doc.createElementNS(xcal, propertyName); + + /* setup parameters */ + let paramEl = doc.createElementNS(xcal, 'params'); + for (var key in params) { + /* Check if the key actually belongs to us. + At least (my) format also appears when iterating + over the parameters. Probably a case of builtins + vs user defined. + + This is also the reason we can't check if params + is empty beforehand, and instead check the + number of children of paramEl below. + */ + if (! params.hasOwnProperty(key)) continue; + + let el = doc.createElementNS(xcal, key); + + for (let v of asList(params[key])) { + let text = doc.createElementNS(xcal, 'text'); + text.textContent = '' + v; + el.appendChild(text); + } + + paramEl.appendChild(el); + } + + if (paramEl.childCount > 0) { + tag.appendChild(paramEl); + } + + /* setup value (and type) */ + // let typeEl = doc.createElementNS(xcal, type); + + switch (propertyName) { + case 'geo': + if (type == 'float') { + // assert values[0] == [x, y] + let [x, y] = values[0]; + let lat = doc.createElementNS(xcal, 'latitude') + let lon = doc.createElementNS(xcal, 'longitude') + lat.textContent = x; + lon.textContent = y; + tag.appendChild(lat); + tag.appendChild(lon); + } else { + /* TODO, error */ + } + break; + case 'request-status': + if (type == 'text') { + // assert values[0] instanceof Array + let [code, desc, ...data] = values[0]; + let codeEl = doc.createElementNS(xcal, 'code') + code.textContent = code; + tag.appendChild(codeEl); + + + let descEl = doc.createElementNS(xcal, 'description') + desc.textContent = desc; + tag.appendChild(descEl); + + if (data !== []) { + data = data[0]; + let dataEl = doc.createElementNS(xcal, 'data') + data.textContent = data; + tag.appendChild(dataEl); + } + } else { + /* TODO, error */ + } + break; + default: + for (let value of values) { + tag.appendChild(jcal_type_to_xcal(doc, type, value)) + } + } + + return tag; +} + + +function jcal_to_xcal(...jcals) { + let doc = document.implementation.createDocument(xcal, 'icalendar'); + for (let jcal of jcals) { + doc.documentElement.appendChild(jcal_to_xcal_inner(doc, jcal)); + } + return doc; +} + +function jcal_to_xcal_inner(doc, jcal) { + let [tagname, properties, components] = jcal; + + let xcal_tag = doc.createElementNS(xcal, tagname); + + /* I'm not sure if the properties and components tag should be left out + when empty. It should however NOT be an error to leave them in. + */ + + let xcal_properties = doc.createElementNS(xcal, 'properties'); + for (let property of properties) { + xcal_properties.appendChild(jcal_property_to_xcal_property(doc, property)); + } + + let xcal_children = doc.createElementNS(xcal, 'components'); + for (let child of components) { + xcal_children.appendChild(jcal_to_xcal_inner(doc, child)); + } + + xcal_tag.appendChild(xcal_properties); + xcal_tag.appendChild(xcal_children); + + return xcal_tag; + +} -- cgit v1.2.3 From 8ec2f441d40ab89b40cc3158f65c914eff497cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 4 Oct 2021 23:18:24 +0200 Subject: Major typescript work. --- static/jcal.ts | 143 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 74 insertions(+), 69 deletions(-) (limited to 'static/jcal.ts') diff --git a/static/jcal.ts b/static/jcal.ts index 003294d1..db833a3c 100644 --- a/static/jcal.ts +++ b/static/jcal.ts @@ -1,60 +1,63 @@ -function jcal_type_to_xcal(doc, type, value) { +function jcal_type_to_xcal(doc: Document, type: ical_type, value: any): Element { let el = doc.createElementNS(xcal, type); switch (type) { - case 'boolean': - el.textContent = value ? "true" : "false"; - break; - - case 'float': - case 'integer': - el.textContent = '' + value; - break; - - case 'period': - let [start, end] = value; - let startEl = doc.createElementNS(xcal, 'start'); - startEl.textContent = start; - let endEL; - if (end.find('P')) { - endEl = doc.createElementNS(xcal, 'duration'); - } else { - endEl = doc.createElementNS(xcal, 'end'); - } - endEl.textContent = end; - el.appendChild(startEl); - el.appendChild(endEl); - break; - - case 'recur': - for (var key in value) { - if (! value.hasOwnProperty(key)) continue; - let e = doc.createElementNS(xcal, key); - e.textContent = value[key]; - el.appendChild(e); - } - break; + case 'boolean': + el.textContent = value ? "true" : "false"; + break; + + case 'float': + case 'integer': + el.textContent = '' + value; + break; + + case 'period': + let [start, end] = value; + let startEl = doc.createElementNS(xcal, 'start'); + startEl.textContent = start; + let endEl: Element; + if (end.find('P')) { + endEl = doc.createElementNS(xcal, 'duration'); + } else { + endEl = doc.createElementNS(xcal, 'end'); + } + endEl.textContent = end; + el.appendChild(startEl); + el.appendChild(endEl); + break; + + case 'recur': + for (var key in value) { + if (!value.hasOwnProperty(key)) continue; + let e = doc.createElementNS(xcal, key); + e.textContent = value[key]; + el.appendChild(e); + } + break; - case 'date': - case 'time': - case 'date-time': + case 'date': + // case 'time': + case 'date-time': - case 'duration': + case 'duration': - case 'binary': - case 'text': - case 'uri': - case 'cal-address': - case 'utc-offset': - el.textContent = value; - break; + case 'binary': + case 'text': + case 'uri': + case 'cal-address': + case 'utc-offset': + el.textContent = value; + break; - default: + default: /* TODO error */ } return el; } -function jcal_property_to_xcal_property(doc, jcal) { +function jcal_property_to_xcal_property( + doc: Document, + jcal: JCalProperty +): Element { let [propertyName, params, type, ...values] = jcal; let tag = doc.createElementNS(xcal, propertyName); @@ -71,7 +74,7 @@ function jcal_property_to_xcal_property(doc, jcal) { is empty beforehand, and instead check the number of children of paramEl below. */ - if (! params.hasOwnProperty(key)) continue; + if (!params.hasOwnProperty(key)) continue; let el = doc.createElementNS(xcal, key); @@ -84,7 +87,7 @@ function jcal_property_to_xcal_property(doc, jcal) { paramEl.appendChild(el); } - if (paramEl.childCount > 0) { + if (paramEl.childElementCount > 0) { tag.appendChild(paramEl); } @@ -92,20 +95,21 @@ function jcal_property_to_xcal_property(doc, jcal) { // let typeEl = doc.createElementNS(xcal, type); switch (propertyName) { - case 'geo': - if (type == 'float') { - // assert values[0] == [x, y] - let [x, y] = values[0]; - let lat = doc.createElementNS(xcal, 'latitude') - let lon = doc.createElementNS(xcal, 'longitude') - lat.textContent = x; - lon.textContent = y; - tag.appendChild(lat); - tag.appendChild(lon); - } else { - /* TODO, error */ - } - break; + case 'geo': + if (type == 'float') { + // assert values[0] == [x, y] + let [x, y] = values[0]; + let lat = doc.createElementNS(xcal, 'latitude') + let lon = doc.createElementNS(xcal, 'longitude') + lat.textContent = x; + lon.textContent = y; + tag.appendChild(lat); + tag.appendChild(lon); + } else { + /* TODO, error */ + } + break; + /* TODO reenable this case 'request-status': if (type == 'text') { // assert values[0] instanceof Array @@ -126,20 +130,21 @@ function jcal_property_to_xcal_property(doc, jcal) { tag.appendChild(dataEl); } } else { - /* TODO, error */ + /* TODO, error * / } break; - default: - for (let value of values) { - tag.appendChild(jcal_type_to_xcal(doc, type, value)) - } + */ + default: + for (let value of values) { + tag.appendChild(jcal_type_to_xcal(doc, type, value)) + } } return tag; } -function jcal_to_xcal(...jcals) { +function jcal_to_xcal(...jcals: JCal[]): Document { let doc = document.implementation.createDocument(xcal, 'icalendar'); for (let jcal of jcals) { doc.documentElement.appendChild(jcal_to_xcal_inner(doc, jcal)); @@ -147,7 +152,7 @@ function jcal_to_xcal(...jcals) { return doc; } -function jcal_to_xcal_inner(doc, jcal) { +function jcal_to_xcal_inner(doc: Document, jcal: JCal) { let [tagname, properties, components] = jcal; let xcal_tag = doc.createElementNS(xcal, tagname); -- cgit v1.2.3 From 2d0ec2b162e3e2851fef7f280aab21c9f00cd171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 31 Oct 2021 20:48:23 +0100 Subject: Everything but lib. --- static/jcal.ts | 3 +++ 1 file changed, 3 insertions(+) (limited to 'static/jcal.ts') diff --git a/static/jcal.ts b/static/jcal.ts index db833a3c..51a1130b 100644 --- a/static/jcal.ts +++ b/static/jcal.ts @@ -1,3 +1,6 @@ +export { jcal_to_xcal } +import { ical_type, JCalProperty, JCal } from './types' + function jcal_type_to_xcal(doc: Document, type: ical_type, value: any): Element { let el = doc.createElementNS(xcal, type); switch (type) { -- cgit v1.2.3 From 0712c416259e4860ff1910c4a5bcd7b37da6b237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 31 Oct 2021 21:18:37 +0100 Subject: lib. --- static/jcal.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'static/jcal.ts') diff --git a/static/jcal.ts b/static/jcal.ts index 51a1130b..327a560d 100644 --- a/static/jcal.ts +++ b/static/jcal.ts @@ -1,5 +1,6 @@ export { jcal_to_xcal } import { ical_type, JCalProperty, JCal } from './types' +import { xcal, asList } from './lib' function jcal_type_to_xcal(doc: Document, type: ical_type, value: any): Element { let el = doc.createElementNS(xcal, type); -- cgit v1.2.3 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/jcal.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'static/jcal.ts') 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); -- cgit v1.2.3 From 410404cfdd54c083b6609fd52334e02d320145d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 10 Nov 2021 01:40:22 +0100 Subject: Re-modularize javascript. This moves almost everything out of globals.ts, into sepparate files. Things are still slightly to tightly coupled. But that is worked on. --- static/jcal.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'static/jcal.ts') diff --git a/static/jcal.ts b/static/jcal.ts index 59ee93b4..605f41e7 100644 --- a/static/jcal.ts +++ b/static/jcal.ts @@ -1,6 +1,7 @@ export { jcal_to_xcal } -import { ical_type, JCalProperty, JCal } from './types' -import { xcal, asList } from './lib' + +import { xcal, ical_type, JCalProperty, JCal } from './types' +import { asList } from './lib' function jcal_type_to_xcal(doc: Document, type: ical_type, value: any): Element { let el = doc.createElementNS(xcal, type); -- cgit v1.2.3 From 15e50471776e702333920b188932f03ee1f8573b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 9 Dec 2021 19:17:46 +0100 Subject: Propagate recurring events to frontend. This handles each instance of a recurring event as its own unique event, which allows us to properly send it to the frontend. It's currently not possible to submit the repeating events back, but that is probably a underlying problem. --- static/jcal.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'static/jcal.ts') diff --git a/static/jcal.ts b/static/jcal.ts index 605f41e7..41f33db4 100644 --- a/static/jcal.ts +++ b/static/jcal.ts @@ -33,9 +33,17 @@ function jcal_type_to_xcal(doc: Document, type: ical_type, value: any): Element case 'recur': for (var key in value) { if (!value.hasOwnProperty(key)) continue; - let e = doc.createElementNS(xcal, key); - e.textContent = value[key]; - el.appendChild(e); + if (key === 'byday') { + for (let v of value[key]) { + let e = doc.createElementNS(xcal, key); + e.textContent = v; + el.appendChild(e); + } + } else { + let e = doc.createElementNS(xcal, key); + e.textContent = value[key]; + el.appendChild(e); + } } break; -- cgit v1.2.3