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