Spaces:
Build error
Build error
Update templates/index.html
Browse files- templates/index.html +283 -292
templates/index.html
CHANGED
|
@@ -140,364 +140,355 @@ const suggestionCache = {};
|
|
| 140 |
let prefetchTimeout;
|
| 141 |
|
| 142 |
function debounce(func, delay) {
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
}
|
| 150 |
-
|
| 151 |
async function fetchSuggestions(query) {
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
}
|
| 170 |
-
|
| 171 |
// Event listeners for Intro Search
|
| 172 |
searchQueryInput.addEventListener("input", () => {
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
});
|
| 184 |
-
|
| 185 |
searchForm.addEventListener("submit", async (event) => {
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
});
|
| 191 |
-
|
| 192 |
// Event listeners for Main Content Search
|
| 193 |
mainSearchQueryInput.addEventListener("input", () => {
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
});
|
| 205 |
-
|
| 206 |
mainSearchForm.addEventListener("submit", async (event) => {
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
});
|
| 212 |
|
| 213 |
function displaySuggestions(suggestions, container = suggestionsContainer) {
|
| 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 |
function updateSuggestionSelection(container) {
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
}
|
| 255 |
|
| 256 |
function showLoading() {
|
| 257 |
-
|
| 258 |
}
|
| 259 |
|
| 260 |
function hideLoading() {
|
| 261 |
-
|
| 262 |
}
|
| 263 |
-
|
| 264 |
let startTime;
|
| 265 |
async function performSearch(query) {
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
}
|
| 288 |
|
| 289 |
function updateURLWithQuery(query) {
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
| 292 |
}
|
| 293 |
|
| 294 |
function removeDuplicateResults(results) {
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
}
|
| 305 |
-
|
| 306 |
async function fetchResults(query, resultsPerPage) {
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
}
|
| 316 |
|
| 317 |
function displayResults(results, append = false) {
|
| 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 |
function displayError(message) {
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
}
|
| 366 |
-
|
| 367 |
searchQueryInput.addEventListener("input", debounce(async () => {
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
}));
|
| 377 |
-
|
| 378 |
mainSearchQueryInput.addEventListener("input", debounce(async () => {
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
}));
|
| 388 |
-
|
| 389 |
searchQueryInput.addEventListener("focus", () => {
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
});
|
| 394 |
mainSearchQueryInput.addEventListener("focus", () => {
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
});
|
| 399 |
-
|
| 400 |
document.addEventListener("click", (event) => {
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
});
|
| 406 |
-
|
| 407 |
searchQueryInput.addEventListener("keydown", async (event) => {
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
});
|
| 432 |
mainSearchQueryInput.addEventListener("keydown", async (event) => {
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
});
|
| 457 |
-
|
| 458 |
searchForm.addEventListener("submit", async (event) => {
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
});
|
| 464 |
mainSearchForm.addEventListener("submit", async (event) => {
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
});
|
| 470 |
-
|
| 471 |
window.addEventListener("scroll", () => {
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
});
|
| 486 |
|
| 487 |
function getQueryParameter(name) {
|
| 488 |
-
|
| 489 |
-
|
| 490 |
}
|
| 491 |
-
|
| 492 |
window.addEventListener('load', () => {
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
});
|
| 502 |
</script>
|
| 503 |
</body>
|
|
|
|
| 140 |
let prefetchTimeout;
|
| 141 |
|
| 142 |
function debounce(func, delay) {
|
| 143 |
+
return function() {
|
| 144 |
+
clearTimeout(suggestionRequestTimeout);
|
| 145 |
+
suggestionRequestTimeout = setTimeout(() => {
|
| 146 |
+
func.apply(this, arguments);
|
| 147 |
+
}, delay);
|
| 148 |
+
};
|
| 149 |
}
|
|
|
|
| 150 |
async function fetchSuggestions(query) {
|
| 151 |
+
if (suggestionCache[query]) {
|
| 152 |
+
return suggestionCache[query];
|
| 153 |
+
}
|
| 154 |
+
try {
|
| 155 |
+
const response = await fetch(`${BASE_URL}/api/suggestions?q=${encodeURIComponent(query)}`);
|
| 156 |
+
if (response.ok) {
|
| 157 |
+
const suggestions = await response.json();
|
| 158 |
+
suggestionCache[query] = suggestions;
|
| 159 |
+
return suggestions;
|
| 160 |
+
} else {
|
| 161 |
+
console.error("Error fetching suggestions:", response.status);
|
| 162 |
+
return [];
|
| 163 |
+
}
|
| 164 |
+
} catch (error) {
|
| 165 |
+
console.error("Error fetching suggestions:", error);
|
| 166 |
+
return [];
|
| 167 |
+
}
|
| 168 |
}
|
|
|
|
| 169 |
// Event listeners for Intro Search
|
| 170 |
searchQueryInput.addEventListener("input", () => {
|
| 171 |
+
clearTimeout(prefetchTimeout);
|
| 172 |
+
const searchQuery = searchQueryInput.value.trim();
|
| 173 |
+
if (searchQuery === "") {
|
| 174 |
+
suggestionsContainer.style.display = "none";
|
| 175 |
+
return;
|
| 176 |
+
}
|
| 177 |
+
prefetchTimeout = setTimeout(async () => {
|
| 178 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
| 179 |
+
displaySuggestions(suggestions);
|
| 180 |
+
}, 100);
|
| 181 |
});
|
|
|
|
| 182 |
searchForm.addEventListener("submit", async (event) => {
|
| 183 |
+
event.preventDefault();
|
| 184 |
+
selectedSuggestionIndex = -1;
|
| 185 |
+
const searchQuery = searchQueryInput.value;
|
| 186 |
+
performSearch(searchQuery);
|
| 187 |
});
|
|
|
|
| 188 |
// Event listeners for Main Content Search
|
| 189 |
mainSearchQueryInput.addEventListener("input", () => {
|
| 190 |
+
clearTimeout(prefetchTimeout);
|
| 191 |
+
const searchQuery = mainSearchQueryInput.value.trim();
|
| 192 |
+
if (searchQuery === "") {
|
| 193 |
+
mainSuggestionsContainer.style.display = "none";
|
| 194 |
+
return;
|
| 195 |
+
}
|
| 196 |
+
prefetchTimeout = setTimeout(async () => {
|
| 197 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
| 198 |
+
displaySuggestions(suggestions, mainSuggestionsContainer);
|
| 199 |
+
}, 100);
|
| 200 |
});
|
|
|
|
| 201 |
mainSearchForm.addEventListener("submit", async (event) => {
|
| 202 |
+
event.preventDefault();
|
| 203 |
+
selectedSuggestionIndex = -1;
|
| 204 |
+
const searchQuery = mainSearchQueryInput.value;
|
| 205 |
+
performSearch(searchQuery);
|
| 206 |
});
|
| 207 |
|
| 208 |
function displaySuggestions(suggestions, container = suggestionsContainer) {
|
| 209 |
+
container.innerHTML = "";
|
| 210 |
+
if (suggestions.length === 0 || (searchQueryInput.value.trim() === "" && mainSearchQueryInput.value.trim() === "")) {
|
| 211 |
+
container.style.display = "none";
|
| 212 |
+
container.style.opacity = 0;
|
| 213 |
+
return;
|
| 214 |
+
}
|
| 215 |
+
const suggestionList = document.createElement("ul");
|
| 216 |
+
suggestions.forEach((suggestion, index) => {
|
| 217 |
+
const listItem = document.createElement("li");
|
| 218 |
+
listItem.textContent = suggestion.phrase;
|
| 219 |
+
listItem.addEventListener("click", () => {
|
| 220 |
+
if (container === suggestionsContainer) {
|
| 221 |
+
searchQueryInput.value = suggestion.phrase;
|
| 222 |
+
} else {
|
| 223 |
+
mainSearchQueryInput.value = suggestion.phrase;
|
| 224 |
+
}
|
| 225 |
+
container.style.display = "none";
|
| 226 |
+
container.style.opacity = 0;
|
| 227 |
+
performSearch(suggestion.phrase);
|
| 228 |
+
});
|
| 229 |
+
listItem.addEventListener("focus", () => {
|
| 230 |
+
selectedSuggestionIndex = index;
|
| 231 |
+
updateSuggestionSelection(container);
|
| 232 |
+
});
|
| 233 |
+
suggestionList.appendChild(listItem);
|
| 234 |
+
});
|
| 235 |
+
container.appendChild(suggestionList);
|
| 236 |
+
container.style.display = "block";
|
| 237 |
+
container.style.opacity = 1;
|
| 238 |
}
|
| 239 |
|
| 240 |
function updateSuggestionSelection(container) {
|
| 241 |
+
const suggestionItems = container.querySelectorAll("li");
|
| 242 |
+
suggestionItems.forEach((item, index) => {
|
| 243 |
+
item.classList.toggle("selected", index === selectedSuggestionIndex);
|
| 244 |
+
});
|
| 245 |
+
// Make sure the mainSuggestionsContainer is displayed when needed
|
| 246 |
+
if (container === mainSuggestionsContainer && selectedSuggestionIndex !== -1) {
|
| 247 |
+
mainSuggestionsContainer.style.display = "block";
|
| 248 |
+
}
|
| 249 |
}
|
| 250 |
|
| 251 |
function showLoading() {
|
| 252 |
+
loadingOverlay.style.display = 'block';
|
| 253 |
}
|
| 254 |
|
| 255 |
function hideLoading() {
|
| 256 |
+
loadingOverlay.style.display = 'none';
|
| 257 |
}
|
|
|
|
| 258 |
let startTime;
|
| 259 |
async function performSearch(query) {
|
| 260 |
+
showLoading();
|
| 261 |
+
seenUrls.clear();
|
| 262 |
+
allResultsFetched = false;
|
| 263 |
+
resultsContainer.innerHTML = '';
|
| 264 |
+
noResultsMessage.style.display = 'none';
|
| 265 |
+
loadingMoreIndicator.classList.remove('active');
|
| 266 |
+
suggestionsContainer.style.display = 'none';
|
| 267 |
+
mainSuggestionsContainer.style.display = 'none';
|
| 268 |
+
startTime = performance.now(); // Start time calculation here
|
| 269 |
+
const initialResults = await fetchResults(query, INITIAL_RESULTS);
|
| 270 |
+
updateURLWithQuery(query);
|
| 271 |
+
displayResults(initialResults);
|
| 272 |
+
hideLoading();
|
| 273 |
+
if (initialResults.length > 0) {
|
| 274 |
+
cachedSearchResults = await fetchResults(query, CACHED_RESULTS);
|
| 275 |
+
cachedSearchResults = removeDuplicateResults(cachedSearchResults);
|
| 276 |
+
} else {
|
| 277 |
+
cachedSearchResults = [];
|
| 278 |
+
}
|
| 279 |
+
introPage.style.display = 'none';
|
| 280 |
+
mainContent.style.display = 'flex';
|
| 281 |
}
|
| 282 |
|
| 283 |
function updateURLWithQuery(query) {
|
| 284 |
+
const newURL = `${window.location.pathname}?query=${encodeURIComponent(query)}`;
|
| 285 |
+
window.history.pushState({
|
| 286 |
+
path: newURL
|
| 287 |
+
}, '', newURL);
|
| 288 |
}
|
| 289 |
|
| 290 |
function removeDuplicateResults(results) {
|
| 291 |
+
const uniqueResults = [];
|
| 292 |
+
const seen = new Set();
|
| 293 |
+
for (const result of results) {
|
| 294 |
+
if (!seen.has(result.href)) {
|
| 295 |
+
seen.add(result.href);
|
| 296 |
+
uniqueResults.push(result);
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
return uniqueResults;
|
| 300 |
}
|
|
|
|
| 301 |
async function fetchResults(query, resultsPerPage) {
|
| 302 |
+
const response = await fetch(`${BASE_URL}/api/search?q=${encodeURIComponent(query)}&max_results=${resultsPerPage}`);
|
| 303 |
+
if (!response.ok) {
|
| 304 |
+
displayError("An error occurred while fetching results.");
|
| 305 |
+
hideLoading();
|
| 306 |
+
return [];
|
| 307 |
+
}
|
| 308 |
+
const searchResults = await response.json();
|
| 309 |
+
return searchResults;
|
| 310 |
}
|
| 311 |
|
| 312 |
function displayResults(results, append = false) {
|
| 313 |
+
if (!append) {
|
| 314 |
+
resultsContainer.innerHTML = '';
|
| 315 |
+
}
|
| 316 |
+
const newResults = results.filter(result => !seenUrls.has(result.href));
|
| 317 |
+
newResults.forEach((result, index) => {
|
| 318 |
+
seenUrls.add(result.href);
|
| 319 |
+
const resultElement = document.createElement("div");
|
| 320 |
+
resultElement.classList.add("result");
|
| 321 |
+
const titleElement = document.createElement("h3");
|
| 322 |
+
const titleLink = document.createElement("a");
|
| 323 |
+
titleLink.href = result.href;
|
| 324 |
+
titleLink.textContent = result.title;
|
| 325 |
+
titleLink.target = "_blank";
|
| 326 |
+
titleLink.rel = "noopener noreferrer";
|
| 327 |
+
titleElement.appendChild(titleLink);
|
| 328 |
+
const urlElement = document.createElement("div");
|
| 329 |
+
urlElement.classList.add("url");
|
| 330 |
+
const urlLink = document.createElement("a");
|
| 331 |
+
urlLink.href = result.href;
|
| 332 |
+
urlLink.textContent = result.href;
|
| 333 |
+
urlLink.target = "_blank";
|
| 334 |
+
urlLink.rel = "noopener noreferrer";
|
| 335 |
+
urlElement.appendChild(urlLink);
|
| 336 |
+
const descriptionElement = document.createElement("p");
|
| 337 |
+
descriptionElement.textContent = result.body;
|
| 338 |
+
resultElement.appendChild(titleElement);
|
| 339 |
+
resultElement.appendChild(urlElement);
|
| 340 |
+
resultElement.appendChild(descriptionElement);
|
| 341 |
+
resultsContainer.appendChild(resultElement);
|
| 342 |
+
setTimeout(() => {
|
| 343 |
+
resultElement.classList.add("show");
|
| 344 |
+
}, 100 * index);
|
| 345 |
+
});
|
| 346 |
+
noResultsMessage.style.display = resultsContainer.children.length === 0 ? 'block' : 'none';
|
| 347 |
+
if (!append) {
|
| 348 |
+
const endTime = performance.now();
|
| 349 |
+
const timeTaken = (endTime / 2 - startTime).toFixed(2);
|
| 350 |
+
document.getElementById('results-info').textContent = `About ${timeTaken} milliseconds`;
|
| 351 |
+
}
|
| 352 |
}
|
| 353 |
|
| 354 |
function displayError(message) {
|
| 355 |
+
resultsContainer.innerHTML = "";
|
| 356 |
+
const errorElement = document.createElement("p");
|
| 357 |
+
errorElement.textContent = message;
|
| 358 |
+
errorElement.style.color = "red";
|
| 359 |
+
resultsContainer.appendChild(errorElement);
|
| 360 |
}
|
|
|
|
| 361 |
searchQueryInput.addEventListener("input", debounce(async () => {
|
| 362 |
+
selectedSuggestionIndex = -1;
|
| 363 |
+
const searchQuery = searchQueryInput.value;
|
| 364 |
+
if (searchQuery.trim() === "") {
|
| 365 |
+
suggestionsContainer.style.display = "none";
|
| 366 |
+
return;
|
| 367 |
+
}
|
| 368 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
| 369 |
+
displaySuggestions(suggestions);
|
| 370 |
}));
|
|
|
|
| 371 |
mainSearchQueryInput.addEventListener("input", debounce(async () => {
|
| 372 |
+
selectedSuggestionIndex = -1;
|
| 373 |
+
const searchQuery = mainSearchQueryInput.value;
|
| 374 |
+
if (searchQuery.trim() === "") {
|
| 375 |
+
mainSuggestionsContainer.style.display = "none";
|
| 376 |
+
return;
|
| 377 |
+
}
|
| 378 |
+
const suggestions = await fetchSuggestions(searchQuery);
|
| 379 |
+
displaySuggestions(suggestions, mainSuggestionsContainer);
|
| 380 |
}));
|
|
|
|
| 381 |
searchQueryInput.addEventListener("focus", () => {
|
| 382 |
+
if (searchQueryInput.value.trim() !== "") {
|
| 383 |
+
suggestionsContainer.style.display = "block";
|
| 384 |
+
}
|
| 385 |
});
|
| 386 |
mainSearchQueryInput.addEventListener("focus", () => {
|
| 387 |
+
if (mainSearchQueryInput.value.trim() !== "") {
|
| 388 |
+
mainSuggestionsContainer.style.display = "block";
|
| 389 |
+
}
|
| 390 |
});
|
|
|
|
| 391 |
document.addEventListener("click", (event) => {
|
| 392 |
+
if (!searchForm.contains(event.target) && !mainSearchForm.contains(event.target)) {
|
| 393 |
+
suggestionsContainer.style.display = "none";
|
| 394 |
+
mainSuggestionsContainer.style.display = "none";
|
| 395 |
+
}
|
| 396 |
});
|
|
|
|
| 397 |
searchQueryInput.addEventListener("keydown", async (event) => {
|
| 398 |
+
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
| 399 |
+
event.preventDefault();
|
| 400 |
+
const suggestionItems = suggestionsContainer.querySelectorAll("li");
|
| 401 |
+
const numSuggestions = suggestionItems.length;
|
| 402 |
+
if (event.key === "ArrowUp") {
|
| 403 |
+
selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
|
| 404 |
+
} else {
|
| 405 |
+
selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
|
| 406 |
+
}
|
| 407 |
+
updateSuggestionSelection(suggestionsContainer);
|
| 408 |
+
if (selectedSuggestionIndex !== -1 && suggestionItems[selectedSuggestionIndex]) {
|
| 409 |
+
searchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
|
| 410 |
+
suggestionItems[selectedSuggestionIndex].focus();
|
| 411 |
+
}
|
| 412 |
+
} else if (event.key === "Enter" && selectedSuggestionIndex !== -1) {
|
| 413 |
+
event.preventDefault();
|
| 414 |
+
const selectedSuggestion = suggestionsContainer.querySelectorAll("li")[selectedSuggestionIndex];
|
| 415 |
+
if (selectedSuggestion) {
|
| 416 |
+
searchQueryInput.value = selectedSuggestion.textContent;
|
| 417 |
+
suggestionsContainer.style.display = "none";
|
| 418 |
+
performSearch(searchQueryInput.value);
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
});
|
| 422 |
mainSearchQueryInput.addEventListener("keydown", async (event) => {
|
| 423 |
+
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
| 424 |
+
event.preventDefault();
|
| 425 |
+
const suggestionItems = mainSuggestionsContainer.querySelectorAll("li");
|
| 426 |
+
const numSuggestions = suggestionItems.length;
|
| 427 |
+
if (event.key === "ArrowUp") {
|
| 428 |
+
selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
|
| 429 |
+
} else {
|
| 430 |
+
selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
|
| 431 |
+
}
|
| 432 |
+
updateSuggestionSelection(mainSuggestionsContainer);
|
| 433 |
+
if (selectedSuggestionIndex !== -1 && suggestionItems[selectedSuggestionIndex]) {
|
| 434 |
+
mainSearchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
|
| 435 |
+
suggestionItems[selectedSuggestionIndex].focus();
|
| 436 |
+
}
|
| 437 |
+
} else if (event.key === "Enter" && selectedSuggestionIndex !== -1) {
|
| 438 |
+
event.preventDefault();
|
| 439 |
+
const selectedSuggestion = mainSuggestionsContainer.querySelectorAll("li")[selectedSuggestionIndex];
|
| 440 |
+
if (selectedSuggestion) {
|
| 441 |
+
mainSearchQueryInput.value = selectedSuggestion.textContent;
|
| 442 |
+
mainSuggestionsContainer.style.display = "none";
|
| 443 |
+
performSearch(mainSearchQueryInput.value);
|
| 444 |
+
}
|
| 445 |
+
}
|
| 446 |
});
|
|
|
|
| 447 |
searchForm.addEventListener("submit", async (event) => {
|
| 448 |
+
event.preventDefault();
|
| 449 |
+
selectedSuggestionIndex = -1;
|
| 450 |
+
const searchQuery = searchQueryInput.value;
|
| 451 |
+
performSearch(searchQuery);
|
| 452 |
});
|
| 453 |
mainSearchForm.addEventListener("submit", async (event) => {
|
| 454 |
+
event.preventDefault();
|
| 455 |
+
selectedSuggestionIndex = -1;
|
| 456 |
+
const searchQuery = mainSearchQueryInput.value;
|
| 457 |
+
performSearch(searchQuery);
|
| 458 |
});
|
|
|
|
| 459 |
window.addEventListener("scroll", () => {
|
| 460 |
+
if (allResultsFetched || cachedSearchResults.length === 0) return;
|
| 461 |
+
const {
|
| 462 |
+
scrollTop,
|
| 463 |
+
scrollHeight,
|
| 464 |
+
clientHeight
|
| 465 |
+
} = document.documentElement;
|
| 466 |
+
if (scrollTop + clientHeight >= scrollHeight - 100 && !loadingMoreIndicator.classList.contains("active")) {
|
| 467 |
+
loadingMoreIndicator.classList.add("active");
|
| 468 |
+
const resultsToDisplay = cachedSearchResults.splice(0, RESULTS_PER_PAGE);
|
| 469 |
+
if (resultsToDisplay.length > 0) {
|
| 470 |
+
displayResults(resultsToDisplay, true);
|
| 471 |
+
}
|
| 472 |
+
if (cachedSearchResults.length === 0) {
|
| 473 |
+
allResultsFetched = true;
|
| 474 |
+
}
|
| 475 |
+
loadingMoreIndicator.classList.remove("active");
|
| 476 |
+
}
|
| 477 |
});
|
| 478 |
|
| 479 |
function getQueryParameter(name) {
|
| 480 |
+
const urlParams = new URLSearchParams(window.location.search);
|
| 481 |
+
return urlParams.get(name);
|
| 482 |
}
|
|
|
|
| 483 |
window.addEventListener('load', () => {
|
| 484 |
+
const initialQuery = getQueryParameter('query');
|
| 485 |
+
if (initialQuery) {
|
| 486 |
+
mainSearchQueryInput.value = initialQuery;
|
| 487 |
+
performSearch(initialQuery);
|
| 488 |
+
} else {
|
| 489 |
+
introPage.style.display = 'flex';
|
| 490 |
+
mainContent.style.display = 'none';
|
| 491 |
+
}
|
| 492 |
});
|
| 493 |
</script>
|
| 494 |
</body>
|