2
|
1 import { addHours, endOfToday, endOfTomorrow, format, isAfter, isBefore, isToday, isTomorrow, isWithinInterval, parseISO, startOfToday } from "date-fns";
|
|
2 import { css, html, LitElement, TemplateResult } from "lit";
|
|
3 import { customElement, property } from "lit/decorators.js";
|
|
4 import { sortBy } from "lodash";
|
|
5 import { DataFactory, NamedNode, Parser, Quad_Predicate, Quad_Subject, Store, Term } from "n3";
|
|
6 import { hideFeeds, hideTitles } from "./private";
|
|
7 import { shared } from "./shared";
|
|
8 const { namedNode } = DataFactory;
|
|
9 export { WeekGuide } from "./WeekGuide";
|
|
10 const EV = "http://bigasterisk.com/event#";
|
|
11 const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
|
12
|
|
13 function getLiteral(store: Store, graph: Term, subj: Quad_Subject, pred: Quad_Predicate, missing: string | null): string {
|
|
14 let out = null;
|
|
15 store.getObjects(subj, pred, graph).forEach((attr) => {
|
|
16 out = attr.value;
|
|
17 });
|
|
18 if (!out) {
|
|
19 if (missing === null) {
|
|
20 throw new Error();
|
|
21 }
|
|
22 return missing;
|
|
23 }
|
|
24 return out;
|
|
25 }
|
|
26
|
|
27 class DisplayEvent {
|
|
28 constructor(private store: Store, private graph: Term, public uri: Quad_Subject) {}
|
|
29 get title(): string {
|
|
30 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "title"), "(unnamed)");
|
|
31 }
|
|
32 get start(): string {
|
|
33 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "start"), null);
|
|
34 }
|
|
35 get feed(): NamedNode {
|
|
36 return namedNode(getLiteral(this.store, this.graph, this.uri, namedNode(EV + "feed"), null));
|
|
37 }
|
|
38 shortDate(): TemplateResult {
|
|
39 const t = parseISO(this.start);
|
|
40 return html`<span class="d">${format(t, "EEE, MMM d,")}</span> <span class="t">${format(t, "HH:mm")}</span>`;
|
|
41 }
|
|
42 show(): boolean {
|
|
43 const now = new Date();
|
|
44 const t = parseISO(this.start);
|
|
45
|
|
46 const start = startOfToday();
|
|
47 let end = endOfToday();
|
|
48 if (isAfter(now, addHours(startOfToday(), 18))) {
|
|
49 end = endOfTomorrow();
|
|
50 }
|
|
51
|
|
52 return isWithinInterval(t, { start, end }) && !hideTitles.has(this.title) && !hideFeeds.has(this.feed.value);
|
|
53 }
|
|
54 toHtml(): TemplateResult {
|
|
55 return html`
|
|
56 <li>
|
|
57 <span class="date">${this.shortDate()}</span> ${this.title}
|
|
58 <!--${this.feed}-->
|
|
59 </li>
|
|
60 `;
|
|
61 }
|
|
62 }
|
|
63
|
|
64 @customElement("fd-upcoming-events")
|
|
65 export class UpcomingEvents extends LitElement {
|
|
66 @property() evs: DisplayEvent[];
|
|
67 constructor() {
|
|
68 super();
|
|
69 this.evs = [];
|
|
70 this.load();
|
|
71 setInterval(this.load.bind(this), 5 * 60 * 1000);
|
|
72 }
|
|
73
|
|
74 async load() {
|
|
75 const store = new Store();
|
|
76 const r = await fetch("/gcalendarwatch/graph/calendar/upcoming",
|
|
77
|
|
78 {
|
|
79 method: 'GET',
|
|
80 headers: {
|
|
81 Accept: 'application/json',
|
|
82 'X-Pomerium-Authorization': document.cookie.substring(
|
|
83 document.cookie.indexOf('=') + 1,
|
|
84 ),
|
|
85 },
|
|
86 }
|
|
87
|
|
88 );
|
|
89 const n3txt = await r.text();
|
|
90 const parser = new Parser({ format: "application/trig" });
|
|
91 parser.parse(n3txt, (error, quad, prefixes) => {
|
|
92 if (quad) {
|
|
93 store.addQuad(quad);
|
|
94 } else {
|
|
95 const graph = namedNode(EV + "gcalendar");
|
|
96 this.evs = [];
|
|
97 store.getSubjects(namedNode(RDF + "type"), namedNode(EV + "Event"), graph).forEach((ev: Quad_Subject) => {
|
|
98 const de = new DisplayEvent(store, graph, ev);
|
|
99 if (de.show()) {
|
|
100 this.evs = [...this.evs, de];
|
|
101 }
|
|
102 });
|
|
103 this.evs = sortBy(this.evs, "start");
|
|
104 }
|
|
105 });
|
|
106 }
|
|
107 static styles = [
|
|
108 shared,
|
|
109 css`
|
|
110 ol {
|
|
111 list-style-type: circle;
|
|
112 font-size: 16px;
|
|
113 background: #cd66bb2e;
|
|
114 padding: 9px;
|
|
115 width: fit-content;
|
|
116 position: relative;
|
|
117 top: -21px;
|
|
118 border-radius: 14px;
|
|
119 }
|
|
120 span.d {
|
|
121 opacity: 0.5;
|
|
122 }
|
|
123 span.t {
|
|
124 color: #50fa7b;
|
|
125 }
|
|
126 `,
|
|
127 ];
|
|
128 render() {
|
|
129 return html`
|
|
130 <h1 data-text="Calendar">Calendar</h1>
|
|
131 <ol>
|
|
132 ${this.evs.map((d) => d.toHtml())}
|
|
133 </ol>
|
|
134 `;
|
|
135 }
|
|
136 }
|