From 6c44e85b820902a4d7bb640324d5ed927e414bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 7 Jul 2022 20:29:11 +0200 Subject: Extend javascript formatters to also take VEvent. --- doc/ref/javascript/formatters.texi | 9 +++++---- static/components/vevent-description.ts | 4 ++-- static/formatters.ts | 15 +++++++++------ static/user/user-additions.js | 4 ++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/doc/ref/javascript/formatters.texi b/doc/ref/javascript/formatters.texi index 16a988c4..71394b39 100644 --- a/doc/ref/javascript/formatters.texi +++ b/doc/ref/javascript/formatters.texi @@ -4,13 +4,14 @@ Formatting procedures used by some components. @c TODO can we have a backref of every node containing @ref{formatters-proc}? -@deftypevar {Map void>} formatters +@deftypevar {Map void>} formatters @anchor{formatters-proc} -Each procedure takes two arguments. The HTML-element which contents -should be replaced, along with the target value, as returned by @ref{VEvent.getProperty}. +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 @ref{VEvent.getProperty}. @end deftypevar -@deftypevr {Window Value} {Map void>} formatters +@deftypevr {Window Value} {Map void>} formatters Same object as @xref{formatters-proc}. Provided for @xref{user-additions.js}. @end deftypevr diff --git a/static/components/vevent-description.ts b/static/components/vevent-description.ts index f0d224be..463725f1 100644 --- a/static/components/vevent-description.ts +++ b/static/components/vevent-description.ts @@ -28,8 +28,8 @@ class ComponentDescription extends ComponentVEvent { if ((d = data.getProperty(p))) { let key = p.toLowerCase(); let f = formatters.get(key); - if (f) f(el, d); - else window.formatters.get('default')!(el, d); + if (f) f(el, data, d); + else window.formatters.get('default')!(el, data, d); } } diff --git a/static/formatters.ts b/static/formatters.ts index 70f63504..5605e051 100644 --- a/static/formatters.ts +++ b/static/formatters.ts @@ -3,18 +3,21 @@ export { } import { makeElement } from './lib' +import { VEvent } from './vevent' + +type formatter = (e: HTMLElement, d: VEvent, s: any) => void declare global { interface Window { - formatters: Map void>; + formatters: Map; } } -let formatters: Map void>; +let formatters: Map; formatters = window.formatters = new Map(); -formatters.set('categories', (el, d) => { +formatters.set('categories', (el, _, d) => { for (let item of d) { let q = encodeURIComponent( `(member "${item}" (or (prop event (quote CATEGORIES)) (quote ())))`) @@ -25,7 +28,7 @@ formatters.set('categories', (el, d) => { } }) -function format_time_tag(el: HTMLElement, d: any): void { +function format_time_tag(el: HTMLElement, ev: VEvent, d: any): void { if (el instanceof HTMLTimeElement) { if (d instanceof Date) { let fmt = ''; @@ -40,13 +43,13 @@ function format_time_tag(el: HTMLElement, d: any): void { } } - formatters.get('default')!(el, d); + formatters.get('default')!(el, ev, d); } formatters.set('dtstart', format_time_tag) formatters.set('dtend', format_time_tag) -formatters.set('default', (el, d) => { +formatters.set('default', (el, _, d) => { let fmt; if ((fmt = el.dataset.fmt)) { el.textContent = d.format(fmt); diff --git a/static/user/user-additions.js b/static/user/user-additions.js index c0579df5..5f5357eb 100644 --- a/static/user/user-additions.js +++ b/static/user/user-additions.js @@ -1,4 +1,4 @@ -window.formatters.set('description', (el, d) => { +window.formatters.set('description', (el, ev, d) => { if (/<\/?\w+( +\w+(=["']?\w+["']?)?)* *\/?>/.exec(d)) { /* Assume that the text is HTML if it contains something which looks like an HTML tag */ @@ -40,7 +40,7 @@ window.salar = new Promise((resolve, reject) => ) -window.formatters.set('location', async function(el, d) { +window.formatters.set('location', async function(el, _, d) { let rx = /Lokal: (.*)/ let m = rx.exec(d) if (! m) { -- cgit v1.2.3