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