]> git.proxmox.com Git - mirror_novnc.git/blob - app/ui.js
Merge pull request #725 from CendioOssman/rightbar
[mirror_novnc.git] / app / ui.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2016 Samuel Mannehed for Cendio AB
5 * Copyright (C) 2016 Pierre Ossman for Cendio AB
6 * Licensed under MPL 2.0 (see LICENSE.txt)
7 *
8 * See README.md for usage and integration instructions.
9 */
10
11 /* jslint white: false, browser: true */
12 /* global window, document.getElementById, Util, WebUtil, RFB, Display */
13
14 /* [module]
15 * import Util from "../core/util";
16 * import KeyTable from "../core/input/keysym";
17 * import keysyms from "./keysymdef";
18 * import RFB from "../core/rfb";
19 * import Display from "../core/display";
20 * import WebUtil from "./webutil";
21 */
22
23 var UI;
24
25 (function () {
26 "use strict";
27
28 // Fallback for all uncought errors
29 window.addEventListener('error', function(event) {
30 try {
31 var msg = "";
32
33 msg += "<div>";
34 msg += event.message;
35 msg += "</div>";
36
37 msg += " <div class=\"noVNC_location\">";
38 msg += event.filename;
39 msg += ":" + event.lineno + ":" + event.colno;
40 msg += "</div>";
41
42 if ((event.error !== undefined) &&
43 (event.error.stack !== undefined)) {
44 msg += "<div class=\"noVNC_stack\">";
45 msg += event.error.stack;
46 msg += "</div>";
47 }
48
49 document.getElementById('noVNC_fallback_error')
50 .classList.add("noVNC_open");
51 document.getElementById('noVNC_fallback_errormsg').innerHTML = msg;
52 } catch (exc) {
53 document.write("noVNC encountered an error.");
54 }
55 // Don't return true since this would prevent the error
56 // from being printed to the browser console.
57 return false;
58 });
59
60 // Set up translations
61 var LINGUAS = ["de", "el", "nl", "sv"];
62 Util.Localisation.setup(LINGUAS);
63 if (Util.Localisation.language !== "en") {
64 WebUtil.load_scripts(
65 {'app': ["locale/" + Util.Localisation.language + ".js"]});
66 }
67
68 /* [begin skip-as-module] */
69 // Load supporting scripts
70 WebUtil.load_scripts(
71 {'core': ["base64.js", "websock.js", "des.js", "input/keysymdef.js",
72 "input/xtscancodes.js", "input/util.js", "input/devices.js",
73 "display.js", "inflator.js", "rfb.js", "input/keysym.js"]});
74
75 window.onscriptsload = function () { UI.load(); };
76 /* [end skip-as-module] */
77
78 var _ = Util.Localisation.get;
79
80 UI = {
81
82 connected: false,
83 desktopName: "",
84
85 resizeTimeout: null,
86 statusTimeout: null,
87 hideKeyboardTimeout: null,
88 idleControlbarTimeout: null,
89 closeControlbarTimeout: null,
90
91 controlbarGrabbed: false,
92 controlbarDrag: false,
93 controlbarMouseDownClientY: 0,
94 controlbarMouseDownOffsetY: 0,
95
96 isSafari: false,
97 rememberedClipSetting: null,
98 lastKeyboardinput: null,
99 defaultKeyboardinputLen: 100,
100
101 // Setup rfb object, load settings from browser storage, then call
102 // UI.init to setup the UI/menus
103 load: function(callback) {
104 WebUtil.initSettings(UI.start, callback);
105 },
106
107 // Render default UI and initialize settings menu
108 start: function(callback) {
109
110 // Setup global variables first
111 UI.isSafari = (navigator.userAgent.indexOf('Safari') !== -1 &&
112 navigator.userAgent.indexOf('Chrome') === -1);
113
114 UI.initSettings();
115
116 // Translate the DOM
117 Util.Localisation.translateDOM();
118
119 // Adapt the interface for touch screen devices
120 if (Util.isTouchDevice) {
121 document.documentElement.classList.add("noVNC_touch");
122 // Remove the address bar
123 setTimeout(function() { window.scrollTo(0, 1); }, 100);
124 UI.forceSetting('clip', true);
125 } else {
126 UI.initSetting('clip', false);
127 }
128
129 // Restore control bar position
130 if (WebUtil.readSetting('controlbar_pos') === 'right') {
131 UI.toggleControlbarSide();
132 }
133
134 // Setup and initialize event handlers
135 UI.setupWindowEvents();
136 UI.setupFullscreen();
137 UI.addControlbarHandlers();
138 UI.addTouchSpecificHandlers();
139 UI.addExtraKeysHandlers();
140 UI.addXvpHandlers();
141 UI.addConnectionControlHandlers();
142 UI.addClipboardHandlers();
143 UI.addSettingsHandlers();
144
145 // Show the connect panel on first load unless autoconnecting
146 if (!autoconnect) {
147 UI.openConnectPanel();
148 }
149
150 UI.updateViewClip();
151
152 UI.updateVisualState();
153
154 document.getElementById('noVNC_setting_host').focus();
155
156 var autoconnect = WebUtil.getConfigVar('autoconnect', false);
157 if (autoconnect === 'true' || autoconnect == '1') {
158 autoconnect = true;
159 UI.connect();
160 } else {
161 autoconnect = false;
162 }
163
164 if (typeof callback === "function") {
165 callback(UI.rfb);
166 }
167 },
168
169 initSettings: function() {
170 // Stylesheet selection dropdown
171 var sheet = WebUtil.selectStylesheet();
172 var sheets = WebUtil.getStylesheets();
173 var i;
174 for (i = 0; i < sheets.length; i += 1) {
175 UI.addOption(document.getElementById('noVNC_setting_stylesheet'),sheets[i].title, sheets[i].title);
176 }
177
178 // Logging selection dropdown
179 var llevels = ['error', 'warn', 'info', 'debug'];
180 for (i = 0; i < llevels.length; i += 1) {
181 UI.addOption(document.getElementById('noVNC_setting_logging'),llevels[i], llevels[i]);
182 }
183
184 // Settings with immediate effects
185 UI.initSetting('logging', 'warn');
186 WebUtil.init_logging(UI.getSetting('logging'));
187
188 UI.initSetting('stylesheet', 'default');
189 WebUtil.selectStylesheet(null);
190 // call twice to get around webkit bug
191 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
192
193 // if port == 80 (or 443) then it won't be present and should be
194 // set manually
195 var port = window.location.port;
196 if (!port) {
197 if (window.location.protocol.substring(0,5) == 'https') {
198 port = 443;
199 }
200 else if (window.location.protocol.substring(0,4) == 'http') {
201 port = 80;
202 }
203 }
204
205 /* Populate the controls if defaults are provided in the URL */
206 UI.initSetting('host', window.location.hostname);
207 UI.initSetting('port', port);
208 UI.initSetting('password', '');
209 UI.initSetting('encrypt', (window.location.protocol === "https:"));
210 UI.initSetting('true_color', true);
211 UI.initSetting('cursor', !Util.isTouchDevice);
212 UI.initSetting('resize', 'off');
213 UI.initSetting('shared', true);
214 UI.initSetting('view_only', false);
215 UI.initSetting('path', 'websockify');
216 UI.initSetting('repeaterID', '');
217 UI.initSetting('token', '');
218 },
219
220 setupWindowEvents: function() {
221 window.addEventListener('resize', UI.applyResizeMode);
222 window.addEventListener('resize', UI.updateViewClip);
223 window.addEventListener('resize', UI.updateViewDrag);
224
225 document.getElementById("noVNC_status")
226 .addEventListener('click', UI.hideStatus);
227 },
228
229 setupFullscreen: function() {
230 // Only show the button if fullscreen is properly supported
231 // * Safari doesn't support alphanumerical input while in fullscreen
232 if (!UI.isSafari &&
233 (document.documentElement.requestFullscreen ||
234 document.documentElement.mozRequestFullScreen ||
235 document.documentElement.webkitRequestFullscreen ||
236 document.body.msRequestFullscreen)) {
237 document.getElementById('noVNC_fullscreen_button')
238 .classList.remove("noVNC_hidden");
239 UI.addFullscreenHandlers();
240 }
241 },
242
243 addControlbarHandlers: function() {
244 document.getElementById("noVNC_control_bar")
245 .addEventListener('mousemove', UI.activateControlbar);
246 document.getElementById("noVNC_control_bar")
247 .addEventListener('mouseup', UI.activateControlbar);
248 document.getElementById("noVNC_control_bar")
249 .addEventListener('mousedown', UI.activateControlbar);
250 document.getElementById("noVNC_control_bar")
251 .addEventListener('keypress', UI.activateControlbar);
252
253 document.getElementById("noVNC_control_bar")
254 .addEventListener('mousedown', UI.keepControlbar);
255 document.getElementById("noVNC_control_bar")
256 .addEventListener('keypress', UI.keepControlbar);
257
258 document.getElementById("noVNC_view_drag_button")
259 .addEventListener('click', UI.toggleViewDrag);
260
261 document.getElementById("noVNC_control_bar_handle")
262 .addEventListener('mousedown', UI.controlbarHandleMouseDown);
263 document.getElementById("noVNC_control_bar_handle")
264 .addEventListener('mouseup', UI.controlbarHandleMouseUp);
265 document.getElementById("noVNC_control_bar_handle")
266 .addEventListener('mousemove', UI.dragControlbarHandle);
267 // resize events aren't available for elements
268 window.addEventListener('resize', UI.updateControlbarHandle);
269 },
270
271 addTouchSpecificHandlers: function() {
272 document.getElementById("noVNC_mouse_button0")
273 .addEventListener('click', function () { UI.setMouseButton(1); });
274 document.getElementById("noVNC_mouse_button1")
275 .addEventListener('click', function () { UI.setMouseButton(2); });
276 document.getElementById("noVNC_mouse_button2")
277 .addEventListener('click', function () { UI.setMouseButton(4); });
278 document.getElementById("noVNC_mouse_button4")
279 .addEventListener('click', function () { UI.setMouseButton(0); });
280 document.getElementById("noVNC_keyboard_button")
281 .addEventListener('click', UI.toggleVirtualKeyboard);
282
283 document.getElementById("noVNC_keyboardinput")
284 .addEventListener('input', UI.keyInput);
285 document.getElementById("noVNC_keyboardinput")
286 .addEventListener('focus', UI.onfocusVirtualKeyboard);
287 document.getElementById("noVNC_keyboardinput")
288 .addEventListener('blur', UI.onblurVirtualKeyboard);
289 document.getElementById("noVNC_keyboardinput")
290 .addEventListener('submit', function () { return false; });
291
292 document.documentElement
293 .addEventListener('mousedown', UI.keepVirtualKeyboard, true);
294
295 document.getElementById("noVNC_control_bar")
296 .addEventListener('touchstart', UI.activateControlbar);
297 document.getElementById("noVNC_control_bar")
298 .addEventListener('touchmove', UI.activateControlbar);
299 document.getElementById("noVNC_control_bar")
300 .addEventListener('touchend', UI.activateControlbar);
301 document.getElementById("noVNC_control_bar")
302 .addEventListener('input', UI.activateControlbar);
303
304 document.getElementById("noVNC_control_bar")
305 .addEventListener('touchstart', UI.keepControlbar);
306 document.getElementById("noVNC_control_bar")
307 .addEventListener('input', UI.keepControlbar);
308
309 document.getElementById("noVNC_control_bar_handle")
310 .addEventListener('touchstart', UI.controlbarHandleMouseDown);
311 document.getElementById("noVNC_control_bar_handle")
312 .addEventListener('touchend', UI.controlbarHandleMouseUp);
313 document.getElementById("noVNC_control_bar_handle")
314 .addEventListener('touchmove', UI.dragControlbarHandle);
315
316 window.addEventListener('load', UI.keyboardinputReset);
317 },
318
319 addExtraKeysHandlers: function() {
320 document.getElementById("noVNC_toggle_extra_keys_button")
321 .addEventListener('click', UI.toggleExtraKeys);
322 document.getElementById("noVNC_toggle_ctrl_button")
323 .addEventListener('click', UI.toggleCtrl);
324 document.getElementById("noVNC_toggle_alt_button")
325 .addEventListener('click', UI.toggleAlt);
326 document.getElementById("noVNC_send_tab_button")
327 .addEventListener('click', UI.sendTab);
328 document.getElementById("noVNC_send_esc_button")
329 .addEventListener('click', UI.sendEsc);
330 document.getElementById("noVNC_send_ctrl_alt_del_button")
331 .addEventListener('click', UI.sendCtrlAltDel);
332 },
333
334 addXvpHandlers: function() {
335 document.getElementById("noVNC_xvp_shutdown_button")
336 .addEventListener('click', function() { UI.rfb.xvpShutdown(); });
337 document.getElementById("noVNC_xvp_reboot_button")
338 .addEventListener('click', function() { UI.rfb.xvpReboot(); });
339 document.getElementById("noVNC_xvp_reset_button")
340 .addEventListener('click', function() { UI.rfb.xvpReset(); });
341 document.getElementById("noVNC_xvp_button")
342 .addEventListener('click', UI.toggleXvpPanel);
343 },
344
345 addConnectionControlHandlers: function() {
346 document.getElementById("noVNC_connect_controls_button")
347 .addEventListener('click', UI.toggleConnectPanel);
348 document.getElementById("noVNC_disconnect_button")
349 .addEventListener('click', UI.disconnect);
350 document.getElementById("noVNC_connect_button")
351 .addEventListener('click', UI.connect);
352
353 document.getElementById("noVNC_password_button")
354 .addEventListener('click', UI.setPassword);
355 },
356
357 addClipboardHandlers: function() {
358 document.getElementById("noVNC_clipboard_button")
359 .addEventListener('click', UI.toggleClipboardPanel);
360 document.getElementById("noVNC_clipboard_text")
361 .addEventListener('focus', UI.displayBlur);
362 document.getElementById("noVNC_clipboard_text")
363 .addEventListener('blur', UI.displayFocus);
364 document.getElementById("noVNC_clipboard_text")
365 .addEventListener('change', UI.clipboardSend);
366 document.getElementById("noVNC_clipboard_clear_button")
367 .addEventListener('click', UI.clipboardClear);
368 },
369
370 addSettingsHandlers: function() {
371 document.getElementById("noVNC_settings_button")
372 .addEventListener('click', UI.toggleSettingsPanel);
373 document.getElementById("noVNC_settings_apply")
374 .addEventListener('click', UI.settingsApply);
375
376 document.getElementById("noVNC_setting_resize")
377 .addEventListener('change', UI.enableDisableViewClip);
378 },
379
380 addFullscreenHandlers: function() {
381 document.getElementById("noVNC_fullscreen_button")
382 .addEventListener('click', UI.toggleFullscreen);
383
384 window.addEventListener('fullscreenchange', UI.updateFullscreenButton);
385 window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton);
386 window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton);
387 window.addEventListener('msfullscreenchange', UI.updateFullscreenButton);
388 },
389
390 initRFB: function() {
391 try {
392 UI.rfb = new RFB({'target': document.getElementById('noVNC_canvas'),
393 'onNotification': UI.notification,
394 'onUpdateState': UI.updateState,
395 'onDisconnected': UI.disconnectFinished,
396 'onPasswordRequired': UI.passwordRequired,
397 'onXvpInit': UI.updateXvpButton,
398 'onClipboard': UI.clipboardReceive,
399 'onBell': UI.bell,
400 'onFBUComplete': UI.initialResize,
401 'onFBResize': UI.updateViewDrag,
402 'onDesktopName': UI.updateDesktopName});
403 return true;
404 } catch (exc) {
405 var msg = "Unable to create RFB client -- " + exc;
406 Util.Error(msg);
407 UI.showStatus(msg, 'error');
408 return false;
409 }
410 },
411
412 /* ------^-------
413 * /INIT
414 * ==============
415 * VISUAL
416 * ------v------*/
417
418 updateState: function(rfb, state, oldstate) {
419 var msg;
420
421 document.documentElement.classList.remove("noVNC_connecting");
422 document.documentElement.classList.remove("noVNC_connected");
423 document.documentElement.classList.remove("noVNC_disconnecting");
424
425 switch (state) {
426 case 'connecting':
427 document.getElementById("noVNC_transition_text").innerHTML = _("Connecting...");
428 document.documentElement.classList.add("noVNC_connecting");
429 break;
430 case 'connected':
431 UI.connected = true;
432 document.documentElement.classList.add("noVNC_connected");
433 if (rfb && rfb.get_encrypt()) {
434 msg = _("Connected (encrypted) to ") + UI.desktopName;
435 } else {
436 msg = _("Connected (unencrypted) to ") + UI.desktopName;
437 }
438 UI.showStatus(msg);
439 break;
440 case 'disconnecting':
441 UI.connected = false;
442 document.getElementById("noVNC_transition_text").innerHTML = _("Disconnecting...");
443 document.documentElement.classList.add("noVNC_disconnecting");
444 break;
445 case 'disconnected':
446 UI.showStatus(_("Disconnected"));
447 break;
448 default:
449 msg = "Invalid UI state";
450 Util.Error(msg);
451 UI.showStatus(msg, 'error');
452 break;
453 }
454
455 UI.updateVisualState();
456 },
457
458 // Disable/enable controls depending on connection state
459 updateVisualState: function() {
460 //Util.Debug(">> updateVisualState");
461 document.getElementById('noVNC_setting_encrypt').disabled = UI.connected;
462 document.getElementById('noVNC_setting_true_color').disabled = UI.connected;
463 if (Util.browserSupportsCursorURIs()) {
464 document.getElementById('noVNC_setting_cursor').disabled = UI.connected;
465 } else {
466 UI.updateSetting('cursor', !Util.isTouchDevice);
467 document.getElementById('noVNC_setting_cursor').disabled = true;
468 }
469
470 UI.enableDisableViewClip();
471 document.getElementById('noVNC_setting_resize').disabled = UI.connected;
472 document.getElementById('noVNC_setting_shared').disabled = UI.connected;
473 document.getElementById('noVNC_setting_view_only').disabled = UI.connected;
474 document.getElementById('noVNC_setting_path').disabled = UI.connected;
475 document.getElementById('noVNC_setting_repeaterID').disabled = UI.connected;
476
477 if (UI.connected) {
478 UI.updateViewClip();
479 UI.setMouseButton(1);
480
481 // Hide the controlbar after 2 seconds
482 UI.closeControlbarTimeout = setTimeout(UI.closeControlbar, 2000);
483 } else {
484 UI.updateXvpButton(0);
485 UI.keepControlbar();
486 }
487
488 // Hide input related buttons in view only mode
489 if (UI.rfb && UI.rfb.get_view_only()) {
490 document.getElementById('noVNC_keyboard_button')
491 .classList.add('noVNC_hidden');
492 document.getElementById('noVNC_toggle_extra_keys_button')
493 .classList.add('noVNC_hidden');
494 } else {
495 document.getElementById('noVNC_keyboard_button')
496 .classList.remove('noVNC_hidden');
497 document.getElementById('noVNC_toggle_extra_keys_button')
498 .classList.remove('noVNC_hidden');
499 }
500
501 // State change disables viewport dragging.
502 // It is enabled (toggled) by direct click on the button
503 UI.setViewDrag(false);
504
505 // State change also closes the password dialog
506 document.getElementById('noVNC_password_dlg')
507 .classList.remove('noVNC_open');
508
509 //Util.Debug("<< updateVisualState");
510 },
511
512 showStatus: function(text, status_type, time) {
513 var statusElem = document.getElementById('noVNC_status');
514
515 clearTimeout(UI.statusTimeout);
516
517 if (typeof status_type === 'undefined') {
518 status_type = 'normal';
519 }
520
521 statusElem.classList.remove("noVNC_status_normal",
522 "noVNC_status_warn",
523 "noVNC_status_error");
524
525 switch (status_type) {
526 case 'warning':
527 case 'warn':
528 statusElem.classList.add("noVNC_status_warn");
529 break;
530 case 'error':
531 statusElem.classList.add("noVNC_status_error");
532 break;
533 case 'normal':
534 case 'info':
535 default:
536 statusElem.classList.add("noVNC_status_normal");
537 break;
538 }
539
540 statusElem.innerHTML = text;
541 statusElem.classList.add("noVNC_open");
542
543 // If no time was specified, show the status for 1.5 seconds
544 if (typeof time === 'undefined') {
545 time = 1500;
546 }
547
548 // Error messages do not timeout
549 if (status_type !== 'error') {
550 UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
551 }
552 },
553
554 hideStatus: function() {
555 clearTimeout(UI.statusTimeout);
556 document.getElementById('noVNC_status').classList.remove("noVNC_open");
557 },
558
559 notification: function (rfb, msg, level, options) {
560 UI.showStatus(msg, level);
561 },
562
563 activateControlbar: function(event) {
564 clearTimeout(UI.idleControlbarTimeout);
565 // We manipulate the anchor instead of the actual control
566 // bar in order to avoid creating new a stacking group
567 document.getElementById('noVNC_control_bar_anchor')
568 .classList.remove("noVNC_idle");
569 UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
570 },
571
572 idleControlbar: function() {
573 document.getElementById('noVNC_control_bar_anchor')
574 .classList.add("noVNC_idle");
575 },
576
577 keepControlbar: function() {
578 clearTimeout(UI.closeControlbarTimeout);
579 },
580
581 openControlbar: function() {
582 document.getElementById('noVNC_control_bar')
583 .classList.add("noVNC_open");
584 },
585
586 closeControlbar: function() {
587 UI.closeAllPanels();
588 document.getElementById('noVNC_control_bar')
589 .classList.remove("noVNC_open");
590 },
591
592 toggleControlbar: function() {
593 if (document.getElementById('noVNC_control_bar')
594 .classList.contains("noVNC_open")) {
595 UI.closeControlbar();
596 } else {
597 UI.openControlbar();
598 }
599 },
600
601 toggleControlbarSide: function () {
602 // Temporarily disable animation to avoid weird movement
603 var bar = document.getElementById('noVNC_control_bar');
604 bar.style.transitionDuration = '0s';
605 bar.addEventListener('transitionend', function () { this.style.transitionDuration = ""; });
606
607 var anchor = document.getElementById('noVNC_control_bar_anchor');
608 if (anchor.classList.contains("noVNC_right")) {
609 WebUtil.writeSetting('controlbar_pos', 'left');
610 anchor.classList.remove("noVNC_right");
611 } else {
612 WebUtil.writeSetting('controlbar_pos', 'right');
613 anchor.classList.add("noVNC_right");
614 }
615
616 // Consider this a movement of the handle
617 UI.controlbarDrag = true;
618 },
619
620 dragControlbarHandle: function (e) {
621 if (!UI.controlbarGrabbed) return;
622
623 var ptr = Util.getPointerEvent(e);
624
625 var anchor = document.getElementById('noVNC_control_bar_anchor');
626 if (ptr.clientX < (window.innerWidth * 0.1)) {
627 if (anchor.classList.contains("noVNC_right")) {
628 UI.toggleControlbarSide();
629 }
630 } else if (ptr.clientX > (window.innerWidth * 0.9)) {
631 if (!anchor.classList.contains("noVNC_right")) {
632 UI.toggleControlbarSide();
633 }
634 }
635
636 if (!UI.controlbarDrag) {
637 // The goal is to trigger on a certain physical width, the
638 // devicePixelRatio brings us a bit closer but is not optimal.
639 var dragThreshold = 10 * (window.devicePixelRatio || 1);
640 var dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
641
642 if (dragDistance < dragThreshold) return;
643
644 UI.controlbarDrag = true;
645 }
646
647 var eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
648
649 UI.moveControlbarHandle(eventY);
650
651 e.preventDefault();
652 e.stopPropagation();
653 UI.keepControlbar();
654 UI.activateControlbar();
655 },
656
657 // Move the handle but don't allow any position outside the bounds
658 moveControlbarHandle: function (viewportRelativeY) {
659 var handle = document.getElementById("noVNC_control_bar_handle");
660 var handleHeight = handle.getBoundingClientRect().height;
661 var controlbarBounds = document.getElementById("noVNC_control_bar")
662 .getBoundingClientRect();
663 var margin = 10;
664
665 // These heights need to be non-zero for the below logic to work
666 if (handleHeight === 0 || controlbarBounds.height === 0) {
667 return;
668 }
669
670 var newY = viewportRelativeY;
671
672 // Check if the coordinates are outside the control bar
673 if (newY < controlbarBounds.top + margin) {
674 // Force coordinates to be below the top of the control bar
675 newY = controlbarBounds.top + margin;
676
677 } else if (newY > controlbarBounds.top +
678 controlbarBounds.height - handleHeight - margin) {
679 // Force coordinates to be above the bottom of the control bar
680 newY = controlbarBounds.top +
681 controlbarBounds.height - handleHeight - margin;
682 }
683
684 // Corner case: control bar too small for stable position
685 if (controlbarBounds.height < (handleHeight + margin * 2)) {
686 newY = controlbarBounds.top +
687 (controlbarBounds.height - handleHeight) / 2;
688 }
689
690 // The transform needs coordinates that are relative to the parent
691 var parentRelativeY = newY - controlbarBounds.top;
692 handle.style.transform = "translateY(" + parentRelativeY + "px)";
693 },
694
695 updateControlbarHandle: function () {
696 // Since the control bar is fixed on the viewport and not the page,
697 // the move function expects coordinates relative the the viewport.
698 var handle = document.getElementById("noVNC_control_bar_handle");
699 var handleBounds = handle.getBoundingClientRect();
700 UI.moveControlbarHandle(handleBounds.top);
701 },
702
703 controlbarHandleMouseUp: function(e) {
704 if ((e.type == "mouseup") && (e.button != 0)) return;
705
706 // mouseup and mousedown on the same place toggles the controlbar
707 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
708 UI.toggleControlbar();
709 e.preventDefault();
710 e.stopPropagation();
711 UI.keepControlbar();
712 UI.activateControlbar();
713 }
714 UI.controlbarGrabbed = false;
715 },
716
717 controlbarHandleMouseDown: function(e) {
718 if ((e.type == "mousedown") && (e.button != 0)) return;
719
720 var ptr = Util.getPointerEvent(e);
721
722 var handle = document.getElementById("noVNC_control_bar_handle");
723 var bounds = handle.getBoundingClientRect();
724
725 WebUtil.setCapture(handle);
726 UI.controlbarGrabbed = true;
727 UI.controlbarDrag = false;
728
729 UI.controlbarMouseDownClientY = ptr.clientY;
730 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
731 e.preventDefault();
732 e.stopPropagation();
733 UI.keepControlbar();
734 UI.activateControlbar();
735 },
736
737 /* ------^-------
738 * /VISUAL
739 * ==============
740 * SETTINGS
741 * ------v------*/
742
743 // Initial page load read/initialization of settings
744 initSetting: function(name, defVal) {
745 // Check Query string followed by cookie
746 var val = WebUtil.getConfigVar(name);
747 if (val === null) {
748 val = WebUtil.readSetting(name, defVal);
749 }
750 UI.updateSetting(name, val);
751 return val;
752 },
753
754 // Update cookie and form control setting. If value is not set, then
755 // updates from control to current cookie setting.
756 updateSetting: function(name, value) {
757
758 // Save the cookie for this session
759 if (typeof value !== 'undefined') {
760 WebUtil.writeSetting(name, value);
761 }
762
763 // Update the settings control
764 value = UI.getSetting(name);
765
766 var ctrl = document.getElementById('noVNC_setting_' + name);
767 if (ctrl.type === 'checkbox') {
768 ctrl.checked = value;
769
770 } else if (typeof ctrl.options !== 'undefined') {
771 for (var i = 0; i < ctrl.options.length; i += 1) {
772 if (ctrl.options[i].value === value) {
773 ctrl.selectedIndex = i;
774 break;
775 }
776 }
777 } else {
778 /*Weird IE9 error leads to 'null' appearring
779 in textboxes instead of ''.*/
780 if (value === null) {
781 value = "";
782 }
783 ctrl.value = value;
784 }
785 },
786
787 // Save control setting to cookie
788 saveSetting: function(name) {
789 var val, ctrl = document.getElementById('noVNC_setting_' + name);
790 if (ctrl.type === 'checkbox') {
791 val = ctrl.checked;
792 } else if (typeof ctrl.options !== 'undefined') {
793 val = ctrl.options[ctrl.selectedIndex].value;
794 } else {
795 val = ctrl.value;
796 }
797 WebUtil.writeSetting(name, val);
798 //Util.Debug("Setting saved '" + name + "=" + val + "'");
799 return val;
800 },
801
802 // Force a setting to be a certain value
803 forceSetting: function(name, val) {
804 UI.updateSetting(name, val);
805 return val;
806 },
807
808 // Read form control compatible setting from cookie
809 getSetting: function(name) {
810 var ctrl = document.getElementById('noVNC_setting_' + name);
811 var val = WebUtil.readSetting(name);
812 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
813 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
814 val = false;
815 } else {
816 val = true;
817 }
818 }
819 return val;
820 },
821
822 // Save/apply settings when 'Apply' button is pressed
823 settingsApply: function() {
824 //Util.Debug(">> settingsApply");
825 UI.saveSetting('encrypt');
826 UI.saveSetting('true_color');
827 if (Util.browserSupportsCursorURIs()) {
828 UI.saveSetting('cursor');
829 }
830
831 UI.saveSetting('resize');
832
833 if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
834 UI.forceSetting('clip', false);
835 }
836
837 UI.saveSetting('clip');
838 UI.saveSetting('shared');
839 UI.saveSetting('view_only');
840 UI.saveSetting('path');
841 UI.saveSetting('repeaterID');
842 UI.saveSetting('stylesheet');
843 UI.saveSetting('logging');
844
845 // Settings with immediate (non-connected related) effect
846 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
847 WebUtil.init_logging(UI.getSetting('logging'));
848 UI.updateViewClip();
849 UI.updateViewDrag();
850 //Util.Debug("<< settingsApply");
851 },
852
853 /* ------^-------
854 * /SETTINGS
855 * ==============
856 * PANELS
857 * ------v------*/
858
859 closeAllPanels: function() {
860 UI.closeSettingsPanel();
861 UI.closeXvpPanel();
862 UI.closeClipboardPanel();
863 UI.closeConnectPanel();
864 UI.closeExtraKeys();
865 },
866
867 /* ------^-------
868 * /PANELS
869 * ==============
870 * SETTINGS (panel)
871 * ------v------*/
872
873 openSettingsPanel: function() {
874 UI.closeAllPanels();
875 UI.openControlbar();
876
877 UI.updateSetting('encrypt');
878 UI.updateSetting('true_color');
879 if (Util.browserSupportsCursorURIs()) {
880 UI.updateSetting('cursor');
881 } else {
882 UI.updateSetting('cursor', !Util.isTouchDevice);
883 document.getElementById('noVNC_setting_cursor').disabled = true;
884 }
885 UI.updateSetting('clip');
886 UI.updateSetting('resize');
887 UI.updateSetting('shared');
888 UI.updateSetting('view_only');
889 UI.updateSetting('path');
890 UI.updateSetting('repeaterID');
891 UI.updateSetting('stylesheet');
892 UI.updateSetting('logging');
893
894 document.getElementById('noVNC_settings')
895 .classList.add("noVNC_open");
896 document.getElementById('noVNC_settings_button')
897 .classList.add("noVNC_selected");
898 },
899
900 closeSettingsPanel: function() {
901 document.getElementById('noVNC_settings')
902 .classList.remove("noVNC_open");
903 document.getElementById('noVNC_settings_button')
904 .classList.remove("noVNC_selected");
905 },
906
907 // Toggle the settings menu:
908 // On open, settings are refreshed from saved cookies.
909 // On close, settings are applied
910 toggleSettingsPanel: function() {
911 if (document.getElementById('noVNC_settings')
912 .classList.contains("noVNC_open")) {
913 UI.settingsApply();
914 UI.closeSettingsPanel();
915 } else {
916 UI.openSettingsPanel();
917 }
918 },
919
920 /* ------^-------
921 * /SETTINGS
922 * ==============
923 * XVP
924 * ------v------*/
925
926 openXvpPanel: function() {
927 UI.closeAllPanels();
928 UI.openControlbar();
929
930 document.getElementById('noVNC_xvp')
931 .classList.add("noVNC_open");
932 document.getElementById('noVNC_xvp_button')
933 .classList.add("noVNC_selected");
934 },
935
936 closeXvpPanel: function() {
937 document.getElementById('noVNC_xvp')
938 .classList.remove("noVNC_open");
939 document.getElementById('noVNC_xvp_button')
940 .classList.remove("noVNC_selected");
941 },
942
943 toggleXvpPanel: function() {
944 if (document.getElementById('noVNC_xvp')
945 .classList.contains("noVNC_open")) {
946 UI.closeXvpPanel();
947 } else {
948 UI.openXvpPanel();
949 }
950 },
951
952 // Disable/enable XVP button
953 updateXvpButton: function(ver) {
954 if (ver >= 1 && !UI.rfb.get_view_only()) {
955 document.getElementById('noVNC_xvp_button')
956 .classList.remove("noVNC_hidden");
957 } else {
958 document.getElementById('noVNC_xvp_button')
959 .classList.add("noVNC_hidden");
960 // Close XVP panel if open
961 UI.closeXvpPanel();
962 }
963 },
964
965 /* ------^-------
966 * /XVP
967 * ==============
968 * CLIPBOARD
969 * ------v------*/
970
971 openClipboardPanel: function() {
972 UI.closeAllPanels();
973 UI.openControlbar();
974
975 document.getElementById('noVNC_clipboard')
976 .classList.add("noVNC_open");
977 document.getElementById('noVNC_clipboard_button')
978 .classList.add("noVNC_selected");
979 },
980
981 closeClipboardPanel: function() {
982 document.getElementById('noVNC_clipboard')
983 .classList.remove("noVNC_open");
984 document.getElementById('noVNC_clipboard_button')
985 .classList.remove("noVNC_selected");
986 },
987
988 toggleClipboardPanel: function() {
989 if (document.getElementById('noVNC_clipboard')
990 .classList.contains("noVNC_open")) {
991 UI.closeClipboardPanel();
992 } else {
993 UI.openClipboardPanel();
994 }
995 },
996
997 clipboardReceive: function(rfb, text) {
998 Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "...");
999 document.getElementById('noVNC_clipboard_text').value = text;
1000 Util.Debug("<< UI.clipboardReceive");
1001 },
1002
1003 clipboardClear: function() {
1004 document.getElementById('noVNC_clipboard_text').value = "";
1005 UI.rfb.clipboardPasteFrom("");
1006 },
1007
1008 clipboardSend: function() {
1009 var text = document.getElementById('noVNC_clipboard_text').value;
1010 Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "...");
1011 UI.rfb.clipboardPasteFrom(text);
1012 Util.Debug("<< UI.clipboardSend");
1013 },
1014
1015 /* ------^-------
1016 * /CLIPBOARD
1017 * ==============
1018 * CONNECTION
1019 * ------v------*/
1020
1021 openConnectPanel: function() {
1022 UI.closeAllPanels();
1023 UI.openControlbar();
1024
1025 document.getElementById('noVNC_connect_controls')
1026 .classList.add("noVNC_open");
1027 document.getElementById('noVNC_connect_controls_button')
1028 .classList.add("noVNC_selected");
1029
1030 document.getElementById('noVNC_setting_host').focus();
1031 },
1032
1033 closeConnectPanel: function() {
1034 document.getElementById('noVNC_connect_controls')
1035 .classList.remove("noVNC_open");
1036 document.getElementById('noVNC_connect_controls_button')
1037 .classList.remove("noVNC_selected");
1038
1039 UI.saveSetting('host');
1040 UI.saveSetting('port');
1041 UI.saveSetting('token');
1042 //UI.saveSetting('password');
1043 },
1044
1045 toggleConnectPanel: function() {
1046 if (document.getElementById('noVNC_connect_controls')
1047 .classList.contains("noVNC_open")) {
1048 UI.closeConnectPanel();
1049 } else {
1050 UI.openConnectPanel();
1051 }
1052 },
1053
1054 connect: function() {
1055 var host = document.getElementById('noVNC_setting_host').value;
1056 var port = document.getElementById('noVNC_setting_port').value;
1057 var password = document.getElementById('noVNC_setting_password').value;
1058 var token = document.getElementById('noVNC_setting_token').value;
1059 var path = document.getElementById('noVNC_setting_path').value;
1060
1061 //if token is in path then ignore the new token variable
1062 if (token) {
1063 path = WebUtil.injectParamIfMissing(path, "token", token);
1064 }
1065
1066 if ((!host) || (!port)) {
1067 var msg = _("Must set host and port");
1068 Util.Error(msg);
1069 UI.showStatus(msg, 'error');
1070 return;
1071 }
1072
1073 if (!UI.initRFB()) return;
1074
1075 UI.closeAllPanels();
1076
1077 UI.rfb.set_encrypt(UI.getSetting('encrypt'));
1078 UI.rfb.set_true_color(UI.getSetting('true_color'));
1079 UI.rfb.set_local_cursor(UI.getSetting('cursor'));
1080 UI.rfb.set_shared(UI.getSetting('shared'));
1081 UI.rfb.set_view_only(UI.getSetting('view_only'));
1082 UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
1083
1084 UI.rfb.connect(host, port, password, path);
1085 },
1086
1087 disconnect: function() {
1088 UI.closeAllPanels();
1089 UI.rfb.disconnect();
1090
1091 // Restore the callback used for initial resize
1092 UI.rfb.set_onFBUComplete(UI.initialResize);
1093
1094 // Don't display the connection settings until we're actually disconnected
1095 },
1096
1097 disconnectFinished: function (rfb, reason) {
1098 if (typeof reason !== 'undefined') {
1099 UI.showStatus(reason, 'error');
1100 }
1101 UI.openConnectPanel();
1102 },
1103
1104 /* ------^-------
1105 * /CONNECTION
1106 * ==============
1107 * PASSWORD
1108 * ------v------*/
1109
1110 passwordRequired: function(rfb, msg) {
1111
1112 document.getElementById('noVNC_password_dlg')
1113 .classList.add('noVNC_open');
1114
1115 setTimeout(function () {
1116 document.getElementById('noVNC_password_input').focus();
1117 }, 100);
1118
1119 if (typeof msg === 'undefined') {
1120 msg = _("Password is required");
1121 }
1122 Util.Warn(msg);
1123 UI.showStatus(msg, "warning");
1124 },
1125
1126 setPassword: function() {
1127 UI.rfb.sendPassword(document.getElementById('noVNC_password_input').value);
1128 document.getElementById('noVNC_password_dlg')
1129 .classList.remove('noVNC_open');
1130 return false;
1131 },
1132
1133 /* ------^-------
1134 * /PASSWORD
1135 * ==============
1136 * FULLSCREEN
1137 * ------v------*/
1138
1139 toggleFullscreen: function() {
1140 if (document.fullscreenElement || // alternative standard method
1141 document.mozFullScreenElement || // currently working methods
1142 document.webkitFullscreenElement ||
1143 document.msFullscreenElement) {
1144 if (document.exitFullscreen) {
1145 document.exitFullscreen();
1146 } else if (document.mozCancelFullScreen) {
1147 document.mozCancelFullScreen();
1148 } else if (document.webkitExitFullscreen) {
1149 document.webkitExitFullscreen();
1150 } else if (document.msExitFullscreen) {
1151 document.msExitFullscreen();
1152 }
1153 } else {
1154 if (document.documentElement.requestFullscreen) {
1155 document.documentElement.requestFullscreen();
1156 } else if (document.documentElement.mozRequestFullScreen) {
1157 document.documentElement.mozRequestFullScreen();
1158 } else if (document.documentElement.webkitRequestFullscreen) {
1159 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1160 } else if (document.body.msRequestFullscreen) {
1161 document.body.msRequestFullscreen();
1162 }
1163 }
1164 UI.enableDisableViewClip();
1165 UI.updateFullscreenButton();
1166 },
1167
1168 updateFullscreenButton: function() {
1169 if (document.fullscreenElement || // alternative standard method
1170 document.mozFullScreenElement || // currently working methods
1171 document.webkitFullscreenElement ||
1172 document.msFullscreenElement ) {
1173 document.getElementById('noVNC_fullscreen_button')
1174 .classList.add("noVNC_selected");
1175 } else {
1176 document.getElementById('noVNC_fullscreen_button')
1177 .classList.remove("noVNC_selected");
1178 }
1179 },
1180
1181 /* ------^-------
1182 * /FULLSCREEN
1183 * ==============
1184 * RESIZE
1185 * ------v------*/
1186
1187 // Apply remote resizing or local scaling
1188 applyResizeMode: function() {
1189 if (!UI.rfb) return;
1190
1191 var screen = UI.screenSize();
1192
1193 if (screen && UI.connected && UI.rfb.get_display()) {
1194
1195 var display = UI.rfb.get_display();
1196 var resizeMode = UI.getSetting('resize');
1197
1198 if (resizeMode === 'remote') {
1199
1200 // Request changing the resolution of the remote display to
1201 // the size of the local browser viewport.
1202
1203 // In order to not send multiple requests before the browser-resize
1204 // is finished we wait 0.5 seconds before sending the request.
1205 clearTimeout(UI.resizeTimeout);
1206 UI.resizeTimeout = setTimeout(function(){
1207
1208 // Limit the viewport to the size of the browser window
1209 display.set_maxWidth(screen.w);
1210 display.set_maxHeight(screen.h);
1211
1212 // Request a remote size covering the viewport
1213 if (UI.rfb.requestDesktopSize(screen.w, screen.h)) {
1214 Util.Debug('Requested new desktop size: ' +
1215 screen.w + 'x' + screen.h);
1216 }
1217 }, 500);
1218
1219 } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
1220 var downscaleOnly = resizeMode === 'downscale';
1221 var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
1222
1223 if (!UI.rfb.get_view_only()) {
1224 UI.rfb.get_mouse().set_scale(scaleRatio);
1225 Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
1226 }
1227 }
1228 }
1229 },
1230
1231 // Gets the the size of the available viewport in the browser window
1232 screenSize: function() {
1233 var screen = document.getElementById('noVNC_screen');
1234
1235 // Hide the scrollbars until the size is calculated
1236 screen.style.overflow = "hidden";
1237
1238 var pos = Util.getPosition(screen);
1239 var w = pos.width;
1240 var h = pos.height;
1241
1242 screen.style.overflow = "visible";
1243
1244 if (isNaN(w) || isNaN(h)) {
1245 return false;
1246 } else {
1247 return {w: w, h: h};
1248 }
1249 },
1250
1251 // Normally we only apply the current resize mode after a window resize
1252 // event. This means that when a new connection is opened, there is no
1253 // resize mode active.
1254 // We have to wait until the first FBU because this is where the client
1255 // will find the supported encodings of the server. Some calls later in
1256 // the chain is dependant on knowing the server-capabilities.
1257 initialResize: function(rfb, fbu) {
1258 UI.applyResizeMode();
1259 // After doing this once, we remove the callback.
1260 UI.rfb.set_onFBUComplete(function() { });
1261 },
1262
1263 /* ------^-------
1264 * /RESIZE
1265 * ==============
1266 * CLIPPING
1267 * ------v------*/
1268
1269 // Set and configure viewport clipping
1270 setViewClip: function(clip) {
1271 UI.updateSetting('clip', clip);
1272 UI.updateViewClip();
1273 },
1274
1275 // Update parameters that depend on the clip setting
1276 updateViewClip: function() {
1277 if (!UI.rfb) return;
1278
1279 var display = UI.rfb.get_display();
1280 var cur_clip = display.get_viewport();
1281 var new_clip = UI.getSetting('clip');
1282
1283 if (cur_clip !== new_clip) {
1284 display.set_viewport(new_clip);
1285 }
1286
1287 var size = UI.screenSize();
1288
1289 if (new_clip && size) {
1290 // When clipping is enabled, the screen is limited to
1291 // the size of the browser window.
1292 display.set_maxWidth(size.w);
1293 display.set_maxHeight(size.h);
1294
1295 var screen = document.getElementById('noVNC_screen');
1296 var canvas = document.getElementById('noVNC_canvas');
1297
1298 // Hide potential scrollbars that can skew the position
1299 screen.style.overflow = "hidden";
1300
1301 // The x position marks the left margin of the canvas,
1302 // remove the margin from both sides to keep it centered.
1303 var new_w = size.w - (2 * Util.getPosition(canvas).x);
1304
1305 screen.style.overflow = "visible";
1306
1307 display.viewportChangeSize(new_w, size.h);
1308 } else {
1309 // Disable max dimensions
1310 display.set_maxWidth(0);
1311 display.set_maxHeight(0);
1312 display.viewportChangeSize();
1313 }
1314 },
1315
1316 // Handle special cases where clipping is forced on/off or locked
1317 enableDisableViewClip: function() {
1318 var resizeSetting = document.getElementById('noVNC_setting_resize');
1319
1320 if (UI.isSafari) {
1321 // Safari auto-hides the scrollbars which makes them
1322 // impossible to use in most cases
1323 UI.setViewClip(true);
1324 document.getElementById('noVNC_setting_clip').disabled = true;
1325 } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') {
1326 // Disable clipping if we are scaling
1327 UI.setViewClip(false);
1328 document.getElementById('noVNC_setting_clip').disabled = true;
1329 } else if (document.msFullscreenElement) {
1330 // The browser is IE and we are in fullscreen mode.
1331 // - We need to force clipping while in fullscreen since
1332 // scrollbars doesn't work.
1333 var msg = _("Forcing clipping mode since " +
1334 "scrollbars aren't supported " +
1335 "by IE in fullscreen");
1336 Util.Debug(msg);
1337 UI.showStatus(msg);
1338 UI.rememberedClipSetting = UI.getSetting('clip');
1339 UI.setViewClip(true);
1340 document.getElementById('noVNC_setting_clip').disabled = true;
1341 } else if (document.body.msRequestFullscreen &&
1342 UI.rememberedClipSetting !== null) {
1343 // Restore view clip to what it was before fullscreen on IE
1344 UI.setViewClip(UI.rememberedClipSetting);
1345 document.getElementById('noVNC_setting_clip').disabled =
1346 UI.connected || Util.isTouchDevice;
1347 } else {
1348 document.getElementById('noVNC_setting_clip').disabled =
1349 UI.connected || Util.isTouchDevice;
1350 if (Util.isTouchDevice) {
1351 UI.setViewClip(true);
1352 }
1353 }
1354 },
1355
1356 /* ------^-------
1357 * /CLIPPING
1358 * ==============
1359 * VIEWDRAG
1360 * ------v------*/
1361
1362 toggleViewDrag: function() {
1363 if (!UI.rfb) return;
1364
1365 var drag = UI.rfb.get_viewportDrag();
1366 UI.setViewDrag(!drag);
1367 },
1368
1369 // Set the view drag mode which moves the viewport on mouse drags
1370 setViewDrag: function(drag) {
1371 if (!UI.rfb) return;
1372
1373 UI.rfb.set_viewportDrag(drag);
1374
1375 UI.updateViewDrag();
1376 },
1377
1378 updateViewDrag: function() {
1379 var clipping = false;
1380
1381 if (!UI.connected) return;
1382
1383 // Check if viewport drag is possible. It is only possible
1384 // if the remote display is clipping the client display.
1385 if (UI.rfb.get_display().get_viewport() &&
1386 UI.rfb.get_display().clippingDisplay()) {
1387 clipping = true;
1388 }
1389
1390 var viewDragButton = document.getElementById('noVNC_view_drag_button');
1391
1392 if (!clipping &&
1393 UI.rfb.get_viewportDrag()) {
1394 // The size of the remote display is the same or smaller
1395 // than the client display. Make sure viewport drag isn't
1396 // active when it can't be used.
1397 UI.rfb.set_viewportDrag(false);
1398 }
1399
1400 if (UI.rfb.get_viewportDrag()) {
1401 viewDragButton.classList.add("noVNC_selected");
1402 } else {
1403 viewDragButton.classList.remove("noVNC_selected");
1404 }
1405
1406 // Different behaviour for touch vs non-touch
1407 // The button is disabled instead of hidden on touch devices
1408 if (Util.isTouchDevice) {
1409 viewDragButton.classList.remove("noVNC_hidden");
1410
1411 if (clipping) {
1412 viewDragButton.disabled = false;
1413 } else {
1414 viewDragButton.disabled = true;
1415 }
1416 } else {
1417 viewDragButton.disabled = false;
1418
1419 if (clipping) {
1420 viewDragButton.classList.remove("noVNC_hidden");
1421 } else {
1422 viewDragButton.classList.add("noVNC_hidden");
1423 }
1424 }
1425 },
1426
1427 /* ------^-------
1428 * /VIEWDRAG
1429 * ==============
1430 * KEYBOARD
1431 * ------v------*/
1432
1433 showVirtualKeyboard: function() {
1434 if (!Util.isTouchDevice) return;
1435
1436 var input = document.getElementById('noVNC_keyboardinput');
1437
1438 if (document.activeElement == input) return;
1439
1440 input.focus();
1441
1442 try {
1443 var l = input.value.length;
1444 // Move the caret to the end
1445 input.setSelectionRange(l, l);
1446 } catch (err) {} // setSelectionRange is undefined in Google Chrome
1447 },
1448
1449 hideVirtualKeyboard: function() {
1450 if (!Util.isTouchDevice) return;
1451
1452 var input = document.getElementById('noVNC_keyboardinput');
1453
1454 if (document.activeElement != input) return;
1455
1456 input.blur();
1457 },
1458
1459 toggleVirtualKeyboard: function () {
1460 if (document.getElementById('noVNC_keyboard_button')
1461 .classList.contains("noVNC_selected")) {
1462 UI.hideVirtualKeyboard();
1463 } else {
1464 UI.showVirtualKeyboard();
1465 }
1466 },
1467
1468 onfocusVirtualKeyboard: function(event) {
1469 document.getElementById('noVNC_keyboard_button')
1470 .classList.add("noVNC_selected");
1471 },
1472
1473 onblurVirtualKeyboard: function(event) {
1474 document.getElementById('noVNC_keyboard_button')
1475 .classList.remove("noVNC_selected");
1476 },
1477
1478 keepVirtualKeyboard: function(event) {
1479 var input = document.getElementById('noVNC_keyboardinput');
1480
1481 // Only prevent focus change if the virtual keyboard is active
1482 if (document.activeElement != input) {
1483 return;
1484 }
1485
1486 // Allow clicking on links
1487 if (event.target.tagName === "A") {
1488 return;
1489 }
1490
1491 // And form elements, except standard noVNC buttons
1492 if ((event.target.form !== undefined) &&
1493 !event.target.classList.contains("noVNC_button")) {
1494 return;
1495 }
1496
1497 event.preventDefault();
1498 },
1499
1500 keyboardinputReset: function() {
1501 var kbi = document.getElementById('noVNC_keyboardinput');
1502 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1503 UI.lastKeyboardinput = kbi.value;
1504 },
1505
1506 // When normal keyboard events are left uncought, use the input events from
1507 // the keyboardinput element instead and generate the corresponding key events.
1508 // This code is required since some browsers on Android are inconsistent in
1509 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1510 keyInput: function(event) {
1511
1512 if (!UI.rfb) return;
1513
1514 var newValue = event.target.value;
1515
1516 if (!UI.lastKeyboardinput) {
1517 UI.keyboardinputReset();
1518 }
1519 var oldValue = UI.lastKeyboardinput;
1520
1521 var newLen;
1522 try {
1523 // Try to check caret position since whitespace at the end
1524 // will not be considered by value.length in some browsers
1525 newLen = Math.max(event.target.selectionStart, newValue.length);
1526 } catch (err) {
1527 // selectionStart is undefined in Google Chrome
1528 newLen = newValue.length;
1529 }
1530 var oldLen = oldValue.length;
1531
1532 var backspaces;
1533 var inputs = newLen - oldLen;
1534 if (inputs < 0) {
1535 backspaces = -inputs;
1536 } else {
1537 backspaces = 0;
1538 }
1539
1540 // Compare the old string with the new to account for
1541 // text-corrections or other input that modify existing text
1542 var i;
1543 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1544 if (newValue.charAt(i) != oldValue.charAt(i)) {
1545 inputs = newLen - i;
1546 backspaces = oldLen - i;
1547 break;
1548 }
1549 }
1550
1551 // Send the key events
1552 for (i = 0; i < backspaces; i++) {
1553 UI.rfb.sendKey(KeyTable.XK_BackSpace);
1554 }
1555 for (i = newLen - inputs; i < newLen; i++) {
1556 UI.rfb.sendKey(keysyms.fromUnicode(newValue.charCodeAt(i)).keysym);
1557 }
1558
1559 // Control the text content length in the keyboardinput element
1560 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1561 UI.keyboardinputReset();
1562 } else if (newLen < 1) {
1563 // There always have to be some text in the keyboardinput
1564 // element with which backspace can interact.
1565 UI.keyboardinputReset();
1566 // This sometimes causes the keyboard to disappear for a second
1567 // but it is required for the android keyboard to recognize that
1568 // text has been added to the field
1569 event.target.blur();
1570 // This has to be ran outside of the input handler in order to work
1571 setTimeout(event.target.focus.bind(event.target), 0);
1572 } else {
1573 UI.lastKeyboardinput = newValue;
1574 }
1575 },
1576
1577 /* ------^-------
1578 * /KEYBOARD
1579 * ==============
1580 * EXTRA KEYS
1581 * ------v------*/
1582
1583 openExtraKeys: function() {
1584 UI.closeAllPanels();
1585 UI.openControlbar();
1586
1587 document.getElementById('noVNC_modifiers')
1588 .classList.add("noVNC_open");
1589 document.getElementById('noVNC_toggle_extra_keys_button')
1590 .classList.add("noVNC_selected");
1591 },
1592
1593 closeExtraKeys: function() {
1594 document.getElementById('noVNC_modifiers')
1595 .classList.remove("noVNC_open");
1596 document.getElementById('noVNC_toggle_extra_keys_button')
1597 .classList.remove("noVNC_selected");
1598 },
1599
1600 toggleExtraKeys: function() {
1601 if(document.getElementById('noVNC_modifiers')
1602 .classList.contains("noVNC_open")) {
1603 UI.closeExtraKeys();
1604 } else {
1605 UI.openExtraKeys();
1606 }
1607 },
1608
1609 sendEsc: function() {
1610 UI.rfb.sendKey(KeyTable.XK_Escape);
1611 },
1612
1613 sendTab: function() {
1614 UI.rfb.sendKey(KeyTable.XK_Tab);
1615 },
1616
1617 toggleCtrl: function() {
1618 var btn = document.getElementById('noVNC_toggle_ctrl_button');
1619 if (btn.classList.contains("noVNC_selected")) {
1620 UI.rfb.sendKey(KeyTable.XK_Control_L, false);
1621 btn.classList.remove("noVNC_selected");
1622 } else {
1623 UI.rfb.sendKey(KeyTable.XK_Control_L, true);
1624 btn.classList.add("noVNC_selected");
1625 }
1626 },
1627
1628 toggleAlt: function() {
1629 var btn = document.getElementById('noVNC_toggle_alt_button');
1630 if (btn.classList.contains("noVNC_selected")) {
1631 UI.rfb.sendKey(KeyTable.XK_Alt_L, false);
1632 btn.classList.remove("noVNC_selected");
1633 } else {
1634 UI.rfb.sendKey(KeyTable.XK_Alt_L, true);
1635 btn.classList.add("noVNC_selected");
1636 }
1637 },
1638
1639 sendCtrlAltDel: function() {
1640 UI.rfb.sendCtrlAltDel();
1641 },
1642
1643 /* ------^-------
1644 * /EXTRA KEYS
1645 * ==============
1646 * MISC
1647 * ------v------*/
1648
1649 setMouseButton: function(num) {
1650 var view_only = UI.rfb.get_view_only();
1651 if (UI.rfb && !view_only) {
1652 UI.rfb.get_mouse().set_touchButton(num);
1653 }
1654
1655 var blist = [0, 1,2,4];
1656 for (var b = 0; b < blist.length; b++) {
1657 var button = document.getElementById('noVNC_mouse_button' +
1658 blist[b]);
1659 if (blist[b] === num && !view_only) {
1660 button.classList.remove("noVNC_hidden");
1661 } else {
1662 button.classList.add("noVNC_hidden");
1663 }
1664 }
1665 },
1666
1667 displayBlur: function() {
1668 if (UI.rfb && !UI.rfb.get_view_only()) {
1669 UI.rfb.get_keyboard().set_focused(false);
1670 UI.rfb.get_mouse().set_focused(false);
1671 }
1672 },
1673
1674 displayFocus: function() {
1675 if (UI.rfb && !UI.rfb.get_view_only()) {
1676 UI.rfb.get_keyboard().set_focused(true);
1677 UI.rfb.get_mouse().set_focused(true);
1678 }
1679 },
1680
1681 updateDesktopName: function(rfb, name) {
1682 UI.desktopName = name;
1683 // Display the desktop name in the document title
1684 document.title = name + " - noVNC";
1685 },
1686
1687 bell: function(rfb) {
1688 if (WebUtil.getConfigVar('bell', 'on') === 'on') {
1689 document.getElementById('noVNC_bell').play();
1690 }
1691 },
1692
1693 //Helper to add options to dropdown.
1694 addOption: function(selectbox, text, value) {
1695 var optn = document.createElement("OPTION");
1696 optn.text = text;
1697 optn.value = value;
1698 selectbox.options.add(optn);
1699 },
1700
1701 /* ------^-------
1702 * /MISC
1703 * ==============
1704 */
1705 };
1706
1707 /* [module] UI.load(); */
1708 })();
1709
1710 /* [module] export default UI; */