5 /* Detection Constants */
7 "LONG_PRESS_DETECT_TIMEOUT" : 800, /* milliseconds */
8 "LONG_PRESS_MOVEMENT_THRESHOLD" : 10, /* pixels */
9 "MENU_CLOSE_DETECT_TIMEOUT" : 500, /* milliseconds */
10 "MENU_OPEN_DETECT_TIMEOUT" : 325, /* milliseconds */
11 "KEYBOARD_AUTO_RESIZE_INTERVAL" : 30, /* milliseconds */
13 /* Animation Constants */
15 "MENU_SHADE_STEPS" : 10, /* frames */
16 "MENU_SHADE_INTERVAL" : 30, /* milliseconds */
17 "MENU_SHOW_STEPS" : 5, /* frames */
18 "MENU_SHOW_INTERVAL" : 30, /* milliseconds */
20 /* OSK Mode Constants */
21 "OSK_MODE_NATIVE" : 1, /* "Show Keyboard" will show the platform's native OSK */
22 "OSK_MODE_GUAC" : 2, /* "Show Keyboard" will show Guac's built-in OSK */
26 "viewport" : document.getElementById("viewportClone"),
27 "display" : document.getElementById("display"),
28 "menu" : document.getElementById("menu"),
29 "menuControl" : document.getElementById("menuControl"),
30 "touchMenu" : document.getElementById("touchMenu"),
31 "logo" : document.getElementById("status-logo"),
32 "eventTarget" : document.getElementById("eventTarget"),
36 "showClipboard": document.getElementById("showClipboard"),
37 "showKeyboard" : document.getElementById("showKeyboard"),
38 "ctrlAltDelete": document.getElementById("ctrlAltDelete"),
39 "reconnect" : document.getElementById("reconnect"),
40 "logout" : document.getElementById("logout"),
42 "touchShowClipboard" : document.getElementById("touchShowClipboard"),
43 "touchShowKeyboard" : document.getElementById("touchShowKeyboard"),
44 "touchLogout" : document.getElementById("touchLogout")
49 "state" : document.getElementById("statusDialog"),
50 "clipboard": document.getElementById("clipboardDiv"),
51 "keyboard" : document.getElementById("keyboardContainer")
54 "state" : document.getElementById("statusText"),
55 "clipboard" : document.getElementById("clipboard")
59 // Constant UI initialization and behavior
62 var menu_shaded = false;
64 var shade_interval = null;
65 var show_interval = null;
67 // Cache error image (might not be available when error occurs)
68 var guacErrorImage = new Image();
69 guacErrorImage.src = "images/noguacamole-logo-24.png";
71 // Function for adding a class to an element
74 // Function for removing a class from an element
77 // If Node.classList is supported, implement addClass/removeClass using that
80 addClass = function(element, classname) {
81 element.classList.add(classname);
84 removeClass = function(element, classname) {
85 element.classList.remove(classname);
90 // Otherwise, implement own
93 addClass = function(element, classname) {
95 // Simply add new class
96 element.className += " " + classname;
100 removeClass = function(element, classname) {
102 // Filter out classes with given name
103 element.className = element.className.replace(/([^ ]+)[ ]*/g,
104 function(match, testClassname, spaces, offset, string) {
106 // If same class, remove
107 if (testClassname == classname)
121 GuacamoleUI.hideStatus = function() {
122 removeClass(document.body, "guac-error");
123 GuacamoleUI.containers.state.style.visibility = "hidden";
124 GuacamoleUI.display.style.opacity = "1";
127 GuacamoleUI.showStatus = function(text) {
128 removeClass(document.body, "guac-error");
129 GuacamoleUI.containers.state.style.visibility = "visible";
130 GuacamoleUI.state.textContent = text;
131 GuacamoleUI.display.style.opacity = "1";
134 GuacamoleUI.showError = function(error) {
135 addClass(document.body, "guac-error");
136 GuacamoleUI.state.textContent = error;
137 GuacamoleUI.display.style.opacity = "0.1";
140 GuacamoleUI.hideTouchMenu = function() {
141 GuacamoleUI.touchMenu.style.visibility = "hidden";
144 GuacamoleUI.showTouchMenu = function() {
146 GuacamoleUI.touchMenu.style.left =
147 ((GuacamoleUI.viewport.offsetWidth - GuacamoleUI.touchMenu.offsetWidth) / 2
148 + window.pageXOffset)
151 GuacamoleUI.touchMenu.style.top =
152 ((GuacamoleUI.viewport.offsetHeight - GuacamoleUI.touchMenu.offsetHeight) / 2
153 + window.pageYOffset)
156 GuacamoleUI.touchMenu.style.visibility = "visible";
160 GuacamoleUI.shadeMenu = function() {
164 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHADE_STEPS) + 1;
168 window.clearInterval(show_interval);
169 shade_interval = window.setInterval(function() {
173 GuacamoleUI.menu.style.transform =
174 GuacamoleUI.menu.style.WebkitTransform =
175 GuacamoleUI.menu.style.MozTransform =
176 GuacamoleUI.menu.style.OTransform =
177 GuacamoleUI.menu.style.msTransform =
179 "translateY(" + offset + "px)";
181 if (offset <= -GuacamoleUI.menu.offsetHeight) {
182 window.clearInterval(shade_interval);
183 GuacamoleUI.menu.style.visiblity = "hidden";
186 }, GuacamoleUI.MENU_SHADE_INTERVAL);
191 GuacamoleUI.showMenu = function() {
195 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHOW_STEPS) + 1;
196 var offset = -GuacamoleUI.menu.offsetHeight;
198 GuacamoleUI.menu.style.visiblity = "";
200 window.clearInterval(shade_interval);
201 show_interval = window.setInterval(function() {
207 window.clearInterval(show_interval);
210 GuacamoleUI.menu.style.transform =
211 GuacamoleUI.menu.style.WebkitTransform =
212 GuacamoleUI.menu.style.MozTransform =
213 GuacamoleUI.menu.style.OTransform =
214 GuacamoleUI.menu.style.msTransform =
216 "translateY(" + offset + "px)";
218 }, GuacamoleUI.MENU_SHOW_INTERVAL);
223 // Show/Hide clipboard
224 GuacamoleUI.buttons.showClipboard.onclick = function() {
226 var displayed = GuacamoleUI.containers.clipboard.style.display;
227 if (displayed != "block") {
228 GuacamoleUI.containers.clipboard.style.display = "block";
229 GuacamoleUI.buttons.showClipboard.innerHTML = "Hide Clipboard";
232 GuacamoleUI.containers.clipboard.style.display = "none";
233 GuacamoleUI.buttons.showClipboard.innerHTML = "Show Clipboard";
234 GuacamoleUI.clipboard.onchange();
239 GuacamoleUI.buttons.touchShowClipboard.onclick = function() {
241 alert("Not yet implemented... Sorry.");
244 // Show/Hide keyboard
245 var keyboardResizeInterval = null;
246 GuacamoleUI.buttons.showKeyboard.onclick = function() {
248 // If Guac OSK shown, hide it.
249 var displayed = GuacamoleUI.containers.keyboard.style.display;
250 if (displayed == "block") {
251 GuacamoleUI.containers.keyboard.style.display = "none";
252 GuacamoleUI.buttons.showKeyboard.textContent = "Show Keyboard";
254 window.onresize = null;
255 window.clearInterval(keyboardResizeInterval);
258 // Otherwise, show it
261 // Ensure event target is NOT focused if we are using the Guac OSK.
262 GuacamoleUI.eventTarget.blur();
264 GuacamoleUI.containers.keyboard.style.display = "block";
265 GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
267 // Automatically update size
268 window.onresize = updateKeyboardSize;
269 keyboardResizeInterval = window.setInterval(updateKeyboardSize,
270 GuacamoleUI.KEYBOARD_AUTO_RESIZE_INTERVAL);
272 updateKeyboardSize();
278 // Touch-specific keyboard show
279 GuacamoleUI.buttons.touchShowKeyboard.onclick =
282 // Center event target in case browser automatically centers
283 // input fields on focus.
284 GuacamoleUI.eventTarget.style.left =
285 (window.pageXOffset + GuacamoleUI.viewport.offsetWidth / 2) + "px";
287 GuacamoleUI.eventTarget.style.top =
288 (window.pageYOffset + GuacamoleUI.viewport.offsetHeight / 2) + "px";
290 GuacamoleUI.eventTarget.focus();
291 GuacamoleUI.hideTouchMenu();
296 GuacamoleUI.buttons.logout.onclick =
297 GuacamoleUI.buttons.touchLogout.onclick =
299 window.location.href = "logout";
302 // Timeouts for detecting if users wants menu to open or close
303 var detectMenuOpenTimeout = null;
304 var detectMenuCloseTimeout = null;
306 // Clear detection timeouts
307 GuacamoleUI.resetMenuDetect = function() {
309 if (detectMenuOpenTimeout != null) {
310 window.clearTimeout(detectMenuOpenTimeout);
311 detectMenuOpenTimeout = null;
314 if (detectMenuCloseTimeout != null) {
315 window.clearTimeout(detectMenuCloseTimeout);
316 detectMenuCloseTimeout = null;
321 // Initiate detection of menu open action. If not canceled through some
322 // user event, menu will open.
323 GuacamoleUI.startMenuOpenDetect = function() {
325 if (!detectMenuOpenTimeout) {
327 // Clear detection state
328 GuacamoleUI.resetMenuDetect();
330 // Wait and then show menu
331 detectMenuOpenTimeout = window.setTimeout(function() {
333 // If menu opened via mouse, do not show native OSK
334 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
336 GuacamoleUI.showMenu();
337 detectMenuOpenTimeout = null;
338 }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
344 // Initiate detection of menu close action. If not canceled through some
345 // user mouse event, menu will close.
346 GuacamoleUI.startMenuCloseDetect = function() {
348 if (!detectMenuCloseTimeout) {
350 // Clear detection state
351 GuacamoleUI.resetMenuDetect();
353 // Wait and then shade menu
354 detectMenuCloseTimeout = window.setTimeout(function() {
355 GuacamoleUI.shadeMenu();
356 detectMenuCloseTimeout = null;
357 }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
363 // Show menu if mouseover any part of menu
364 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
366 // Stop detecting menu state change intents if mouse is over menu
367 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
369 // When mouse hovers over top of screen, start detection of intent to open menu
370 GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
372 var long_press_start_x = 0;
373 var long_press_start_y = 0;
374 var menuShowLongPressTimeout = null;
376 GuacamoleUI.startLongPressDetect = function() {
378 if (!menuShowLongPressTimeout) {
380 menuShowLongPressTimeout = window.setTimeout(function() {
382 menuShowLongPressTimeout = null;
384 // Assume native OSK if menu shown via long-press
385 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
386 GuacamoleUI.showTouchMenu();
388 }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
393 GuacamoleUI.stopLongPressDetect = function() {
394 window.clearTimeout(menuShowLongPressTimeout);
395 menuShowLongPressTimeout = null;
398 // Detect long-press at bottom of screen
399 GuacamoleUI.display.addEventListener('touchstart', function(e) {
401 // Close menu if shown
402 GuacamoleUI.shadeMenu();
403 GuacamoleUI.hideTouchMenu();
405 // Record touch location
406 if (e.touches.length == 1) {
407 var touch = e.touches[0];
408 long_press_start_x = touch.screenX;
409 long_press_start_y = touch.screenY;
413 GuacamoleUI.startLongPressDetect();
417 // Stop detection if touch moves significantly
418 GuacamoleUI.display.addEventListener('touchmove', function(e) {
420 // If touch distance from start exceeds threshold, cancel long press
421 var touch = e.touches[0];
422 if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
423 || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
424 GuacamoleUI.stopLongPressDetect();
428 // Stop detection if press stops
429 GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
431 // Close menu on mouse movement
432 GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
433 GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
436 GuacamoleUI.buttons.reconnect.onclick = function() {
437 window.location.reload();
440 // On-screen keyboard
441 GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty-mobile.xml");
442 GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
444 // Function for automatically updating keyboard size
445 var lastKeyboardWidth;
446 function updateKeyboardSize() {
447 var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
448 if (lastKeyboardWidth != currentSize) {
449 GuacamoleUI.keyboard.resize(currentSize);
450 lastKeyboardWidth = currentSize;
454 // Turn off autocorrect and autocapitalization on eventTarget
455 GuacamoleUI.eventTarget.setAttribute("autocorrect", "off");
456 GuacamoleUI.eventTarget.setAttribute("autocapitalize", "off");
458 // Automatically reposition event target on scroll
459 window.addEventListener("scroll", function() {
460 GuacamoleUI.eventTarget.style.left = window.pageXOffset + "px";
461 GuacamoleUI.eventTarget.style.top = window.pageYOffset + "px";
466 // Tie UI events / behavior to a specific Guacamole client
467 GuacamoleUI.attach = function(guac) {
469 var title_prefix = null;
470 var connection_name = "Guacamole";
472 var guac_display = guac.getDisplay();
474 // Set document title appropriately, based on prefix and connection name
475 function updateTitle() {
477 // Use title prefix if present
480 document.title = title_prefix;
482 // Include connection name, if present
484 document.title += " " + connection_name;
488 // Otherwise, just set to connection name
489 else if (connection_name)
490 document.title = connection_name;
494 // When mouse enters display, start detection of intent to close menu
495 guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
497 guac_display.onclick = function(e) {
503 var mouse = new Guacamole.Mouse(guac_display);
504 mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
505 function(mouseState) {
507 // Determine mouse position within view
508 var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
509 var mouse_view_y = mouseState.y + guac_display.offsetTop - window.pageYOffset;
511 // Determine viewport dimensioins
512 var view_width = GuacamoleUI.viewport.offsetWidth;
513 var view_height = GuacamoleUI.viewport.offsetHeight;
515 // Determine scroll amounts based on mouse position relative to document
518 if (mouse_view_x > view_width)
519 scroll_amount_x = mouse_view_x - view_width;
520 else if (mouse_view_x < 0)
521 scroll_amount_x = mouse_view_x;
526 if (mouse_view_y > view_height)
527 scroll_amount_y = mouse_view_y - view_height;
528 else if (mouse_view_y < 0)
529 scroll_amount_y = mouse_view_y;
533 // Scroll (if necessary) to keep mouse on screen.
534 window.scrollBy(scroll_amount_x, scroll_amount_y);
537 guac.sendMouseState(mouseState);
542 var keyboard = new Guacamole.Keyboard(document);
544 // Monitor whether the event target is focused
545 var eventTargetFocused = false;
547 // Save length for calculation of changed value
548 var currentLength = GuacamoleUI.eventTarget.value.length;
550 GuacamoleUI.eventTarget.onfocus = function() {
551 eventTargetFocused = true;
552 GuacamoleUI.eventTarget.value = "";
556 GuacamoleUI.eventTarget.onblur = function() {
557 eventTargetFocused = false;
560 // If text is input directly into event target without typing (as with
561 // voice input, for example), type automatically.
562 GuacamoleUI.eventTarget.oninput = function(e) {
564 // Calculate current length and change in length
565 var oldLength = currentLength;
566 currentLength = GuacamoleUI.eventTarget.value.length;
568 // If deleted or replaced text, ignore
569 if (currentLength <= oldLength)
573 var text = GuacamoleUI.eventTarget.value.substring(oldLength);
575 // Send each character
576 for (var i=0; i<text.length; i++) {
579 var charCode = text.charCodeAt(i);
582 var keysym = 0x003F; // Default to a question mark
583 if (charCode >= 0x0000 && charCode <= 0x00FF)
585 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
586 keysym = 0x01000000 | charCode;
588 // Send keysym only if not already pressed
589 if (!keyboard.pressed[keysym]) {
591 // Press and release key
592 guac.sendKeyEvent(1, keysym);
593 guac.sendKeyEvent(0, keysym);
601 function isTypableCharacter(keysym) {
602 return (keysym & 0xFFFF00) != 0xFF00;
605 function disableKeyboard() {
606 keyboard.onkeydown = null;
607 keyboard.onkeyup = null;
610 function enableKeyboard() {
612 keyboard.onkeydown = function (keysym) {
613 guac.sendKeyEvent(1, keysym);
614 return eventTargetFocused && isTypableCharacter(keysym);
617 keyboard.onkeyup = function (keysym) {
618 guac.sendKeyEvent(0, keysym);
619 return eventTargetFocused && isTypableCharacter(keysym);
624 // Enable keyboard by default
627 // Handle client state change
628 guac.onstatechange = function(clientState) {
630 switch (clientState) {
634 GuacamoleUI.showStatus("Idle.");
635 title_prefix = "[Idle]";
640 GuacamoleUI.shadeMenu();
641 GuacamoleUI.showStatus("Connecting...");
642 title_prefix = "[Connecting...]";
645 // Connected + waiting
647 GuacamoleUI.showStatus("Connected, waiting for first update...");
648 title_prefix = "[Waiting...]";
653 GuacamoleUI.hideStatus();
659 GuacamoleUI.showStatus("Disconnecting...");
660 title_prefix = "[Disconnecting...]";
665 GuacamoleUI.showStatus("Disconnected.");
666 title_prefix = "[Disconnected]";
669 // Unknown status code
671 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
678 // Name instruction handler
679 guac.onname = function(name) {
680 connection_name = name;
685 guac.onerror = function(error) {
687 // Disconnect, if connected
690 // Display error message
691 GuacamoleUI.showError(error);
695 // Disconnect on close
696 window.onunload = function() {
700 // Handle clipboard events
701 GuacamoleUI.clipboard.onchange = function() {
703 var text = GuacamoleUI.clipboard.value;
704 guac.setClipboard(text);
708 // Ignore keypresses when clipboard is focused
709 GuacamoleUI.clipboard.onfocus = function() {
713 // Capture keypresses when clipboard is not focused
714 GuacamoleUI.clipboard.onblur = function() {
718 // Server copy handler
719 guac.onclipboard = function(data) {
720 GuacamoleUI.clipboard.value = data;
723 GuacamoleUI.keyboard.onkeydown = function(keysym) {
724 guac.sendKeyEvent(1, keysym);
727 GuacamoleUI.keyboard.onkeyup = function(keysym) {
728 guac.sendKeyEvent(0, keysym);
731 // Send Ctrl-Alt-Delete
732 GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
734 var KEYSYM_CTRL = 0xFFE3;
735 var KEYSYM_ALT = 0xFFE9;
736 var KEYSYM_DELETE = 0xFFFF;
738 guac.sendKeyEvent(1, KEYSYM_CTRL);
739 guac.sendKeyEvent(1, KEYSYM_ALT);
740 guac.sendKeyEvent(1, KEYSYM_DELETE);
741 guac.sendKeyEvent(0, KEYSYM_DELETE);
742 guac.sendKeyEvent(0, KEYSYM_ALT);
743 guac.sendKeyEvent(0, KEYSYM_CTRL);