From 04a683494b762f6fc6ed8a237874d2009ba5bee1 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 2 May 2024 14:17:42 -0300 Subject: client: general search code cleanup --- src/static/js/client.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/static/js/client.js b/src/static/js/client.js index 8a1ee021..4836bedd 100644 --- a/src/static/js/client.js +++ b/src/static/js/client.js @@ -6,8 +6,15 @@ // ephemeral nature. import {getColors} from '../shared-util/colors.js'; -import {accumulateSum, atOffset, empty, filterMultipleArrays, stitchArrays} - from '../shared-util/sugar.js'; + +import { + accumulateSum, + atOffset, + empty, + filterMultipleArrays, + stitchArrays, + withEntries, +} from '../shared-util/sugar.js'; import FlexSearch from '../lib/flexsearch/flexsearch.bundle.module.min.js'; @@ -3423,9 +3430,8 @@ clientSteps.addPageListeners.push(addArtistExternalLinkTooltipPageListeners); // Internal search functionality -------------------------- async function initSearch() { - - // Copied directly from server search.js - const indexes = makeSearchIndexes(FlexSearch); + const indexes = + makeSearchIndexes(FlexSearch); const searchData = await fetch('/search-data/index.json') @@ -3444,13 +3450,12 @@ async function initSearch() { } function searchAll(query, options = {}) { - const results = {}; - - for (const [indexName, index] of Object.entries(window.searchIndexes)) { - results[indexName] = index.search(query, options); - } - - return results; + return ( + withEntries(window.searchIndexes, entries => entries + .map(([indexName, index]) => [ + indexName, + index.search(query, options), + ]))); } document.addEventListener('DOMContentLoaded', initSearch); @@ -3539,7 +3544,16 @@ function addSidebarSearchListeners() { } function activateSidebarSearch(query) { - showSidebarSearchResults(searchAll(query, {enrich: true})); + const {state} = sidebarSearchInfo; + + if (state.stoppedTypingTimeout) { + clearTimeout(state.stoppedTypingTimeout); + state.stoppedTypingTimeout = null; + } + + const results = searchAll(query, {enrich: true}); + + showSidebarSearchResults(results); } function clearSidebarSearch() { @@ -3565,8 +3579,6 @@ function showSidebarSearchResults(results) { data: doc, })))); - console.log(flatResults); - while (info.results.firstChild) { info.results.firstChild.remove(); } -- cgit 1.3.0-6-gf8a5