40b8d62768262d2fb46e26c87a5ec948a6cb0251
braney
  Wed Dec 11 13:03:51 2024 -0800
checking in some code to hgSuggest that supports javascript autocomplete
when seaching for tracks in trackDb

diff --git src/hg/hgSuggest/hgSuggest.c src/hg/hgSuggest/hgSuggest.c
index c90dcf3..f6fe982 100644
--- src/hg/hgSuggest/hgSuggest.c
+++ src/hg/hgSuggest/hgSuggest.c
@@ -7,30 +7,52 @@
 #include "jksql.h"
 #include "hdb.h"
 #include "cheapcgi.h"
 #include "htmshell.h"
 #include "dystring.h"
 #include "jsonParse.h"
 #include "jsonWrite.h"
 #include "suggest.h"
 #include "genbank.h"
 #include "hgFind.h"
 #include "trix.h"
 
 // Optional CGI param type can specify what kind of thing to suggest (default: gene)
 #define ALT_OR_PATCH "altOrPatch"
 #define HELP_DOCS "helpDocs"
+#define TRACK "track"
+
+void suggestTrack(char *database,  char *prefix)
+{
+char query[4096];
+sqlSafef(query, sizeof query,
+    "select tableName, shortLabel from trackDb where shortLabel like '%%%s%%'", prefix);
+struct sqlConnection *conn = hAllocConn(database);
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **row;
+struct jsonWrite *jw = jsonWriteNew();
+jsonWriteListStart(jw, NULL);
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    jsonWriteObjectStart(jw, NULL);
+    jsonWriteString(jw, "label", row[1]);
+    jsonWriteObjectEnd(jw);
+    }
+jsonWriteListEnd(jw);
+puts(jw->dy->string);
+jsonWriteFree(&jw);
+}
 
 void suggestGene(char *database, char *table, char *prefix)
 /* Print out a Javascript list of objects describing genes that start with prefix. */
 {
 struct dyString *str = dyStringNew(10000);
 dyStringPrintf(str, "[\n");
 
 int exact = cgiOptionalInt("exact", 0);
 boolean hasKnownCanonical = sameString(table, "knownCanonical");
 initGenbankTableNames(database);
 char query[2048];
 if(exact)
     {
     // NOTE that exact is no longer used by the UI as of v271, but there are still some robots
     // using it so we still support it.
@@ -249,31 +271,31 @@
     }
 jsonWriteListEnd(jw);
 puts(jw->dy->string);
 jsonWriteFree(&jw);
 }
 
 char *checkParams(char *database, char *prefix, char *type)
 /* If we don't have valid CGI parameters, quit with a Bad Request HTTP response. */
 {
 pushWarnHandler(htmlVaBadRequestAbort);
 pushAbortHandler(htmlVaBadRequestAbort);
 if(prefix == NULL || database == NULL)
     errAbort("%s", "Missing prefix and/or db CGI parameter");
 if (! hDbIsActive(database))
     errAbort("'%s' is not a valid, active database", htmlEncode(database));
-if (isNotEmpty(type) && differentString(type, ALT_OR_PATCH) && differentString(type, HELP_DOCS))
+if (isNotEmpty(type) && differentString(type, ALT_OR_PATCH) && differentString(type, HELP_DOCS) && differentString(type, TRACK))
     errAbort("'%s' is not a valid type", type);
 char *table = NULL;
 if (! sameOk(type, ALT_OR_PATCH) && !sameOk(type, HELP_DOCS))
     {
     char *knownDatabase = hdbDefaultKnownDb(database);
     struct sqlConnection *conn = hAllocConn(knownDatabase);
     table = connGeneSuggestTable(conn);
     hFreeConn(&conn);
     if(table == NULL)
         errAbort("gene autosuggest is not supported for db '%s'", database);
     }
 popWarnHandler();
 popAbortHandler();
 return table;
 }
@@ -283,21 +305,23 @@
 long enteredMainTime = clock1000();
 
 cgiSpoof(&argc, argv);
 char *database = cgiOptionalString("db");
 char *prefix = cgiOptionalString("prefix");
 char *type = cgiOptionalString("type");
 char *table = checkParams(database, prefix, type);
 
 puts("Content-Type:text/plain");
 puts("\n");
 
 if (sameOk(type, ALT_OR_PATCH))
     suggestAltOrPatch(database, prefix);
 else if (sameOk(type, HELP_DOCS))
     suggestHelpPage(prefix);
+else if (sameOk(type, TRACK))
+    suggestTrack(database, prefix);
 else
     suggestGene(database, table, prefix);
 
 cgiExitTime("hgSuggest", enteredMainTime);
 return 0;
 }