5 "LOGOUT_PROMPT" : "Logging out will disconnect all of your active "
6 + "Guacamole sessions. Are you sure you wish to log out?",
8 /* Detection Constants */
10 "LONG_PRESS_DETECT_TIMEOUT" : 800, /* milliseconds */
11 "LONG_PRESS_MOVEMENT_THRESHOLD" : 10, /* pixels */
12 "MENU_CLOSE_DETECT_TIMEOUT" : 500, /* milliseconds */
13 "MENU_OPEN_DETECT_TIMEOUT" : 325, /* milliseconds */
14 "KEYBOARD_AUTO_RESIZE_INTERVAL" : 30, /* milliseconds */
16 /* Animation Constants */
18 "MENU_SHADE_STEPS" : 10, /* frames */
19 "MENU_SHADE_INTERVAL" : 30, /* milliseconds */
20 "MENU_SHOW_STEPS" : 5, /* frames */
21 "MENU_SHOW_INTERVAL" : 30, /* milliseconds */
23 /* OSK Mode Constants */
24 "OSK_MODE_NATIVE" : 1, /* "Show Keyboard" will show the platform's native OSK */
25 "OSK_MODE_GUAC" : 2, /* "Show Keyboard" will show Guac's built-in OSK */
29 "viewport" : document.getElementById("viewportClone"),
30 "display" : document.getElementById("display"),
31 "menu" : document.getElementById("menu"),
32 "menuControl" : document.getElementById("menuControl"),
33 "touchMenu" : document.getElementById("touchMenu"),
34 "logo" : document.getElementById("status-logo"),
35 "eventTarget" : document.getElementById("eventTarget"),
39 "showClipboard": document.getElementById("showClipboard"),
40 "showKeyboard" : document.getElementById("showKeyboard"),
41 "ctrlAltDelete": document.getElementById("ctrlAltDelete"),
42 "reconnect" : document.getElementById("reconnect"),
43 "logout" : document.getElementById("logout"),
45 "touchShowClipboard" : document.getElementById("touchShowClipboard"),
46 "touchShowKeyboard" : document.getElementById("touchShowKeyboard"),
47 "touchLogout" : document.getElementById("touchLogout")
52 "state" : document.getElementById("statusDialog"),
53 "clipboard" : document.getElementById("clipboardDiv"),
54 "touchClipboard": document.getElementById("touchClipboardDiv"),
55 "keyboard" : document.getElementById("keyboardContainer")
58 "state" : document.getElementById("statusText"),
59 "clipboard" : document.getElementById("clipboard"),
60 "touchClipboard" : document.getElementById("touchClipboard")
64 // Constant UI initialization and behavior
67 var menu_shaded = false;
69 var shade_interval = null;
70 var show_interval = null;
72 // Cache error image (might not be available when error occurs)
73 var guacErrorImage = new Image();
74 guacErrorImage.src = "images/noguacamole-logo-24.png";
76 // Function for adding a class to an element
79 // Function for removing a class from an element
82 // If Node.classList is supported, implement addClass/removeClass using that
85 addClass = function(element, classname) {
86 element.classList.add(classname);
89 removeClass = function(element, classname) {
90 element.classList.remove(classname);
95 // Otherwise, implement own
98 addClass = function(element, classname) {
100 // Simply add new class
101 element.className += " " + classname;
105 removeClass = function(element, classname) {
107 // Filter out classes with given name
108 element.className = element.className.replace(/([^ ]+)[ ]*/g,
109 function(match, testClassname, spaces, offset, string) {
111 // If same class, remove
112 if (testClassname == classname)
126 GuacamoleUI.hideStatus = function() {
127 removeClass(document.body, "guac-error");
128 GuacamoleUI.containers.state.style.visibility = "hidden";
129 GuacamoleUI.display.style.opacity = "1";
132 GuacamoleUI.showStatus = function(text) {
133 removeClass(document.body, "guac-error");
134 GuacamoleUI.containers.state.style.visibility = "visible";
135 GuacamoleUI.state.textContent = text;
136 GuacamoleUI.display.style.opacity = "1";
139 GuacamoleUI.showError = function(error) {
140 addClass(document.body, "guac-error");
141 GuacamoleUI.state.textContent = error;
142 GuacamoleUI.display.style.opacity = "0.1";
145 GuacamoleUI.hideTouchMenu = function() {
146 GuacamoleUI.touchMenu.style.visibility = "hidden";
149 function positionCentered(element) {
151 ((GuacamoleUI.viewport.offsetWidth - element.offsetWidth) / 2
152 + window.pageXOffset)
156 ((GuacamoleUI.viewport.offsetHeight - element.offsetHeight) / 2
157 + window.pageYOffset)
161 GuacamoleUI.showTouchMenu = function() {
162 positionCentered(GuacamoleUI.touchMenu);
163 GuacamoleUI.touchMenu.style.visibility = "visible";
166 GuacamoleUI.hideTouchClipboard = function() {
167 GuacamoleUI.touchClipboard.blur();
168 GuacamoleUI.containers.touchClipboard.style.visibility = "hidden";
171 GuacamoleUI.showTouchClipboard = function() {
172 positionCentered(GuacamoleUI.containers.touchClipboard);
173 GuacamoleUI.containers.touchClipboard.style.visibility = "visible";
176 GuacamoleUI.shadeMenu = function() {
180 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHADE_STEPS) + 1;
184 window.clearInterval(show_interval);
185 shade_interval = window.setInterval(function() {
189 GuacamoleUI.menu.style.transform =
190 GuacamoleUI.menu.style.WebkitTransform =
191 GuacamoleUI.menu.style.MozTransform =
192 GuacamoleUI.menu.style.OTransform =
193 GuacamoleUI.menu.style.msTransform =
195 "translateY(" + offset + "px)";
197 if (offset <= -GuacamoleUI.menu.offsetHeight) {
198 window.clearInterval(shade_interval);
199 GuacamoleUI.menu.style.visiblity = "hidden";
202 }, GuacamoleUI.MENU_SHADE_INTERVAL);
207 GuacamoleUI.showMenu = function() {
211 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHOW_STEPS) + 1;
212 var offset = -GuacamoleUI.menu.offsetHeight;
214 GuacamoleUI.menu.style.visiblity = "";
216 window.clearInterval(shade_interval);
217 show_interval = window.setInterval(function() {
223 window.clearInterval(show_interval);
226 GuacamoleUI.menu.style.transform =
227 GuacamoleUI.menu.style.WebkitTransform =
228 GuacamoleUI.menu.style.MozTransform =
229 GuacamoleUI.menu.style.OTransform =
230 GuacamoleUI.menu.style.msTransform =
232 "translateY(" + offset + "px)";
234 }, GuacamoleUI.MENU_SHOW_INTERVAL);
239 // Show/Hide clipboard
240 GuacamoleUI.buttons.showClipboard.onclick = function() {
242 var displayed = GuacamoleUI.containers.clipboard.style.display;
243 if (displayed != "block") {
244 GuacamoleUI.containers.clipboard.style.display = "block";
245 GuacamoleUI.buttons.showClipboard.innerHTML = "Hide Clipboard";
248 GuacamoleUI.containers.clipboard.style.display = "none";
249 GuacamoleUI.buttons.showClipboard.innerHTML = "Show Clipboard";
250 GuacamoleUI.clipboard.onchange();
255 GuacamoleUI.buttons.touchShowClipboard.onclick = function() {
256 GuacamoleUI.hideTouchMenu();
257 GuacamoleUI.showTouchClipboard();
260 // Show/Hide keyboard
261 var keyboardResizeInterval = null;
262 GuacamoleUI.buttons.showKeyboard.onclick = function() {
264 // If Guac OSK shown, hide it.
265 var displayed = GuacamoleUI.containers.keyboard.style.display;
266 if (displayed == "block") {
267 GuacamoleUI.containers.keyboard.style.display = "none";
268 GuacamoleUI.buttons.showKeyboard.textContent = "Show Keyboard";
270 window.onresize = null;
271 window.clearInterval(keyboardResizeInterval);
274 // Otherwise, show it
277 // Ensure event target is NOT focused if we are using the Guac OSK.
278 GuacamoleUI.eventTarget.blur();
280 GuacamoleUI.containers.keyboard.style.display = "block";
281 GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
283 // Automatically update size
284 window.onresize = updateKeyboardSize;
285 keyboardResizeInterval = window.setInterval(updateKeyboardSize,
286 GuacamoleUI.KEYBOARD_AUTO_RESIZE_INTERVAL);
288 updateKeyboardSize();
294 // Touch-specific keyboard show
295 GuacamoleUI.buttons.touchShowKeyboard.onclick =
298 // Center event target in case browser automatically centers
299 // input fields on focus.
300 GuacamoleUI.eventTarget.style.left =
301 (window.pageXOffset + GuacamoleUI.viewport.offsetWidth / 2) + "px";
303 GuacamoleUI.eventTarget.style.top =
304 (window.pageYOffset + GuacamoleUI.viewport.offsetHeight / 2) + "px";
306 GuacamoleUI.eventTarget.focus();
307 GuacamoleUI.hideTouchMenu();
312 GuacamoleUI.buttons.logout.onclick =
313 GuacamoleUI.buttons.touchLogout.onclick =
316 // Logout after warning user about session disconnect
317 if (confirm(GuacamoleUI.LOGOUT_PROMPT)) {
318 window.location.href = "logout";
319 GuacamoleUI.hideTouchMenu();
324 // Timeouts for detecting if users wants menu to open or close
325 var detectMenuOpenTimeout = null;
326 var detectMenuCloseTimeout = null;
328 // Clear detection timeouts
329 GuacamoleUI.resetMenuDetect = function() {
331 if (detectMenuOpenTimeout != null) {
332 window.clearTimeout(detectMenuOpenTimeout);
333 detectMenuOpenTimeout = null;
336 if (detectMenuCloseTimeout != null) {
337 window.clearTimeout(detectMenuCloseTimeout);
338 detectMenuCloseTimeout = null;
343 // Initiate detection of menu open action. If not canceled through some
344 // user event, menu will open.
345 GuacamoleUI.startMenuOpenDetect = function() {
347 if (!detectMenuOpenTimeout) {
349 // Clear detection state
350 GuacamoleUI.resetMenuDetect();
352 // Wait and then show menu
353 detectMenuOpenTimeout = window.setTimeout(function() {
355 // If menu opened via mouse, do not show native OSK
356 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
358 GuacamoleUI.showMenu();
359 detectMenuOpenTimeout = null;
360 }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
366 // Initiate detection of menu close action. If not canceled through some
367 // user mouse event, menu will close.
368 GuacamoleUI.startMenuCloseDetect = function() {
370 if (!detectMenuCloseTimeout) {
372 // Clear detection state
373 GuacamoleUI.resetMenuDetect();
375 // Wait and then shade menu
376 detectMenuCloseTimeout = window.setTimeout(function() {
377 GuacamoleUI.shadeMenu();
378 detectMenuCloseTimeout = null;
379 }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
385 // Show menu if mouseover any part of menu
386 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
388 // Stop detecting menu state change intents if mouse is over menu
389 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
391 // When mouse hovers over top of screen, start detection of intent to open menu
392 GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
394 var long_press_start_x = 0;
395 var long_press_start_y = 0;
396 var menuShowLongPressTimeout = null;
398 GuacamoleUI.startLongPressDetect = function() {
400 if (!menuShowLongPressTimeout) {
402 menuShowLongPressTimeout = window.setTimeout(function() {
404 menuShowLongPressTimeout = null;
406 // Assume native OSK if menu shown via long-press
407 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
408 GuacamoleUI.showTouchMenu();
410 }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
415 GuacamoleUI.stopLongPressDetect = function() {
416 window.clearTimeout(menuShowLongPressTimeout);
417 menuShowLongPressTimeout = null;
420 // Detect long-press at bottom of screen
421 GuacamoleUI.display.addEventListener('touchstart', function(e) {
423 // Close menu if shown
424 GuacamoleUI.shadeMenu();
425 GuacamoleUI.hideTouchMenu();
426 GuacamoleUI.hideTouchClipboard();
428 // Record touch location
429 if (e.touches.length == 1) {
430 var touch = e.touches[0];
431 long_press_start_x = touch.screenX;
432 long_press_start_y = touch.screenY;
436 GuacamoleUI.startLongPressDetect();
440 // Stop detection if touch moves significantly
441 GuacamoleUI.display.addEventListener('touchmove', function(e) {
443 // If touch distance from start exceeds threshold, cancel long press
444 var touch = e.touches[0];
445 if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
446 || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
447 GuacamoleUI.stopLongPressDetect();
451 // Stop detection if press stops
452 GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
454 // Close menu on mouse movement
455 GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
456 GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
459 GuacamoleUI.buttons.reconnect.onclick = function() {
460 window.location.reload();
463 // On-screen keyboard
464 GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty.xml");
465 GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
467 // Function for automatically updating keyboard size
468 var lastKeyboardWidth;
469 function updateKeyboardSize() {
470 var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
471 if (lastKeyboardWidth != currentSize) {
472 GuacamoleUI.keyboard.resize(currentSize);
473 lastKeyboardWidth = currentSize;
477 // Turn off autocorrect and autocapitalization on eventTarget
478 GuacamoleUI.eventTarget.setAttribute("autocorrect", "off");
479 GuacamoleUI.eventTarget.setAttribute("autocapitalize", "off");
481 // Automatically reposition event target on scroll
482 window.addEventListener("scroll", function() {
483 GuacamoleUI.eventTarget.style.left = window.pageXOffset + "px";
484 GuacamoleUI.eventTarget.style.top = window.pageYOffset + "px";
489 // Tie UI events / behavior to a specific Guacamole client
490 GuacamoleUI.attach = function(guac) {
492 var title_prefix = null;
493 var connection_name = "Guacamole";
495 var guac_display = guac.getDisplay();
497 // Set document title appropriately, based on prefix and connection name
498 function updateTitle() {
500 // Use title prefix if present
503 document.title = title_prefix;
505 // Include connection name, if present
507 document.title += " " + connection_name;
511 // Otherwise, just set to connection name
512 else if (connection_name)
513 document.title = connection_name;
517 // When mouse enters display, start detection of intent to close menu
518 guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
520 guac_display.onclick = function(e) {
526 var mouse = new Guacamole.Mouse(guac_display);
527 var touch = new Guacamole.Mouse.Touchpad(guac_display);
528 touch.onmousedown = touch.onmouseup = touch.onmousemove =
529 mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
530 function(mouseState) {
532 // Determine mouse position within view
533 var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
534 var mouse_view_y = mouseState.y + guac_display.offsetTop - window.pageYOffset;
536 // Determine viewport dimensioins
537 var view_width = GuacamoleUI.viewport.offsetWidth;
538 var view_height = GuacamoleUI.viewport.offsetHeight;
540 // Determine scroll amounts based on mouse position relative to document
543 if (mouse_view_x > view_width)
544 scroll_amount_x = mouse_view_x - view_width;
545 else if (mouse_view_x < 0)
546 scroll_amount_x = mouse_view_x;
551 if (mouse_view_y > view_height)
552 scroll_amount_y = mouse_view_y - view_height;
553 else if (mouse_view_y < 0)
554 scroll_amount_y = mouse_view_y;
558 // Scroll (if necessary) to keep mouse on screen.
559 window.scrollBy(scroll_amount_x, scroll_amount_y);
562 guac.sendMouseState(mouseState);
567 var keyboard = new Guacamole.Keyboard(document);
569 // Monitor whether the event target is focused
570 var eventTargetFocused = false;
572 // Save length for calculation of changed value
573 var currentLength = GuacamoleUI.eventTarget.value.length;
575 GuacamoleUI.eventTarget.onfocus = function() {
576 eventTargetFocused = true;
577 GuacamoleUI.eventTarget.value = "";
581 GuacamoleUI.eventTarget.onblur = function() {
582 eventTargetFocused = false;
585 // If text is input directly into event target without typing (as with
586 // voice input, for example), type automatically.
587 GuacamoleUI.eventTarget.oninput = function(e) {
589 // Calculate current length and change in length
590 var oldLength = currentLength;
591 currentLength = GuacamoleUI.eventTarget.value.length;
593 // If deleted or replaced text, ignore
594 if (currentLength <= oldLength)
598 var text = GuacamoleUI.eventTarget.value.substring(oldLength);
600 // Send each character
601 for (var i=0; i<text.length; i++) {
604 var charCode = text.charCodeAt(i);
607 var keysym = 0x003F; // Default to a question mark
608 if (charCode >= 0x0000 && charCode <= 0x00FF)
610 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
611 keysym = 0x01000000 | charCode;
613 // Send keysym only if not already pressed
614 if (!keyboard.pressed[keysym]) {
616 // Press and release key
617 guac.sendKeyEvent(1, keysym);
618 guac.sendKeyEvent(0, keysym);
626 function isTypableCharacter(keysym) {
627 return (keysym & 0xFFFF00) != 0xFF00;
630 function disableKeyboard() {
631 keyboard.onkeydown = null;
632 keyboard.onkeyup = null;
635 function enableKeyboard() {
637 keyboard.onkeydown = function (keysym) {
638 guac.sendKeyEvent(1, keysym);
639 return eventTargetFocused && isTypableCharacter(keysym);
642 keyboard.onkeyup = function (keysym) {
643 guac.sendKeyEvent(0, keysym);
644 return eventTargetFocused && isTypableCharacter(keysym);
649 // Enable keyboard by default
652 // Handle client state change
653 guac.onstatechange = function(clientState) {
655 switch (clientState) {
659 GuacamoleUI.showStatus("Idle.");
660 title_prefix = "[Idle]";
665 GuacamoleUI.shadeMenu();
666 GuacamoleUI.showStatus("Connecting...");
667 title_prefix = "[Connecting...]";
670 // Connected + waiting
672 GuacamoleUI.showStatus("Connected, waiting for first update...");
673 title_prefix = "[Waiting...]";
678 GuacamoleUI.hideStatus();
684 GuacamoleUI.showStatus("Disconnecting...");
685 title_prefix = "[Disconnecting...]";
690 GuacamoleUI.showStatus("Disconnected.");
691 title_prefix = "[Disconnected]";
694 // Unknown status code
696 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
703 // Name instruction handler
704 guac.onname = function(name) {
705 connection_name = name;
710 guac.onerror = function(error) {
712 // Disconnect, if connected
715 // Display error message
716 GuacamoleUI.showError(error);
720 // Disconnect on close
721 window.onunload = function() {
725 // Handle clipboard events
726 GuacamoleUI.clipboard.onchange = function() {
728 var text = GuacamoleUI.clipboard.value;
729 GuacamoleUI.touchClipboard.value = text;
730 guac.setClipboard(text);
734 GuacamoleUI.touchClipboard.onchange = function() {
736 var text = GuacamoleUI.touchClipboard.value;
737 GuacamoleUI.clipboard.value = text;
738 guac.setClipboard(text);
742 // Ignore keypresses when clipboard is focused
743 GuacamoleUI.clipboard.onfocus =
744 GuacamoleUI.touchClipboard.onfocus = function() {
748 // Capture keypresses when clipboard is not focused
749 GuacamoleUI.clipboard.onblur =
750 GuacamoleUI.touchClipboard.onblur = function() {
754 // Server copy handler
755 guac.onclipboard = function(data) {
756 GuacamoleUI.clipboard.value = data;
757 GuacamoleUI.touchClipboard.value = data;
760 GuacamoleUI.keyboard.onkeydown = function(keysym) {
761 guac.sendKeyEvent(1, keysym);
764 GuacamoleUI.keyboard.onkeyup = function(keysym) {
765 guac.sendKeyEvent(0, keysym);
768 // Send Ctrl-Alt-Delete
769 GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
771 var KEYSYM_CTRL = 0xFFE3;
772 var KEYSYM_ALT = 0xFFE9;
773 var KEYSYM_DELETE = 0xFFFF;
775 guac.sendKeyEvent(1, KEYSYM_CTRL);
776 guac.sendKeyEvent(1, KEYSYM_ALT);
777 guac.sendKeyEvent(1, KEYSYM_DELETE);
778 guac.sendKeyEvent(0, KEYSYM_DELETE);
779 guac.sendKeyEvent(0, KEYSYM_ALT);
780 guac.sendKeyEvent(0, KEYSYM_CTRL);