« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static
diff options
context:
space:
mode:
Diffstat (limited to 'src/static')
-rw-r--r--src/static/js/client/sidebar-search.js30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/static/js/client/sidebar-search.js b/src/static/js/client/sidebar-search.js
index ae18c930..f46ce8e8 100644
--- a/src/static/js/client/sidebar-search.js
+++ b/src/static/js/client/sidebar-search.js
@@ -76,6 +76,8 @@ export const info = {
     stoppedScrollingTimeout: null,
 
     indexDownloadStatuses: Object.create(null),
+
+    currentValue: null,
   },
 
   session: {
@@ -300,22 +302,32 @@ export function mutatePageContent() {
   info.searchBox.appendChild(info.endSearchLine);
 }
 
+function trackSidebarSearchInputChanged() {
+  const {state} = info;
+
+  const newValue = info.searchInput.value;
+
+  if (newValue === state.currentValue) {
+    return false;
+  } else {
+    state.currentValue = newValue;
+    return !!newValue;
+  }
+}
+
 export function addPageListeners() {
   if (!info.searchInput) return;
 
-  let prevValue = null;
   info.searchInput.addEventListener('change', _domEvent => {
-    if (info.searchInput.value && info.searchInput.value !== prevValue) {
+    if (trackSidebarSearchInputChanged()) {
       activateSidebarSearch(info.searchInput.value);
     }
-    prevValue = info.searchInput.value;
   });
 
   info.searchInput.addEventListener('input', _domEvent => {
     const {settings, state} = info;
 
-    if (prevValue === info.searchInput.value) return;
-    prevValue = info.searchInput.value;
+    trackSidebarSearchInputChanged();
 
     if (!info.searchInput.value) {
       clearSidebarSearch();
@@ -608,7 +620,7 @@ function showSidebarSearchResults(results) {
   }
 
   for (const result of flatResults) {
-    const el = generateSidebarSearchResult(result, info);
+    const el = generateSidebarSearchResult(result);
     if (!el) continue;
 
     info.results.appendChild(el);
@@ -624,7 +636,7 @@ function showSidebarSearchResults(results) {
   restoreSidebarSearchResultsScrollOffset();
 }
 
-function generateSidebarSearchResult(result, info) {
+function generateSidebarSearchResult(result) {
   const preparedSlots = {
     color:
       result.data.color ?? null,
@@ -695,7 +707,7 @@ function generateSidebarSearchResult(result, info) {
       return null;
   }
 
-  return generateSidebarSearchResultTemplate(preparedSlots, info);
+  return generateSidebarSearchResultTemplate(preparedSlots);
 }
 
 function getSearchResultImageSource(result) {
@@ -708,7 +720,7 @@ function getSearchResultImageSource(result) {
       'rebaseThumb'));
 }
 
-function generateSidebarSearchResultTemplate(slots, info) {
+function generateSidebarSearchResultTemplate(slots) {
   const link = document.createElement('a');
   link.classList.add('wiki-search-result');