302062e64efcf0d93b1955d4890e7456c5fdd524
chmalee
  Fri Nov 13 09:50:56 2020 -0800
hgPublicSessions: Do not overwrite previous search if there is no 'search' variable on the URL, refs #18470

diff --git src/hg/hgPublicSessions/hgPublicSessions.c src/hg/hgPublicSessions/hgPublicSessions.c
index 9717dc0..3668479 100644
--- src/hg/hgPublicSessions/hgPublicSessions.c
+++ src/hg/hgPublicSessions/hgPublicSessions.c
@@ -126,71 +126,80 @@
     "%s s left join gbMembers m on m.userName = s.userName where shared = 2%s"
     , namedSessionTable, otherConstraints);
 sr = sqlGetResult(conn, query);
 
 while ((row = sqlNextRow(sr)) != NULL)
     {
     gal = galLoad(row);
     slAddHead (&galList, gal);
     }
 
 sqlFreeResult(&sr);
 hDisconnectCentral(&conn);
 return galList;
 }
 
-void galleryDisplay(struct galleryEntry *galList, char *searchString)
+void galleryDisplay(struct galleryEntry *galList)
 /* Print a table containing the gallery data from galList */
 {
 struct galleryEntry *thisSession = galList;
+boolean searchStrExists = cgiVarExists("search");
+char *searchStr = NULL;
+if (searchStrExists)
+    searchStr = cgiOptionalString("search");
 
 /* Hide the orderable columns and disable ordering on the visible columns
  * https://datatables.net/reference/option/columnDefs for more info.
  * Then set up the ordering drop-down menu */
 jsInlineF(
    "$(document).ready(function () {\n"
     "    $('#sessionTable').DataTable({\"columnDefs\": [{\"visible\":false, \"targets\":[2,3]},\n"
     "                                                   {\"orderable\":false, \"targets\":[0,1]}\n"
     "                                                  ],\n"
     "                                       \"dom\":\"lftip\",\n"
     "                                       \"stateSave\":true,\n"
     "                                       \"stateSaveCallback\": %s,\n"
     "                                       \"stateLoadCallback\": %s,\n"
-    "                                });\n"
-    /* Recover previous sorting/searching choice from the cart settings, if available */
-    "    $('#sessionTable').DataTable().search(\"%s\").draw();\n"
+    "                                });\n",
+    jsDataTableStateSave(hgPublicSessionsPrefix), jsDataTableStateLoad(hgPublicSessionsPrefix, cart));
+
+// the user may have cleared the previous search via cgi option, or tried a new search:
+if (searchStrExists)
+    jsInlineF("     $('#sessionTable').DataTable().search(\"%s\").draw();\n", searchStr);
+
+/* Recover previous sorting choice from the cart settings, if available */
+jsInlineF(
     "    var startOrder = $('#sessionTable').DataTable().order();\n"
     "    if (startOrder[0][0] == 3) {\n"
     "        if (startOrder[0][1] == \"asc\") {\n"
     "            $('#sortMethod').val(\"useAsc\");\n"
     "        } else {\n"
     "            $('#sortMethod').val(\"useDesc\");\n"
     "        }\n"
     "    } else {\n"
     "        if (startOrder[0][0] == 2) {\n"
     "            if (startOrder[0][1] == \"asc\") {\n"
     "                $('#sortMethod').val(\"dateAsc\");\n"
     "            } else {\n"
     "                $('#sortMethod').val(\"dateDesc\");\n"
     "            }\n"
     "        } else {\n"
     "            $('#sessionTable').DataTable().order([2,'desc']).draw();\n"
     "            $('#sortMethod').val(\"dateDesc\");\n"
     "        }\n"
     "    }\n"
-    "});\n",
-    jsDataTableStateSave(hgPublicSessionsPrefix), jsDataTableStateLoad(hgPublicSessionsPrefix, cart), searchString != NULL ? searchString : "");
+    "});\n");
 
 jsInline(
    "function changeSort() {\n"
     "    var newSort = document.getElementById('sortMethod').value;\n"
     "    var theTable = $('#sessionTable').DataTable();\n"
     "    if (newSort == \"useDesc\") {theTable.order([3,'desc']).draw(); }\n"
     "    if (newSort == \"useAsc\") {theTable.order([3,'asc']).draw(); }\n"
     "    if (newSort == \"dateDesc\") {theTable.order([2,'desc']).draw(); }\n"
     "    if (newSort == \"dateAsc\") {theTable.order([2,'asc']).draw(); }\n"
     "}\n");
 
 printf ("<p>\n");
 printf ("<b>Sort by:</b> <select id=\"sortMethod\">\n");
 printf ("\t\t<option value=\"useDesc\">Popularity (descending)</option>\n");
 printf ("\t\t<option value=\"useAsc\">Popularity (ascending)</option>\n");
@@ -247,62 +256,61 @@
     printf ("\t\t</td>\n");
     struct tm creationDate;
     ZeroVar(&creationDate);
     strptime(thisSession->firstUse, "%Y-%m-%d %T", &creationDate);
     /* Hidden columns */
     printf ("\t\t<td>%ld</td>\n", mktime(&creationDate));
     printf ("\t\t<td>%ld</td>\n", thisSession->useCount);
     printf ("\t</tr>\n");
     thisSession = thisSession->next;
     }
 
 printf ("</tbody>\n");
 printf ("</table>\n");
 }
 
-void showGalleryTab (char *searchString)
+void showGalleryTab ()
 /* Rather boring now, but a placeholder against the day that there's also a "favorites" tab */
 {
 struct galleryEntry *galList = galleryFetch();
-galleryDisplay(galList, searchString);
+galleryDisplay(galList);
 }
 
 void doMiddle(struct cart *theCart)
 /* Set up globals and make web page */
 {
 cart = theCart;
 char *db = cartUsualString(cart, "db", hDefaultDb());
-char *searchString = cgiOptionalString("search");
 cartWebStart(cart, db, "Public Sessions");
 
 /* Not in a form; can't use cartSaveSession() to set up an hgsid input */
 jsInlineF("var common = {hgsid:\"%s\"};\n", cartSessionId(cart));
 
 jsIncludeDataTablesLibs();
 
 printf("<p>Sessions allow users to save snapshots of the Genome Browser "
 "and its current configuration, including displayed tracks, position, "
 "and custom track data. The Public Sessions tool allows users to easily "
 "share those sessions that they deem interesting with the rest of the "
 "world's researchers. You can add your own sessions to this list by "
 "checking the appropriate box on the "
 "<a href=\"../cgi-bin/hgSession?%s\">Session Management</a> page.</p>\n"
 "<p>See the "
 "<a href=\"../goldenPath/help/hgSessionHelp.html\">Sessions User's Guide</a> "
 "for more information.\n</p>", cartSidUrlString(cart));
 
-showGalleryTab(searchString);
+showGalleryTab();
 
 cartWebEnd();
 }
 
 /* Null terminated list of CGI Variables we don't want to save
  * permanently. */
 char *excludeVars[] = {"Submit", "submit", NULL,};
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 cgiSpoof(&argc, argv);
 cartEmptyShell(doMiddle, hUserCookie(), excludeVars, oldVars);
 return 0;
 }