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