Update focus rules to failback in safari
[invirt/packages/invirt-web.git] / code / static / ajaxterm / ajaxterm.js
index 07eca3b..c4501b4 100644 (file)
@@ -1,10 +1,10 @@
 ajaxterm={};
-ajaxterm.Terminal_ctor=function(id,width,height) {
+ajaxterm.Terminal_ctor=function(id,machine_id) {
        var ie=0;
        if(window.ActiveXObject)
                ie=1;
-       var sid=""+Math.round(Math.random()*1000000000);
-       var query0="s="+sid+"&w="+width+"&h="+height;
+       var base_path="machine/"+machine_id+"/at";
+       var query0="";
        var query1=query0+"&c=1&k=";
        var buf="";
        var timeout;
@@ -12,11 +12,11 @@ ajaxterm.Terminal_ctor=function(id,width,height) {
        var keybuf=[];
        var sending=0;
        var rmax=1;
+       var force=true;
 
        var div=document.getElementById(id);
        var dstat=document.createElement('pre');
        var sled=document.createElement('span');
-       var opt_get=document.createElement('a');
        var opt_color=document.createElement('a');
        var opt_paste=document.createElement('a');
        var sdebug=document.createElement('span');
@@ -35,10 +35,6 @@ ajaxterm.Terminal_ctor=function(id,width,height) {
                dstat.appendChild(opt);
                dstat.appendChild(document.createTextNode(' '));
        }
-       function do_get(event) {
-               opt_get.className=(opt_get.className=='off')?'on':'off';
-               debug('GET '+opt_get.className);
-       }
        function do_color(event) {
                var o=opt_color.className=(opt_color.className=='off')?'on':'off';
                if(o=='on')
@@ -102,14 +98,11 @@ ajaxterm.Terminal_ctor=function(id,width,height) {
                                send+=keybuf.pop();
                        }
                        var query=query1+send;
-                       if(opt_get.className=='on') {
-                               r.open("GET","u?"+query,true);
-                               if(ie) {
-                                       r.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
-                               }
-                       } else {
-                               r.open("POST","u",true);
+                       if (force) {
+                           query=query+"&force=1";
+                           force=false;
                        }
+                       r.open("POST",base_path,true);
                        r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                        r.onreadystatechange = function () {
 //                             debug("xhr:"+((new Date).getTime())+" state:"+r.readyState+" status:"+r.status+" statusText:"+r.statusText);
@@ -140,11 +133,7 @@ ajaxterm.Terminal_ctor=function(id,width,height) {
                                }
                        }
                        error_timeout=window.setTimeout(error,5000);
-                       if(opt_get.className=='on') {
-                               r.send(null);
-                       } else {
-                               r.send(query);
-                       }
+                       r.send(query);
                }
        }
        function queue(s) {
@@ -252,28 +241,33 @@ ajaxterm.Terminal_ctor=function(id,width,height) {
                dstat.appendChild(document.createTextNode(' '));
                opt_add(opt_color,'Colors');
                opt_color.className='on';
-               opt_add(opt_get,'GET');
                opt_add(opt_paste,'Paste');
                dstat.appendChild(sdebug);
                dstat.className='stat';
                div.appendChild(dstat);
                div.appendChild(dterm);
                if(opt_color.addEventListener) {
-                       opt_get.addEventListener('click',do_get,true);
                        opt_color.addEventListener('click',do_color,true);
                        opt_paste.addEventListener('click',do_paste,true);
                } else {
-                       opt_get.attachEvent("onclick", do_get);
                        opt_color.attachEvent("onclick", do_color);
                        opt_paste.attachEvent("onclick", do_paste);
                }
-               document.onkeypress=keypress;
-               document.onkeydown=keydown;
+                // If tabIndex works, only grab keyboard events when terminal is focused
+                // Otherwise, grab from whole document (e.g. in Safari).
+                var focus_element = document;
+                if(typeof $('term').tabIndex != "undefined") {
+                    focus_element = $('term');
+                    focus_element.focus();
+                }
+               focus_element.onkeypress=keypress;
+               focus_element.onkeydown=keydown;
+               force=true;
                timeout=window.setTimeout(update,100);
        }
        init();
 }
-ajaxterm.Terminal=function(id,width,height) {
-       return new this.Terminal_ctor(id,width,height);
+ajaxterm.Terminal=function(id,machine_id) {
+       return new this.Terminal_ctor(id,machine_id);
 }