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")
45 "state" : document.getElementById("statusDialog"),
46 "clipboard": document.getElementById("clipboardDiv"),
47 "keyboard" : document.getElementById("keyboardContainer")
50 "state" : document.getElementById("statusText"),
51 "clipboard" : document.getElementById("clipboard")
55 // Constant UI initialization and behavior
58 var menu_shaded = false;
60 var shade_interval = null;
61 var show_interval = null;
63 // Cache error image (might not be available when error occurs)
64 var guacErrorImage = new Image();
65 guacErrorImage.src = "images/noguacamole-logo-24.png";
67 // Function for adding a class to an element
70 // Function for removing a class from an element
73 // If Node.classList is supported, implement addClass/removeClass using that
76 addClass = function(element, classname) {
77 element.classList.add(classname);
80 removeClass = function(element, classname) {
81 element.classList.remove(classname);
86 // Otherwise, implement own
89 addClass = function(element, classname) {
91 // Simply add new class
92 element.className += " " + classname;
96 removeClass = function(element, classname) {
98 // Filter out classes with given name
99 element.className = element.className.replace(/([^ ]+)[ ]*/g,
100 function(match, testClassname, spaces, offset, string) {
102 // If same class, remove
103 if (testClassname == classname)
117 GuacamoleUI.hideStatus = function() {
118 removeClass(document.body, "guac-error");
119 GuacamoleUI.containers.state.style.visibility = "hidden";
120 GuacamoleUI.display.style.opacity = "1";
123 GuacamoleUI.showStatus = function(text) {
124 removeClass(document.body, "guac-error");
125 GuacamoleUI.containers.state.style.visibility = "visible";
126 GuacamoleUI.state.textContent = text;
127 GuacamoleUI.display.style.opacity = "1";
130 GuacamoleUI.showError = function(error) {
131 addClass(document.body, "guac-error");
132 GuacamoleUI.state.textContent = error;
133 GuacamoleUI.display.style.opacity = "0.1";
136 GuacamoleUI.shadeMenu = function() {
140 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHADE_STEPS) + 1;
144 window.clearInterval(show_interval);
145 shade_interval = window.setInterval(function() {
148 GuacamoleUI.menu.style.top = offset + "px";
150 if (offset <= -GuacamoleUI.menu.offsetHeight) {
151 window.clearInterval(shade_interval);
152 GuacamoleUI.menu.style.visiblity = "hidden";
155 }, GuacamoleUI.MENU_SHADE_INTERVAL);
160 GuacamoleUI.showMenu = function() {
164 var step = Math.floor(GuacamoleUI.menu.offsetHeight / GuacamoleUI.MENU_SHOW_STEPS) + 1;
165 var offset = -GuacamoleUI.menu.offsetHeight;
167 GuacamoleUI.menu.style.visiblity = "";
169 window.clearInterval(shade_interval);
170 show_interval = window.setInterval(function() {
176 window.clearInterval(show_interval);
179 GuacamoleUI.menu.style.top = offset + "px";
181 }, GuacamoleUI.MENU_SHOW_INTERVAL);
186 // Show/Hide clipboard
187 GuacamoleUI.buttons.showClipboard.onclick = function() {
189 var displayed = GuacamoleUI.containers.clipboard.style.display;
190 if (displayed != "block") {
191 GuacamoleUI.containers.clipboard.style.display = "block";
192 GuacamoleUI.buttons.showClipboard.innerHTML = "Hide Clipboard";
195 GuacamoleUI.containers.clipboard.style.display = "none";
196 GuacamoleUI.buttons.showClipboard.innerHTML = "Show Clipboard";
197 GuacamoleUI.clipboard.onchange();
202 // Assume no native OSK by default
203 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
205 // Show/Hide keyboard
206 var keyboardResizeInterval = null;
207 GuacamoleUI.buttons.showKeyboard.onclick = function() {
209 // If Guac OSK shown, hide it.
210 var displayed = GuacamoleUI.containers.keyboard.style.display;
211 if (displayed == "block") {
212 GuacamoleUI.containers.keyboard.style.display = "none";
213 GuacamoleUI.buttons.showKeyboard.textContent = "Show Keyboard";
215 window.onresize = null;
216 window.clearInterval(keyboardResizeInterval);
219 // If not shown ... action depends on OSK mode.
222 // If we think the platform has a native OSK, use the event target to
223 // cause it to display.
224 if (GuacamoleUI.oskMode == GuacamoleUI.OSK_MODE_NATIVE) {
226 // ...but use the Guac OSK if clicked again
227 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
229 // Try to show native OSK by focusing eventTarget.
230 GuacamoleUI.eventTarget.focus();
235 // Ensure event target is NOT focused if we are using the Guac OSK.
236 GuacamoleUI.eventTarget.blur();
238 GuacamoleUI.containers.keyboard.style.display = "block";
239 GuacamoleUI.buttons.showKeyboard.textContent = "Hide Keyboard";
241 // Automatically update size
242 window.onresize = updateKeyboardSize;
243 keyboardResizeInterval = window.setInterval(updateKeyboardSize, GuacamoleUI.KEYBOARD_AUTO_RESIZE_INTERVAL);
245 updateKeyboardSize();
252 GuacamoleUI.buttons.logout.onclick = function() {
253 window.location.href = "logout";
256 // Timeouts for detecting if users wants menu to open or close
257 var detectMenuOpenTimeout = null;
258 var detectMenuCloseTimeout = null;
260 // Clear detection timeouts
261 GuacamoleUI.resetMenuDetect = function() {
263 if (detectMenuOpenTimeout != null) {
264 window.clearTimeout(detectMenuOpenTimeout);
265 detectMenuOpenTimeout = null;
268 if (detectMenuCloseTimeout != null) {
269 window.clearTimeout(detectMenuCloseTimeout);
270 detectMenuCloseTimeout = null;
275 // Initiate detection of menu open action. If not canceled through some
276 // user event, menu will open.
277 GuacamoleUI.startMenuOpenDetect = function() {
279 if (!detectMenuOpenTimeout) {
281 // Clear detection state
282 GuacamoleUI.resetMenuDetect();
284 // Wait and then show menu
285 detectMenuOpenTimeout = window.setTimeout(function() {
287 // If menu opened via mouse, do not show native OSK
288 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_GUAC;
290 GuacamoleUI.showMenu();
291 detectMenuOpenTimeout = null;
292 }, GuacamoleUI.MENU_OPEN_DETECT_TIMEOUT);
298 // Initiate detection of menu close action. If not canceled through some
299 // user mouse event, menu will close.
300 GuacamoleUI.startMenuCloseDetect = function() {
302 if (!detectMenuCloseTimeout) {
304 // Clear detection state
305 GuacamoleUI.resetMenuDetect();
307 // Wait and then shade menu
308 detectMenuCloseTimeout = window.setTimeout(function() {
309 GuacamoleUI.shadeMenu();
310 detectMenuCloseTimeout = null;
311 }, GuacamoleUI.MENU_CLOSE_DETECT_TIMEOUT);
317 // Show menu if mouseover any part of menu
318 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.showMenu, true);
320 // Stop detecting menu state change intents if mouse is over menu
321 GuacamoleUI.menu.addEventListener('mouseover', GuacamoleUI.resetMenuDetect, true);
323 // When mouse hovers over top of screen, start detection of intent to open menu
324 GuacamoleUI.menuControl.addEventListener('mousemove', GuacamoleUI.startMenuOpenDetect, true);
326 var long_press_start_x = 0;
327 var long_press_start_y = 0;
328 var menuShowLongPressTimeout = null;
330 GuacamoleUI.startLongPressDetect = function() {
332 if (!menuShowLongPressTimeout) {
334 menuShowLongPressTimeout = window.setTimeout(function() {
336 menuShowLongPressTimeout = null;
338 // Assume native OSK if menu shown via long-press
339 GuacamoleUI.oskMode = GuacamoleUI.OSK_MODE_NATIVE;
340 GuacamoleUI.showMenu();
342 }, GuacamoleUI.LONG_PRESS_DETECT_TIMEOUT);
347 GuacamoleUI.stopLongPressDetect = function() {
348 window.clearTimeout(menuShowLongPressTimeout);
349 menuShowLongPressTimeout = null;
352 // Reset event target (add content, reposition cursor in middle.
353 GuacamoleUI.resetEventTarget = function() {
354 GuacamoleUI.eventTarget.value = "";
357 // Detect long-press at bottom of screen
358 GuacamoleUI.display.addEventListener('touchstart', function(e) {
360 // Close menu if shown
361 GuacamoleUI.shadeMenu();
363 // Record touch location
364 if (e.touches.length == 1) {
365 var touch = e.touches[0];
366 long_press_start_x = touch.screenX;
367 long_press_start_y = touch.screenY;
371 GuacamoleUI.startLongPressDetect();
375 // Stop detection if touch moves significantly
376 GuacamoleUI.display.addEventListener('touchmove', function(e) {
378 if (e.touches.length == 1) {
380 // If touch distance from start exceeds threshold, cancel long press
381 var touch = e.touches[0];
382 if (Math.abs(touch.screenX - long_press_start_x) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD
383 || Math.abs(touch.screenY - long_press_start_y) >= GuacamoleUI.LONG_PRESS_MOVEMENT_THRESHOLD)
384 GuacamoleUI.stopLongPressDetect();
390 // Stop detection if press stops
391 GuacamoleUI.display.addEventListener('touchend', GuacamoleUI.stopLongPressDetect, true);
393 // Close menu on mouse movement
394 GuacamoleUI.display.addEventListener('mousemove', GuacamoleUI.startMenuCloseDetect, true);
395 GuacamoleUI.display.addEventListener('mousedown', GuacamoleUI.startMenuCloseDetect, true);
398 GuacamoleUI.buttons.reconnect.onclick = function() {
399 window.location.reload();
402 // On-screen keyboard
403 GuacamoleUI.keyboard = new Guacamole.OnScreenKeyboard("layouts/en-us-qwerty-mobile.xml");
404 GuacamoleUI.containers.keyboard.appendChild(GuacamoleUI.keyboard.getElement());
406 // Function for automatically updating keyboard size
407 var lastKeyboardWidth;
408 function updateKeyboardSize() {
409 var currentSize = GuacamoleUI.keyboard.getElement().offsetWidth;
410 if (lastKeyboardWidth != currentSize) {
411 GuacamoleUI.keyboard.resize(currentSize);
412 lastKeyboardWidth = currentSize;
418 // Tie UI events / behavior to a specific Guacamole client
419 GuacamoleUI.attach = function(guac) {
421 var title_prefix = null;
422 var connection_name = "Guacamole";
424 var guac_display = guac.getDisplay();
426 // Set document title appropriately, based on prefix and connection name
427 function updateTitle() {
429 // Use title prefix if present
432 document.title = title_prefix;
434 // Include connection name, if present
436 document.title += " " + connection_name;
440 // Otherwise, just set to connection name
441 else if (connection_name)
442 document.title = connection_name;
446 // When mouse enters display, start detection of intent to close menu
447 guac_display.addEventListener('mouseover', GuacamoleUI.startMenuCloseDetect, true);
449 guac_display.onclick = function(e) {
455 var mouse = new Guacamole.Mouse(guac_display);
456 mouse.onmousedown = mouse.onmouseup = mouse.onmousemove =
457 function(mouseState) {
459 // Determine mouse position within view
460 var mouse_view_x = mouseState.x + guac_display.offsetLeft - window.pageXOffset;
461 var mouse_view_y = mouseState.y + guac_display.offsetTop - window.pageYOffset;
463 // Determine viewport dimensioins
464 var view_width = GuacamoleUI.viewport.offsetWidth;
465 var view_height = GuacamoleUI.viewport.offsetHeight;
467 // Determine scroll amounts based on mouse position relative to document
470 if (mouse_view_x > view_width)
471 scroll_amount_x = mouse_view_x - view_width;
472 else if (mouse_view_x < 0)
473 scroll_amount_x = mouse_view_x;
478 if (mouse_view_y > view_height)
479 scroll_amount_y = mouse_view_y - view_height;
480 else if (mouse_view_y < 0)
481 scroll_amount_y = mouse_view_y;
485 // Scroll (if necessary) to keep mouse on screen.
486 window.scrollBy(scroll_amount_x, scroll_amount_y);
489 guac.sendMouseState(mouseState);
494 var keyboard = new Guacamole.Keyboard(document);
496 function disableKeyboard() {
497 keyboard.onkeydown = null;
498 keyboard.onkeyup = null;
501 function enableKeyboard() {
505 // If we're using native OSK, ensure event target is reset
506 // on each key event.
507 if (GuacamoleUI.oskMode == GuacamoleUI.OSK_MODE_NATIVE)
508 GuacamoleUI.resetEventTarget();
510 guac.sendKeyEvent(1, keysym);
515 guac.sendKeyEvent(0, keysym);
519 // Enable keyboard by default
522 // Handle client state change
523 guac.onstatechange = function(clientState) {
525 switch (clientState) {
529 GuacamoleUI.showStatus("Idle.");
530 title_prefix = "[Idle]";
535 GuacamoleUI.shadeMenu();
536 GuacamoleUI.showStatus("Connecting...");
537 title_prefix = "[Connecting...]";
540 // Connected + waiting
542 GuacamoleUI.showStatus("Connected, waiting for first update...");
543 title_prefix = "[Waiting...]";
548 GuacamoleUI.hideStatus();
554 GuacamoleUI.showStatus("Disconnecting...");
555 title_prefix = "[Disconnecting...]";
560 GuacamoleUI.showStatus("Disconnected.");
561 title_prefix = "[Disconnected]";
564 // Unknown status code
566 GuacamoleUI.showStatus("[UNKNOWN STATUS]");
573 // Name instruction handler
574 guac.onname = function(name) {
575 connection_name = name;
580 guac.onerror = function(error) {
582 // Disconnect, if connected
585 // Display error message
586 GuacamoleUI.showError(error);
590 // Disconnect on close
591 window.onunload = function() {
595 // If text is input directly into event target without typing (as with
596 // voice input, for example), type automatically.
597 GuacamoleUI.eventTarget.oninput = function(e) {
600 var text = GuacamoleUI.eventTarget.value;
602 // Send each character
603 for (var i=0; i<text.length; i++) {
606 var charCode = text.charCodeAt(i);
609 var keysym = 0x003F; // Default to a question mark
610 if (charCode >= 0x0000 && charCode <= 0x00FF)
612 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
613 keysym = 0x01000000 | charCode;
615 // Press and release key
616 guac.sendKeyEvent(1, keysym);
617 guac.sendKeyEvent(0, keysym);
622 GuacamoleUI.resetEventTarget();
630 // Handle clipboard events
631 GuacamoleUI.clipboard.onchange = function() {
633 var text = GuacamoleUI.clipboard.value;
634 guac.setClipboard(text);
638 // Ignore keypresses when clipboard is focused
639 GuacamoleUI.clipboard.onfocus = function() {
643 // Capture keypresses when clipboard is not focused
644 GuacamoleUI.clipboard.onblur = function() {
648 // Server copy handler
649 guac.onclipboard = function(data) {
650 GuacamoleUI.clipboard.value = data;
653 GuacamoleUI.keyboard.onkeydown = function(keysym) {
654 guac.sendKeyEvent(1, keysym);
657 GuacamoleUI.keyboard.onkeyup = function(keysym) {
658 guac.sendKeyEvent(0, keysym);
661 // Send Ctrl-Alt-Delete
662 GuacamoleUI.buttons.ctrlAltDelete.onclick = function() {
664 var KEYSYM_CTRL = 0xFFE3;
665 var KEYSYM_ALT = 0xFFE9;
666 var KEYSYM_DELETE = 0xFFFF;
668 guac.sendKeyEvent(1, KEYSYM_CTRL);
669 guac.sendKeyEvent(1, KEYSYM_ALT);
670 guac.sendKeyEvent(1, KEYSYM_DELETE);
671 guac.sendKeyEvent(0, KEYSYM_DELETE);
672 guac.sendKeyEvent(0, KEYSYM_ALT);
673 guac.sendKeyEvent(0, KEYSYM_CTRL);