-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (230 loc) · 8.3 KB
/
index.js
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
import projects from "./content.js";
let lastRandomPick;
function addProjectToList(data) {
const newItem = document.getElementById("template-project_card").content.cloneNode(true);
const aElem = newItem.firstElementChild.firstElementChild;
// Link
aElem.href =
`https://scratch.mit.edu/projects/${data.id}`;
// Image source
aElem.firstElementChild.src =
`https://uploads.scratch.mit.edu/get_image/project/${data.id}_480x360.png`;
// Alt text for image
aElem.firstElementChild.alt = `Thumbnail for project ${data.title}`;
// Title of project
aElem.lastElementChild.textContent = data.title;
// (Debugging) Tags of project
// newItem.firstElementChild.lastElementChild.textContent = data.tags.join(", ");
// Metadata
newItem.firstElementChild.dataset.item_keywords = data.title.toLowerCase();
newItem.firstElementChild.dataset.item_tags = data.tags + ",";
newItem.firstElementChild.dataset.event_specific = data.eventSpecific ?? false;
document.getElementById("list").appendChild(newItem);
}
function filterList(keywords, tag) {
// Gray out the "Search or filter..." button if filters are active
if (keywords === "" && tag === "") {
document.getElementById("btn-filter").removeAttribute("disabled");
document.getElementById("btn-filter").removeAttribute("title");
} else {
document.getElementById("btn-filter").setAttribute("disabled", "");
document.getElementById("btn-filter").setAttribute("title", "You have filters active");
}
let count = 0;
for (const element of document.querySelectorAll(".card")) {
const matches = (
// Is in the selected category
element.dataset.item_tags.includes(tag + ",")
// Includes search query
&& element.dataset.item_keywords.includes(keywords)
// Is related to the selected event (e.g. April Fools) or is not related to an event
&& (
element.dataset.event_specific !== "true"
|| (tag !== "" && element.dataset.item_tags.includes(tag + ","))
)
);
element.dataset.exclude = !matches;
if (matches) count++;
}
if (count) {
document.getElementById("no_results").setAttribute("hidden", "hidden");
lastRandomPick = undefined;
} else {
document.getElementById("no_results").removeAttribute("hidden");
}
}
const elements = {
filtersRow: document.getElementById("filters_row"),
topButton: document.querySelector(".top_button"),
}
let filter = "";
// Do a search whenever something is typed into the search bar
document.querySelector("input").addEventListener(
"input",
(e) => {
filterList(e.target.value.toLowerCase(), filter);
},
{ passive: true },
);
// Build the list
for (const i of projects) {
addProjectToList(i);
}
// Show all results initially
filterList("", filter);
// Show filters button
document.getElementById("btn-filter").addEventListener("click", (e) => {
const filtersRow = elements.filtersRow;
e.target.ariaPressed = !filtersRow.toggleAttribute("data-hidden");
});
// Random item button (link)
document.getElementById("btn-random").addEventListener("click", (e) => {
e.preventDefault(); // Prevent following link "#"
const items = document.querySelectorAll(".card:not([data-exclude=true])");
if (items.length) {
// Pick a random item
const number = items.length === 1
? 0 // Only one possible outcome, easy
: typeof lastRandomPick === "number"
? (lastRandomPick + 1 + Math.floor(Math.random() * (items.length - 1))) % items.length // Don't roll the same item twice in a row
: Math.floor(Math.random() * items.length);
lastRandomPick = number;
items[number].style.animation = "bounce 0.2s 6 alternate cubic-bezier(0.25, 0.62, 0.62, 0.97)";
items[number].classList.add("chosen");
items[number].addEventListener(
"animationend",
() => {
items[number].style.removeProperty("animation");
items[number].classList.remove("chosen");
},
{ once: true },
);
if (e.isTrusted) {
items[number].firstElementChild.focus({ focusVisible: true });
}
}
});
// Show more filter options if certain filters are set
elements.filtersRow.querySelector("select").addEventListener("change", (e) => {
elements.filtersRow.querySelectorAll("select[data-if]").forEach(element => element.setAttribute("data-hidden", ""));
if (e.target.value === "game") {
elements.filtersRow.querySelector("select[data-if='game']").removeAttribute("data-hidden");
}
});
// Other filter dropdowns
elements.filtersRow.querySelectorAll("select").forEach(element => {
element.addEventListener("change", () => {
const filterFields = elements.filtersRow.querySelectorAll("select");
switch (filterFields[0].value) {
case "game": {
filter = filterFields[1].value === "" ? "game" : "game_" + filterFields[1].value;
break;
}
default: filter = filterFields[0].value;
}
filterList(document.querySelector("input").value.toLowerCase(), filter);
});
});
// When submitting search
document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
if (!e.isTrusted) return;
try { document.querySelector(".card:not([data-exclude=true]) a").focus(); } catch {
// If we can't find an element corresponding to a displayed result (means there are no results)...
document.getElementById("no_results").focus();
}
});
// Clear search query when pressing Esc
document.querySelector("input").addEventListener("keydown", (e) => {
if (e.key === "Escape") {
e.target.value = "";
filterList("", filter);
}
});
// Focus search bar when pressing Enter on "No results"
document.getElementById("no_results").addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
if (e.isTrusted) document.querySelector("input").select();
};
});
function focusSearchBar(event) {
event.preventDefault();
if (elements.filtersRow.hasAttribute("data-hidden")) document.getElementById("btn-filter").click(); // Show filters
document.querySelector("input").focus();
document.querySelector("input").select();
}
// Global keyboard shortcuts
document.addEventListener("keydown", (e) => {
// Only when focus is outside of the search bar
if (!e.target.closest("input, dialog")) {
switch (e.key) {
case "/": {
focusSearchBar(e);
break;
}
}
switch (e.key) {
case "k": {
if (e.ctrlKey || e.metaKey) {
focusSearchBar(e);
break;
}
}
}
switch (e.key) {
case "f": {
if (e.ctrlKey || e.metaKey) {
focusSearchBar(e);
break;
}
}
}
}
});
document.querySelectorAll("[data-action='modal-privacy']").forEach(elem => elem.addEventListener("click", (e) => {
e.preventDefault(); // Prevent following link "#"
document.querySelector("dialog").showModal();
}));
// Dialog close buttons
document.querySelectorAll("dialog").forEach(element => {
element.querySelector("button").addEventListener("click", () => {
element.close();
});
});
// Show "Back to top" button if you've scrolled past a certain point
document.addEventListener("scroll", () => {
const show = window.scrollY > 300;
elements.topButton.style.pointerEvents = show ? "all" : "none";
elements.topButton.style.opacity = show ? 1 : 0;
elements.topButton.tabindex = show ? 0 : -1;
});
// "Back to top" button
elements.topButton.addEventListener("click", () => {
// Remove the fragment from the current URL, if there is one
const currentURL = new URL(location);
if (currentURL.hash) {
let target = currentURL;
target.hash = "";
history.pushState({}, "", target);
}
window.scrollTo(0, 0);
document.querySelector(".card:not([data-exclude=true]) a").focus();
});
// "Skip to content" button
document.querySelector(".skip_button").addEventListener("click", () => {
document.querySelector(".card:not([data-exclude=true]) a").focus();
});
// Event-specific categories
function addCategoryOption(value, name) {
const newOption = document.createElement("option");
newOption.value = value;
newOption.textContent = name;
elements.filtersRow.querySelector("select").querySelector('option[value=""]').insertAdjacentElement("afterend", newOption);
}
const date = new Date();
const stamp = {
date: date.getDate(),
month: date.getMonth() + 1,
};
if (stamp.month === 4 && stamp.date === 1) addCategoryOption("aprilfools", "📅 April Fools");