Changeset 17912 for misc/runpugs
- Timestamp:
- 09/18/07 21:11:06 (14 months ago)
- Files:
-
- 1 modified
-
misc/runpugs/htdocs/runpugs/runpugs.js (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
misc/runpugs/htdocs/runpugs/runpugs.js
r17902 r17912 1 //debugging flag: 1 = enable, 0 = disable 2 var debug=0; 3 1 4 var histlist=new Array(); 2 5 var histentry=0; 3 6 var sessionid=0; 4 7 var reldev=0; 5 6 7 8 var curpos=0; 8 9 9 var cmd = ""; 10 11 10 var prompt = "pugs> "; 12 13 11 var cmds = new Array(); 14 15 12 var theme = "wb_theme"; 16 17 13 var fixedCharWidth; 18 19 20 14 21 15 //show the cursor 22 16 function showCursor() { 23 24 17 var cmdLen = cmds.length; 25 26 18 var cursorEl = (cmdLen == 0) ? "#d0" : "#d" + (cmdLen - 1); 27 28 19 $(cursorEl).toggleClass('cursorOff'); 29 30 20 $(cursorEl).toggleClass('cursorOn'); 31 32 21 setTimeout('showCursor()',1000); 33 34 } 35 36 22 } 37 23 38 24 //move the cursor by css 39 40 25 function moveCursor() { 41 42 26 var left = -(cmd.length-curpos) * fixedCharWidth; 43 44 27 var cursorEl = "#d" + (cmds.length - 1); 45 46 28 $(cursorEl).css('left',left + "px"); 47 48 } 49 50 29 } 51 30 52 31 //show last command on console 53 54 32 function showCmd() { 55 56 33 var cmdEl = "#c" + (cmds.length - 1); 57 58 34 $(cmdEl).text(prompt + cmd); 59 60 } 61 62 35 } 63 36 64 37 //update the console 65 66 38 function updateConsole() { 67 68 39 $.each(cmds,function(i,n) { 69 70 40 if(i == 0) { 71 72 41 //clear only on first usage... 73 74 42 $("#tt").empty(); 75 76 } 77 43 } 78 44 var l = (n == "") ? " " : n; 79 80 45 var tr = "<tr><td><pre id='c" + i + 81 82 46 "' class='"+ theme +"'>" + l + "</pre>"; 83 84 47 if(i == cmds.length - 1) { 85 86 48 tr += "<span id='d" + i + 87 88 49 "' class='cursorOff'> </span>"; 89 90 50 } 91 92 51 $("#tt").append(tr + "</td></tr>"); 93 94 }); 95 52 }); 96 53 var scrollHeight = $("#termwin")[0].scrollHeight; 97 98 54 $("#termwin").animate({ scrollTop: scrollHeight }, "fast") 99 100 } 101 102 55 } 103 56 104 57 //wait for when the document is ready 105 106 58 $(document).ready( function() { 107 108 59 //display waiting msg and try to calculate width of 109 110 60 //a single fixed character... 111 112 61 var msg = "Please wait while Pugs starts up..."; 113 114 62 $("#tt").empty(); 115 116 63 $("#tt").append("<tr><td><pre id='c0' class='wb_theme'>" + msg + 117 118 64 "</pre><span id='d0' class='cursorOff'> </span></td></tr>"); 119 120 65 fixedCharWidth = ($("#c0")[0]) ? 121 122 66 $("#c0")[0].offsetWidth / msg.length : 8; 123 67 124 125 126 68 //apply theme and animate logo 127 128 69 $("#theme").change(function() { 129 130 70 $("pre").toggleClass(theme); 131 132 71 theme = $("#theme").val(); 133 134 72 $("pre").toggleClass(theme); 135 136 }); 137 73 }); 138 74 $("#logo").slideDown(2000); 139 75 140 141 142 76 //repaint & start showing the cursor... 143 144 77 showCursor(); 145 78 146 147 79 //attach keyboard listeners... 148 149 80 $(document).keydown(function(event) { 150 151 81 return onKeyDown(event); 152 153 }); 154 82 }); 155 83 $(document).keypress(function(event) { 156 157 84 return onKeyPress(event); 158 159 }); 160 161 85 }); 162 86 163 87 //attach pugs cleanup listener... 164 165 88 $(window).unload(function() { 166 167 89 //send :q to pugs on onload 168 169 90 $.ajax({ 170 171 91 url: "/perl/runpugs.pl?" + "sessionid=" + sessionid + 172 173 92 "&reldev=1&ia=1&cmd=%3Aq", 174 175 93 async: true 176 177 94 }); 178 179 }); 180 181 95 }); 182 96 183 97 //start loading pugs session after page has loaded... 184 185 98 $("#hidden_iframe").append( 186 187 99 '<iframe src="/perl/runpugs.pl" id="scratch" name="scratch" ' + 188 189 100 'style="visibility:hidden" width="700px" height="1px" ' + 190 191 101 'onLoad="getreply()"></iframe>'); 192 193 102 }); 194 103 195 196 197 104 //insert character 'ch' at index 'pos' in string str 198 199 105 //and return the result 200 201 106 function insert(str,ch,pos) { 202 203 107 var s = str.substring(0,pos); 204 205 108 var t = str.substring(pos,str.length); 206 207 109 return s + ch + t; 208 209 } 210 211 110 } 212 111 213 112 //focus on last command very hard ;-) 214 215 113 function focusOnCmd(e) { 216 217 114 $(e).focus(); 218 219 115 var scrollHeight = $("#termwin")[0].scrollHeight; 220 221 116 $("#termwin").animate({ scrollTop: scrollHeight }, "fast"); 222 223 } 224 225 117 } 226 118 227 119 //$.keydown 228 120 function onKeyDown(event) { 229 230 121 if(event.ctrlKey || event.altKey || event.shiftKey) { 231 232 122 //ignore ctrl and alt modifiers 233 234 123 return true; 235 236 } 237 238 124 } 239 125 240 126 var keyCode = event.keyCode; 241 242 if (keyCode == 0){ 243 return true; 244 } 245 246 127 247 128 focusOnCmd("#status"); 248 249 250 129 251 130 if(keyCode == 13) { 252 253 131 //enter 254 255 132 var sessCmds=document.terminal.cmd.value + cmd; 256 257 133 var tmpCmds=sessCmds.split(prompt); 258 259 134 var tmpCmd=tmpCmds[tmpCmds.length-1]; 260 261 135 if($.trim(tmpCmd) != "") { 262 263 136 histlist.push(tmpCmd); 264 265 } 266 137 } 138 139 if(debug) { 140 alert("data to be sent: " + sessCmds); 141 } 267 142 frames['scratch'].document.getElementById("cmd").value=sessCmds; 268 269 143 frames['scratch'].document.terminal.submit(); 270 271 144 cmd = ""; 272 273 return false; 274 275 276 145 return false; 146 277 147 } else if(keyCode == 8) { 278 279 148 //backspace 280 281 149 if(curpos > 0) { 282 283 150 curpos -=1 284 285 151 var newCmd = ""; 286 287 152 for(var i = 0; i < cmd.length; i++) { 288 289 153 if(i != curpos) { 290 291 154 newCmd += cmd.charAt(i); 292 293 155 } 294 295 156 } 296 297 157 cmd = newCmd; 298 299 158 showCmd(); 300 301 } 302 303 return false; 304 305 306 159 160 debugKeys(); 161 } 162 return false; 163 307 164 } else if(keyCode == 38) { 308 309 165 //up 310 311 166 hist_next(); 312 313 return false; 314 315 316 167 return false; 168 317 169 } else if(keyCode == 40) { 318 319 170 //down 320 321 171 hist_prev(); 322 323 return false; 324 325 326 172 return false; 173 327 174 } else if(keyCode == 37) { 328 329 175 //left 330 331 176 if(curpos > 0) { 332 333 177 curpos--; 334 335 178 moveCursor(); 336 337 } 338 339 return false; 340 341 342 179 } 180 return false; 181 343 182 } else if(keyCode == 39) { 344 345 183 //right 346 347 184 if(curpos < cmd.length) { 348 349 185 curpos++; 350 351 186 moveCursor(); 352 353 } 354 355 return false; 356 187 } 188 return false; 357 189 } else if(keyCode == 36) { 358 359 190 //home 360 361 191 curpos = 0; 362 363 192 moveCursor(); 364 365 return false; 366 367 368 193 return false; 194 369 195 } else if(keyCode == 35) { 370 371 196 //end 372 373 197 curpos = cmd.length; 374 375 198 moveCursor(); 376 377 return false; 378 199 return false; 379 200 380 381 201 } else if(keyCode == 46) { 382 383 202 //del 384 385 203 if(curpos >= 0){ 386 387 204 var newCmd = ""; 388 389 205 for(var i = 0; i < cmd.length; i++) { 390 391 206 if(i != curpos) { 392 393 207 newCmd += cmd.charAt(i); 394 395 208 } 396 397 209 } 398 399 210 cmd = newCmd; 400 401 211 showCmd(); 402 403 212 moveCursor(); 404 405 } 406 407 return false; 408 409 } 410 213 214 debugKeys(); 215 } 216 return false; 217 } 411 218 return true; 412 413 } 414 415 219 } 220 221 //Convert a string character to its ascii integer value 222 //return the ascii integer if string 'ch' is in the range 32-126 223 //otherwise, will return a zero 224 function toAscii(ch) { 225 var loAZ = "abcdefghijklmnopqrstuvwxyz"; 226 var symbols = " !\"#$%&'()*+'-./0123456789:;<=>?@" + 227 loAZ.toUpperCase() + "[\\]^_`" + loAZ + "{|}~"; 228 var loc = symbols.indexOf(ch); 229 var ascii = (loc > -1) ? (32 + loc) : 0; 230 return ascii; 231 } 416 232 417 233 //$.keypress 418 419 234 function onKeyPress(event) { 420 235 421 422 423 236 if(event.ctrlKey || event.altKey) { 424 425 237 //ignore ctrl and alt modifiers 426 427 238 return; 428 429 } 430 431 432 239 } 240 241 var keyCode = event.keyCode; 242 433 243 focusOnCmd("#status"); 434 435 436 437 var keyCode = event.keyCode; 438 244 245 439 246 if($.browser.msie || $.browser.opera || $.browser.safari) { 440 441 247 var key = String.fromCharCode(keyCode); 442 443 248 if(key >= ' ') { 444 445 249 if(($.browser.opera && (keyCode < 35 || keyCode > 40)) || $.browser.msie || $.browser.safari) { 446 447 250 //insert key at curpos 448 449 251 cmd = insert(cmd,key,curpos); 450 451 252 showCmd(); 452 453 253 curpos++; 454 254 255 debugKeys(key); 455 256 } 456 457 } 458 459 return false; 460 257 } 258 return false; 461 259 } else if($.browser.mozilla && keyCode == 0) { 462 463 260 var key = String.fromCharCode(event.charCode ? event.charCode : event.keyCode); 464 465 261 //insert key at curpos 466 467 cmd = insert(cmd,key,curpos); 468 469 showCmd(); 470 471 curpos++; 472 473 return false; 474 475 } 476 262 if(key >= ' ') { 263 cmd = insert(cmd,key,curpos); 264 showCmd(); 265 curpos++; 266 267 debugKeys(key); 268 } 269 270 return false; 271 } 477 272 return true; 478 479 } 480 273 } 274 275 //debug commands... 276 function debugKeys(key) { 277 if(debug) { 278 var cmdBytes = ""; 279 for(var i = 0; i < cmd.length; i++) { 280 var asciiCode = toAscii(cmd.charAt(i)); 281 cmdBytes += asciiCode + ","; 282 } 283 $("#status").text(((key) ? ("key = '" + key) : "") + 284 "', cmd = '" + cmd + 285 "', cmd.length=" + cmd.length + 286 ", ascii=(" + cmdBytes + ")"); 287 } 288 } 481 289 482 290 //called by textarea: TODO should be removed when textarea 483 484 291 //is replaced by async $.ajax 485 292 function getreply () { … … 489 296 sessionid=scratchpad.terminal.sessionid.value; 490 297 document.terminal.cmd.value=reply; 491 492 493 298 494 299 if(scratchpad.terminal.prompt) { 495 496 300 //safely assign prompt... 497 498 301 var val = scratchpad.terminal.prompt.value; 499 500 302 if(val && val.length == 'pugs> '.length) { 501 502 303 prompt = val; 503 504 } 505 506 } 507 508 509 304 } 305 } 306 510 307 //escape html from whitespace and html entities 511 512 308 //and then split lines 513 514 cmds = 515 516 reply.replace(/&/g,'&') 517 309 var escapedReply = reply.replace(/&/g,'&') 518 310 .replace(/ /g,' ') 519 520 311 .replace(/</g,'<') 521 522 312 .replace(/>/g,'>') 523 524 .replace(/"/g,'"') 525 526 .split(/\r\n|\n|\r/g); 527 313 .replace(/"/g,'"'); 314 cmds = escapedReply.split(/\r\n|\n|\r/g); 315 316 if(debug) { 317 alert("reply (unescaped): " + reply); 318 //alert("reply (escaped): " + escapedReply); 319 } 320 528 321 updateConsole(); 529 530 322 cmd = ""; 531 532 323 curpos=0; 533 534 324 showCmd(); 535 536 } 537 325 } 538 326 539 327 //next in history (triggered by DOWN) … … 542 330 histentry--; 543 331 cmd=histlist[histentry]; 544 545 332 curpos=cmd.length; 546 547 333 showCmd(); 548 549 334 moveCursor(); 550 551 335 } 552 336 return false; 553 337 } 554 555 338 556 339 //previous in history (triggered by UP) … … 559 342 histentry++; 560 343 cmd=histlist[histentry]; 561 562 344 curpos=cmd.length; 563 564 345 showCmd(); 565 566 346 moveCursor(); 567 568 } 569 } 570 347 } 348 } 571 349 572 350 //triggered by user selecting release/development version … … 580 358 frames['scratch'].document.terminal.reldev[1].checked=true; 581 359 } 582 583 360 frames['scratch'].document.terminal.submit(); 584 361 585
