Repository navigation
Expand file tree
/
Copy pathnot-found.js
More file actions
91 lines (73 loc) · 2.49 KB
/
Copy pathnot-found.js
File metadata and controls
91 lines (73 loc) · 2.49 KB
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
let SORTED_ENTRIES = [];
let currentEntryIndex = null;
function getParams() {
const p = new URLSearchParams(window.location.search);
return {
year: Number(p.get("year")),
date: p.get("date")
};
}
function shiftDay(monthDay, offset) {
const [m, d] = monthDay.split("-").map(Number);
const date = new Date(2000, m - 1, d + offset);
return `${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
}
async function loadIndex() {
const res = await fetch("entries.json");
SORTED_ENTRIES = await res.json();
}
function updateEntryNav() {
const prev = document.getElementById("prevEntryBtn");
const next = document.getElementById("nextEntryBtn");
prev.disabled = currentEntryIndex <= 0;
next.disabled = currentEntryIndex >= SORTED_ENTRIES.length - 1;
if (currentEntryIndex > 0) {
const e = SORTED_ENTRIES[currentEntryIndex - 1];
prev.onclick = () =>
location.href = `index.html?year=${e.year}&date=${e.date}`;
}
if (currentEntryIndex < SORTED_ENTRIES.length - 1) {
const e = SORTED_ENTRIES[currentEntryIndex + 1];
next.onclick = () =>
location.href = `index.html?year=${e.year}&date=${e.date}`;
}
}
(async function init() {
await loadIndex();
const { year, date } = getParams();
document.getElementById("message").textContent =
`No entry exists for ${date}, ${year}.`;
// Alternatives (same day, other years)
const matches = SORTED_ENTRIES.filter(e => e.date === date);
const list = document.getElementById("alternatives");
if (matches.length === 0) {
list.innerHTML = "<li>No entries exist for this date in any year.</li>";
} else {
matches.forEach(e => {
const li = document.createElement("li");
const a = document.createElement("a");
a.href = `index.html?year=${e.year}&date=${e.date}`;
a.textContent = `${e.date} (${e.year})`;
li.appendChild(a);
list.appendChild(li);
});
}
// Entry-based nav anchor
currentEntryIndex = SORTED_ENTRIES.findIndex(
e => e.year === year && e.date === date
);
updateEntryNav();
// Day navigation
document.getElementById("prevDayBtn").onclick = () => {
const d = shiftDay(date, -1);
location.href = `index.html?year=${year}&date=${d}`;
};
document.getElementById("nextDayBtn").onclick = () => {
const d = shiftDay(date, 1);
location.href = `index.html?year=${year}&date=${d}`;
};
// Browse
document.getElementById("browseBtn").onclick = () => {
location.href = "list.html";
};
})();