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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
declare const FRIENDS_URL: string;
declare const LOCALE: string;
/**
* Object that we get back from the friend API.
*/
interface JsonFriend {
name: string,
id: number,
}
interface Window {
fietsboekImageIndex: number,
fietsboekCurrentImage: HTMLDivElement | null,
}
// Make eslint happy about the Window redefinition
(_: Window) => null;
/**
* Type alias to make it clear what kind of string we are expecting.
*/
type Selector = string;
/**
* Installs a listener to the given DOM objects.
*
* @param selector - The query selector to find the DOM objects.
* @param event - The event name to listen to.
* @param handler - The handler function.
*/
function addHandler<K extends keyof GlobalEventHandlersEventMap>(
selector: Selector,
event: K,
handler: (ev: GlobalEventHandlersEventMap[K]) => any,
) {
document.querySelectorAll(selector).
forEach((obj) => obj.addEventListener(event, handler as EventListener));
}
/**
* Handler for when a tag is clicked. Removes the tag from the tag list.
*
* @param event - The triggering event.
*/
function tagClicked(event: MouseEvent) {
const span = (event.target as HTMLElement).closest('span')!;
span.parentNode!.removeChild(span);
}
addHandler(".tag-badge", "click", tagClicked);
/**
* Handler to add a new tag when the button is pressed.
*/
function addTag() {
const newTag = document.querySelector("#new-tag") as HTMLInputElement;
if (newTag.value === "") {
return;
}
const node = document.createElement("span");
node.classList.add("tag-badge");
node.classList.add("badge");
node.classList.add("rounded-pill");
node.classList.add("bg-info");
node.classList.add("text-dark");
node.addEventListener("click", tagClicked);
const text = document.createTextNode(newTag.value);
node.appendChild(text);
const icon = document.createElement("i");
icon.classList.add("bi");
icon.classList.add("bi-x");
node.appendChild(icon);
const input = document.createElement("input");
input.hidden = true;
input.name = "tag[]";
input.value = newTag.value;
node.appendChild(input);
document.querySelector("#formTags")?.appendChild(node);
const space = document.createTextNode(" ");
document.querySelector("#formTags")?.appendChild(space);
newTag.value = "";
}
addHandler("#add-tag-btn", "click", addTag);
// Also add a tag when enter is pressed
addHandler("#new-tag", "keypress", (event) => {
if (event.code == "Enter") {
event.preventDefault();
addTag();
}
});
/**
* Function to check for password validity.
*
* @param main - Selector for the actual entered password input.
* @param repeat - Selector for the repeated password, must match `main`.
*/
function checkPasswordValidity(main: Selector, repeat: Selector) {
const mainPassword = document.querySelector(main) as HTMLInputElement;
const repeatPassword = document.querySelector(repeat) as HTMLInputElement;
const form = mainPassword.closest('form')!;
form.classList.remove('was-validated');
// Check password requirements. The validation errors are not actually
// displayed, as the HTML template contains pre-filled texts for that.
if (mainPassword.value.length != 0 && mainPassword.value.length < 8) {
mainPassword.setCustomValidity('Too short');
} else {
mainPassword.setCustomValidity('');
}
if (mainPassword.value != repeatPassword.value) {
repeatPassword.setCustomValidity('Needs to match');
} else {
repeatPassword.setCustomValidity('');
}
}
// This function is used via a HTML onchange= handler, so make eslint happy
checkPasswordValidity;
/**
* Function to check for name validity.
*
* @param name - Selector name that should be checked.
*/
function checkNameValidity(name: Selector) {
const nameField = document.querySelector(name) as HTMLInputElement;
if (nameField.value.length == 0) {
nameField.setCustomValidity('Needs a name');
}
}
// This function is used via a HTML onchange= handler, so make eslint happy
checkNameValidity;
/**
* Hit the endpoint to search for friends. This populates the friend selector
* when tagging friends.
*/
function searchFriends() {
const searchPattern = (document.querySelector("#friendSearchQuery") as HTMLInputElement).
value.toLowerCase();
const friendSearch = document.querySelector("#friendSearch")!;
friendSearch.innerHTML = "";
fetch(FRIENDS_URL)
.then((response) => response.json())
.then((response: [JsonFriend]) => {
const blueprint = document.querySelector("#friendSearchBlueprint") as HTMLLIElement;
// Only show friends with a matching name
const friends = response.filter(
(obj) => obj.name.toLowerCase().indexOf(searchPattern) != -1
);
friends.forEach((friend) => {
const copy = blueprint.cloneNode(true) as HTMLLIElement;
copy.removeAttribute("id");
(copy.querySelector(".friend-name") as HTMLSpanElement).textContent = friend.name;
copy.querySelector("button")?.addEventListener("click", (event: MouseEvent) => {
const button = (event.target as HTMLElement).closest("button")!;
button.parentNode!.parentNode!.removeChild(button.parentNode!);
const added = document.querySelector("#friendAddedBlueprint")!.
cloneNode(true) as HTMLLIElement;
added.removeAttribute("id");
(added.querySelector(".friend-name") as HTMLSpanElement).
textContent = friend.name;
added.querySelector("input")!.value = friend.id.toString();
added.querySelector("input")!.removeAttribute("disabled");
added.querySelector("button")!.addEventListener("click", removeFriendClicked);
document.querySelector('#taggedFriends')!.appendChild(added);
});
friendSearch.appendChild(copy);
});
});
}
addHandler("#add-friend-btn", "click", () => searchFriends());
// Also trigger the search on Enter keypress
addHandler("#friendSearchQuery", "keypress", (event) => {
if (event.code == "Enter") {
event.preventDefault();
searchFriends();
}
});
/**
* Handler for when a "Remove friend" button is clicked.
*
* @param event - The triggering event.
*/
function removeFriendClicked(event: MouseEvent) {
const button = (event.target as HTMLElement).closest("button")!;
button.parentNode!.parentNode!.removeChild(button.parentNode!);
}
addHandler(".remove-friend-button", "click", removeFriendClicked);
/**
* Handler for when the image input is changed.
*
* This handler splits the multiple images up into single input fields, such
* that each one can be removed individually. It also adds preview images, and
* adds the button to delete and edit the image's description.
*
* @param event - The triggering event.
*/
function imageSelectorChanged(event: Event) {
console.log(event);
const target = event.target as HTMLInputElement;
for (const file of Array.from(target.files ?? [])) {
window.fietsboekImageIndex++;
const input = document.createElement("input");
input.type = "file";
input.hidden = true;
input.name = `image[${window.fietsboekImageIndex}]`;
const transfer = new DataTransfer();
transfer.items.add(file);
input.files = transfer.files;
const preview = document.querySelector("#trackImagePreviewBlueprint")!.
cloneNode(true) as HTMLDivElement;
preview.removeAttribute("id");
preview.querySelector("img")!.src = URL.createObjectURL(file);
preview.querySelector("button.delete-image")!.
addEventListener("click", deleteImageButtonClicked as EventListener);
preview.querySelector("button.edit-image-description")!.
addEventListener("click", editImageDescriptionClicked as EventListener);
(preview.querySelector("input.image-description-input") as HTMLInputElement).
name = `image-description[${window.fietsboekImageIndex}]`;
preview.appendChild(input);
document.querySelector("#trackImageList")!.appendChild(preview);
}
target.value = "";
}
addHandler("#imageSelector", "change", imageSelectorChanged);
/**
* Handler to remove a picture from a track.
*
* @param event - The triggering event.
*/
function deleteImageButtonClicked(event: MouseEvent) {
const preview = (event.target as HTMLElement).closest("div.track-image-preview")!;
/* If this was a image yet to be uploaded, simply remove it */
const input = preview.querySelector("input[type=file]");
if (input) {
preview.parentNode!.removeChild(preview);
return;
}
/* Otherwise, we need to remove it but also insert a "delete-image" input */
const deleter = preview.querySelector("input.image-deleter-input")!;
deleter.removeAttribute("disabled");
preview.removeChild(deleter);
preview.parentNode!.appendChild(deleter);
preview.parentNode!.removeChild(preview);
}
addHandler("button.delete-image", "click", deleteImageButtonClicked);
/**
* Handler to show the image description editor.
*
* @param event - The triggering event.
*/
function editImageDescriptionClicked(event: MouseEvent) {
window.fietsboekCurrentImage = (event.target as HTMLElement).closest("div")!;
const imageInput = (
<HTMLInputElement>
window.fietsboekCurrentImage.querySelector("input.image-description-input")
);
const currentDescription = imageInput.value;
const modalDom = document.getElementById("imageDescriptionModal")!;
modalDom.querySelector("textarea")!.value = currentDescription;
const modal = bootstrap.Modal.getOrCreateInstance(modalDom, {});
modal.show();
}
addHandler("button.edit-image-description", "click", editImageDescriptionClicked);
/**
* Handler to save the image description of the currently edited image.
*
* @param event - The triggering event.
*/
function saveImageDescriptionClicked(_event: MouseEvent) {
const modalDom = document.getElementById("imageDescriptionModal")!;
const wantedDescription = modalDom.querySelector("textarea")!.value;
(window.fietsboekCurrentImage!.
querySelector("input.image-description-input") as HTMLInputElement).
value = wantedDescription;
window.fietsboekCurrentImage!.
querySelector("img")!.title = wantedDescription;
const modal = bootstrap.Modal.getOrCreateInstance(modalDom, {});
modal.hide();
window.fietsboekCurrentImage = null;
}
addHandler("#imageDescriptionModal button.btn-success", "click", saveImageDescriptionClicked);
/**
* Handler to toggle (collapse/expand) the yearly/monthly summary.
*
* @param event - The triggering event.
*/
function toggleSummary(event: MouseEvent) {
const chevron = event.target as HTMLElement;
const containing = chevron.closest("a")!;
const summary = containing.nextElementSibling!;
bootstrap.Collapse.getOrCreateInstance(summary).toggle();
if (chevron.classList.contains("bi-chevron-down")) {
chevron.classList.remove("bi-chevron-down");
chevron.classList.add("bi-chevron-right");
} else {
chevron.classList.remove("bi-chevron-right");
chevron.classList.add("bi-chevron-down");
}
}
addHandler(".summary-toggler", "click", toggleSummary);
/*
* Handler to enable the "Download archive button" ...
*/
addHandler("#archiveDownloadButton", "click", () => {
const checked = document.querySelectorAll(".archive-checkbox:checked");
const url = new URL("/track/archive", window.location.href);
checked.forEach((c) => {
url.searchParams.append("track_id[]", (c as HTMLInputElement).value);
});
window.location.assign(url);
});
/*
* ... and the listeners on the checkboxes to disable and enable the button.
*/
addHandler(".archive-checkbox", "change", () => {
const checked = document.querySelectorAll(".archive-checkbox:checked");
const downloadButton = document.querySelector("#archiveDownloadButton") as HTMLButtonElement;
downloadButton.disabled = (checked.length == 0);
});
/**
* Handler to clear the input when a .button-clear-input is pressed.
*
* The button must be in an input-group with the input.
*
* @param event - The triggering event.
*/
function clearInputButtonClicked(event: MouseEvent) {
const target = event.target as HTMLElement;
target.closest(".input-group")!.querySelectorAll("input").forEach((i) => i.value = "");
target.closest(".input-group")!.querySelectorAll("select").forEach((i) => i.value = "");
}
addHandler(".button-clear-input", "click", clearInputButtonClicked);
document.addEventListener('DOMContentLoaded', function() {
window.fietsboekImageIndex = 0;
/* Enable tooltips */
const tooltipTriggerList = [].slice.call(
document.querySelectorAll('[data-bs-toggle="tooltip"]')
);
tooltipTriggerList.map((tooltipTriggerEl) => {
return new bootstrap.Tooltip(tooltipTriggerEl, { sanitize: false });
});
/* Enable Bootstrap form validation */
const forms = document.querySelectorAll('.needs-validation');
Array.from(forms).forEach((form) => {
form.addEventListener('submit', (event) => {
if (!(form as HTMLFormElement).checkValidity()) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
/* Format all datetimes to the local timezone */
document.querySelectorAll(".fietsboek-local-datetime").forEach((obj) => {
const timestamp = parseFloat(obj.attributes.getNamedItem("data-utc-timestamp")!.value);
const date = new Date(timestamp * 1000);
// TypeScript complains about this, but according to MDN it is fine, at
// least in "somewhat modern" browsers
const intl = new Intl.DateTimeFormat(LOCALE, {
dateStyle: "medium",
timeStyle: "medium",
} as any);
obj.innerHTML = intl.format(date);
});
});
|