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.containers.touchClipboard.style.visibility = "hidden";
170 GuacamoleUI.showTouchClipboard = function() {
171 positionCentered(GuacamoleUI.containers.touchClipboard);
172 GuacamoleUI.containers.touchClipboard.style.visibility = "visible";
175 GuacamoleUI.shadeMenu = function() {
179 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHADE_STEPS) + 1;
183 window.clearInterval(show_interval);
184 shade_interval = window.setInterval(function() {
188 GuacamoleUI.menu.style.transform =
189 GuacamoleUI.menu.style.WebkitTransform =
190 GuacamoleUI.menu.style.MozTransform =
191 GuacamoleUI.menu.style.OTransform =
192 GuacamoleUI.menu.style.msTransform =
194 "translateY(" + offset + "px)";
196 if (offset <= -GuacamoleUI.menu.offsetHeight) {
197 window.clearInterval(shade_interval);
198 GuacamoleUI.menu.style.visiblity = "hidden";
201 }, GuacamoleUI.MENU_SHADE_INTERVAL);
206 GuacamoleUI.showMenu = function() {
210 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHOW_STEPS) + 1;
211 var offset = -GuacamoleUI.menu.offsetHeight;
213 GuacamoleUI.menu.style.visiblity = "";
215 window.clearInterval(shade_interval);
216 show_interval = window.setInterval(function() {
222 window.clearInterval(show_interval);
225 GuacamoleUI.menu.style.transform =
226 GuacamoleUI.menu.style.WebkitTransform =
227 GuacamoleUI.menu.style.MozTransform =
228 GuacamoleUI.menu.style.OTransform =
229 GuacamoleUI.menu.style.msTransform =
231 "translateY(" + offset + "px)";
233 }, GuacamoleUI.MENU_SHOW_INTERVAL);
238 // Show/Hide clipboard
239 GuacamoleUI.buttons.showClipboard.onclick = function() {
241 var displayed = GuacamoleUI.containers.clipboard.style.display;
242 if (displayed != "block") {
243 GuacamoleUI.containers.clipboard.style.display = "block";
244 GuacamoleUI.buttons.showClipboard.innerHTML = "Hide Clipboard";
247 GuacamoleUI.containers.clipboard.style.display = "none";
248 GuacamoleUI.buttons.showClipboard.innerHTML = "Show Clipboard";
249 GuacamoleUI.clipboard.onchange();
254 GuacamoleUI.buttons.touchShowClipboard.onclick = function() {
255 GuacamoleUI.hideTouchMenu();
256 GuacamoleUI.showTouchClipboard();
259 // Show/Hide keyboard
260 var keyboardResizeInterval = null;
261 GuacamoleUI.buttons.showKeyboard.onclick = function() {
263 // If Guac OSK shown, hide it.
264 var displayed = GuacamoleUI.containers.keyboard.style.display;
265 if (displayed == "block") {
266 GuacamoleUI.containers.keyboard.style.display = "none";
267 GuacamoleUI.buttons.showKeyboard.textContent = "Show Keyboard";
269 window.onresize = null;
270 window.clearInterval(keyboardResizeInterval);
273 // Otherwise, show it
276 // Ensure event target is NOT focused if we are using the Guac OSK.
277 GuacamoleUI.eventTarget.blur();
279 GuacamoleUI.containers.keyboard.style.display = "block";
280 GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
282 // Automatically update size
283 window.onresize = updateKeyboardSize;
284 keyboardResizeInterval = window.setInterval(updateKeyboardSize,
285 GuacamoleUI.KEYBOARD_AUTO_RESIZE_INTERVAL);
287 updateKeyboardSize();
293 // Touch-specific keyboard show
294 GuacamoleUI.buttons.touchShowKeyboard.onclick =
297 // Center event target in case browser automatically centers
298 // input fields on focus.
299 GuacamoleUI.eventTarget.style.left =
300 (window.pageXOffset + GuacamoleUI.viewport.offsetWidth / 2) + "px";
302 GuacamoleUI.eventTarget.style.top =
303 (window.pageYOffset + GuacamoleUI.viewport.offsetHeight / 2) + "px";
305 GuacamoleUI.eventTarget.focus();
306 GuacamoleUI.hideTouchMenu();
311 GuacamoleUI.buttons.logout.onclick =
312 GuacamoleUI.buttons.touchLogout.onclick =
315 // Logout after warning user about session disconnect
316 if (confirm(GuacamoleUI.LOGOUT_PROMPT)) {
317 window.location.href = "logout";
318 GuacamoleUI.hideTouchMenu();
323 // Timeouts for detecting if users wants menu to open or close
324 var detectMenuOpenTimeout = null;
325 var detectMenuCloseTimeout = null;
327 // Clear detection timeouts
328 GuacamoleUI.resetMenuDetect = function() {
330 if (detectMenuOpenTimeout != null) {
331 window.clearTimeout(detectMenuOpenTimeout);
332 detectMenuOpenTimeout = null;
335 if (detectMenuCloseTimeout != null) {
336 window.clearTimeout(detectMenuCloseTimeout);
337 detectMenuCloseTimeout = null;
342 // Initiate detection of menu open action. If not canceled through some
343 // user event, menu will open.
344 GuacamoleUI.startMenuOpenDetect = function() {
346 if (!detectMenuOpenTimeout) {
348 // Clear detection state
349 GuacamoleUI.resetMenuDetect();
351 // Wait and then show menu
352 detectMenuOpenTimeout = window.setTimeout(function() {
354 // If menu opened via mouse, do not show native OSK
355 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
357 GuacamoleUI.showMenu();
358 detectMenuOpenTimeout = null;
359 }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
365 // Initiate detection of menu close action. If not canceled through some
366 // user mouse event, menu will close.
367 GuacamoleUI.startMenuCloseDetect = function() {
369 if (!detectMenuCloseTimeout) {
371 // Clear detection state
372 GuacamoleUI.resetMenuDetect();
374 // Wait and then shade menu
375 detectMenuCloseTimeout = window.setTimeout(function() {
376 GuacamoleUI.shadeMenu();
377 detectMenuCloseTimeout = null;
378 }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
384 // Show menu if mouseover any part of menu
385 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
387 // Stop detecting menu state change intents if mouse is over menu
388 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
390 // When mouse hovers over top of screen, start detection of intent to open menu
391 GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
393 var long_press_start_x = 0;
394 var long_press_start_y = 0;
395 var menuShowLongPressTimeout = null;
397 GuacamoleUI.startLongPressDetect = function() {
399 if (!menuShowLongPressTimeout) {
401 menuShowLongPressTimeout = window.setTimeout(function() {
403 menuShowLongPressTimeout = null;
405 // Assume native OSK if menu shown via long-press
406 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
407 GuacamoleUI.showTouchMenu();
409 }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
414 GuacamoleUI.stopLongPressDetect = function() {
415 window.clearTimeout(menuShowLongPressTimeout);
416 menuShowLongPressTimeout = null;
419 // Detect long-press at bottom of screen
420 GuacamoleUI.display.addEventListener('touchstart', function(e) {
422 // Close menu if shown
423 GuacamoleUI.shadeMenu();
424 GuacamoleUI.hideTouchMenu();
425 GuacamoleUI.hideTouchClipboard();
427 // Record touch location
428 if (e.touches.length == 1) {
429 var touch = e.touches[0];
430 long_press_start_x = touch.screenX;
431 long_press_start_y = touch.screenY;
435 GuacamoleUI.startLongPressDetect();
439 // Stop detection if touch moves significantly
440 GuacamoleUI.display.addEventListener('touchmove', function(e) {
442 // If touch distance from start exceeds threshold, cancel long press
443 var touch = e.touches[0];
444 if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
445 || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
446 GuacamoleUI.stopLongPressDetect();
450 // Stop detection if press stops
451 GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
453 // Close menu on mouse movement
454 GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
455 GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
458 GuacamoleUI.buttons.reconnect.onclick = function() {
459 window.location.reload();
462 // On-screen keyboard
463 GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty.xml");
464 GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
466 // Function for automatically updating keyboard size
467 var lastKeyboardWidth;
468 function updateKeyboardSize() {
469 var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
470 if (lastKeyboardWidth != currentSize) {
471 GuacamoleUI.keyboard.resize(currentSize);
472 lastKeyboardWidth = currentSize;
476 // Turn off autocorrect and autocapitalization on eventTarget
477 GuacamoleUI.eventTarget.setAttribute("autocorrect", "off");
478 GuacamoleUI.eventTarget.setAttribute("autocapitalize", "off");
480 // Automatically reposition event target on scroll
481 window.addEventListener("scroll", function() {
482 GuacamoleUI.eventTarget.style.left = window.pageXOffset + "px";
483 GuacamoleUI.eventTarget.style.top = window.pageYOffset + "px";
488 // Tie UI events / behavior to a specific Guacamole client
489 GuacamoleUI.attach = function(guac) {
491 var title_prefix = null;
492 var connection_name = "Guacamole";
494 var guac_display = guac.getDisplay();
496 // Set document title appropriately, based on prefix and connection name
497 function updateTitle() {
499 // Use title prefix if present
502 document.title = title_prefix;
504 // Include connection name, if present
506 document.title += " " + connection_name;
510 // Otherwise, just set to connection name
511 else if (connection_name)
512 document.title = connection_name;
516 // When mouse enters display, start detection of intent to close menu
517 guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
519 guac_display.onclick = function(e) {
525 var mouse = new Guacamole.Mouse(guac_display);
526 mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
527 function(mouseState) {
529 // Determine mouse position within view
530 var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
531 var mouse_view_y = mouseState.y + guac_display.offsetTop - window.pageYOffset;
533 // Determine viewport dimensioins
534 var view_width = GuacamoleUI.viewport.offsetWidth;
535 var view_height = GuacamoleUI.viewport.offsetHeight;
537 // Determine scroll amounts based on mouse position relative to document
540 if (mouse_view_x > view_width)
541 scroll_amount_x = mouse_view_x - view_width;
542 else if (mouse_view_x < 0)
543 scroll_amount_x = mouse_view_x;
548 if (mouse_view_y > view_height)
549 scroll_amount_y = mouse_view_y - view_height;
550 else if (mouse_view_y < 0)
551 scroll_amount_y = mouse_view_y;
555 // Scroll (if necessary) to keep mouse on screen.
556 window.scrollBy(scroll_amount_x, scroll_amount_y);
559 guac.sendMouseState(mouseState);
564 var keyboard = new Guacamole.Keyboard(document);
566 // Monitor whether the event target is focused
567 var eventTargetFocused = false;
569 // Save length for calculation of changed value
570 var currentLength = GuacamoleUI.eventTarget.value.length;
572 GuacamoleUI.eventTarget.onfocus = function() {
573 eventTargetFocused = true;
574 GuacamoleUI.eventTarget.value = "";
578 GuacamoleUI.eventTarget.onblur = function() {
579 eventTargetFocused = false;
582 // If text is input directly into event target without typing (as with
583 // voice input, for example), type automatically.
584 GuacamoleUI.eventTarget.oninput = function(e) {
586 // Calculate current length and change in length
587 var oldLength = currentLength;
588 currentLength = GuacamoleUI.eventTarget.value.length;
590 // If deleted or replaced text, ignore
591 if (currentLength <= oldLength)
595 var text = GuacamoleUI.eventTarget.value.substring(oldLength);
597 // Send each character
598 for (var i=0; i<text.length; i++) {
601 var charCode = text.charCodeAt(i);
604 var keysym = 0x003F; // Default to a question mark
605 if (charCode >= 0x0000 && charCode <= 0x00FF)
607 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
608 keysym = 0x01000000 | charCode;
610 // Send keysym only if not already pressed
611 if (!keyboard.pressed[keysym]) {
613 // Press and release key
614 guac.sendKeyEvent(1, keysym);
615 guac.sendKeyEvent(0, keysym);
623 function isTypableCharacter(keysym) {
624 return (keysym & 0xFFFF00) != 0xFF00;
627 function disableKeyboard() {
628 keyboard.onkeydown = null;
629 keyboard.onkeyup = null;
632 function enableKeyboard() {
634 keyboard.onkeydown = function (keysym) {
635 guac.sendKeyEvent(1, keysym);
636 return eventTargetFocused && isTypableCharacter(keysym);
639 keyboard.onkeyup = function (keysym) {
640 guac.sendKeyEvent(0, keysym);
641 return eventTargetFocused && isTypableCharacter(keysym);
646 // Enable keyboard by default
649 // Handle client state change
650 guac.onstatechange = function(clientState) {
652 switch (clientState) {
656 GuacamoleUI.showStatus("Idle.");
657 title_prefix = "[Idle]";
662 GuacamoleUI.shadeMenu();
663 GuacamoleUI.showStatus("Connecting...");
664 title_prefix = "[Connecting...]";
667 // Connected + waiting
669 GuacamoleUI.showStatus("Connected, waiting for first update...");
670 title_prefix = "[Waiting...]";
675 GuacamoleUI.hideStatus();
681 GuacamoleUI.showStatus("Disconnecting...");
682 title_prefix = "[Disconnecting...]";
687 GuacamoleUI.showStatus("Disconnected.");
688 title_prefix = "[Disconnected]";
691 // Unknown status code
693 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
700 // Name instruction handler
701 guac.onname = function(name) {
702 connection_name = name;
707 guac.onerror = function(error) {
709 // Disconnect, if connected
712 // Display error message
713 GuacamoleUI.showError(error);
717 // Disconnect on close
718 window.onunload = function() {
722 // Handle clipboard events
723 GuacamoleUI.clipboard.onchange = function() {
725 var text = GuacamoleUI.clipboard.value;
726 GuacamoleUI.touchClipboard.value = text;
727 guac.setClipboard(text);
731 GuacamoleUI.touchClipboard.onchange = function() {
733 var text = GuacamoleUI.touchClipboard.value;
734 GuacamoleUI.clipboard.value = text;
735 guac.setClipboard(text);
739 // Ignore keypresses when clipboard is focused
740 GuacamoleUI.clipboard.onfocus =
741 GuacamoleUI.touchClipboard.onfocus = function() {
745 // Capture keypresses when clipboard is not focused
746 GuacamoleUI.clipboard.onblur =
747 GuacamoleUI.touchClipboard.onblur = function() {
751 // Server copy handler
752 guac.onclipboard = function(data) {
753 GuacamoleUI.clipboard.value = data;
754 GuacamoleUI.touchClipboard.value = data;
757 GuacamoleUI.keyboard.onkeydown = function(keysym) {
758 guac.sendKeyEvent(1, keysym);
761 GuacamoleUI.keyboard.onkeyup = function(keysym) {
762 guac.sendKeyEvent(0, keysym);
765 // Send Ctrl-Alt-Delete
766 GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
768 var KEYSYM_CTRL = 0xFFE3;
769 var KEYSYM_ALT = 0xFFE9;
770 var KEYSYM_DELETE = 0xFFFF;
772 guac.sendKeyEvent(1, KEYSYM_CTRL);
773 guac.sendKeyEvent(1, KEYSYM_ALT);
774 guac.sendKeyEvent(1, KEYSYM_DELETE);
775 guac.sendKeyEvent(0, KEYSYM_DELETE);
776 guac.sendKeyEvent(0, KEYSYM_ALT);
777 guac.sendKeyEvent(0, KEYSYM_CTRL);