b7b15a261ae3e28941a2891ee568c9b472fcda7e angie Wed Jul 30 14:44:14 2014 -0700 Added jsMakeSetClearContainer / jsEndContainer to jsHelper.c, and apppliedthem to a couple long lists of checkboxes in hgVai. While at it, I moved jsHelper.c's javascript-inlined-in-HTML out to a new .js file, jsHelper.js. diff --git src/hg/lib/jsHelper.c src/hg/lib/jsHelper.c index 0e9701a..9914549 100644 --- src/hg/lib/jsHelper.c +++ src/hg/lib/jsHelper.c @@ -27,140 +27,43 @@ * We accomplish this by maintaining two forms - a mainForm and a * hiddenForm. The hiddenForm maintains echo's of all the variables * in the main form, which get updated onChange of controls that need * to 'ripple' to other controls. The onChange also submits the * control. */ 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" -"function f_scrollTop() {\n" -" return f_filterResults (\n" -" window.pageYOffset ? window.pageYOffset : 0,\n" -" document.documentElement ? document.documentElement.scrollTop : 0,\n" -" document.body ? document.body.scrollTop : 0\n" -" );\n" -"}\n" -"function f_filterResults(n_win, n_docel, n_body) {\n" -" var n_result = n_win ? n_win : 0;\n" -" if (n_docel && (!n_result || (n_result > n_docel)))\n" -" n_result = n_docel;\n" -" return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;\n" -"}\n" -"</script>\n"); + // jsh_pageVertPos trick taken from + // http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html + puts("<INPUT TYPE=HIDDEN NAME=\"jsh_pageVertPos\" VALUE=0>"); int pos = cgiOptionalInt("jsh_pageVertPos", 0); if (pos > 0) - printf("\n<script language=\"javascript\">" + printf("<script language=\"javascript\">" "window.onload = function () { window.scrollTo(0, %d); }" "</script>\n", pos); - jsWriteFunctions(); jsInited = TRUE; + jsIncludeFile("jsHelper.js", NULL); } } -void jsWriteFunctions() -/* Write out Javascript functions. */ -{ -hPrintf("\n<SCRIPT>\n"); -hPrintf("%s\n", -"function setRadioCheck(varName, value) \n" -"{\n" -"var len = document.mainForm.elements.length;\n" -"var i = 0;\n" -"for (i = 0; i < len; i++) \n" -" {\n" -" if (document.mainForm.elements[i].name == varName) \n" -" {\n" -" if (document.mainForm.elements[i].value == value)\n" -" document.mainForm.elements[i].checked = true;\n" -" else\n" -" document.mainForm.elements[i].checked = false;\n" -" }\n" -" }\n" -"}\n" -"\n" -"function getKeyCode(e)\n" -"{\n" -"if (window.event) // IE\n" -" {\n" -" return e.keyCode;\n" -" }\n" -"else \n" -" {\n" -" return e.which;\n" -" }\n" -"}\n" -"\n" -"function getKey(e)\n" -"{\n" -"return String.fromCharCode(getKeyCode(e));\n" -"}\n" -"\n" -"function gotEnterKey(e)\n" -"{\n" -"return getKeyCode(e) == 13;\n" -"}\n" -"\n" -"var submitted = false;\n" -"\n" -"function submitOnEnter(e,f)\n" -"{\n" -"if(gotEnterKey(e))\n" -" {\n" -" if (!submitted)\n" -" {\n" -" submitted = true;\n" -" f.submit();\n" -" }\n" -" return false;\n" -" }\n" -"else\n" -" return true;\n" -"}\n" -"\n" -"function noSubmitOnEnter(e)\n" -"{\n" -"return !gotEnterKey(e);\n" -"}\n" -"function pressOnEnter(e, button)\n" -"{\n" -"if (gotEnterKey(e))\n" -" {\n" -" button.click();\n" -" return false;\n" -" }\n" -"else\n" -" {\n" -" return true;\n" -" }\n" -"}\n" -); -hPrintf("</SCRIPT>\n"); -} - struct dyString *jsOnChangeStart() /* Start up an onChange string */ { struct dyString *dy = dyStringNew(1024); dyStringAppend(dy, "onChange=\""); return dy; } char *jsOnChangeEnd(struct dyString **pDy) /* Finish up javascript onChange command. */ { dyStringAppend(*pDy, "document.hiddenForm.submit();\""); return dyStringCannibalize(pDy); } @@ -264,30 +167,47 @@ "document.%s.jsh_pageVertPos.value = f_scrollTop(); ", form); return vertPosSet; } void jsMakeCheckboxGroupSetClearButton(char *buttonVar, boolean isSet) /* Make a button for setting or clearing a set of checkboxes with the same name. * Uses only javascript to change the checkboxes, no resubmit. */ { char javascript[256]; safef(javascript, sizeof(javascript), "var list = document.getElementsByName('%s'); " "for (var ix = 0; ix < list.length; ix++) {list[ix].checked = %s}", buttonVar, isSet ? "true" : "false"); cgiMakeOnClickButton(javascript, isSet ? JS_SET_ALL_BUTTON_LABEL : JS_CLEAR_ALL_BUTTON_LABEL); } +void jsMakeSetClearContainer() +/* Begin a wrapper div with class setClearContainer, plus 'Set all' and 'Clear all' buttons. + * This should be followed by a bunch of checkboxes, and then a call to jsEndContainer. */ +{ +puts("<div class=\"setClearContainer\">\n" + "<input type=button value=\""JS_SET_ALL_BUTTON_LABEL"\" title=\"Check all checkboxes\">\n" + "<input type=button value=\""JS_CLEAR_ALL_BUTTON_LABEL"\" title=\"Uncheck all checkboxes\">\n" + "<br>" + ); +} + +void jsEndContainer() +/* End a wrapper div. */ +{ +puts("</div>"); +} + char *jsPressOnEnter(char *button) /* Returns a javascript statement that clicks button when the Enter key * has been pressed; typically this would go in a text input. * jsInit must be called first. * Do not free return value! */ { if (! jsInited) errAbort("jsPressOnEnter: jsInit must be called first."); static char poe[2048]; safef(poe, sizeof(poe), "return pressOnEnter(event, %s);", button); return poe; } void jsIncludeFile(char *fileName, char *noScriptMsg) {