15
|
1 import { addHours, endOfToday, endOfTomorrow, format, isAfter, isWithinInterval, parseISO, startOfToday } from "date-fns";
|
|
2 import { TemplateResult, html } from "lit";
|
|
3 import { DataFactory, NamedNode, Quad_Subject, Store, Term } from "n3";
|
|
4 import { getLiteral } from "./parseRdf";
|
|
5 import { hideFeeds, hideTitles } from "./private";
|
|
6 const EV = "http://bigasterisk.com/event#";
|
|
7 const { namedNode } = DataFactory;
|
|
8
|
|
9 export class DisplayEvent {
|
|
10 constructor(private store: Store, private graph: Term, public uri: Quad_Subject) {}
|
|
11 get title(): string {
|
|
12 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "title"), "(unnamed)");
|
|
13 }
|
|
14 get start(): string {
|
|
15 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "start"), null);
|
|
16 }
|
|
17 get feed(): NamedNode {
|
|
18 return namedNode(getLiteral(this.store, this.graph, this.uri, namedNode(EV + "feed"), null));
|
|
19 }
|
|
20 shortDate(): TemplateResult {
|
|
21 const t = parseISO(this.start);
|
|
22 return html`<span class="d">${format(t, "EEE, MMM d,")}</span> <span class="t">${format(t, "HH:mm")}</span>`;
|
|
23 }
|
|
24 inHowLong(): TemplateResult {
|
|
25 // returns start()-now, like '5 days'
|
|
26 const t = parseISO(this.start).valueOf();
|
|
27 const now = Date.now();
|
|
28 const daysAway = (t - now) / 1000 / 86400;
|
|
29 const prec = daysAway < 2 ? 1 : 0;
|
|
30 const cls = "until " + (daysAway < 2 ? "until-2d" : daysAway < 7 ? "until-7d" : daysAway < 30 ? "until-1mo" : "");
|
|
31 return html`<span class="${cls}">${daysAway.toFixed(prec)} days</span>`;
|
|
32 }
|
|
33 show(): boolean {
|
|
34 const now = new Date();
|
|
35 const t = parseISO(this.start);
|
|
36
|
|
37 const start = startOfToday();
|
|
38 let end = endOfToday();
|
|
39 if (isAfter(now, addHours(startOfToday(), 18))) {
|
|
40 end = endOfTomorrow();
|
|
41 }
|
|
42
|
|
43 return isWithinInterval(t, { start, end }) && !hideTitles.has(this.title) && !hideFeeds.has(this.feed.value);
|
|
44 }
|
|
45 }
|