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.ontouchstart =
280 GuacamoleUI.buttons.touchShowKeyboard.onclick =
282 GuacamoleUI.eventTarget.focus();
283 GuacamoleUI.hideTouchMenu();
288 GuacamoleUI.buttons.logout.onclick =
289 GuacamoleUI.buttons.touchLogout.onclick =
291 window.location.href = "logout";
294 // Timeouts for detecting if users wants menu to open or close
295 var detectMenuOpenTimeout = null;
296 var detectMenuCloseTimeout = null;
298 // Clear detection timeouts
299 GuacamoleUI.resetMenuDetect = function() {
301 if (detectMenuOpenTimeout != null) {
302 window.clearTimeout(detectMenuOpenTimeout);
303 detectMenuOpenTimeout = null;
306 if (detectMenuCloseTimeout != null) {
307 window.clearTimeout(detectMenuCloseTimeout);
308 detectMenuCloseTimeout = null;
313 // Initiate detection of menu open action. If not canceled through some
314 // user event, menu will open.
315 GuacamoleUI.startMenuOpenDetect = function() {
317 if (!detectMenuOpenTimeout) {
319 // Clear detection state
320 GuacamoleUI.resetMenuDetect();
322 // Wait and then show menu
323 detectMenuOpenTimeout = window.setTimeout(function() {
325 // If menu opened via mouse, do not show native OSK
326 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
328 GuacamoleUI.showMenu();
329 detectMenuOpenTimeout = null;
330 }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
336 // Initiate detection of menu close action. If not canceled through some
337 // user mouse event, menu will close.
338 GuacamoleUI.startMenuCloseDetect = function() {
340 if (!detectMenuCloseTimeout) {
342 // Clear detection state
343 GuacamoleUI.resetMenuDetect();
345 // Wait and then shade menu
346 detectMenuCloseTimeout = window.setTimeout(function() {
347 GuacamoleUI.shadeMenu();
348 detectMenuCloseTimeout = null;
349 }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
355 // Show menu if mouseover any part of menu
356 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
358 // Stop detecting menu state change intents if mouse is over menu
359 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
361 // When mouse hovers over top of screen, start detection of intent to open menu
362 GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
364 var long_press_start_x = 0;
365 var long_press_start_y = 0;
366 var menuShowLongPressTimeout = null;
368 GuacamoleUI.startLongPressDetect = function() {
370 if (!menuShowLongPressTimeout) {
372 menuShowLongPressTimeout = window.setTimeout(function() {
374 menuShowLongPressTimeout = null;
376 // Assume native OSK if menu shown via long-press
377 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
378 GuacamoleUI.showTouchMenu();
380 }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
385 GuacamoleUI.stopLongPressDetect = function() {
386 window.clearTimeout(menuShowLongPressTimeout);
387 menuShowLongPressTimeout = null;
390 // Detect long-press at bottom of screen
391 GuacamoleUI.display.addEventListener('touchstart', function(e) {
393 // Close menu if shown
394 GuacamoleUI.shadeMenu();
395 GuacamoleUI.hideTouchMenu();
397 // Record touch location
398 if (e.touches.length == 1) {
399 var touch = e.touches[0];
400 long_press_start_x = touch.screenX;
401 long_press_start_y = touch.screenY;
405 GuacamoleUI.startLongPressDetect();
409 // Stop detection if touch moves significantly
410 GuacamoleUI.display.addEventListener('touchmove', function(e) {
412 // If touch distance from start exceeds threshold, cancel long press
413 var touch = e.touches[0];
414 if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
415 || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
416 GuacamoleUI.stopLongPressDetect();
420 // Stop detection if press stops
421 GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
423 // Close menu on mouse movement
424 GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
425 GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
428 GuacamoleUI.buttons.reconnect.onclick = function() {
429 window.location.reload();
432 // On-screen keyboard
433 GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty-mobile.xml");
434 GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
436 // Function for automatically updating keyboard size
437 var lastKeyboardWidth;
438 function updateKeyboardSize() {
439 var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
440 if (lastKeyboardWidth != currentSize) {
441 GuacamoleUI.keyboard.resize(currentSize);
442 lastKeyboardWidth = currentSize;
446 // Turn off autocorrect and autocapitalization on eventTarget
447 GuacamoleUI.eventTarget.setAttribute("autocorrect", "off");
448 GuacamoleUI.eventTarget.setAttribute("autocapitalize", "off");
452 // Tie UI events / behavior to a specific Guacamole client
453 GuacamoleUI.attach = function(guac) {
455 var title_prefix = null;
456 var connection_name = "Guacamole";
458 var guac_display = guac.getDisplay();
460 // Set document title appropriately, based on prefix and connection name
461 function updateTitle() {
463 // Use title prefix if present
466 document.title = title_prefix;
468 // Include connection name, if present
470 document.title += " " + connection_name;
474 // Otherwise, just set to connection name
475 else if (connection_name)
476 document.title = connection_name;
480 // When mouse enters display, start detection of intent to close menu
481 guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
483 guac_display.onclick = function(e) {
489 var mouse = new Guacamole.Mouse(guac_display);
490 mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
491 function(mouseState) {
493 // Determine mouse position within view
494 var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
495 var mouse_view_y = mouseState.y + guac_display.offsetTop - window.pageYOffset;
497 // Determine viewport dimensioins
498 var view_width = GuacamoleUI.viewport.offsetWidth;
499 var view_height = GuacamoleUI.viewport.offsetHeight;
501 // Determine scroll amounts based on mouse position relative to document
504 if (mouse_view_x > view_width)
505 scroll_amount_x = mouse_view_x - view_width;
506 else if (mouse_view_x < 0)
507 scroll_amount_x = mouse_view_x;
512 if (mouse_view_y > view_height)
513 scroll_amount_y = mouse_view_y - view_height;
514 else if (mouse_view_y < 0)
515 scroll_amount_y = mouse_view_y;
519 // Scroll (if necessary) to keep mouse on screen.
520 window.scrollBy(scroll_amount_x, scroll_amount_y);
523 guac.sendMouseState(mouseState);
528 var keyboard = new Guacamole.Keyboard(document);
530 // Monitor whether the event target is focused
531 var eventTargetFocused = false;
533 // Save length for calculation of changed value
534 var currentLength = GuacamoleUI.eventTarget.value.length;
536 GuacamoleUI.eventTarget.onfocus = function() {
537 eventTargetFocused = true;
538 GuacamoleUI.eventTarget.value = "";
542 GuacamoleUI.eventTarget.onblur = function() {
543 eventTargetFocused = false;
546 // If text is input directly into event target without typing (as with
547 // voice input, for example), type automatically.
548 GuacamoleUI.eventTarget.oninput = function(e) {
550 // Calculate current length and change in length
551 var oldLength = currentLength;
552 currentLength = GuacamoleUI.eventTarget.value.length;
554 // If deleted or replaced text, ignore
555 if (currentLength <= oldLength)
559 var text = GuacamoleUI.eventTarget.value.substring(oldLength);
561 // Send each character
562 for (var i=0; i<text.length; i++) {
565 var charCode = text.charCodeAt(i);
568 var keysym = 0x003F; // Default to a question mark
569 if (charCode >= 0x0000 && charCode <= 0x00FF)
571 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
572 keysym = 0x01000000 | charCode;
574 // Send keysym only if not already pressed
575 if (!keyboard.pressed[keysym]) {
577 // Press and release key
578 guac.sendKeyEvent(1, keysym);
579 guac.sendKeyEvent(0, keysym);
587 function isTypableCharacter(keysym) {
588 return (keysym & 0xFFFF00) != 0xFF00;
591 function disableKeyboard() {
592 keyboard.onkeydown = null;
593 keyboard.onkeyup = null;
596 function enableKeyboard() {
598 keyboard.onkeydown = function (keysym) {
599 guac.sendKeyEvent(1, keysym);
600 return eventTargetFocused && isTypableCharacter(keysym);
603 keyboard.onkeyup = function (keysym) {
604 guac.sendKeyEvent(0, keysym);
605 return eventTargetFocused && isTypableCharacter(keysym);
610 // Enable keyboard by default
613 // Handle client state change
614 guac.onstatechange = function(clientState) {
616 switch (clientState) {
620 GuacamoleUI.showStatus("Idle.");
621 title_prefix = "[Idle]";
626 GuacamoleUI.shadeMenu();
627 GuacamoleUI.showStatus("Connecting...");
628 title_prefix = "[Connecting...]";
631 // Connected + waiting
633 GuacamoleUI.showStatus("Connected, waiting for first update...");
634 title_prefix = "[Waiting...]";
639 GuacamoleUI.hideStatus();
645 GuacamoleUI.showStatus("Disconnecting...");
646 title_prefix = "[Disconnecting...]";
651 GuacamoleUI.showStatus("Disconnected.");
652 title_prefix = "[Disconnected]";
655 // Unknown status code
657 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
664 // Name instruction handler
665 guac.onname = function(name) {
666 connection_name = name;
671 guac.onerror = function(error) {
673 // Disconnect, if connected
676 // Display error message
677 GuacamoleUI.showError(error);
681 // Disconnect on close
682 window.onunload = function() {
686 // Handle clipboard events
687 GuacamoleUI.clipboard.onchange = function() {
689 var text = GuacamoleUI.clipboard.value;
690 guac.setClipboard(text);
694 // Ignore keypresses when clipboard is focused
695 GuacamoleUI.clipboard.onfocus = function() {
699 // Capture keypresses when clipboard is not focused
700 GuacamoleUI.clipboard.onblur = function() {
704 // Server copy handler
705 guac.onclipboard = function(data) {
706 GuacamoleUI.clipboard.value = data;
709 GuacamoleUI.keyboard.onkeydown = function(keysym) {
710 guac.sendKeyEvent(1, keysym);
713 GuacamoleUI.keyboard.onkeyup = function(keysym) {
714 guac.sendKeyEvent(0, keysym);
717 // Send Ctrl-Alt-Delete
718 GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
720 var KEYSYM_CTRL = 0xFFE3;
721 var KEYSYM_ALT = 0xFFE9;
722 var KEYSYM_DELETE = 0xFFFF;
724 guac.sendKeyEvent(1, KEYSYM_CTRL);
725 guac.sendKeyEvent(1, KEYSYM_ALT);
726 guac.sendKeyEvent(1, KEYSYM_DELETE);
727 guac.sendKeyEvent(0, KEYSYM_DELETE);
728 guac.sendKeyEvent(0, KEYSYM_ALT);
729 guac.sendKeyEvent(0, KEYSYM_CTRL);