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