javascript.tcl

Go to the documentation of this file.
00001 /*  javascript.tcl --*/
00002 /* */
00003 /*  This file contains procedures that create HTML and Java Script*/
00004 /*  functions that implement objects such as:*/
00005 /* */
00006 /*      paired multi-selection boxes*/
00007 /*      guarded submit buttons*/
00008 /*      parent and child checkboxes*/
00009 /* */
00010 /*  Copyright (c) 1998-2000 by Ajuba Solutions.*/
00011 /* */
00012 /*  See the file "license.terms" for information on usage and redistribution*/
00013 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00014 /* */
00015 /*  RCS: @(#) $Id: javascript.tcl,v 1.5 2005/09/30 05:36:39 andreas_kupries Exp $*/
00016 
00017 package require Tcl 8
00018 package require ncgi 1
00019 package provide javascript 1.0.2
00020 
00021 
00022 namespace ::javascript {
00023 
00024     /*  The SelectionObjList namespace variable is used to keep the list of*/
00025     /*  selection boxes that were created as parts of paired multi-selection*/
00026     /*  boxes.  When a submit button is made for pages that have paired*/
00027     /*  multi-selection boxes, we set a hidden field to store the initial values*/
00028     /*  in the box.*/
00029 
00030     variable SelectionObjList {}
00031 }
00032 
00033 /*  ::javascript::BeginJS --*/
00034 /* */
00035 /*  Create HTML code to begin a java script program.*/
00036 /* */
00037 /*  Arguments:*/
00038 /*  none.*/
00039 /* */
00040 /*  Results:*/
00041 /*  Returns HTML code.*/
00042 
00043 ret  ::javascript::BeginJS () {
00044     return "\n<SCRIPT LANGUAGE=\"JavaScript\">\n"
00045 }
00046 
00047 /*  ::javascript::EndJS --*/
00048 /* */
00049 /*  Create HTML code to end a java script program.*/
00050 /* */
00051 /*  Arguments:*/
00052 /*  none.*/
00053 /* */
00054 /*  Results:*/
00055 /*  Returns HTML code.*/
00056 
00057 ret  ::javascript::EndJS () {
00058     return "\n</SCRIPT>\n"
00059 }
00060 
00061 /*  ::javascript::MakeMultiSel --*/
00062 /* */
00063 /*  Construct HTML code to create a multi-selection box.*/
00064 /* */
00065 /*  Arguments:*/
00066 /*  id      The suffix of all HTML objects in this megawidget.*/
00067 /*  side        Either "left" or "right".*/
00068 /*  eltValues   The values to populate the selection box with.*/
00069 /*  eltNames    The values to populate the selection box with.*/
00070 /*  emptyElts   The number of empty box entry to stuff in the*/
00071 /*          Selection box as placeholders for elts to be added.*/
00072 /*  length      The number of elts to show before adding a vertical*/
00073 /*          scrollbar.*/
00074 /*  minWidth    Number of spaces to determin the minimum box width.*/
00075 /* */
00076 /*  Results:*/
00077 /*  Returns HTML to show the selection box.*/
00078 
00079 ret  ::javascript::MakeMultiSel (type id , type side , type eltValues , type eltNames , type emptyElts \
00080     , type length , type minWidth) {
00081 
00082     variable SelectionObjList
00083 
00084     # Add this selection box to the list.
00085 
00086     set name "$side$id"
00087     lappend SelectionObjList $name
00088 
00089     # Create the selection box and populate it with elts.
00090 
00091     set html ""
00092     append html "<select name=$name multiple size=$length>"
00093     foreach elt $eltValues name $eltNames {
00094     set encodedElt [ncgi::encode $elt]
00095     append html "<option value=$encodedElt>$name"
00096     }
00097 
00098     # Add empty values for the remaining elements.
00099 
00100     for {set i 0} {$i < $emptyElts} {incr i} {
00101     append html "<option value=\"\"> "
00102     }
00103 
00104     # Add an empty value with text that is as wide as the minWidth.
00105 
00106     set filler ""
00107     for {set i 0} {$i < $minWidth} {incr i} {
00108     append filler "&nbsp;&nbsp;"
00109     }
00110     append html "<option value=\"\">$filler"
00111 
00112     append html "</select>"
00113     return $html
00114 }
00115 
00116 /*  ::javascript::MakeClickProc --*/
00117 /* */
00118 /*  Create a "moveSelected$id" java script procedure to move selected items*/
00119 /*  from one selection box to the other.*/
00120 /* */
00121 /*  Arguments:*/
00122 /*  id  The suffix of all objects in this multiselection megawidget.*/
00123 /* */
00124 /*  Results:*/
00125 /*  Returns java script code.*/
00126 
00127 ret  ::javascript::MakeClickProc (type id) {
00128 
00129     set result "\nfunction moveSelected${id}(fromObj,toObj) \{\n"
00130 
00131     # If nothing is selected, do nothing.
00132 
00133     append result "\n    if (fromObj.selectedIndex > -1) \{"
00134 
00135     # Find the first empty element in the toObj.
00136 
00137     append result {
00138         for (var k = 0; toObj.options[k].value != ""; k++) {}
00139 }
00140 
00141     # Move the selected elements from the fromObj to the end of the toObj.
00142     # Shift the objects in the fromObj to fill any empty spots.
00143     # Clear out any extra slots in the fromObj.
00144     # Deselect any selected elements (deselect with both 'selected = false'
00145     # and by setting selectedIndex to -1, because setting selectedIndex to
00146     # -1 didn't seem to clear selection on all windows browsers.
00147 
00148     append result {
00149         for (var i = fromObj.selectedIndex, j = fromObj.selectedIndex; fromObj.options[i].value != ""; i++) {
00150             if (fromObj.options[i].selected) {
00151                 toObj.options[k].text = fromObj.options[i].text
00152                 toObj.options[k++].value = fromObj.options[i].value
00153                 fromObj.options[i].selected = false
00154             } else {
00155                 fromObj.options[j].text = fromObj.options[i].text
00156                 fromObj.options[j++].value = fromObj.options[i].value
00157             }
00158         }
00159         for (; j < i; j++) {
00160             fromObj.options[j].text = ""
00161             fromObj.options[j].value = ""
00162         }
00163         fromObj.selectedIndex = -1
00164 }
00165 
00166     # Close the if statement and the function
00167 
00168     append result "    \}
00169 \}
00170 "
00171     return $result
00172 }
00173 
00174 /*  ::javascript::makeSelectorWidget --*/
00175 /* */
00176 /*  Construct HTML code to create a dual-multi-selection megawidget.  This*/
00177 /*  megawidget consists of two side-by-side multi-selection boxes*/
00178 /*  separated by a left arrow and a right arrow button.  The right arrow*/
00179 /*  button moves all items selected in the left box to the right box.  The*/
00180 /*  left arrow button moves all items selected in the right box to the left*/
00181 /*  box.*/
00182 /* */
00183 /*  Arguments:*/
00184 /*  id      The suffix of all HTML objects in this megawidget.*/
00185 /*  leftLabel   The text that appears above the left selection box.*/
00186 /*  leftValueList   The values of items in the left selection box.*/
00187 /*  leftNameList    The names to appear in the left selection box.*/
00188 /*  rightLabel  The text that appears above the right selection box.*/
00189 /*  rightValueList  The values of items in the right selection box.*/
00190 /*  rightNameList   The names to appear in the right selection box.*/
00191 /*  length      (optional) The number of elts to show before adding a*/
00192 /*          vertical scrollbar.  Defaults to 8.*/
00193 /*  minWidth    (optional) The number of spaces to determin the*/
00194 /*          minimum box width.  Defaults to 32.*/
00195 /* */
00196 /*  Results:*/
00197 /*  Returns HTML to show the dual-multi-selection megawidget.*/
00198 
00199 ret  ::javascript::makeSelectorWidget (type id , type leftLabel , type leftValueList , type leftNameList \
00200     , type rightLabel , type rightValueList , type rightNameList , optional length =8 , optional minWidth =32) {
00201 
00202     set html ""
00203     append html [BeginJS] \
00204         [MakeClickProc $id] \
00205         [EndJS]
00206 
00207     append html "<table border=0 cellspacing=0 cellpadding=2>\n<tr><th>" \
00208         $leftLabel "</th><th></th><th>" $rightLabel "</th></tr>\n<tr>"
00209 
00210     set leftLen [llength $leftValueList]
00211     set rightLen [llength $rightValueList]
00212     set len [expr {$leftLen + $rightLen}]
00213 
00214     append html "<td valign=top colspan=1>" \
00215         [MakeMultiSel $id "left" $leftValueList $leftNameList \
00216         $rightLen $length $minWidth] \
00217         "&nbsp;&nbsp;</td>\n"
00218 
00219     append html "<td>" \
00220         "<table border=0 cellspacing=0 cellpadding=2>\n"
00221 
00222     set args "this.form.left${id},this.form.right${id}"
00223 
00224     append html "<tr><td><input type=button name=left${id}Button
00225     onClick=\"moveSelected${id}(${args})\" value=\" >> \"></td></tr>"
00226 
00227     set args "this.form.right${id},this.form.left${id}"
00228 
00229     append html "<tr><td><input type=button name=right${id}Button
00230     onClick=\"moveSelected${id}(${args})\" value=\" << \"></td></tr>"
00231 
00232     append html "</table>\n" \
00233         "</td>\n"
00234 
00235     append html "<td valign=top colspan=1>" \
00236         [MakeMultiSel $id "right" $rightValueList $rightNameList \
00237         $leftLen $length $minWidth] \
00238         "&nbsp;&nbsp;</td>\n"
00239 
00240     append html "</tr>\n" \
00241         "</table>\n"
00242 
00243     # Add a hidden field to collect the data.
00244 
00245     append html "<input type=hidden name=valleft${id} " \
00246         "value=\"$leftValueList\">\n" \
00247         "<input type=hidden name=valright${id} " \
00248         "value=\"$rightValueList\">\n"
00249 
00250     return $html
00251 }
00252 
00253 /*  ::javascript::makeSubmitButton --*/
00254 /* */
00255 /*  Create an HTML submit button that resets a hidden field for each*/
00256 /*  registered multi-selection box.*/
00257 /* */
00258 /*  Arguments:*/
00259 /*  name    the name of the HTML button object to create.*/
00260 /*  value   the label of the HTML button object to create.*/
00261 /* */
00262 /*  Results:*/
00263 /*  Returns HTML submit button code.*/
00264 
00265 ret  ::javascript::makeSubmitButton (type name , type value) {
00266     variable SelectionObjList
00267     set html ""
00268 
00269     # Create the java script procedure that gathers the current values for each
00270     # registered multi-selection box.
00271 
00272     append html [BeginJS]
00273     append html "\nfunction getSelections(form) \{\n"
00274 
00275     # For each registered selection box, reset hidden field to
00276     # store nonempty values.
00277 
00278     foreach obj $SelectionObjList {
00279     set selObj "form.$obj"
00280     set hiddenObj "form.val$obj"
00281     append html "    var tmp$obj = \"\"\n"
00282     append html "    for (var i$obj = 0; i$obj < $selObj.length; i$obj++) {\n"
00283     append html "        if ($selObj.options\[i$obj\].value == \"\") {\n"
00284     append html "            break\n"
00285     append html "        }\n"
00286     append html "        tmp$obj += \" \" + $selObj.options\[i$obj\].value\n"
00287     append html "    }\n"
00288     append html "    $hiddenObj.value = tmp$obj \n"
00289     }
00290     append html "\}\n"
00291     append html [EndJS]
00292 
00293     # Empty the selection box for the next page.
00294 
00295     set SelectionObjList {}
00296 
00297     # Create the HTML submit button.
00298 
00299     append html "<input type=submit name=\"$name\" value=\"$value\" 
00300     onClick=\"getSelections(this.form)\">"
00301 
00302     return $html
00303 }
00304 
00305 /*  ::javascript::makeProtectedSubmitButton --*/
00306 /* */
00307 /*  Create an HTML submit button that prompts the user with a*/
00308 /*  continue/cancel shutdown warning before the form is submitted.*/
00309 /* */
00310 /*  Arguments:*/
00311 /*  name    the name of the HTML button object to create.*/
00312 /*  value   the label of the HTML button object to create.*/
00313 /*  msg The message to display when the button is pressed.*/
00314 /* */
00315 /*  Results:*/
00316 /*  Returns HTML submit button code.*/
00317 
00318 ret  ::javascript::makeProtectedSubmitButton (type name , type value , type msg) {
00319     set html ""
00320 
00321     # Create the java script procedure that gives the user the option to cancel
00322     # the server shutdown request.
00323 
00324     append html [BeginJS]
00325     append html "\nfunction areYouSure${name}(form) \{\n"
00326     append html "    if (confirm(\"$msg\")) \{\n"
00327     append html "        return true\n"
00328     append html "    \} else \{\n"
00329     append html "        return false\n"
00330     append html "    \}\n"
00331     append html "\}\n"
00332     append html [EndJS]
00333 
00334     # Create the HTML submit button.
00335 
00336     append html "<input type=submit name=\"$name\" value=\"$value\" 
00337     onClick=\"return areYouSure${name}(this.form)\">"
00338 
00339     return $html
00340 }
00341 
00342 /*  ::javascript::makeMasterButton --*/
00343 /* */
00344 /*  Create an HTML button that sets it's slave checkboxs to the boolean*/
00345 /*  value.*/
00346 /* */
00347 /*  Arguments:*/
00348 /*  master  the name of the child's parent html checkbox object.*/
00349 /*  value   the value of the master.*/
00350 /*  slaves  the name of child html checkbox object to create.*/
00351 /*  boolean the java script boolean value that will be given to all the*/
00352 /*      slaves.  Must be true or false.*/
00353 /* */
00354 /*  Results:*/
00355 /*  Returns HTML code to create the child checkbox.*/
00356 
00357 ret  ::javascript::makeMasterButton (type master , type value , type slavePattern , type boolean) {
00358     set html ""
00359 
00360     # Create the java script "checkMaster$name" proc that gets called when the
00361     # master checkbox is selected or de-selected.
00362 
00363     append html [BeginJS]
00364     append html "\nfunction checkMaster${master}(form) \{\n"
00365     append html "    for (var i = 0; i < form.elements.length; i++) \{\n"
00366     append html "        if (form.elements\[i\].name.match('$slavePattern')) \{\n"
00367     append html "            form.elements\[i\].checked = $boolean \n"
00368     append html "        \}\n"
00369     append html "    \}\n"
00370 
00371     append html "\}\n"
00372     append html [EndJS]
00373     
00374     # Create the HTML button object.
00375 
00376     append html "<input type=button name=\"$master\" value=\"$value\" " \
00377         "onClick=\"checkMaster${master}(this.form)\">\n"
00378 
00379     return $html
00380 }
00381 
00382 /*  ::javascript::makeParentCheckbox --*/
00383 /* */
00384 /*  Create an HTML checkbox and tie its value to that of it's child*/
00385 /*  checkbox.  If the parent is unchecked, the child is automatically*/
00386 /*  unchecked.*/
00387 /* */
00388 /*  Arguments:*/
00389 /*  parentName  the name of parent html checkbox object to create.*/
00390 /*  childName   the name of the parent's child html checkbox object*/
00391 /*  Results:*/
00392 /*  Returns HTML code to create the child checkbox.*/
00393 
00394 ret  ::javascript::makeParentCheckbox (type parentName , type childName) {
00395     set parentObj "form.$parentName"
00396     set childObj "form.$childName"
00397     set html ""
00398 
00399     # Create the java script "checkParent$name" proc that gets called when the
00400     # parent checkbox is selected or de-selected.
00401 
00402     append html [BeginJS]
00403     append html "\nfunction checkParent${parentName}(form) \{\n"
00404     append html "    if (!$parentObj.checked && $childObj.checked) \{\n"
00405     append html "        $childObj.checked = false\n"
00406     append html "    \}\n"
00407     append html "\}\n"
00408     append html [EndJS]
00409 
00410     # Create the HTML checkbox object.
00411 
00412     append html "<input type=checkbox name=$parentName value=1 " \
00413         "onClick=\"checkParent${parentName}(this.form)\">"
00414 
00415     return $html
00416 }
00417 
00418 /*  ::javascript::makeChildCheckbox --*/
00419 /* */
00420 /*  Create an HTML checkbox and tie its value to that of it's parent*/
00421 /*  checkbox.  If the child is checked, the parent is automatically*/
00422 /*  checked.*/
00423 /* */
00424 /*  Arguments:*/
00425 /*  parentName  the name of the child's parent html checkbox object*/
00426 /*  childName   the name of child html checkbox object to create.*/
00427 /* */
00428 /*  Results:*/
00429 /*  Returns HTML code to create the child checkbox.*/
00430 
00431 ret  ::javascript::makeChildCheckbox (type parentName , type childName) {
00432     set parentObj "form.$parentName"
00433     set childObj "form.$childName"
00434     set html ""
00435 
00436     # Create the java script "checkChild$name" proc that gets called when the
00437     # child checkbox is selected or de-selected.
00438 
00439     append html [BeginJS]
00440     append html "\nfunction checkChild${childName}(form) \{\n"
00441     append html "    if ($childObj.checked && !$parentObj.checked) \{\n"
00442     append html "        $parentObj.checked = true\n"
00443     append html "    \}\n"
00444     append html "\}\n"
00445     append html [EndJS]
00446 
00447     # Create the HTML checkbox object.
00448 
00449     append html "<input type=checkbox name=$childName value=1 " \
00450         "onClick=\"checkChild${childName}(this.form)\">"
00451 
00452     return $html
00453 }
00454 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1