88003194f390b7e4c5978bcc0cee7df03518203d
chmalee
  Thu Apr 4 15:03:12 2024 -0700
Make collapse/expand buttons on hgGene not do a page refresh each time they are clicked, refs #3216

diff --git src/hg/hgGene/hgGene.c src/hg/hgGene/hgGene.c
index 811e1ec..ce1f9da 100644
--- src/hg/hgGene/hgGene.c
+++ src/hg/hgGene/hgGene.c
@@ -501,54 +501,75 @@
 {
 char *closeVarName = sectionCloseVar(section->name);
 char *vis = sectionSetting(section, "visibility");
 int defaultClosed = (vis && sameString(vis, "hide")) ? 1 : 0;
 return !(cartUsualInt(cart, closeVarName, defaultClosed));
 }
 
 void printSections(struct section *sectionList, struct sqlConnection *conn,
 	char *geneId)
 /* Print each section in turn. */
 {
 struct section *section;
 for (section = sectionList; section != NULL; section = section->next)
     {
     boolean isOpen = sectionIsOpen(section);
-    char *otherState = (isOpen ? "1" : "0");
     char *indicator = (isOpen ? "-" : "+");
     char *indicatorImg = (isOpen ? "../images/remove.gif" : "../images/add.gif");
     struct dyString *header = dyStringNew(0);
     //keep the following line for future debugging need
     //printf("<br>printing %s section\n", section->name);fflush(stdout);
     dyStringPrintf(header, "<A NAME=\"%s\"></A>", section->name);
-    char *closeVarName = sectionCloseVar(section->name);
-    dyStringPrintf(header, "<A HREF=\"%s?%s&%s=%s#%s\" class=\"bigBlue\"><IMG src=\"%s\" alt=\"%s\" class=\"bigBlue\"></A>&nbsp;&nbsp;",
-    	geneCgi, cartSidUrlString(cart), closeVarName, otherState, section->name, indicatorImg, indicator);
+    dyStringPrintf(header, "<IMG id=\"%sBtn\" src=\"%s\" alt=\"%s\" class=\"bigBlue\">&nbsp;&nbsp;",
+        section->name, indicatorImg, indicator);
     dyStringAppend(header, section->longLabel);
     webNewSection("%s",header->string);
     if (isOpen)
 	{
         long startTime = clock1000();
 	section->print(section, conn, geneId);
         section->printTime = clock1000() - startTime;
 	}
     else
 	{
 	printf("Press \"+\" in the title bar above to open this section.");
 	}
     dyStringFree(&header);
     }
+// add some simple javascript that can do the collapse/expand section buttons
+jsInlineF(""
+"function collapseSection() {\n"
+"    let toCollapse = this.parentNode.nextElementSibling;\n"
+"    let isHidden = toCollapse.style.display === \"none\";\n"
+"    if (isHidden) {\n"
+"        toCollapse.style = \"display: \";\n"
+"        this.src = \"../images/remove.gif\";\n"
+"        this.alt = \"-\";\n"
+"    } else {\n"
+"        this.src = \"../images/add.gif\";\n"
+"        toCollapse.style = \"display: none\";\n"
+"        this.alt = \"+\";\n"
+"    }\n"
+"}\n"
+"\n"
+"let btns = document.querySelectorAll(\"[id$=Btn]\");\n"
+"let i;\n"
+"for (i = 0; i < btns.length; i++) {\n"
+"    btn = btns[i];\n"
+"    btn.addEventListener('click', collapseSection);\n"
+"}\n"
+);
 }
 
 void printTiming(struct section *sectionList)
 /* Print timing for each section, if measureTiming is set */
 {
 if (!measureTiming)
     return;
 struct section *section;
 int total = 0;
 printf("<p><b>section, check time, print time, total</b><br>\n");
 for (section = sectionList; section != NULL; section = section->next)
     {
     boolean isOpen = sectionIsOpen(section);
     int sectionTime = section->checkTime + section->printTime;
     printf("%s, %d, %d, %d %s<br>\n", section->shortLabel, section->checkTime, section->printTime,