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