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