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 if(1 == window.SELF_CLOSE){
321 window.location.href = "logout";
322 GuacamoleUI.hideTouchMenu();
328 // Timeouts for detecting if users wants menu to open or close
329 var detectMenuOpenTimeout = null;
330 var detectMenuCloseTimeout = null;
332 // Clear detection timeouts
333 GuacamoleUI.resetMenuDetect = function() {
335 if (detectMenuOpenTimeout != null) {
336 window.clearTimeout(detectMenuOpenTimeout);
337 detectMenuOpenTimeout = null;
340 if (detectMenuCloseTimeout != null) {
341 window.clearTimeout(detectMenuCloseTimeout);
342 detectMenuCloseTimeout = null;
347 // Initiate detection of menu open action. If not canceled through some
348 // user event, menu will open.
349 GuacamoleUI.startMenuOpenDetect = function() {
351 if (!detectMenuOpenTimeout) {
353 // Clear detection state
354 GuacamoleUI.resetMenuDetect();
356 // Wait and then show menu
357 detectMenuOpenTimeout = window.setTimeout(function() {
359 // If menu opened via mouse, do not show native OSK
360 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
362 GuacamoleUI.showMenu();
363 detectMenuOpenTimeout = null;
364 }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
370 // Initiate detection of menu close action. If not canceled through some
371 // user mouse event, menu will close.
372 GuacamoleUI.startMenuCloseDetect = function() {
374 if (!detectMenuCloseTimeout) {
376 // Clear detection state
377 GuacamoleUI.resetMenuDetect();
379 // Wait and then shade menu
380 detectMenuCloseTimeout = window.setTimeout(function() {
381 GuacamoleUI.shadeMenu();
382 detectMenuCloseTimeout = null;
383 }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
389 // Show menu if mouseover any part of menu
390 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
392 // Stop detecting menu state change intents if mouse is over menu
393 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
395 // When mouse hovers over top of screen, start detection of intent to open menu
396 GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
398 var long_press_start_x = 0;
399 var long_press_start_y = 0;
400 var menuShowLongPressTimeout = null;
402 GuacamoleUI.startLongPressDetect = function() {
404 if (!menuShowLongPressTimeout) {
406 menuShowLongPressTimeout = window.setTimeout(function() {
408 menuShowLongPressTimeout = null;
410 // Assume native OSK if menu shown via long-press
411 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
412 GuacamoleUI.showTouchMenu();
414 }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
419 GuacamoleUI.stopLongPressDetect = function() {
420 window.clearTimeout(menuShowLongPressTimeout);
421 menuShowLongPressTimeout = null;
424 // Detect long-press at bottom of screen
425 GuacamoleUI.display.addEventListener('touchstart', function(e) {
427 // Close menu if shown
428 GuacamoleUI.shadeMenu();
429 GuacamoleUI.hideTouchMenu();
430 GuacamoleUI.hideTouchClipboard();
432 // Record touch location
433 if (e.touches.length == 1) {
434 var touch = e.touches[0];
435 long_press_start_x = touch.screenX;
436 long_press_start_y = touch.screenY;
440 GuacamoleUI.startLongPressDetect();
444 // Stop detection if touch moves significantly
445 GuacamoleUI.display.addEventListener('touchmove', function(e) {
447 // If touch distance from start exceeds threshold, cancel long press
448 var touch = e.touches[0];
449 if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
450 || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
451 GuacamoleUI.stopLongPressDetect();
455 // Stop detection if press stops
456 GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
458 // Close menu on mouse movement
459 GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
460 GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
463 GuacamoleUI.buttons.reconnect.onclick = function() {
464 window.location.reload();
467 // On-screen keyboard
468 GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty.xml");
469 GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
471 // Function for automatically updating keyboard size
472 var lastKeyboardWidth;
473 function updateKeyboardSize() {
474 var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
475 if (lastKeyboardWidth != currentSize) {
476 GuacamoleUI.keyboard.resize(currentSize);
477 lastKeyboardWidth = currentSize;
481 // Turn off autocorrect and autocapitalization on eventTarget
482 GuacamoleUI.eventTarget.setAttribute("autocorrect", "off");
483 GuacamoleUI.eventTarget.setAttribute("autocapitalize", "off");
485 // Automatically reposition event target on scroll
486 window.addEventListener("scroll", function() {
487 GuacamoleUI.eventTarget.style.left = window.pageXOffset + "px";
488 GuacamoleUI.eventTarget.style.top = window.pageYOffset + "px";
493 // Tie UI events / behavior to a specific Guacamole client
494 GuacamoleUI.attach = function(guac) {
496 var title_prefix = null;
497 var connection_name = "Guacamole";
499 var guac_display = guac.getDisplay();
501 // Set document title appropriately, based on prefix and connection name
502 function updateTitle() {
504 // Use title prefix if present
507 document.title = title_prefix;
509 // Include connection name, if present
511 document.title += " " + connection_name;
515 // Otherwise, just set to connection name
516 else if (connection_name)
517 document.title = connection_name;
521 // When mouse enters display, start detection of intent to close menu
522 guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
524 guac_display.onclick = function(e) {
530 var mouse = new Guacamole.Mouse(guac_display);
531 var touch = new Guacamole.Mouse.Touchpad(guac_display);
532 touch.onmousedown = touch.onmouseup = touch.onmousemove =
533 mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
534 function(mouseState) {
536 // Determine mouse position within view
537 var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
538 var mouse_view_y = mouseState.y + guac_display.offsetTop - window.pageYOffset;
540 // Determine viewport dimensioins
541 var view_width = GuacamoleUI.viewport.offsetWidth;
542 var view_height = GuacamoleUI.viewport.offsetHeight;
544 // Determine scroll amounts based on mouse position relative to document
547 if (mouse_view_x > view_width)
548 scroll_amount_x = mouse_view_x - view_width;
549 else if (mouse_view_x < 0)
550 scroll_amount_x = mouse_view_x;
555 if (mouse_view_y > view_height)
556 scroll_amount_y = mouse_view_y - view_height;
557 else if (mouse_view_y < 0)
558 scroll_amount_y = mouse_view_y;
562 // Scroll (if necessary) to keep mouse on screen.
563 window.scrollBy(scroll_amount_x, scroll_amount_y);
566 guac.sendMouseState(mouseState);
571 var keyboard = new Guacamole.Keyboard(document);
573 // Monitor whether the event target is focused
574 var eventTargetFocused = false;
576 // Save length for calculation of changed value
577 var currentLength = GuacamoleUI.eventTarget.value.length;
579 GuacamoleUI.eventTarget.onfocus = function() {
580 eventTargetFocused = true;
581 GuacamoleUI.eventTarget.value = "";
585 GuacamoleUI.eventTarget.onblur = function() {
586 eventTargetFocused = false;
589 // If text is input directly into event target without typing (as with
590 // voice input, for example), type automatically.
591 GuacamoleUI.eventTarget.oninput = function(e) {
593 // Calculate current length and change in length
594 var oldLength = currentLength;
595 currentLength = GuacamoleUI.eventTarget.value.length;
597 // If deleted or replaced text, ignore
598 if (currentLength <= oldLength)
602 var text = GuacamoleUI.eventTarget.value.substring(oldLength);
604 // Send each character
605 for (var i=0; i<text.length; i++) {
608 var charCode = text.charCodeAt(i);
611 var keysym = 0x003F; // Default to a question mark
612 if (charCode >= 0x0000 && charCode <= 0x00FF)
614 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
615 keysym = 0x01000000 | charCode;
617 // Send keysym only if not already pressed
618 if (!keyboard.pressed[keysym]) {
620 // Press and release key
621 guac.sendKeyEvent(1, keysym);
622 guac.sendKeyEvent(0, keysym);
630 function isTypableCharacter(keysym) {
631 return (keysym & 0xFFFF00) != 0xFF00;
634 function disableKeyboard() {
635 keyboard.onkeydown = null;
636 keyboard.onkeyup = null;
639 function enableKeyboard() {
641 keyboard.onkeydown = function (keysym) {
642 guac.sendKeyEvent(1, keysym);
643 return eventTargetFocused && isTypableCharacter(keysym);
646 keyboard.onkeyup = function (keysym) {
647 guac.sendKeyEvent(0, keysym);
648 return eventTargetFocused && isTypableCharacter(keysym);
653 // Enable keyboard by default
656 // Handle client state change
657 guac.onstatechange = function(clientState) {
659 switch (clientState) {
663 GuacamoleUI.showStatus("Idle.");
664 title_prefix = "[Idle]";
669 GuacamoleUI.shadeMenu();
670 GuacamoleUI.showStatus("Connecting...");
671 title_prefix = "[Connecting...]";
674 // Connected + waiting
676 GuacamoleUI.showStatus("Connected, waiting for first update...");
677 title_prefix = "[Waiting...]";
682 GuacamoleUI.hideStatus();
688 GuacamoleUI.showStatus("Disconnecting...");
689 title_prefix = "[Disconnecting...]";
694 GuacamoleUI.showStatus("Disconnected.");
695 title_prefix = "[Disconnected]";
698 // Unknown status code
700 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
707 // Name instruction handler
708 guac.onname = function(name) {
709 connection_name = name;
714 guac.onerror = function(error) {
716 // Disconnect, if connected
719 // Display error message
720 GuacamoleUI.showError(error);
724 // Disconnect on close
725 window.onunload = function() {
729 // Handle clipboard events
730 GuacamoleUI.clipboard.onchange = function() {
732 var text = GuacamoleUI.clipboard.value;
733 GuacamoleUI.touchClipboard.value = text;
734 guac.setClipboard(text);
738 GuacamoleUI.touchClipboard.onchange = function() {
740 var text = GuacamoleUI.touchClipboard.value;
741 GuacamoleUI.clipboard.value = text;
742 guac.setClipboard(text);
746 // Ignore keypresses when clipboard is focused
747 GuacamoleUI.clipboard.onfocus =
748 GuacamoleUI.touchClipboard.onfocus = function() {
752 // Capture keypresses when clipboard is not focused
753 GuacamoleUI.clipboard.onblur =
754 GuacamoleUI.touchClipboard.onblur = function() {
758 // Server copy handler
759 guac.onclipboard = function(data) {
760 GuacamoleUI.clipboard.value = data;
761 GuacamoleUI.touchClipboard.value = data;
764 GuacamoleUI.keyboard.onkeydown = function(keysym) {
765 guac.sendKeyEvent(1, keysym);
768 GuacamoleUI.keyboard.onkeyup = function(keysym) {
769 guac.sendKeyEvent(0, keysym);
772 // Send Ctrl-Alt-Delete
773 GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
775 var KEYSYM_CTRL = 0xFFE3;
776 var KEYSYM_ALT = 0xFFE9;
777 var KEYSYM_DELETE = 0xFFFF;
779 guac.sendKeyEvent(1, KEYSYM_CTRL);
780 guac.sendKeyEvent(1, KEYSYM_ALT);
781 guac.sendKeyEvent(1, KEYSYM_DELETE);
782 guac.sendKeyEvent(0, KEYSYM_DELETE);
783 guac.sendKeyEvent(0, KEYSYM_ALT);
784 guac.sendKeyEvent(0, KEYSYM_CTRL);