Use screenX/screenY for touch.
[guacamole.git] / src / main / webapp / scripts / interface.js
index c2d5a5d..be79b38 100644 (file)
@@ -2,10 +2,34 @@
 // UI Definition
 var GuacamoleUI = {
 
-    "display": document.getElementById("display"),
-    "menu"   : document.getElementById("menu"),
-    "logo"   : document.getElementById("status-logo"),
-    "state"  : document.getElementById("state"),
+    /* Detection Constants */
+    
+    "LONG_PRESS_DETECT_TIMEOUT"     : 800, /* milliseconds */
+    "LONG_PRESS_MOVEMENT_THRESHOLD" : 10,  /* pixels */
+    "MENU_CLOSE_DETECT_TIMEOUT"     : 500, /* milliseconds */
+    "MENU_OPEN_DETECT_TIMEOUT"      : 325, /* milliseconds */
+    "KEYBOARD_AUTO_RESIZE_INTERVAL" : 30,  /* milliseconds */
+
+    /* Animation Constants */
+
+    "MENU_SHADE_STEPS"    : 10, /* frames */
+    "MENU_SHADE_INTERVAL" : 30, /* milliseconds */
+    "MENU_SHOW_STEPS"     : 5,  /* frames */
+    "MENU_SHOW_INTERVAL"  : 30, /* milliseconds */
+
+    /* OSK Mode Constants */
+    "OSK_MODE_NATIVE" : 1, /* "Show Keyboard" will show the platform's native OSK */
+    "OSK_MODE_GUAC"   : 2, /* "Show Keyboard" will show Guac's built-in OSK */
+
+    /* UI Elements */
+
+    "viewport"    : document.getElementById("viewportClone"),
+    "display"     : document.getElementById("display"),
+    "menu"        : document.getElementById("menu"),
+    "menuControl" : document.getElementById("menuControl"),
+    "touchMenu"   : document.getElementById("touchMenu"),
+    "logo"        : document.getElementById("status-logo"),
+    "eventTarget" : document.getElementById("eventTarget"),
 
     "buttons": {
 
@@ -18,12 +42,12 @@ var GuacamoleUI = {
     },
 
     "containers": {
-        "error"    : document.getElementById("errorDialog"),
+        "state"    : document.getElementById("statusDialog"),
         "clipboard": document.getElementById("clipboardDiv"),
         "keyboard" : document.getElementById("keyboardContainer")
     },
     
-    "error"     : document.getElementById("errorText"),
+    "state"     : document.getElementById("statusText"),
     "clipboard" : document.getElementById("clipboard")
 
 };
@@ -40,22 +64,80 @@ var GuacamoleUI = {
     var guacErrorImage = new Image();
     guacErrorImage.src = "images/noguacamole-logo-24.png";
 
-    GuacamoleUI.showError = function(error) {
+    // Function for adding a class to an element
+    var addClass;
+
+    // Function for removing a class from an element
+    var removeClass;
+
+    // If Node.classList is supported, implement addClass/removeClass using that
+    if (Node.classList) {
+
+        addClass = function(element, classname) {
+            element.classList.add(classname);
+        };
+        
+        removeClass = function(element, classname) {
+            element.classList.remove(classname);
+        };
+        
+    }
+
+    // Otherwise, implement own
+    else {
+
+        addClass = function(element, classname) {
+
+            // Simply add new class
+            element.className += " " + classname;
+
+        };
+        
+        removeClass = function(element, classname) {
+
+            // Filter out classes with given name
+            element.className = element.className.replace(/([^ ]+)[ ]*/g,
+                function(match, testClassname, spaces, offset, string) {
+
+                    // If same class, remove
+                    if (testClassname == classname)
+                        return "";
+
+                    // Otherwise, allow
+                    return match;
+                    
+                }
+            );
 
-        GuacamoleUI.menu.className = "error";
-        GuacamoleUI.display.className += " guac-error";
+        };
+        
+    }
 
-        GuacamoleUI.logo.src = guacErrorImage.src;
-        GuacamoleUI.error.textContent = error;
-        GuacamoleUI.containers.error.style.visibility = "visible";
 
+    GuacamoleUI.hideStatus = function() {
+        removeClass(document.body, "guac-error");
+        GuacamoleUI.containers.state.style.visibility = "hidden";
+        GuacamoleUI.display.style.opacity = "1";
+    };
+    
+    GuacamoleUI.showStatus = function(text) {
+        removeClass(document.body, "guac-error");
+        GuacamoleUI.containers.state.style.visibility = "visible";
+        GuacamoleUI.state.textContent = text;
+        GuacamoleUI.display.style.opacity = "1";
+    };
+    
+    GuacamoleUI.showError = function(error) {
+        addClass(document.body, "guac-error");
+        GuacamoleUI.state.textContent = error;
+        GuacamoleUI.display.style.opacity = "0.1";
     };
 
     GuacamoleUI.shadeMenu = function() {
 
         if (!menu_shaded) {
 
-            var step = Math.floor(GuacamoleUI.menu.offsetHeight / 5) + 1;
+            var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHADE_STEPS) + 1;
             var offset = 0;
             menu_shaded = true;
 
@@ -70,7 +152,7 @@ var GuacamoleUI = {
                     GuacamoleUI.menu.style.visiblity = "hidden";
                 }
 
-            }, 30);
+            }, GuacamoleUI.MENU_SHADE_INTERVAL);
         }
 
     };
@@ -79,7 +161,7 @@ var GuacamoleUI = {
 
         if (menu_shaded) {
 
-            var step = Math.floor(GuacamoleUI.menu.offsetHeight / 5) + 1;
+            var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHOW_STEPS) + 1;
             var offset = -GuacamoleUI.menu.offsetHeight;
             menu_shaded = false;
             GuacamoleUI.menu.style.visiblity = "";
@@ -96,7 +178,7 @@ var GuacamoleUI = {
 
                 GuacamoleUI.menu.style.top = offset + "px";
 
-            }, 30);
+            }, GuacamoleUI.MENU_SHOW_INTERVAL);
         }
 
     };
@@ -117,18 +199,52 @@ var GuacamoleUI = {
 
     };
 
+    // Assume no native OSK by default
+    GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
+
     // Show/Hide keyboard
+    var keyboardResizeInterval = null;
     GuacamoleUI.buttons.showKeyboard.onclick = function() {
 
+        // If Guac OSK shown, hide it.
         var displayed = GuacamoleUI.containers.keyboard.style.display;
-        if (displayed != "block") {
-            GuacamoleUI.containers.keyboard.style.display = "block";
-            GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
-        }
-        else {
+        if (displayed == "block") {
             GuacamoleUI.containers.keyboard.style.display = "none";
             GuacamoleUI.buttons.showKeyboard.textContent = "Show Keyboard";
+
+            window.onresize = null;
+            window.clearInterval(keyboardResizeInterval);
         }
+        
+        // If not shown ... action depends on OSK mode.
+        else {
+
+            // If we think the platform has a native OSK, use the event target to
+            // cause it to display.
+            if (GuacamoleUI.oskMode == GuacamoleUI.OSK_MODE_NATIVE) {
+
+                // ...but use the Guac OSK if clicked again
+                GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
+
+                // Try to show native OSK by focusing eventTarget.
+                GuacamoleUI.eventTarget.focus();
+                return;
+
+            }
+
+            // Ensure event target is NOT focused if we are using the Guac OSK.
+            GuacamoleUI.eventTarget.blur();
+
+            GuacamoleUI.containers.keyboard.style.display = "block";
+            GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
+
+            // Automatically update size
+            window.onresize = updateKeyboardSize;
+            keyboardResizeInterval = window.setInterval(updateKeyboardSize, GuacamoleUI.KEYBOARD_AUTO_RESIZE_INTERVAL);
+
+            updateKeyboardSize();
+        }
+        
 
     };
 
@@ -137,37 +253,243 @@ var GuacamoleUI = {
         window.location.href = "logout";
     };
 
-    GuacamoleUI.display.onmouseout = function() {
-        GuacamoleUI.showMenu();
+    // Timeouts for detecting if users wants menu to open or close
+    var detectMenuOpenTimeout = null;
+    var detectMenuCloseTimeout = null;
+
+    // Clear detection timeouts
+    GuacamoleUI.resetMenuDetect = function() {
+
+        if (detectMenuOpenTimeout != null) {
+            window.clearTimeout(detectMenuOpenTimeout);
+            detectMenuOpenTimeout = null;
+        }
+
+        if (detectMenuCloseTimeout != null) {
+            window.clearTimeout(detectMenuCloseTimeout);
+            detectMenuCloseTimeout = null;
+        }
+
     };
 
-    GuacamoleUI.display.onmouseover = function() {
-        GuacamoleUI.shadeMenu();
+    // Initiate detection of menu open action. If not canceled through some
+    // user event, menu will open.
+    GuacamoleUI.startMenuOpenDetect = function() {
+
+        if (!detectMenuOpenTimeout) {
+
+            // Clear detection state
+            GuacamoleUI.resetMenuDetect();
+
+            // Wait and then show menu
+            detectMenuOpenTimeout = window.setTimeout(function() {
+
+                // If menu opened via mouse, do not show native OSK
+                GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
+
+                GuacamoleUI.showMenu();
+                detectMenuOpenTimeout = null;
+            }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
+
+        }
+
     };
 
+    // Initiate detection of menu close action. If not canceled through some
+    // user mouse event, menu will close.
+    GuacamoleUI.startMenuCloseDetect = function() {
+
+        if (!detectMenuCloseTimeout) {
+
+            // Clear detection state
+            GuacamoleUI.resetMenuDetect();
+
+            // Wait and then shade menu
+            detectMenuCloseTimeout = window.setTimeout(function() {
+                GuacamoleUI.shadeMenu();
+                detectMenuCloseTimeout = null;
+            }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
+
+        }
+
+    };
+
+    // Show menu if mouseover any part of menu
+    GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
+
+    // Stop detecting menu state change intents if mouse is over menu
+    GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
+
+    // When mouse hovers over top of screen, start detection of intent to open menu
+    GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
+
+    var long_press_start_x = 0;
+    var long_press_start_y = 0;
+    var menuShowLongPressTimeout = null;
+
+    GuacamoleUI.startLongPressDetect = function() {
+
+        if (!menuShowLongPressTimeout) {
+
+            menuShowLongPressTimeout = window.setTimeout(function() {
+                
+                menuShowLongPressTimeout = null;
+
+                // Assume native OSK if menu shown via long-press
+                GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
+                GuacamoleUI.showMenu();
+
+            }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
+
+        }
+    };
+
+    GuacamoleUI.stopLongPressDetect = function() {
+        window.clearTimeout(menuShowLongPressTimeout);
+        menuShowLongPressTimeout = null;
+    };
+
+    // Reset event target (add content, reposition cursor in middle.
+    GuacamoleUI.resetEventTarget = function() {
+        GuacamoleUI.eventTarget.value = "GUAC";
+        GuacamoleUI.eventTarget.selectionStart =
+        GuacamoleUI.eventTarget.selectionEnd   = 2;
+    };
+
+    // Detect long-press at bottom of screen
+    GuacamoleUI.display.addEventListener('touchstart', function(e) {
+        
+        // Close menu if shown
+        GuacamoleUI.shadeMenu();
+        
+        // Record touch location
+        if (e.touches.length == 1) {
+            var touch = e.touches[0];
+            long_press_start_x = touch.screenX;
+            long_press_start_y = touch.screenY;
+        }
+        
+        // Start detection
+        GuacamoleUI.startLongPressDetect();
+        
+    }, true);
+
+    // Stop detection if touch moves significantly
+    GuacamoleUI.display.addEventListener('touchmove', function(e) {
+        
+        if (e.touches.length == 1) {
+
+            // If touch distance from start exceeds threshold, cancel long press
+            var touch = e.touches[0];
+            if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
+                || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
+                GuacamoleUI.stopLongPressDetect();
+
+        }
+        
+    }, true);
+
+    // Stop detection if press stops
+    GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
+
+    // Close menu on mouse movement
+    GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
+    GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
+
     // Reconnect button
     GuacamoleUI.buttons.reconnect.onclick = function() {
         window.location.reload();
     };
 
     // On-screen keyboard
-    GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty.xml");
-    GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard);
+    GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty-mobile.xml");
+    GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
+
+    // Function for automatically updating keyboard size
+    var lastKeyboardWidth;
+    function updateKeyboardSize() {
+        var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
+        if (lastKeyboardWidth != currentSize) {
+            GuacamoleUI.keyboard.resize(currentSize);
+            lastKeyboardWidth = currentSize;
+        }
+    };
 
 })();
 
 // Tie UI events / behavior to a specific Guacamole client
 GuacamoleUI.attach = function(guac) {
 
+    var title_prefix = null;
+    var connection_name = null 
+    
+    var guac_display = guac.getDisplay();
+
+    // Set document title appropriately, based on prefix and connection name
+    function updateTitle() {
+
+        // Use title prefix if present
+        if (title_prefix) {
+            
+            document.title = title_prefix;
+
+            // Include connection name, if present
+            if (connection_name)
+                document.title += " " + connection_name;
+
+        }
+
+        // Otherwise, just set to connection name
+        else if (connection_name)
+            document.title = connection_name;
+
+    }
+
+    // When mouse enters display, start detection of intent to close menu
+    guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
+
+    guac_display.onclick = function(e) {
+        e.preventDefault();
+        return false;
+    };
+
     // Mouse
-    var mouse = new Guacamole.Mouse(GuacamoleUI.display);
+    var mouse = new Guacamole.Mouse(guac_display);
     mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
         function(mouseState) {
-
-            if (mouseState.y <= 5)
-                GuacamoleUI.showMenu();
-
+       
+            // Determine mouse position within view
+            var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
+            var mouse_view_y = mouseState.y + guac_display.offsetTop  - window.pageYOffset;
+
+            // Determine viewport dimensioins
+            var view_width  = GuacamoleUI.viewport.offsetWidth;
+            var view_height = GuacamoleUI.viewport.offsetHeight;
+
+            // Determine scroll amounts based on mouse position relative to document
+
+            var scroll_amount_x;
+            if (mouse_view_x > view_width)
+                scroll_amount_x = mouse_view_x - view_width;
+            else if (mouse_view_x < 0)
+                scroll_amount_x = mouse_view_x;
+            else
+                scroll_amount_x = 0;
+
+            var scroll_amount_y;
+            if (mouse_view_y > view_height)
+                scroll_amount_y = mouse_view_y - view_height;
+            else if (mouse_view_y < 0)
+                scroll_amount_y = mouse_view_y;
+            else
+                scroll_amount_y = 0;
+
+            // Scroll (if necessary) to keep mouse on screen.
+            window.scrollBy(scroll_amount_x, scroll_amount_y);
+       
+            // Send mouse event
             guac.sendMouseState(mouseState);
+            
         };
 
     // Keyboard
@@ -181,6 +503,12 @@ GuacamoleUI.attach = function(guac) {
     function enableKeyboard() {
         keyboard.onkeydown = 
             function (keysym) {
+          
+                // If we're using native OSK, ensure event target is reset
+                // on each key event.
+                if (GuacamoleUI.oskMode == GuacamoleUI.OSK_MODE_NATIVE)
+                    GuacamoleUI.resetEventTarget();
+                
                 guac.sendKeyEvent(1, keysym);
             };
 
@@ -195,54 +523,65 @@ GuacamoleUI.attach = function(guac) {
 
     // Handle client state change
     guac.onstatechange = function(clientState) {
+
         switch (clientState) {
 
             // Idle
             case 0:
-                GuacamoleUI.state.textContent = "Idle."
+                GuacamoleUI.showStatus("Idle.");
+                title_prefix = "[Idle]";
                 break;
 
             // Connecting
             case 1:
-                GuacamoleUI.state.textContent = "Connecting...";
+                GuacamoleUI.shadeMenu();
+                GuacamoleUI.showStatus("Connecting...");
+                title_prefix = "[Connecting...]";
                 break;
 
             // Connected + waiting
             case 2:
-                GuacamoleUI.state.textContent = "Connected, waiting for first update...";
+                GuacamoleUI.showStatus("Connected, waiting for first update...");
+                title_prefix = "[Waiting...]";
                 break;
 
             // Connected
             case 3:
                 
+                GuacamoleUI.hideStatus();
                 GuacamoleUI.display.className =
                     GuacamoleUI.display.className.replace(/guac-loading/, '');
 
                 GuacamoleUI.menu.className = "connected";
-                GuacamoleUI.state.textContent = "Connected.";
-                GuacamoleUI.shadeMenu();
+
+                title_prefix = null;
                 break;
 
             // Disconnecting
             case 4:
-                GuacamoleUI.state.textContent = "Disconnecting...";
+                GuacamoleUI.showStatus("Disconnecting...");
+                title_prefix = "[Disconnecting...]";
                 break;
 
             // Disconnected
             case 5:
-                GuacamoleUI.state.textContent = "Disconnected.";
+                GuacamoleUI.showStatus("Disconnected.");
+                title_prefix = "[Disconnected]";
                 break;
 
             // Unknown status code
             default:
-                GuacamoleUI.state.textContent = "Unknown";
+                GuacamoleUI.showStatus("[UNKNOWN STATUS]");
 
         }
+
+        updateTitle();
     };
 
     // Name instruction handler
     guac.onname = function(name) {
-        document.title = name;
+        connection_name = name;
+        updateTitle();
     };
 
     // Error handler
@@ -253,32 +592,6 @@ GuacamoleUI.attach = function(guac) {
 
         // Display error message
         GuacamoleUI.showError(error);
-
-        // Show error by desaturating display
-        var layers = guac.getLayers();
-        for (var i=0; i<layers.length; i++) {
-            layers[i].filter(desaturateFilter);
-        }
-
-        // Filter for desaturation
-        function desaturateFilter(data, width, height) {
-
-            for (var i=0; i<data.length; i+=4) {
-
-                // Get RGB values
-                var r = data[i];
-                var g = data[i+1];
-                var b = data[i+2];
-
-                // Desaturate
-                var v = Math.max(r, g, b) / 2;
-                data[i]   = v;
-                data[i+1] = v;
-                data[i+2] = v;
-
-            }
-
-        }
         
     };
 
@@ -310,17 +623,13 @@ GuacamoleUI.attach = function(guac) {
         GuacamoleUI.clipboard.value = data;
     };
 
-    GuacamoleUI.keyboard.setKeyPressedHandler(
-        function(keysym) {
-            guac.sendKeyEvent(1, keysym);
-        }
-    );
+    GuacamoleUI.keyboard.onkeydown = function(keysym) {
+        guac.sendKeyEvent(1, keysym);
+    };
 
-    GuacamoleUI.keyboard.setKeyReleasedHandler(
-        function(keysym) {
-            guac.sendKeyEvent(0, keysym);
-        }
-    );
+    GuacamoleUI.keyboard.onkeyup = function(keysym) {
+        guac.sendKeyEvent(0, keysym);
+    };
 
     // Send Ctrl-Alt-Delete
     GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {