14dbfe57d9b670fa554c64cd4604e37fdefe037d
chmalee
  Fri Jan 5 15:33:17 2024 -0800
Working version of saving last five user entered terms in the search bar, refs #29611

diff --git src/hg/js/utils.js src/hg/js/utils.js
index df46939..d47aece 100644
--- src/hg/js/utils.js
+++ src/hg/js/utils.js
@@ -1237,30 +1237,37 @@
     if (db)
         return db.value;
 
     db = getURLParam(window.location.href, "db");
     if (db.length > 0)
         return db;
 
     // This may be moved to 1st position as the most likely source
     if (typeof(common) !== 'undefined' && common.db)
         return common.db;
 
     db = normed($("input#db").first());
     if (db)
         return db.value;
 
+    if (typeof uiState !== "undefined" && uiState.db)
+        return uiState.db;
+
+    db = document.getElementById("selectAssembly");
+    if (db)
+        return db.selectedOptions[0].value;
+
     return "";
 }
 
 function undecoratedTrack(track)
 // return the track name with any hub_id_ stripped
 {
 var retTrack = track;
 if (track.startsWith("hub_")) {
     retTrack = track.split('_', 3)[2];
 }
 return retTrack;
 }
 
 function getTrack()
 {
@@ -4238,15 +4245,73 @@
     logUrl = currUrl.protocol + "//" + currUrl.serverName + "/" + currUrl.pathInfo + "?hgsid=" + getHgsid() + "&_dumpCart=" + encodeURIComponent(seconds) + "&skipNotif=" + skipNotification;
     let xmlhttp = new XMLHttpRequest();
     xmlhttp.open("GET", logUrl, true);
     xmlhttp.send();  // sends request and exits this function
 }
 
 function writeToApacheLog(msg) {
     // send msg to web servers error_log
     // first need to figure out what server and CGI we are requesting:
     let currUrl = parseUrl(window.location.href);
     logUrl = currUrl.protocol + "//" + currUrl.serverName + "/" + currUrl.pathInfo + "?_dumpToLog=" + encodeURIComponent(msg);
     let xmlhttp = new XMLHttpRequest();
     xmlhttp.open("GET", logUrl, true);
     xmlhttp.send();  // sends request and exits this function
 }
+
+function addRecentSearch(db, searchTerm, extra={}) {
+    // Push a searchTerm onto a stack in localStorage to show users their most recent
+    // search terms. If an optional extra argument is supplied (ex: the response from hgSuggest),
+    // save that as well
+    // The searchStack object (note: saved as a string via JSON.stringify in localStorage) keeps
+    // a per database stack of the 5 most recently searched terms, as well as their "result",
+    // which can be an autocomplete object from hgSuggest, something from hgSearch, or just nothing
+    // Example:
+    // var searchStack = {
+    //  hg38: {
+    //   "stack": ["foxp", "flag", "fla"],
+    //   "results: {
+    //     "foxp": {
+    //       "value": "FOXP1 (Homo sap...",
+    //       "id": "chr3:70954708-71583728",
+    //       ...
+    //     },
+    //     "flag": {}, // NOTE: empty object
+    //     "fla": {
+    //       "value": ...,
+    //       "id": ...,
+    //     },
+    //   }
+    // },
+    // mm10: {
+    //  "stack": [...],
+    //  "results": {},
+    // }
+    let searchStack = window.localStorage.getItem("searchStack");
+    let searchObj = {};
+    if (searchStack === null) {
+        searchObj[db] = {"stack": [searchTerm], "results": {}};
+        searchObj[db].results[searchTerm] = extra;
+        window.localStorage.setItem("searchStack", JSON.stringify(searchObj));
+    } else {
+        searchObj = JSON.parse(searchStack);
+        if (db in searchObj) {
+            let searchList = searchObj[db].stack;
+            if (searchList.includes(searchTerm)) {
+                // remove it from wherever it is cause it's going to the front
+                searchList.splice(searchList.indexOf(searchTerm), 1);
+            } else {
+                searchObj[db].results[searchTerm] = extra;
+                if (searchList.length >= 5) {
+                    let toDelete = searchList.pop();
+                    delete searchObj[db].results[toDelete];
+                }
+            }
+            searchList.unshift(searchTerm);
+            searchObj.stack = searchList;
+        } else {
+            searchObj[db] = {"stack": [searchTerm], "results": {}};
+            searchObj[db].results[searchTerm] = extra;
+        }
+        window.localStorage.setItem("searchStack", JSON.stringify(searchObj));
+    }
+}