f1a5c6783582c33961b05d700690fbd1cae20363 larrym Thu Jul 7 17:25:06 2011 -0700 add jsPrintHash and related routines diff --git src/hg/lib/jsHelper.c src/hg/lib/jsHelper.c index 5dc100a..87d41d4 100644 --- src/hg/lib/jsHelper.c +++ src/hg/lib/jsHelper.c @@ -475,15 +475,65 @@ "onclick=\"return setTableRowVisibility(this, '%s', '%s.section', 'section', true);\" " "id='%s_button' src='%s' alt='%s' title='%s this section' class='bigBlue'" " style='cursor:pointer;'>\n", section, track, section, buttonImage, (isOpen ? "-" : "+"), (isOpen ? "Collapse": "Expand")); printf(" %s\n", sectionTitle); printf("", isOpen ? "" : "style='display: none' ", section, 1); } void jsEndCollapsibleSection() /* End the collapsible . */ { puts(""); } +void jsAddString(struct hash *h, char *name, char *val) +{ +// Add a string to a hash which will be used to print a javascript object; +// existing values are replaced. +char *str = needMem(strlen(val) + 3); +val = javaScriptLiteralEncode(val); +sprintf(str, "'%s'", val); +freez(&val); +hashReplace(h, name, str); +} + +void jsAddNumber(struct hash *h, char *name, long val) +{ +// Add a number to a hash which will be used to print a javascript object; +// existing values are replaced. +char buf[256]; +safef(buf, sizeof(buf), "%ld", val); +hashReplace(h, name, cloneString(buf)); +} + +void jsAddBoolean(struct hash *h, char *name, boolean val) +{ +// Add a boolean to a hash which will be used to print a javascript object; +// existing values are replaced. +hashReplace(h, name, cloneString(val ? "true" : "false")); +} + +void jsPrintHash(struct hash *hash, char *name, int indentLevel) +{ +// prints a hash as a javascript variable + +int i; +char *indentBuf; +indentBuf = needMem(indentLevel + 1); +for (i = 0; i < indentLevel; i++) + indentBuf[i] = '\t'; +indentBuf[i] = 0; +if(hashNumEntries(hash)) + { + struct hashEl *el, *list = hashElListHash(hash); + slSort(&list, hashElCmp); + hPrintf("%svar %s = {\n", indentBuf, name); + for (el = list; el != NULL; el = el->next) + { + hPrintf("%s\t%s: %s%s\n", indentBuf, el->name, (char *) el->val, el->next == NULL ? "" : ","); + } + hPrintf("%s};\n", indentBuf); + hashElFreeList(&list); + } +}