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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
import {jsonapi} from "./jsonapi";
import {datetime} from "./datetime";
/**
* The Holiday class represents a holiday in Stud.IP
*/
class Holiday
{
/**
* The day of the holiday.
*/
day: Date;
/**
* The name of the holiday.
*/
name: string;
/**
* Whether the holiday is an official holiday (true) or not (false).
* The latter means that it can be part of a vacation period.
*/
official: boolean;
constructor(day: Date, name: string, official: boolean = true) {
this.day = day;
this.name = name;
this.official = official;
}
}
/**
* The HolidayCache class handles retrieving and caching holidays and vacations.
*/
class HolidayCache
{
holiday_cache: Map<number, Array<Holiday>>;
vacation_cache: Map<number, Array<Holiday>>;
/**
* The constructor has code to load cached holidays and vacations from the session storage.
*/
constructor() {
//Attempt to restore the cache from the session:
const holiday_cache_str = sessionStorage.getItem('fullcalendar_holidays');
this.holiday_cache = new Map<number, Array<Holiday>>();
if (holiday_cache_str != null) {
for (const [year, raw_array] of Object.entries(JSON.parse(holiday_cache_str))) {
const holidays: Array<Holiday> = [];
if (Array.isArray(raw_array)) {
for (const raw_holiday of raw_array) {
holidays.push(
new Holiday(new Date(raw_holiday.day), raw_holiday.name, raw_holiday.official)
);
}
this.holiday_cache.set(parseInt(year), holidays);
}
}
}
const vacation_cache_str = sessionStorage.getItem('fullcalendar_vacations');
this.vacation_cache = new Map<number, Array<Holiday>>();
if (vacation_cache_str != null) {
for (const [year, raw_array] of Object.entries(JSON.parse(vacation_cache_str))) {
const vacations: Array<Holiday> = [];
if (Array.isArray(raw_array)) {
for (const raw_vacation of raw_array) {
vacations.push(
new Holiday(new Date(raw_vacation.day), raw_vacation.name, raw_vacation.official)
);
}
this.vacation_cache.set(parseInt(year), vacations);
}
}
}
}
/**
* Loads the holidays of a year and stores them in the cache.
*
* @param year The year for which to load holidays.
*/
loadHolidays(year: number) : void {
const existing_cache = this.holiday_cache.get(year);
if (existing_cache) {
return;
}
jsonapi.withPromises().GET('holidays', {
data: { 'filter[year]': year }
}).then(response => {
if (!response) {
return;
}
const events: Array<Holiday> = [];
for (const [date, data] of Object.entries(response)) {
const day = new Date(date);
events.push(new Holiday(day, data.holiday, data.mandatory));
}
this.holiday_cache.set(year, events);
//Update the session storage item:
sessionStorage.setItem('fullcalendar_holidays', JSON.stringify(Object.fromEntries(this.holiday_cache)));
});
}
/**
* Loads the vacation days of a year and stores them in the cache.
*
* @param year The year for which to load vacations.
*/
loadVacations(year: number): void {
const existing_cache = this.vacation_cache.get(year);
if (existing_cache) {
return;
}
jsonapi.withPromises().get('vacations', {
data: {'filter[year]': year}
}).then(response => {
if (!response) {
return;
}
const events: Array<Holiday> = [];
for (const vacation_data of Object.values(response)) {
for (let i = parseInt(vacation_data.start); i < parseInt(vacation_data.end); i += 86400) {
const day = new Date(i * 1000);
events.push(new Holiday(day, vacation_data.name, false));
}
}
this.vacation_cache.set(year, events);
//Update the session storage item:
sessionStorage.setItem('fullcalendar_vacations', JSON.stringify(Object.fromEntries(this.vacation_cache)));
});
}
/**
* Returns a vacation day from the cache.
*
* @param date The day of the vacation.
*
* @returns Holiday|null If a vacation exists on the specified day, a Holiday object
* for it is returned. Otherwise, null is returned.
*/
getVacation(date: Date) : Holiday|null {
const year_vacation_cache = this.vacation_cache.get(date.getFullYear());
if (!year_vacation_cache) {
return null;
}
for (const vacation of year_vacation_cache) {
if (datetime.getISODate(date) === datetime.getISODate(vacation.day)) {
//A vacation day has been found.
return vacation;
}
}
return null;
}
/**
* Checks if there is a vacation day on the specified date.
*
* @param date The date to check.
*
* @returns boolean True, if the specified day is a vacation day.
* Otherwise, false is returned.
*/
isVacation(date: Date) {
return this.getVacation(date) !== null;
}
/**
* Returns the name of the vacation day on the specified date.
*
* @param date The date for which to get the vacation name.
*
* @returns string The name of the vacation, if any.
* If there is no vacation day on the specified date, the string is empty.
*/
getVacationName(date: Date) {
const vacation = this.getVacation(date);
if (vacation) {
return vacation.name;
}
return '';
}
/**
* Returns a holiday / vacation day from the cache.
*
* @param date The date of the holiday.
*
* @param regard_vacations Whether to regard vacations in addition to holidays (true)
* or to just regard holidays (false). Defaults to false.
*
* @returns Holiday|null If a holiday can be found, it is returned. Otherwise,
* null is returned.
*/
getHoliday(date: Date, regard_vacations: boolean = false): Holiday|null {
//Check if an entry for the date exists in the holiday or the vacation list:
const year_holiday_cache = this.holiday_cache.get(date.getFullYear());
if (!year_holiday_cache) {
return null;
}
for (const holiday of year_holiday_cache) {
if (datetime.getISODate(date) === datetime.getISODate(holiday.day)) {
//A holiday has been found.
return holiday;
}
}
if (regard_vacations) {
return this.getVacation(date);
}
return null;
}
/**
* Checks if there is a holiday on the specified date.
*
* @param date The date to check.
*
* @param regard_vacations Whether to regard vacations in addition to holidays (true)
* or to just regard holidays (false). Defaults to false.
*
* @returns boolean True, if the specified day is a holiday / vacation day.
* Otherwise, false is returned.
*/
isHoliday(date: Date, regard_vacations: boolean = false) : boolean {
return this.getHoliday(date, regard_vacations) !== null;
}
/**
* Returns the name of the holiday on the specified date.
*
* @param date The date for which to get the holiday name.
*
* @param regard_vacations Whether to regard vacations in addition to holidays (true)
* or to just regard holidays (false). Defaults to false.
*
* @returns string The name of the holiday / vacation, if any.
* If there is no holiday on the specified date, the string is empty.
*/
getHolidayName(date: Date, regard_vacations: boolean = false) : string {
const holiday = this.getHoliday(date, regard_vacations);
if (holiday) {
return holiday.name;
}
return '';
}
}
export {Holiday, HolidayCache};
export const holiday_cache: HolidayCache = new HolidayCache();
|