a20e4c95a5fde014130e2d04d61ce814cbe5413b
galt
Mon May 14 15:25:47 2018 -0700
CIRM facetedSearch
diff --git src/hg/lib/tablesTables.c src/hg/lib/tablesTables.c
index 92dcada..5375c02 100644
--- src/hg/lib/tablesTables.c
+++ src/hg/lib/tablesTables.c
@@ -1,552 +1,760 @@
/* tablesTables - this module deals with two types of tables SQL tables in a database,
* and fieldedTable objects in memory. It has routines to do sortable, filterable web
* displays on tables. */
#include "common.h"
#include "hash.h"
#include "obscure.h"
#include "linefile.h"
#include "jksql.h"
#include "jsHelper.h"
#include "sqlSanity.h"
#include "fieldedTable.h"
#include "cheapcgi.h"
+#include "htmshell.h"
#include "web.h"
#include "cart.h"
+#include "facetField.h"
#include "tablesTables.h"
struct fieldedTable *fieldedTableFromDbQuery(struct sqlConnection *conn, char *query)
/* Return fieldedTable from a database query */
{
struct sqlResult *sr = sqlGetResult(conn, query);
char **fields;
int fieldCount = sqlResultFieldArray(sr, &fields);
struct fieldedTable *table = fieldedTableNew(query, fields, fieldCount);
char **row;
int i = 0;
while ((row = sqlNextRow(sr)) != NULL)
fieldedTableAdd(table, row, fieldCount, ++i);
sqlFreeResult(&sr);
return table;
}
+struct fieldedTable *fieldedTableAndCountsFromDbQuery(struct sqlConnection *conn, char *query, int limit, int offset,
+ char *selectedFields, struct facetField ***pFfArray, int *pResultCount)
+/* Return fieldedTable from a database query and also fetch use and select counts */
+{
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **fields;
+int fieldCount = sqlResultFieldArray(sr, &fields);
+struct facetField **ffArray;
+AllocArray(ffArray, fieldCount);
+struct fieldedTable *table = fieldedTableNew(query, fields, fieldCount);
+
+struct facetField *ffList = facetFieldsFromSqlTableInit(fields, fieldCount, selectedFields, ffArray);
+
+char **row;
+int i = 0;
+int id = 0;
+char *nullVal = "n/a"; // TODO what do we want here?
+/* Scan through result saving it in list. */
+while ((row = sqlNextRow(sr)) != NULL)
+ {
+ if (perRowFacetFields(fieldCount, row, nullVal, ffArray))
+ {
+ if ((i >= offset) && (i < offset+limit))
+ fieldedTableAdd(table, row, fieldCount, ++id);
+ ++i;
+ }
+ }
+facetFieldsFromSqlTableFinish(ffList, facetValCmpSelectCountDesc);
+sqlFreeResult(&sr);
+*pFfArray = ffArray;
+*pResultCount = i;
+return table;
+}
+
static void showTableFilterInstructionsEtc(struct fieldedTable *table,
- char *itemPlural, struct fieldedTableSegment *largerContext, void (*addFunc)(void))
+ char *itemPlural, struct fieldedTableSegment *largerContext, void (*addFunc)(void),
+ char *visibleFacetList)
/* Print instructional text, and basic summary info on who passes filter, and a submit
* button just in case user needs it */
{
/* Print info on matching */
int matchCount = slCount(table->rowList);
if (largerContext != NULL) // Need to page?
matchCount = largerContext->tableSize;
cgiMakeButton("submit", "search");
printf("  ");
cgiMakeOnClickButton("clearButton",
"$(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');\n"
"$('[name=cdwBrowseFiles_page]').val('1');\n"
"$('#submit').click();\n"
, "clear search");
printf(" ");
printf("%d %s found. ", matchCount, itemPlural);
if (addFunc)
addFunc();
+if (!visibleFacetList)
+ {
printf(" \n");
printf("You can further filter search results field by field below. ");
printf("Wildcard * and ? characters are allowed in text fields. ");
printf(">min or <max are allowed in numerical fields. \n");
}
+}
static void printSuggestScript(char *id, struct slName *suggestList)
/* Print out a little javascript to wrap auto-suggester around control with given ID */
{
struct dyString *dy = dyStringNew(256);
dyStringPrintf(dy,"$(document).ready(function() {\n");
dyStringPrintf(dy," $('#%s').autocomplete({\n", id);
dyStringPrintf(dy," delay: 100,\n");
dyStringPrintf(dy," minLength: 0,\n");
dyStringPrintf(dy," source: [");
char *separator = "";
struct slName *suggest;
for (suggest = suggestList; suggest != NULL; suggest = suggest->next)
{
dyStringPrintf(dy,"%s\"%s\"", separator, suggest->name);
separator = ",";
}
dyStringPrintf(dy,"]\n");
dyStringPrintf(dy," });\n");
dyStringPrintf(dy,"});\n");
jsInline(dy->string);
dyStringFree(&dy);
}
static void printWatermark(char *id, char *watermark)
/* Print light text filter prompt as watermark. */
{
jsInlineF(
"$(function() {\n"
" $('#%s').watermark(\"%s\");\n"
"});\n", id, watermark);
}
static void resetPageNumberOnChange(char *id)
/* On change, reset page number to 1. */
{
jsInlineF(
"$(function() {\n"
" $('form').delegate('#%s','change keyup paste',function(e){\n"
" $('[name=cdwBrowseFiles_page]').val('1');\n"
" });\n"
"});\n"
, id);
}
static void showTableFilterControlRow(struct fieldedTable *table, struct cart *cart,
char *varPrefix, int maxLenField, struct hash *suggestHash)
/* Assuming we are in table already drow control row.
* The suggestHash is keyed by field name. If something is there we'll assume
* it's value is slName list of suggestion values */
{
/* Include javascript and style we need */
webIncludeResourceFile("jquery-ui.css");
jsIncludeFile("jquery.js", NULL);
jsIncludeFile("jquery.plugins.js", NULL);
jsIncludeFile("jquery-ui.js", NULL);
jsIncludeFile("jquery.watermark.js", NULL);
int i;
printf("
");
for (i=0; ifieldCount; ++i)
{
char *field = table->fields[i];
char varName[256];
safef(varName, sizeof(varName), "%s_f_%s", varPrefix, field);
webPrintLinkCellStart();
#ifdef MAKES_TOO_WIDE
/* Print out input control. As you can see from all the commented out bits
* this part has been a challenge. We'd like to make the input cell fit the
* table size, but if we do it with style it makes whole table wider. */
char *oldVal = cartUsualString(cart, varName, "");
printf("", varName, oldVal);
#endif /* MAKES_TOO_WIDE */
/* Approximate size of input control in characters */
int size = fieldedTableMaxColChars(table, i);
if (size > maxLenField)
size = maxLenField;
#ifdef ACTUALLY_WORKS
/* This way does work last I checked and is just a line of code.
* Getting an id= property on the input tag though isn't possible this way. */
cartMakeTextVar(cart, varName, "", size + 1);
#endif
/* Print input control getting previous value from cart. Set an id=
* so auto-suggest can find this control. */
char *oldVal = cartUsualString(cart, varName, "");
printf("\n",
varName, varName, size+1, oldVal);
/* Write out javascript to initialize autosuggest on control */
printWatermark(varName, " filter ");
/* Write out javascript to reset page number to 1 if filter changes */
resetPageNumberOnChange(varName);
if (suggestHash != NULL)
{
struct slName *suggestList = hashFindVal(suggestHash, field);
if (suggestList != NULL)
{
printSuggestScript(varName, suggestList);
}
}
webPrintLinkCellEnd();
}
printf("
");
}
static void showTableSortingLabelRow(struct fieldedTable *table, struct cart *cart, char *varPrefix,
char *returnUrl)
/* Put up the label row with sorting fields attached. ALso actually sort table. */
{
/* Get order var */
char orderVar[256];
safef(orderVar, sizeof(orderVar), "%s_order", varPrefix);
char *orderFields = cartUsualString(cart, orderVar, "");
char pageVar[64];
safef(pageVar, sizeof(pageVar), "%s_page", varPrefix);
/* Print column labels */
int i;
for (i=0; ifieldCount; ++i)
{
webPrintLabelCellStart();
printf("fields[i];
if (!isEmpty(orderFields) && sameString(orderFields, field))
printf("-");
printf("%s", field);
printf("\">");
printf("%s", field);
if (!isEmpty(orderFields))
{
char *s = orderFields;
boolean isRev = (s[0] == '-');
if (isRev)
++s;
if (sameString(field, s))
{
if (isRev)
printf("↑");
else
printf("↓");
}
}
printf("");
webPrintLabelCellEnd();
}
/* Sort on field */
if (!isEmpty(orderFields))
{
boolean doReverse = FALSE;
char *field = orderFields;
if (field[0] == '-')
{
field += 1;
doReverse = TRUE;
}
fieldedTableSortOnField(table, field, doReverse);
}
}
static void showTableDataRows(struct fieldedTable *table, int pageSize, int maxLenField,
struct hash *tagOutputWrappers, void *wrapperContext)
/* Render data rows into HTML */
{
int count = 0;
struct fieldedRow *row;
boolean isNum[table->fieldCount];
int i;
for (i=0; ifieldCount; ++i)
isNum[i] = fieldedTableColumnIsNumeric(table, i);
for (row = table->rowList; row != NULL; row = row->next)
{
if (++count > pageSize)
break;
printf("
\n");
int fieldIx = 0;
for (fieldIx=0; fieldIxfieldCount; ++fieldIx)
{
char shortVal[maxLenField+1];
char *longVal = emptyForNull(row->row[fieldIx]);
char *val = longVal;
int valLen = strlen(val);
if (maxLenField > 0 && maxLenField < valLen)
{
if (valLen > maxLenField)
{
memcpy(shortVal, val, maxLenField-3);
shortVal[maxLenField-3] = 0;
strcat(shortVal, "...");
val = shortVal;
}
}
if (isNum[fieldIx])
webPrintLinkCellRightStart();
else
webPrintLinkCellStart();
boolean printed = FALSE;
if (tagOutputWrappers != NULL && !isEmpty(val))
{
char *field = table->fields[fieldIx];
webTableOutputWrapperType *printer = hashFindVal(tagOutputWrappers, field);
if (printer != NULL)
{
printer(table, row, field, longVal, val, wrapperContext);
printed = TRUE;
}
}
if (!printed)
printf("%s", val);
webPrintLinkCellEnd();
}
printf("
\n");
}
}
static void showTablePaging(struct fieldedTable *table, struct cart *cart, char *varPrefix,
struct fieldedTableSegment *largerContext, int pageSize)
/* If larger context exists and is bigger than current display, then draw paging controls. */
{
/* Handle paging if any */
if (largerContext != NULL) // Need to page?
{
if (pageSize < largerContext->tableSize)
{
int curPage = largerContext->tableOffset/pageSize;
int totalPages = (largerContext->tableSize + pageSize - 1)/pageSize;
char id[256];
if ((curPage + 1) > 1)
{
// first page
safef(id, sizeof id, "%s_first", varPrefix);
printf("⏮", id);
jsOnEventByIdF("click", id,
"$('[name=%s_page]').val('1');\n"
"$('#submit').click();\n"
, varPrefix);
printf(" ");
// prev page
safef(id, sizeof id, "%s_prev", varPrefix);
printf("⏪", id);
jsOnEventByIdF("click", id,
"$('[name=%s_page]').val('%d');\n"
"$('#submit').click();\n"
, varPrefix, (curPage+1)-1);
printf(" ");
}
printf("Displaying page ");
char pageVar[64];
safef(pageVar, sizeof(pageVar), "%s_page", varPrefix);
cgiMakeIntVar(pageVar, curPage+1, 3);
printf(" of %d", totalPages);
if ((curPage + 1) < totalPages)
{
// next page
printf(" ");
safef(id, sizeof id, "%s_next", varPrefix);
printf("⏩", id);
jsOnEventByIdF("click", id,
"$('[name=%s_page]').val('%d');\n"
"$('#submit').click();\n"
, varPrefix, (curPage+1)+1);
// last page
printf(" ");
safef(id, sizeof id, "%s_last", varPrefix);
printf("⏭", id);
jsOnEventByIdF("click", id,
"$('[name=%s_page]').val('%d');\n"
"$('#submit').click();\n"
, varPrefix, totalPages);
}
}
}
}
void webFilteredFieldedTable(struct cart *cart, struct fieldedTable *table,
char *returnUrl, char *varPrefix,
int maxLenField, struct hash *tagOutputWrappers, void *wrapperContext,
boolean withFilters, char *itemPlural,
int pageSize, struct fieldedTableSegment *largerContext, struct hash *suggestHash,
+ struct facetField **ffArray, char *visibleFacetList,
void (*addFunc)(void))
/* Show a fielded table that can be sorted by clicking on column labels and optionally
* that includes a row of filter controls above the labels .
* The maxLenField is maximum character length of field before truncation with ...
* Pass in 0 for no max */
{
if (strchr(returnUrl, '?') == NULL)
errAbort("Expecting returnUrl to include ? in showFieldedTable\nIt's %s", returnUrl);
-if (withFilters)
- showTableFilterInstructionsEtc(table, itemPlural, largerContext, addFunc);
+if (withFilters || visibleFacetList)
+ showTableFilterInstructionsEtc(table, itemPlural, largerContext, addFunc, visibleFacetList);
+
+printf("