Accept search parameter in URL

dev
gravel 11 months ago
parent 42e9c71773
commit 3662a88417
Signed by: gravel
GPG Key ID: C0538F3C906B308F

@ -82,9 +82,18 @@ function getTimestamp() {
* Processes initial URL hash and parameter to trigger actions on the page. * Processes initial URL hash and parameter to trigger actions on the page.
*/ */
function reactToURLParameters() { function reactToURLParameters() {
const hash = location.hash; const rawHash = location.hash;
if (hash == "") return; if (rawHash == "") return;
const communityID = hash.slice(1);
const hash = rawHash.slice(1);
if (hash.startsWith("q=")) {
useSearchTerm(hash.slice(2), true);
toggleSearchBarVisibility();
return;
}
const communityID = hash;
const row = dom.community_row(communityID); const row = dom.community_row(communityID);
if (row == null || !(row instanceof HTMLTableRowElement)) { if (row == null || !(row instanceof HTMLTableRowElement)) {
return; return;
@ -448,19 +457,23 @@ function addServerIconInteractions() {
} }
} }
function toggleSearchBarVisibility() {
const container = dom.search_container();
container?.classList.toggle(CLASSES.COMPONENTS.COLLAPSED);
if (!container?.classList.contains(CLASSES.COMPONENTS.COLLAPSED)) {
const searchBar = dom.search_bar();
searchBar?.focus();
// Inconsistent; attempt to align search bar to top to make more space for results.
searchBar?.scrollIntoView({ behavior: 'smooth', inline: 'start' });
} else {
useSearchTerm("");
}
}
function addSearchInteractions() { function addSearchInteractions() {
dom.btn_toggle_search()?.addEventListener('click', function (ev) { dom.btn_toggle_search()?.addEventListener('click', function (ev) {
location.hash="#"; location.hash="#";
const container = dom.search_container(); toggleSearchBarVisibility();
container?.classList.toggle(CLASSES.COMPONENTS.COLLAPSED);
if (!container?.classList.contains(CLASSES.COMPONENTS.COLLAPSED)) {
const searchBar = dom.search_bar();
searchBar?.focus();
// Inconsistent; attempt to align search bar to top to make more space for results.
searchBar?.scrollIntoView({ behavior: 'smooth', inline: 'start' });
} else {
useSearchTerm("");
}
}) })
dom.search_bar()?.addEventListener('keydown', function () { dom.search_bar()?.addEventListener('keydown', function () {
@ -475,16 +488,16 @@ function addSearchInteractions() {
}) })
dom.btn_clear_search()?.addEventListener('click', function () { dom.btn_clear_search()?.addEventListener('click', function () {
useSearchTerm(""); useSearchTerm("", true);
const searchBar = dom.search_bar(); dom.search_bar()?.focus();
searchBar?.focus();
searchBar.value = "";
}) })
Array.from(dom.sample_searches()).forEach(button => button.addEventListener('click', function() { Array.from(dom.sample_searches()).forEach(button => button.addEventListener('click', function() {
const targetSearch = button.getAttribute(ATTRIBUTES.SEARCH.TARGET_SEARCH); const targetSearch = button.getAttribute(ATTRIBUTES.SEARCH.TARGET_SEARCH);
useSearchTerm(targetSearch); if (targetSearch === null) {
dom.search_bar().value = targetSearch; throw new Error("Sample search was null");
}
useSearchTerm(targetSearch, true);
})) }))
} }
@ -624,7 +637,13 @@ function initializeSearch() {
* *
* @param {string} [rawTerm] * @param {string} [rawTerm]
*/ */
function useSearchTerm(rawTerm) { function useSearchTerm(rawTerm, fillSearchBarWithTerm = false) {
const searchBar = dom.search_bar();
if (searchBar === undefined || !(searchBar instanceof HTMLInputElement)) {
throw new Error("Could not find search bar input element");
}
if (!rawTerm) { if (!rawTerm) {
replaceRowsWith(communityFullRowCache); replaceRowsWith(communityFullRowCache);
dom.search_bar()?.classList.remove(CLASSES.SEARCH.NO_RESULTS); dom.search_bar()?.classList.remove(CLASSES.SEARCH.NO_RESULTS);
@ -652,12 +671,17 @@ function useSearchTerm(rawTerm) {
} }
); );
if (newRows.length === 0) { if (newRows.length === 0) {
dom.search_bar()?.classList.add(CLASSES.SEARCH.NO_RESULTS); searchBar.classList.add(CLASSES.SEARCH.NO_RESULTS);
} else { } else {
dom.search_bar()?.classList.remove(CLASSES.SEARCH.NO_RESULTS); searchBar.classList.remove(CLASSES.SEARCH.NO_RESULTS);
} }
replaceRowsWith(newRows); replaceRowsWith(newRows);
} }
if (fillSearchBarWithTerm) {
searchBar.value = rawTerm;
}
sortTable(); sortTable();
} }

Loading…
Cancel
Save