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/hgSearch.js src/hg/js/hgSearch.js index fe1d6f2..2a97818 100644 --- src/hg/js/hgSearch.js +++ src/hg/js/hgSearch.js @@ -499,33 +499,33 @@ } if (match.highlight) { url += url[url.length-1] !== '&' ? '&' : ''; url += "addHighlight=" + match.highlight; } } else { url = "hgc?db=" + db + "&g=" + hgcTitle + "&i=" + match.position + "&c=0&o=0&l=0&r=0" ; } matchTitle = match.posName; //if (match.canonical === true) matchTitle = "" + matchTitle + ""; } var newListObj; if (printCount < 500) { if (printCount + 1 > 10) { - newListObj = "
  • " + matchTitle + " - "; + newListObj = "
  • " + matchTitle + " - "; } printedPos = false; if (!(["helpDocs", "publicHubs", "trackDb"].includes(title))) { newListObj += match.position; printedPos = true; } if (match.description) { if (printedPos) {newListObj += " - ";} newListObj += match.description; } newListObj += "
  • "; list.innerHTML += newListObj; printCount += 1; } }); @@ -752,30 +752,31 @@ uiState.resultHash[match.name] = match; }); } else { // no results for this search uiState.resultHash = {}; uiState.positionMatches = []; } updateFilters(uiState); updateSearchResults(uiState); buildSpeciesDropdown(); fillOutAssemblies(); urlVars = {"db": db, "search": uiState.search, "showSearchResults": ""}; // changing the url allows the history to be associated to a specific url var urlParts = changeUrl(urlVars); $("#searchCategories").jstree(true).refresh(false,true); + saveLinkClicks(); if (doSaveHistory) saveHistory(uiState, urlParts); changeSearchResultsLabel(); } function handleRefreshState(jsonData) { if (checkJsonData(jsonData, 'handleRefreshState')) { updateStateAndPage(jsonData, true); } $("#spinner").remove(); } function handleErrorState(jqXHR, textStatus) { cart.defaultErrorCallback(jqXHR, textStatus); $("#spinner").remove(); @@ -823,30 +824,108 @@ } }, handleRefreshState, handleErrorState); // always update the results when a search has happened cart.flush(); } } function switchAssemblies(newDb) { // reload the page to attach curated hub (if any) re = /db=[\w,\.]*/; window.location = window.location.href.replace(re,"db="+newDb); } + function saveLinkClicks() { + // attach the link handlers to save search history + document.querySelectorAll(".searchResult>a").forEach(function(i,j,k) { + i.addEventListener("click", function(e) { + // i is the element, use parent elements and the href + // to construct a fake autocomplete option and save it + e.preventDefault(); // stops the page from redirecting until this function finishes + let callbackData = {}; + let trackName = i.parentNode.parentNode.parentNode.id.replace(/Results$/,""); + let matchList = uiState.positionMatches.find((matches) => matches.name === trackName); + let id, match, matchStr = i.childNodes[0].textContent; + // switch lookup depending on the different search categories: + let decoder = function(str) { + // helper decoder to change the html encoded entities in + // uiState.positionMatches.posName to what is actually rendered + // in matchStr + return $("