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/formatters.ts | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 static/ts/formatters.ts (limited to 'static/ts/formatters.ts') diff --git a/static/ts/formatters.ts b/static/ts/formatters.ts new file mode 100644 index 00000000..e0018278 --- /dev/null +++ b/static/ts/formatters.ts @@ -0,0 +1,75 @@ +export { + format +} + +import { makeElement } from './lib' +import { VEvent } from './vevent' + +type formatter = (e: HTMLElement, d: VEvent, s: any) => Promise + +declare global { + interface Window { + formatters: Map; + } +} + +let formatters: Map; +formatters = window.formatters = new Map(); + +async function format(targetElement: HTMLElement, data: VEvent, key: string): Promise { + let d = data.getProperty(key); + if (!d) return + let formatter = formatters.get(key.toLowerCase()); + if (formatter) { + try { + await formatter(targetElement, data, d); + } catch (error) { + console.warn('Formatter failed') + console.warn(error); + formatters.get('default')!(targetElement, data, d); + } + } else { + formatters.get('default')!(targetElement, data, d); + } +} + +formatters.set('categories', async (el, _, d) => { + for (let item of d) { + let q = encodeURIComponent( + `(member "${item}" (or (prop event (quote CATEGORIES)) (quote ())))`) + el.appendChild(makeElement('a', { + textContent: item, + href: `/search/?q=${q}`, + })) + } +}) + +async function format_time_tag(el: HTMLElement, ev: VEvent, d: any): Promise { + if (el instanceof HTMLTimeElement) { + if (d instanceof Date) { + let fmt = ''; + if (!d.utc) { + fmt += '~L'; + } + fmt += '~Y-~m-~d' + if (!d.dateonly) { + fmt += 'T~H:~M:~S' + } + el.dateTime = d.format(fmt); + } + } + + formatters.get('default')!(el, ev, d); +} + +formatters.set('dtstart', format_time_tag) +formatters.set('dtend', format_time_tag) + +formatters.set('default', async (el, _, d) => { + let fmt; + if ((fmt = el.dataset.fmt)) { + el.textContent = d.format(fmt); + } else { + el.textContent = d; + } +}) -- 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/formatters.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'static/ts/formatters.ts') diff --git a/static/ts/formatters.ts b/static/ts/formatters.ts index e0018278..05f84d31 100644 --- a/static/ts/formatters.ts +++ b/static/ts/formatters.ts @@ -1,3 +1,19 @@ +/** + * Formatting procedures used by some components. + * + * // TODO can we have a backref of every node containing {@link formatters-proc}? + * + * {@label formatters} + * + * Each procedure takes three arguments. The HTML-element which contents + * should be replaced, the VEvent containing all data, and the target + * value, as returned by {@link VEvent.getProperty}. + * + * Also bound to the window object. + * + * @module + */ + export { format } @@ -16,6 +32,10 @@ declare global { let formatters: Map; formatters = window.formatters = new Map(); +/** + * Checks if a specific formatter exists for the given key, and executes it. + * Defaults to 'default', and also runs that if the regular formatter throws. + */ async function format(targetElement: HTMLElement, data: VEvent, key: string): Promise { let d = data.getProperty(key); if (!d) return -- cgit v1.2.3 From e753d721519f72014241b3d2fc804a919f655769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 7 Sep 2023 02:58:41 +0200 Subject: Document remaining javascript items. --- static/ts/formatters.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'static/ts/formatters.ts') diff --git a/static/ts/formatters.ts b/static/ts/formatters.ts index 05f84d31..b5c55913 100644 --- a/static/ts/formatters.ts +++ b/static/ts/formatters.ts @@ -1,13 +1,13 @@ /** * Formatting procedures used by some components. * - * // TODO can we have a backref of every node containing {@link formatters-proc}? + * // TODO can we have a backref of every node containing `{@link formatters-proc}`? * - * {@label formatters} + * {@label FORMATTERS} * * Each procedure takes three arguments. The HTML-element which contents * should be replaced, the VEvent containing all data, and the target - * value, as returned by {@link VEvent.getProperty}. + * value, as returned by {@linkcode vevent.VEvent.getProperty}. * * Also bound to the window object. * -- cgit v1.2.3