Changeset 17912 for misc/runpugs

Show
Ignore:
Timestamp:
09/18/07 21:11:06 (14 months ago)
Author:
azawawi
Message:

[runpugs] fixed moritz's AltGr?+q problem by filtering all keys on ascii value in keypress
[runpugs] added debug on/off aids in runpugs.js to debug communication

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • misc/runpugs/htdocs/runpugs/runpugs.js

    r17902 r17912  
     1//debugging flag: 1 = enable, 0 = disable 
     2var debug=0; 
     3 
    14var histlist=new Array(); 
    25var histentry=0; 
    36var sessionid=0; 
    47var reldev=0; 
    5  
    6  
    78var curpos=0; 
    8  
    99var cmd = ""; 
    10  
    1110var prompt = "pugs> "; 
    12  
    1311var cmds = new Array(); 
    14  
    1512var theme = "wb_theme"; 
    16  
    1713var fixedCharWidth; 
    18  
    19  
    2014 
    2115//show the cursor 
    2216function showCursor() { 
    23  
    2417    var cmdLen = cmds.length; 
    25  
    2618    var cursorEl = (cmdLen == 0) ? "#d0" : "#d" + (cmdLen - 1); 
    27  
    2819    $(cursorEl).toggleClass('cursorOff'); 
    29  
    3020    $(cursorEl).toggleClass('cursorOn'); 
    31  
    3221    setTimeout('showCursor()',1000); 
    33  
    34 } 
    35  
    36  
     22} 
    3723 
    3824//move the cursor by css 
    39  
    4025function moveCursor() { 
    41  
    4226    var left = -(cmd.length-curpos) * fixedCharWidth; 
    43  
    4427    var cursorEl = "#d" + (cmds.length - 1); 
    45  
    4628    $(cursorEl).css('left',left + "px"); 
    47  
    48 } 
    49  
    50  
     29} 
    5130 
    5231//show last command on console 
    53  
    5432function showCmd() { 
    55  
    5633    var cmdEl = "#c" + (cmds.length - 1); 
    57  
    5834    $(cmdEl).text(prompt + cmd); 
    59  
    60 } 
    61  
    62  
     35} 
    6336 
    6437//update the console 
    65  
    6638function updateConsole() { 
    67  
    6839    $.each(cmds,function(i,n) { 
    69  
    7040        if(i == 0) { 
    71  
    7241            //clear only on first usage... 
    73  
    7442            $("#tt").empty(); 
    75  
    76         } 
    77  
     43        } 
    7844        var l = (n == "") ? " " : n; 
    79  
    8045        var tr = "<tr><td><pre id='c" + i +  
    81  
    8246            "' class='"+ theme +"'>" + l + "</pre>"; 
    83  
    8447        if(i == cmds.length - 1) { 
    85  
    8648            tr += "<span id='d" + i +  
    87  
    8849            "' class='cursorOff'>&nbsp;</span>"; 
    89  
    9050        }  
    91  
    9251        $("#tt").append(tr + "</td></tr>"); 
    93  
    94     }); 
    95  
     52    }); 
    9653    var scrollHeight = $("#termwin")[0].scrollHeight; 
    97  
    9854    $("#termwin").animate({ scrollTop: scrollHeight }, "fast") 
    99  
    100 } 
    101  
    102  
     55} 
    10356 
    10457//wait for when the document is ready 
    105  
    10658$(document).ready( function() { 
    107  
    10859    //display waiting msg and try to calculate width of  
    109  
    11060    //a single fixed character... 
    111  
    11261    var msg = "Please wait while Pugs starts up..."; 
    113  
    11462    $("#tt").empty(); 
    115  
    11663    $("#tt").append("<tr><td><pre id='c0' class='wb_theme'>" + msg + 
    117  
    11864    "</pre><span id='d0' class='cursorOff'>&nbsp;</span></td></tr>"); 
    119  
    12065    fixedCharWidth = ($("#c0")[0]) ?  
    121  
    12266        $("#c0")[0].offsetWidth / msg.length : 8; 
    12367 
    124  
    125  
    12668    //apply theme and animate logo 
    127  
    12869    $("#theme").change(function() { 
    129  
    13070        $("pre").toggleClass(theme); 
    131  
    13271        theme = $("#theme").val(); 
    133  
    13472        $("pre").toggleClass(theme); 
    135  
    136     }); 
    137  
     73    }); 
    13874    $("#logo").slideDown(2000); 
    13975 
    140  
    141  
    14276    //repaint & start showing the cursor... 
    143  
    14477    showCursor(); 
    14578 
    146  
    14779    //attach keyboard listeners... 
    148  
    14980    $(document).keydown(function(event) { 
    150  
    15181        return onKeyDown(event); 
    152  
    153     }); 
    154  
     82    }); 
    15583    $(document).keypress(function(event) {     
    156  
    15784        return onKeyPress(event); 
    158  
    159     }); 
    160  
    161  
     85    }); 
    16286 
    16387    //attach pugs cleanup listener... 
    164  
    16588    $(window).unload(function() { 
    166  
    16789        //send :q to pugs on onload 
    168  
    16990        $.ajax({ 
    170  
    17191            url: "/perl/runpugs.pl?" + "sessionid=" + sessionid +  
    172  
    17392            "&reldev=1&ia=1&cmd=%3Aq", 
    174  
    17593            async: true 
    176  
    17794        }); 
    178  
    179     }); 
    180  
    181  
     95    }); 
    18296 
    18397    //start loading pugs session after page has loaded... 
    184  
    18598    $("#hidden_iframe").append( 
    186  
    18799        '<iframe src="/perl/runpugs.pl" id="scratch" name="scratch" ' + 
    188  
    189100        'style="visibility:hidden" width="700px" height="1px" ' + 
    190  
    191101        'onLoad="getreply()"></iframe>'); 
    192  
    193102}); 
    194103 
    195  
    196  
    197104//insert character 'ch' at index 'pos' in string str  
    198  
    199105//and return the result 
    200  
    201106function insert(str,ch,pos) { 
    202  
    203107    var s = str.substring(0,pos); 
    204  
    205108    var t = str.substring(pos,str.length); 
    206  
    207109    return s + ch + t; 
    208  
    209 } 
    210  
    211  
     110} 
    212111 
    213112//focus on last command very hard ;-) 
    214  
    215113function focusOnCmd(e) { 
    216  
    217114    $(e).focus(); 
    218  
    219115    var scrollHeight = $("#termwin")[0].scrollHeight; 
    220  
    221116    $("#termwin").animate({ scrollTop: scrollHeight }, "fast"); 
    222  
    223 } 
    224  
    225  
     117} 
    226118 
    227119//$.keydown 
    228120function onKeyDown(event) { 
    229  
    230121    if(event.ctrlKey || event.altKey || event.shiftKey) { 
    231  
    232122        //ignore ctrl and alt modifiers 
    233  
    234123        return true; 
    235  
    236     } 
    237  
    238  
     124    } 
    239125 
    240126    var keyCode = event.keyCode; 
    241  
    242         if (keyCode == 0){ 
    243                 return true; 
    244         } 
    245  
    246  
     127     
    247128    focusOnCmd("#status"); 
    248  
    249      
    250  
     129     
    251130    if(keyCode == 13) { 
    252  
    253131        //enter 
    254  
    255132        var sessCmds=document.terminal.cmd.value + cmd; 
    256  
    257133        var tmpCmds=sessCmds.split(prompt); 
    258  
    259134        var tmpCmd=tmpCmds[tmpCmds.length-1]; 
    260  
    261135        if($.trim(tmpCmd) != "") { 
    262  
    263136            histlist.push(tmpCmd); 
    264  
    265         } 
    266  
     137        } 
     138         
     139        if(debug) { 
     140            alert("data to be sent: " + sessCmds); 
     141        } 
    267142        frames['scratch'].document.getElementById("cmd").value=sessCmds; 
    268  
    269143        frames['scratch'].document.terminal.submit();  
    270  
    271144        cmd = ""; 
    272  
    273         return false; 
    274  
    275          
    276  
     145        return false; 
     146         
    277147    } else if(keyCode == 8) { 
    278  
    279148        //backspace 
    280  
    281149        if(curpos > 0) { 
    282  
    283150            curpos -=1 
    284  
    285151            var newCmd = ""; 
    286  
    287152            for(var i = 0; i < cmd.length; i++) { 
    288  
    289153                if(i != curpos) { 
    290  
    291154                    newCmd += cmd.charAt(i); 
    292  
    293155                } 
    294  
    295156            } 
    296  
    297157            cmd = newCmd; 
    298  
    299158            showCmd(); 
    300  
    301         } 
    302  
    303         return false; 
    304  
    305          
    306  
     159             
     160            debugKeys(); 
     161        } 
     162        return false; 
     163         
    307164    } else if(keyCode == 38) { 
    308  
    309165        //up 
    310  
    311166        hist_next(); 
    312  
    313         return false; 
    314  
    315          
    316  
     167        return false; 
     168         
    317169    } else if(keyCode == 40) { 
    318  
    319170        //down 
    320  
    321171        hist_prev(); 
    322  
    323         return false; 
    324  
    325          
    326  
     172        return false; 
     173         
    327174    } else if(keyCode == 37) { 
    328  
    329175        //left 
    330  
    331176        if(curpos > 0) { 
    332  
    333177            curpos--; 
    334  
    335178            moveCursor(); 
    336  
    337         } 
    338  
    339         return false; 
    340  
    341          
    342  
     179        } 
     180        return false; 
     181         
    343182    } else if(keyCode == 39) { 
    344  
    345183        //right 
    346  
    347184        if(curpos < cmd.length) { 
    348  
    349185            curpos++; 
    350  
    351186            moveCursor(); 
    352  
    353         } 
    354  
    355         return false; 
    356  
     187        } 
     188        return false; 
    357189    } else if(keyCode == 36) { 
    358  
    359190        //home 
    360  
    361191        curpos = 0; 
    362  
    363192        moveCursor(); 
    364  
    365         return false; 
    366  
    367      
    368  
     193        return false; 
     194     
    369195    } else if(keyCode == 35) { 
    370  
    371196        //end 
    372  
    373197        curpos = cmd.length; 
    374  
    375198        moveCursor(); 
    376  
    377         return false; 
    378  
     199        return false; 
    379200   
    380  
    381201    } else if(keyCode == 46) { 
    382  
    383202        //del 
    384  
    385203        if(curpos >= 0){ 
    386  
    387204            var newCmd = ""; 
    388  
    389205            for(var i = 0; i < cmd.length; i++) { 
    390  
    391206                if(i != curpos) { 
    392  
    393207                    newCmd += cmd.charAt(i); 
    394  
    395208                } 
    396  
    397209            } 
    398  
    399210            cmd = newCmd; 
    400  
    401211            showCmd(); 
    402  
    403212            moveCursor(); 
    404  
    405         } 
    406  
    407         return false; 
    408  
    409     } 
    410  
     213             
     214            debugKeys(); 
     215        } 
     216        return false; 
     217    } 
    411218    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 
     224function 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} 
    416232 
    417233//$.keypress 
    418  
    419234function onKeyPress(event) { 
    420235 
    421  
    422  
    423236    if(event.ctrlKey || event.altKey) { 
    424  
    425237        //ignore ctrl and alt modifiers 
    426  
    427238        return; 
    428  
    429     } 
    430  
    431  
    432  
     239    } 
     240 
     241    var keyCode = event.keyCode; 
     242     
    433243    focusOnCmd("#status"); 
    434  
    435  
    436  
    437     var keyCode = event.keyCode; 
    438  
     244     
     245     
    439246    if($.browser.msie || $.browser.opera || $.browser.safari) { 
    440  
    441247        var key = String.fromCharCode(keyCode); 
    442  
    443248        if(key >= ' ') { 
    444  
    445249            if(($.browser.opera && (keyCode < 35 || keyCode > 40)) || $.browser.msie || $.browser.safari) { 
    446  
    447250                //insert key at curpos 
    448  
    449251                cmd = insert(cmd,key,curpos); 
    450  
    451252                showCmd(); 
    452  
    453253                curpos++; 
    454  
     254                 
     255                debugKeys(key);                 
    455256            } 
    456  
    457         } 
    458  
    459         return false; 
    460  
     257        } 
     258        return false; 
    461259    } else if($.browser.mozilla && keyCode == 0) { 
    462  
    463260        var key = String.fromCharCode(event.charCode ? event.charCode : event.keyCode); 
    464  
    465261        //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    } 
    477272    return true; 
    478  
    479 } 
    480  
     273} 
     274 
     275//debug commands... 
     276function 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} 
    481289 
    482290//called by textarea: TODO should be removed when textarea  
    483  
    484291//is replaced by async $.ajax  
    485292function getreply () { 
     
    489296    sessionid=scratchpad.terminal.sessionid.value; 
    490297    document.terminal.cmd.value=reply; 
    491  
    492      
    493  
     298     
    494299    if(scratchpad.terminal.prompt) { 
    495  
    496300        //safely assign prompt... 
    497  
    498301        var val = scratchpad.terminal.prompt.value; 
    499  
    500302        if(val && val.length == 'pugs> '.length) { 
    501  
    502303            prompt = val; 
    503  
    504         } 
    505  
    506     } 
    507  
    508      
    509  
     304        } 
     305    } 
     306     
    510307    //escape html from whitespace and html entities 
    511  
    512308    //and then split lines 
    513  
    514         cmds =  
    515  
    516         reply.replace(/&/g,'&amp;') 
    517  
     309    var escapedReply = reply.replace(/&/g,'&amp;') 
    518310        .replace(/ /g,'&nbsp;') 
    519  
    520311        .replace(/</g,'&lt;') 
    521  
    522312        .replace(/>/g,'&gt;') 
    523  
    524         .replace(/"/g,'&quot;') 
    525  
    526         .split(/\r\n|\n|\r/g); 
    527  
     313        .replace(/"/g,'&quot;'); 
     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     
    528321    updateConsole(); 
    529  
    530322    cmd = ""; 
    531  
    532323    curpos=0; 
    533  
    534324    showCmd(); 
    535  
    536 } 
    537  
     325} 
    538326 
    539327//next in history (triggered by DOWN) 
     
    542330        histentry--; 
    543331        cmd=histlist[histentry]; 
    544  
    545332        curpos=cmd.length; 
    546  
    547333        showCmd(); 
    548  
    549334        moveCursor(); 
    550  
    551335    } 
    552336    return false; 
    553337} 
    554  
    555338 
    556339//previous in history (triggered by UP) 
     
    559342        histentry++; 
    560343        cmd=histlist[histentry]; 
    561  
    562344        curpos=cmd.length; 
    563  
    564345        showCmd(); 
    565  
    566346        moveCursor(); 
    567  
    568     } 
    569 } 
    570  
     347    } 
     348} 
    571349 
    572350//triggered by user selecting release/development version 
     
    580358        frames['scratch'].document.terminal.reldev[1].checked=true; 
    581359    } 
    582  
    583360    frames['scratch'].document.terminal.submit(); 
    584361 
    585