javascript.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00025
00026
00027
00028
00029
00030 variable SelectionObjList {}
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 ret ::javascript::BeginJS () {
00044 return "\n<SCRIPT LANGUAGE=\"JavaScript\">\n"
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 ret ::javascript::EndJS () {
00058 return "\n</SCRIPT>\n"
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
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 " "
00109 }
00110 append html "<option value=\"\">$filler"
00111
00112 append html "</select>"
00113 return $html
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
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
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
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 " </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 " </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
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
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
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
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
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
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
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
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
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
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