-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Widget.ts
120 lines (93 loc) · 3.23 KB
/
Widget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
import Handlebars from "handlebars/dist/handlebars";
import Event from "./Event";
import Filter from "./Filter";
import Options from "./Options";
import { messages } from "./i18n";
import { version } from "../package.json";
abstract class Widget {
private url = "https://osmcal.org/api/v2/";
protected filter: Filter = {};
protected future = true;
protected hideCancelled = false;
protected limit = -1;
protected locales: string|string[];
protected past = false;
protected element: HTMLElement;
protected template: string;
constructor (element: HTMLElement, options?: Options) {
this.element = element;
if (typeof this.element.dataset.in !== "undefined") {
this.filter.in = this.element.dataset.in;
}
if (typeof this.element.dataset.limit !== "undefined") {
this.limit = parseInt(this.element.dataset.limit);
}
if (typeof this.element.dataset.locale !== "undefined") {
this.locales = this.element.dataset.locale;
}
if (typeof this.element.dataset.locales !== "undefined") {
this.locales = this.element.dataset.locales.split(",");
}
if (typeof options !== "undefined") {
this.filter = options.filter;
this.hideCancelled = options.hideCancelled;
this.limit = options.limit;
this.locales = options.locales;
if (typeof options.future !== "undefined" || typeof options.past !== "undefined") {
this.future = (options.future === true);
this.past = (options.past === true);
}
}
}
protected async fetch (request: string): Promise<Event[]> {
let url = `${this.url}events/${request}`;
if (typeof this.filter !== "undefined") {
url += `?${new URLSearchParams(Object.entries(this.filter)).toString()}`;
}
const headers = new Headers();
headers.append("Client-App", `osmcal-widget/${version}`);
if (typeof this.locales !== "undefined") {
headers.append("Accept-Language", Array.isArray(this.locales) ? this.locales.join(",") : this.locales);
}
const response = await fetch(url, {
cache: "no-cache",
headers
});
return (await response.json());
}
protected async getEvents (): Promise<Event[]> {
let events: Event[] = [];
if (this.future === true) {
events = events.concat(await this.fetch(""));
}
if (this.past === true) {
events = events.concat(await this.fetch("past/"));
}
if (this.hideCancelled === true) {
events = events.filter((event: Event) => typeof event.cancelled === "undefined" || event.cancelled === false);
}
if (this.limit > 0) {
return events.slice(0, this.limit);
}
return events;
}
protected getMessages (): Record<string, string> {
const lang = Array.isArray(this.locales)
? this.locales[0]
: typeof this.locales !== "undefined"
? this.locales
: "en";
return typeof messages[lang] !== "undefined" ? messages[lang] : messages.en;
}
public setTemplate (template: string): this {
this.template = template;
return this;
}
protected render (event: Event): string {
const template = Handlebars.compile(this.template);
return template(event);
}
public abstract async display(): Promise<Event[]>;
}
export { Widget as default };