Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
215 lines (176 loc) · 6.57 KB
/
Copy pathindex.js
File metadata and controls
215 lines (176 loc) · 6.57 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
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
function convertCmToFeetInches(cm) {
const totalInches = Math.round(cm / 2.54);
const feet = Math.floor(totalInches / 12);
const inches = totalInches % 12;
return { feet, inches };
}
function createCarouselEntry({ name, alt, height, heightOverride, artSrc, artScale, artOffset, artShift }) {
const surround = document.createElement("div");
surround.className = "carousel-entry-surround";
const entry = document.createElement("div");
entry.className = "carousel-entry";
const metricHeightDiv = document.createElement("div");
metricHeightDiv.classList.add("student-height");
metricHeightDiv.classList.add("student-height-metric");
metricHeightDiv.textContent = heightOverride || height.toString();
const imperialHeightDiv = document.createElement("div");
imperialHeightDiv.classList.add("student-height");
imperialHeightDiv.classList.add("student-height-imperial");
if (heightOverride === undefined) {
const { feet, inches } = convertCmToFeetInches(height);
imperialHeightDiv.textContent = `${feet}'${inches}"`;
} else {
imperialHeightDiv.textContent = heightOverride;
}
const nameDiv = document.createElement("div");
nameDiv.className = "student-name";
nameDiv.textContent = name;
const fillDiv = document.createElement("div");
fillDiv.className = "height-fill";
fillDiv.style.height = `${Math.round(480 * height / 230)}px`;
const image = document.createElement("img");
image.className = "student-artwork";
image.src = "student-sprites/" + artSrc + "_00.png";
image.style.setProperty("--artScale", artScale);
if (artShift !== undefined) {
image.style.setProperty("--artShift", `${artShift}px`);
}
image.style.setProperty("--artOffset", artOffset);
image.style.setProperty("--height", height);
fillDiv.appendChild(image);
entry.appendChild(nameDiv);
if (alt !== undefined) {
const altDiv = document.createElement("div");
altDiv.className = "student-alt-name";
altDiv.textContent = `(${alt})`;
entry.appendChild(altDiv);
}
entry.appendChild(metricHeightDiv);
entry.appendChild(imperialHeightDiv);
entry.appendChild(fillDiv);
surround.appendChild(entry);
return surround;
}
const carousel = document.querySelector("#carousel");
let carouselScrollTimeout;
let scrollVelocity = 0;
const easingCap = 3.2;
const velocityStep = 12;
let velocityStepping = 1;
let lastFrameTime = null;
let animationFrameId = null;
function smoothScrollStep(timestamp) {
if (lastFrameTime === null) lastFrameTime = timestamp;
if (Math.abs(scrollVelocity) >= 0.5) {
const actualScrollAmount = Math.pow(0.5 * velocityStepping, 0.5) * 2.7 * velocityStep;
if (scrollVelocity > easingCap) {
carousel.scrollLeft += actualScrollAmount;
} else if (scrollVelocity < -easingCap) {
carousel.scrollLeft -= actualScrollAmount;
} else {
carousel.scrollLeft += scrollVelocity;
velocityStepping = 1;
}
scrollVelocity *= 0.6;
animationFrameId = requestAnimationFrame(smoothScrollStep);
} else {
scrollVelocity = 0;
lastFrameTime = null;
animationFrameId = null;
velocityStepping = 1;
}
// updateScrollParam();
}
carousel.addEventListener("wheel", (event) => {
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) return;
event.preventDefault();
scrollVelocity += event.deltaY;
if (animationFrameId === null) {
requestAnimationFrame(smoothScrollStep);
} else {
velocityStepping += Math.abs(event.deltaY * 0.01);
}
});
// ?scrl=... is disabled until I implement a better solution.
// function updateScrollParam() {
// clearTimeout(carouselScrollTimeout);
// carouselScrollTimeout = setTimeout(() => {
// const scrollLeft = Math.round(carousel.scrollLeft);
// const url = new URL(window.location);
// url.searchParams.set("scrl", scrollLeft.toString());
// history.replaceState(null, '', url);
// }, 50);
// }
function getFilteredSortedStudents() {
const query = document.getElementById("search-input").value.toLowerCase().trim();
const sortOrder = document.getElementById("sort-select").value;
const showBase = document.getElementById("filter-base").checked;
const showAlts = document.getElementById("filter-alts").checked;
let filtered = students.filter((s) => {
const isAlt = s.alt !== undefined;
if (isAlt && !showAlts) return false;
if (!isAlt && !showBase) return false;
if (query) {
const nameMatch = s.name.toLowerCase().includes(query);
const altMatch = s.alt && s.alt.toLowerCase().includes(query);
if (!nameMatch && !altMatch) return false;
}
return true;
});
filtered.sort((a, b) => {
switch (sortOrder) {
case "height-asc":
return a.height - b.height;
case "height-desc":
return b.height - a.height;
case "name-asc":
return a.name.localeCompare(b.name);
case "name-desc":
return b.name.localeCompare(a.name);
default:
return 0;
}
});
return filtered;
}
function renderEntries() {
carousel.innerHTML = "";
const filtered = getFilteredSortedStudents();
carousel.replaceChildren(...
filtered.map(student => {
let c = student.cachedDOM;
if (!c) {
c = createCarouselEntry(student);
student.cachedDOM = c;
}
return c;
})
);
}
window.addEventListener("keydown", (event) => {
if (event.key === "Escape" && document.activeElement === document.getElementById("search-input")) {
document.getElementById("search-input").blur();
} else if ((event.ctrlKey || event.metaKey) && event.key === "f") {
event.preventDefault();
document.getElementById("search-input").focus();
}
});
function updateUnitVisibility() {
const unitSelect = document.getElementById("unit-select");
const carousel = document.getElementById("carousel");
carousel.classList.remove("show-metric", "show-imperial");
carousel.classList.add(`show-${unitSelect.value}`);
}
window.addEventListener("load", () => {
const url = new URL(window.location);
// const scrollLeft = parseInt(url.searchParams.get("scrl") || "0");
document.getElementById("search-input").addEventListener("input", renderEntries);
document.getElementById("sort-select").addEventListener("change", renderEntries);
document.getElementById("filter-base").addEventListener("change", renderEntries);
document.getElementById("filter-alts").addEventListener("change", renderEntries);
document.getElementById("unit-select").addEventListener("change", updateUnitVisibility);
updateUnitVisibility();
renderEntries();
// carousel.scrollLeft = scrollLeft;
});
// carousel.addEventListener("scroll", updateScrollParam);