1a89c8e587e4cdbdb94430448caaa19247871cb8
tdreszer
  Thu Sep 22 13:01:11 2011 -0700
Big load of changes for subCfg.
diff --git src/hg/lib/jsHelper.c src/hg/lib/jsHelper.c
index d010079..704fe29 100644
--- src/hg/lib/jsHelper.c
+++ src/hg/lib/jsHelper.c
@@ -16,30 +16,32 @@
 #include "dystring.h"
 #include "cheapcgi.h"
 #include "cart.h"
 #include "hPrint.h"
 #include "hash.h"
 #include "jsHelper.h"
 #include "web.h"
 #include "hui.h"
 #include "hgConfig.h"
 #include "portable.h"
 
 static char const rcsid[] = "$Id: jsHelper.c,v 1.31 2009/09/10 04:19:26 larrym Exp $";
 
 static boolean jsInited = FALSE;
 
+struct jsonHashElement *jsonGlobalsHash = NULL;
+
 void jsInit()
 /* If this is the first call, set window.onload to the operations
  * performed upon loading a page and print supporting javascript.
  * Currently this just sets the page vertical position if specified on
  * CGI, and also calls jsWriteFunctions.
  * Subsequent calls do nothing, so this can be called many times. */
 {
 if (! jsInited)
     {
     puts(
 
 "<INPUT TYPE=HIDDEN NAME=\"jsh_pageVertPos\" VALUE=0>\n"
 "<script language=\"javascript\">\n"
 "// f_scrollTop and f_filterResults taken from\n"
 "// http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html\n"
@@ -504,30 +506,36 @@
 ele->hash = h;
 return ele;
 }
 
 struct jsonListElement *newJsonList(struct slRef *list)
 {
 struct jsonListElement *ele;
 AllocVar(ele);
 ele->type = jsonList;
 ele->list = list;
 return ele;
 }
 
 void jsonHashAdd(struct jsonHashElement *h, char *name, struct jsonElement *ele)
 {
+if (h == NULL)  // If hash isn't provided, assume global
+    {
+    if (jsonGlobalsHash == NULL)
+        jsonGlobalsHash = newJsonHash(newHash(8));
+    h = jsonGlobalsHash;
+    }
 hashReplace(h->hash, name, ele);
 }
 
 void jsonHashAddString(struct jsonHashElement *h, char *name, char *val)
 {
 // Add a string to a hash which will be used to print a javascript object;
 // existing values are replaced.
 val = javaScriptLiteralEncode(val);
 char *str = needMem(strlen(val) + 3);
 sprintf(str, "'%s'", val);
 freez(&val);
 jsonHashAdd(h, name, (struct jsonElement *) newJsonString(str));
 }
 
 void jsonHashAddNumber(struct jsonHashElement *h, char *name, long val)
@@ -658,28 +666,42 @@
 }
 
 void jsonPrint(struct jsonElement *json, char *name, int indentLevel)
 {
 // print out a jsonElement
 
 char *indentBuf = makeIndentBuf(indentLevel);
 if(name != NULL)
     hPrintf("// START %s\n%svar %s = ", name, indentBuf, name);
 jsonPrintRecurse(json, indentLevel);
 if(name != NULL)
     hPrintf("%s;\n// END %s\n", indentBuf, name);
 freez(&indentBuf);
 }
 
+void jsonPrintGlobals(boolean wrapWithScriptTags)
+// prints out the "common" globals json hash
+// This hash is the one utils.js and therefore all CGIs know about
+{
+if (jsonGlobalsHash != NULL)
+    {
+    if (wrapWithScriptTags)
+        printf("<script type='text/javascript'>\n");
+    jsonPrint((struct jsonElement *) jsonGlobalsHash, "common", 0);
+    if (wrapWithScriptTags)
+        printf("</script>\n");
+    }
+}
+
 void jsonErrPrintf(struct dyString *ds, char *format, ...)
 //  Printf a json error to a dyString for communicating with ajax code; format is:
 //  {"error": error message here}
 {
 va_list args;
 va_start(args, format);
 dyStringPrintf(ds, "{\"error\": \"");
 struct dyString *buf = newDyString(1000);
 dyStringVaPrintf(buf, format, args);
 dyStringAppend(ds, javaScriptLiteralEncode(dyStringCannibalize(&buf)));
 dyStringPrintf(ds, "\"}");
 va_end(args);
 }