« 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/js/client/sidebar-search.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/static/js/client/sidebar-search.js')
-rw-r--r--src/static/js/client/sidebar-search.js85
1 files changed, 52 insertions, 33 deletions
diff --git a/src/static/js/client/sidebar-search.js b/src/static/js/client/sidebar-search.js
index 8b29cf63..c39c38bc 100644
--- a/src/static/js/client/sidebar-search.js
+++ b/src/static/js/client/sidebar-search.js
@@ -38,6 +38,9 @@ export const info = {
   searchLabel: null,
   searchInput: null,
 
+  contextContainer: null,
+  contextBackLink: null,
+
   progressRule: null,
   progressContainer: null,
   progressLabel: null,
@@ -46,9 +49,6 @@ export const info = {
   failedRule: null,
   failedContainer: null,
 
-  contextContainer: null,
-  contextBackLink: null,
-
   filterContainer: null,
   albumFilterLink: null,
   artistFilterLink: null,
@@ -127,6 +127,7 @@ export const info = {
     activeQueryContextPageName: {type: 'string'},
     activeQueryContextPagePathname: {type: 'string'},
     activeQueryContextPageColor: {type: 'string'},
+    zapActiveQueryContext: {type: 'boolean'},
 
     activeQueryResults: {
       type: 'json',
@@ -163,6 +164,8 @@ export function* bindSessionStorage() {
     yield 'activeQueryContextPageName';
     yield 'activeQueryContextPagePathname';
     yield 'activeQueryContextPageColor';
+    yield 'zapActiveQueryContext';
+
     yield 'activeQueryResults';
     yield 'activeFilterType';
     yield 'resultsScrollOffset';
@@ -302,6 +305,25 @@ export function addInternalListeners() {
 export function mutatePageContent() {
   if (!info.searchBox) return;
 
+  // Context section
+
+  info.contextContainer =
+    document.createElement('div');
+
+  info.contextContainer.classList.add('wiki-search-context-container');
+
+  info.contextBackLink =
+    document.createElement('a');
+
+  info.contextContainer.appendChild(
+    templateContent(info.backString, {
+      page: info.contextBackLink,
+    }));
+
+  cssProp(info.contextContainer, 'display', 'none');
+
+  info.searchBox.appendChild(info.contextContainer);
+
   // Progress section
 
   info.progressRule =
@@ -355,25 +377,6 @@ export function mutatePageContent() {
   info.searchBox.appendChild(info.failedRule);
   info.searchBox.appendChild(info.failedContainer);
 
-  // Context section
-
-  info.contextContainer =
-    document.createElement('div');
-
-  info.contextContainer.classList.add('wiki-search-context-container');
-
-  info.contextBackLink =
-    document.createElement('a');
-
-  info.contextContainer.appendChild(
-    templateContent(info.backString, {
-      page: info.contextBackLink,
-    }));
-
-  cssProp(info.contextContainer, 'display', 'none');
-
-  info.searchBox.appendChild(info.contextContainer);
-
   // Filter section
 
   info.filterContainer =
@@ -750,6 +753,20 @@ function recordActiveQueryContext() {
   const {session} = info;
 
   if (document.documentElement.dataset.urlKey === 'localized.home') {
+    session.activeQueryContextPageName = null;
+    session.activeQueryContextPagePathname = null;
+    session.activeQueryContextPageColor = null;
+    session.zapActiveQueryContext = true;
+    return;
+  }
+
+  // Zapping means subsequent searches don't record context.
+  if (session.zapActiveQueryContext) {
+    return;
+  }
+
+  // We also don't overwrite existing context.
+  if (session.activeQueryContextPagePathname) {
     return;
   }
 
@@ -780,11 +797,22 @@ function clearSidebarSearch() {
 
   state.searchStage = null;
 
+  clearActiveQuery();
+
+  hideSidebarSearchResults();
+}
+
+function clearActiveQuery() {
+  const {session} = info;
+
   session.activeQuery = null;
   session.activeQueryResults = null;
   session.resultsScrollOffset = null;
 
-  hideSidebarSearchResults();
+  session.activeQueryContextPageName = null;
+  session.activeQueryContextPagePathname = null;
+  session.activeQueryContextPageColor = null;
+  session.zapActiveQueryContext = false;
 }
 
 function clearSidebarFilter() {
@@ -1537,16 +1565,7 @@ function considerRecallingRecentSidebarSearch() {
 }
 
 function forgetRecentSidebarSearch() {
-  const {session} = info;
-
-  session.activeQuery = null;
-
-  session.activeQueryContextPageName = null;
-  session.activeQueryContextPagePathname = null;
-  session.activeQueryContextPageColor = null;
-
-  session.activeQueryResults = null;
-
+  clearActiveQuery();
   clearSidebarFilter();
 }