]> git.proxmox.com Git - mirror_novnc.git/blame - app/ui.js
Consistently close dialogs on connect/disconnect
[mirror_novnc.git] / app / ui.js
CommitLineData
15046f00
JM
1/*
2 * noVNC: HTML5 VNC client
412d9306 3 * Copyright (C) 2019 The noVNC Authors
1d728ace 4 * Licensed under MPL 2.0 (see LICENSE.txt)
15046f00
JM
5 *
6 * See README.md for usage and integration instructions.
7 */
43cf7bd8 8
6d6f0db0 9import * as Log from '../core/util/logging.js';
7279364c 10import _, { l10n } from './localization.js';
e52a278e 11import { isTouchDevice, isSafari, hasScrollbarGutter, dragThreshold }
ef64917a 12 from '../core/util/browser.js';
6d6f0db0 13import { setCapture, getPointerEvent } from '../core/util/events.js';
3ae0bb09
SR
14import KeyTable from "../core/input/keysym.js";
15import keysyms from "../core/input/keysymdef.js";
867daa98 16import Keyboard from "../core/input/keyboard.js";
3ae0bb09 17import RFB from "../core/rfb.js";
6d6f0db0 18import * as WebUtil from "./webutil.js";
bbbf42bb 19
c90245da
NL
20const PAGE_TITLE = "noVNC";
21
2b5f94fa 22const UI = {
6d6f0db0
SR
23
24 connected: false,
25 desktopName: "",
26
6d6f0db0
SR
27 statusTimeout: null,
28 hideKeyboardTimeout: null,
29 idleControlbarTimeout: null,
30 closeControlbarTimeout: null,
31
32 controlbarGrabbed: false,
33 controlbarDrag: false,
34 controlbarMouseDownClientY: 0,
35 controlbarMouseDownOffsetY: 0,
36
6d6f0db0
SR
37 lastKeyboardinput: null,
38 defaultKeyboardinputLen: 100,
39
40 inhibit_reconnect: true,
41 reconnect_callback: null,
42 reconnect_password: null,
43
1c9b904d
JD
44 prime() {
45 return WebUtil.initSettings().then(() => {
46 if (document.readyState === "interactive" || document.readyState === "complete") {
47 return UI.start();
48 }
529c64e1 49
1c9b904d
JD
50 return new Promise((resolve, reject) => {
51 document.addEventListener('DOMContentLoaded', () => UI.start().then(resolve).catch(reject));
52 });
53 });
6d6f0db0 54 },
529c64e1 55
6d6f0db0 56 // Render default UI and initialize settings menu
1c9b904d 57 start() {
044d54ed 58
6d6f0db0 59 UI.initSettings();
0f6af1e3 60
6d6f0db0
SR
61 // Translate the DOM
62 l10n.translateDOM();
0f6af1e3 63
546edcd4 64 WebUtil.fetchJSON('./package.json')
15c7b7a6
JD
65 .then((packageInfo) => {
66 Array.from(document.getElementsByClassName('noVNC_version')).forEach(el => el.innerText = packageInfo.version);
67 })
897b465b 68 .catch((err) => {
15c7b7a6
JD
69 Log.Error("Couldn't fetch package.json: " + err);
70 Array.from(document.getElementsByClassName('noVNC_version_wrapper'))
71 .concat(Array.from(document.getElementsByClassName('noVNC_version_separator')))
72 .forEach(el => el.style.display = 'none');
73 });
74
6d6f0db0
SR
75 // Adapt the interface for touch screen devices
76 if (isTouchDevice) {
77 document.documentElement.classList.add("noVNC_touch");
78 // Remove the address bar
651c23ec 79 setTimeout(() => window.scrollTo(0, 1), 100);
6d6f0db0 80 }
0f6af1e3 81
6d6f0db0
SR
82 // Restore control bar position
83 if (WebUtil.readSetting('controlbar_pos') === 'right') {
84 UI.toggleControlbarSide();
85 }
edffd9e2 86
6d6f0db0 87 UI.initFullscreen();
0f6af1e3 88
6d6f0db0 89 // Setup event handlers
6d6f0db0
SR
90 UI.addControlbarHandlers();
91 UI.addTouchSpecificHandlers();
92 UI.addExtraKeysHandlers();
cd523e8f 93 UI.addMachineHandlers();
6d6f0db0
SR
94 UI.addConnectionControlHandlers();
95 UI.addClipboardHandlers();
96 UI.addSettingsHandlers();
97 document.getElementById("noVNC_status")
98 .addEventListener('click', UI.hideStatus);
cf348b78 99
55988e7a
PO
100 // Bootstrap fallback input handler
101 UI.keyboardinputReset();
102
6d6f0db0 103 UI.openControlbar();
b3d91b78 104
ee5cae9f 105 UI.updateVisualState('init');
b3c932c3 106
e25f9c40 107 document.documentElement.classList.remove("noVNC_loading");
0f6af1e3 108
2b5f94fa 109 let autoconnect = WebUtil.getConfigVar('autoconnect', false);
6d6f0db0
SR
110 if (autoconnect === 'true' || autoconnect == '1') {
111 autoconnect = true;
112 UI.connect();
113 } else {
114 autoconnect = false;
067accb8
GK
115 // Show the connect panel on first load unless autoconnecting
116 UI.openConnectPanel();
6d6f0db0 117 }
0f6af1e3 118
1c9b904d 119 return Promise.resolve(UI.rfb);
6d6f0db0
SR
120 },
121
0e4808bf 122 initFullscreen() {
6d6f0db0
SR
123 // Only show the button if fullscreen is properly supported
124 // * Safari doesn't support alphanumerical input while in fullscreen
47b3eac8 125 if (!isSafari() &&
6d6f0db0
SR
126 (document.documentElement.requestFullscreen ||
127 document.documentElement.mozRequestFullScreen ||
128 document.documentElement.webkitRequestFullscreen ||
129 document.body.msRequestFullscreen)) {
130 document.getElementById('noVNC_fullscreen_button')
131 .classList.remove("noVNC_hidden");
132 UI.addFullscreenHandlers();
133 }
134 },
0f6af1e3 135
0e4808bf 136 initSettings() {
6d6f0db0 137 // Logging selection dropdown
2b5f94fa
JD
138 const llevels = ['error', 'warn', 'info', 'debug'];
139 for (let i = 0; i < llevels.length; i += 1) {
140 UI.addOption(document.getElementById('noVNC_setting_logging'), llevels[i], llevels[i]);
6d6f0db0 141 }
b3d91b78 142
6d6f0db0
SR
143 // Settings with immediate effects
144 UI.initSetting('logging', 'warn');
145 UI.updateLogging();
bbbf42bb 146
6d6f0db0
SR
147 // if port == 80 (or 443) then it won't be present and should be
148 // set manually
2b5f94fa 149 let port = window.location.port;
6d6f0db0 150 if (!port) {
6786fd87 151 if (window.location.protocol.substring(0, 5) == 'https') {
6d6f0db0 152 port = 443;
6786fd87 153 } else if (window.location.protocol.substring(0, 4) == 'http') {
6d6f0db0 154 port = 80;
bbbf42bb 155 }
6d6f0db0 156 }
bbbf42bb 157
6d6f0db0
SR
158 /* Populate the controls if defaults are provided in the URL */
159 UI.initSetting('host', window.location.hostname);
160 UI.initSetting('port', port);
161 UI.initSetting('encrypt', (window.location.protocol === "https:"));
11715f20 162 UI.initSetting('view_clip', false);
6d6f0db0 163 UI.initSetting('resize', 'off');
5243cbf6 164 UI.initSetting('quality', 6);
479d8cef 165 UI.initSetting('compression', 2);
6d6f0db0
SR
166 UI.initSetting('shared', true);
167 UI.initSetting('view_only', false);
4c38179d 168 UI.initSetting('show_dot', false);
6d6f0db0
SR
169 UI.initSetting('path', 'websockify');
170 UI.initSetting('repeaterID', '');
171 UI.initSetting('reconnect', false);
172 UI.initSetting('reconnect_delay', 5000);
173
174 UI.setupSettingLabels();
175 },
176 // Adds a link to the label elements on the corresponding input elements
0e4808bf 177 setupSettingLabels() {
2b5f94fa
JD
178 const labels = document.getElementsByTagName('LABEL');
179 for (let i = 0; i < labels.length; i++) {
180 const htmlFor = labels[i].htmlFor;
6d6f0db0 181 if (htmlFor != '') {
2b5f94fa 182 const elem = document.getElementById(htmlFor);
6d6f0db0
SR
183 if (elem) elem.label = labels[i];
184 } else {
185 // If 'for' isn't set, use the first input element child
2b5f94fa
JD
186 const children = labels[i].children;
187 for (let j = 0; j < children.length; j++) {
6d6f0db0
SR
188 if (children[j].form !== undefined) {
189 children[j].label = labels[i];
190 break;
24584cca
SM
191 }
192 }
193 }
6d6f0db0
SR
194 }
195 },
59387b34 196
6d6f0db0
SR
197/* ------^-------
198* /INIT
199* ==============
200* EVENT HANDLERS
201* ------v------*/
202
0e4808bf 203 addControlbarHandlers() {
6d6f0db0
SR
204 document.getElementById("noVNC_control_bar")
205 .addEventListener('mousemove', UI.activateControlbar);
206 document.getElementById("noVNC_control_bar")
207 .addEventListener('mouseup', UI.activateControlbar);
208 document.getElementById("noVNC_control_bar")
209 .addEventListener('mousedown', UI.activateControlbar);
210 document.getElementById("noVNC_control_bar")
06fe4a3e 211 .addEventListener('keydown', UI.activateControlbar);
6d6f0db0
SR
212
213 document.getElementById("noVNC_control_bar")
214 .addEventListener('mousedown', UI.keepControlbar);
215 document.getElementById("noVNC_control_bar")
06fe4a3e 216 .addEventListener('keydown', UI.keepControlbar);
6d6f0db0
SR
217
218 document.getElementById("noVNC_view_drag_button")
219 .addEventListener('click', UI.toggleViewDrag);
220
221 document.getElementById("noVNC_control_bar_handle")
222 .addEventListener('mousedown', UI.controlbarHandleMouseDown);
223 document.getElementById("noVNC_control_bar_handle")
224 .addEventListener('mouseup', UI.controlbarHandleMouseUp);
225 document.getElementById("noVNC_control_bar_handle")
226 .addEventListener('mousemove', UI.dragControlbarHandle);
227 // resize events aren't available for elements
228 window.addEventListener('resize', UI.updateControlbarHandle);
229
2b5f94fa
JD
230 const exps = document.getElementsByClassName("noVNC_expander");
231 for (let i = 0;i < exps.length;i++) {
6d6f0db0
SR
232 exps[i].addEventListener('click', UI.toggleExpander);
233 }
234 },
235
0e4808bf 236 addTouchSpecificHandlers() {
6d6f0db0 237 document.getElementById("noVNC_mouse_button0")
651c23ec 238 .addEventListener('click', () => UI.setMouseButton(1));
6d6f0db0 239 document.getElementById("noVNC_mouse_button1")
651c23ec 240 .addEventListener('click', () => UI.setMouseButton(2));
6d6f0db0 241 document.getElementById("noVNC_mouse_button2")
651c23ec 242 .addEventListener('click', () => UI.setMouseButton(4));
6d6f0db0 243 document.getElementById("noVNC_mouse_button4")
651c23ec 244 .addEventListener('click', () => UI.setMouseButton(0));
6d6f0db0
SR
245 document.getElementById("noVNC_keyboard_button")
246 .addEventListener('click', UI.toggleVirtualKeyboard);
247
747b4623
PO
248 UI.touchKeyboard = new Keyboard(document.getElementById('noVNC_keyboardinput'));
249 UI.touchKeyboard.onkeyevent = UI.keyEvent;
867daa98 250 UI.touchKeyboard.grab();
6d6f0db0
SR
251 document.getElementById("noVNC_keyboardinput")
252 .addEventListener('input', UI.keyInput);
253 document.getElementById("noVNC_keyboardinput")
254 .addEventListener('focus', UI.onfocusVirtualKeyboard);
255 document.getElementById("noVNC_keyboardinput")
256 .addEventListener('blur', UI.onblurVirtualKeyboard);
257 document.getElementById("noVNC_keyboardinput")
651c23ec 258 .addEventListener('submit', () => false);
6d6f0db0
SR
259
260 document.documentElement
261 .addEventListener('mousedown', UI.keepVirtualKeyboard, true);
262
263 document.getElementById("noVNC_control_bar")
264 .addEventListener('touchstart', UI.activateControlbar);
265 document.getElementById("noVNC_control_bar")
266 .addEventListener('touchmove', UI.activateControlbar);
267 document.getElementById("noVNC_control_bar")
268 .addEventListener('touchend', UI.activateControlbar);
269 document.getElementById("noVNC_control_bar")
270 .addEventListener('input', UI.activateControlbar);
271
272 document.getElementById("noVNC_control_bar")
273 .addEventListener('touchstart', UI.keepControlbar);
274 document.getElementById("noVNC_control_bar")
275 .addEventListener('input', UI.keepControlbar);
276
277 document.getElementById("noVNC_control_bar_handle")
278 .addEventListener('touchstart', UI.controlbarHandleMouseDown);
279 document.getElementById("noVNC_control_bar_handle")
280 .addEventListener('touchend', UI.controlbarHandleMouseUp);
281 document.getElementById("noVNC_control_bar_handle")
282 .addEventListener('touchmove', UI.dragControlbarHandle);
6d6f0db0
SR
283 },
284
0e4808bf 285 addExtraKeysHandlers() {
6d6f0db0
SR
286 document.getElementById("noVNC_toggle_extra_keys_button")
287 .addEventListener('click', UI.toggleExtraKeys);
288 document.getElementById("noVNC_toggle_ctrl_button")
289 .addEventListener('click', UI.toggleCtrl);
3e835a5d
SS
290 document.getElementById("noVNC_toggle_windows_button")
291 .addEventListener('click', UI.toggleWindows);
6d6f0db0
SR
292 document.getElementById("noVNC_toggle_alt_button")
293 .addEventListener('click', UI.toggleAlt);
294 document.getElementById("noVNC_send_tab_button")
295 .addEventListener('click', UI.sendTab);
296 document.getElementById("noVNC_send_esc_button")
297 .addEventListener('click', UI.sendEsc);
298 document.getElementById("noVNC_send_ctrl_alt_del_button")
299 .addEventListener('click', UI.sendCtrlAltDel);
300 },
301
0e4808bf 302 addMachineHandlers() {
cd523e8f 303 document.getElementById("noVNC_shutdown_button")
651c23ec 304 .addEventListener('click', () => UI.rfb.machineShutdown());
cd523e8f 305 document.getElementById("noVNC_reboot_button")
651c23ec 306 .addEventListener('click', () => UI.rfb.machineReboot());
cd523e8f 307 document.getElementById("noVNC_reset_button")
651c23ec 308 .addEventListener('click', () => UI.rfb.machineReset());
cd523e8f
PO
309 document.getElementById("noVNC_power_button")
310 .addEventListener('click', UI.togglePowerPanel);
6d6f0db0
SR
311 },
312
0e4808bf 313 addConnectionControlHandlers() {
6d6f0db0
SR
314 document.getElementById("noVNC_disconnect_button")
315 .addEventListener('click', UI.disconnect);
316 document.getElementById("noVNC_connect_button")
317 .addEventListener('click', UI.connect);
318 document.getElementById("noVNC_cancel_reconnect_button")
319 .addEventListener('click', UI.cancelReconnect);
320
5b453ed4
CKB
321 document.getElementById("noVNC_credentials_button")
322 .addEventListener('click', UI.setCredentials);
6d6f0db0
SR
323 },
324
0e4808bf 325 addClipboardHandlers() {
6d6f0db0
SR
326 document.getElementById("noVNC_clipboard_button")
327 .addEventListener('click', UI.toggleClipboardPanel);
6d6f0db0
SR
328 document.getElementById("noVNC_clipboard_text")
329 .addEventListener('change', UI.clipboardSend);
330 document.getElementById("noVNC_clipboard_clear_button")
331 .addEventListener('click', UI.clipboardClear);
332 },
333
334 // Add a call to save settings when the element changes,
335 // unless the optional parameter changeFunc is used instead.
0e4808bf 336 addSettingChangeHandler(name, changeFunc) {
2b5f94fa 337 const settingElem = document.getElementById("noVNC_setting_" + name);
6d6f0db0 338 if (changeFunc === undefined) {
651c23ec 339 changeFunc = () => UI.saveSetting(name);
6d6f0db0
SR
340 }
341 settingElem.addEventListener('change', changeFunc);
342 },
343
0e4808bf 344 addSettingsHandlers() {
6d6f0db0
SR
345 document.getElementById("noVNC_settings_button")
346 .addEventListener('click', UI.toggleSettingsPanel);
347
348 UI.addSettingChangeHandler('encrypt');
6d6f0db0 349 UI.addSettingChangeHandler('resize');
6d6f0db0 350 UI.addSettingChangeHandler('resize', UI.applyResizeMode);
4ddcc753 351 UI.addSettingChangeHandler('resize', UI.updateViewClip);
5243cbf6
PO
352 UI.addSettingChangeHandler('quality');
353 UI.addSettingChangeHandler('quality', UI.updateQuality);
479d8cef
SM
354 UI.addSettingChangeHandler('compression');
355 UI.addSettingChangeHandler('compression', UI.updateCompression);
a49ade5f
SM
356 UI.addSettingChangeHandler('view_clip');
357 UI.addSettingChangeHandler('view_clip', UI.updateViewClip);
6d6f0db0
SR
358 UI.addSettingChangeHandler('shared');
359 UI.addSettingChangeHandler('view_only');
360 UI.addSettingChangeHandler('view_only', UI.updateViewOnly);
4c38179d
AP
361 UI.addSettingChangeHandler('show_dot');
362 UI.addSettingChangeHandler('show_dot', UI.updateShowDotCursor);
6d6f0db0
SR
363 UI.addSettingChangeHandler('host');
364 UI.addSettingChangeHandler('port');
365 UI.addSettingChangeHandler('path');
366 UI.addSettingChangeHandler('repeaterID');
367 UI.addSettingChangeHandler('logging');
368 UI.addSettingChangeHandler('logging', UI.updateLogging);
369 UI.addSettingChangeHandler('reconnect');
370 UI.addSettingChangeHandler('reconnect_delay');
371 },
372
0e4808bf 373 addFullscreenHandlers() {
6d6f0db0
SR
374 document.getElementById("noVNC_fullscreen_button")
375 .addEventListener('click', UI.toggleFullscreen);
376
377 window.addEventListener('fullscreenchange', UI.updateFullscreenButton);
378 window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton);
379 window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton);
380 window.addEventListener('msfullscreenchange', UI.updateFullscreenButton);
381 },
0f6af1e3 382
95dd6001 383/* ------^-------
59387b34 384 * /EVENT HANDLERS
95dd6001 385 * ==============
386 * VISUAL
387 * ------v------*/
58ded70d 388
ee5cae9f 389 // Disable/enable controls depending on connection state
0e4808bf 390 updateVisualState(state) {
6d6f0db0
SR
391
392 document.documentElement.classList.remove("noVNC_connecting");
393 document.documentElement.classList.remove("noVNC_connected");
394 document.documentElement.classList.remove("noVNC_disconnecting");
395 document.documentElement.classList.remove("noVNC_reconnecting");
396
2b5f94fa 397 const transition_elem = document.getElementById("noVNC_transition_text");
ee5cae9f
SM
398 switch (state) {
399 case 'init':
400 break;
6d6f0db0 401 case 'connecting':
ee5cae9f 402 transition_elem.textContent = _("Connecting...");
6d6f0db0
SR
403 document.documentElement.classList.add("noVNC_connecting");
404 break;
405 case 'connected':
6d6f0db0 406 document.documentElement.classList.add("noVNC_connected");
6d6f0db0
SR
407 break;
408 case 'disconnecting':
ee5cae9f 409 transition_elem.textContent = _("Disconnecting...");
6d6f0db0
SR
410 document.documentElement.classList.add("noVNC_disconnecting");
411 break;
412 case 'disconnected':
6d6f0db0 413 break;
ee5cae9f
SM
414 case 'reconnecting':
415 transition_elem.textContent = _("Reconnecting...");
416 document.documentElement.classList.add("noVNC_reconnecting");
6d6f0db0 417 break;
6d6f0db0 418 default:
ee5cae9f 419 Log.Error("Invalid visual state: " + state);
2592c243 420 UI.showStatus(_("Internal error"), 'error');
ee5cae9f 421 return;
6d6f0db0 422 }
8d710e8b 423
6d6f0db0 424 if (UI.connected) {
4ddcc753
SM
425 UI.updateViewClip();
426
6d6f0db0 427 UI.disableSetting('encrypt');
6d6f0db0
SR
428 UI.disableSetting('shared');
429 UI.disableSetting('host');
430 UI.disableSetting('port');
431 UI.disableSetting('path');
432 UI.disableSetting('repeaterID');
6d6f0db0
SR
433 UI.setMouseButton(1);
434
435 // Hide the controlbar after 2 seconds
436 UI.closeControlbarTimeout = setTimeout(UI.closeControlbar, 2000);
437 } else {
438 UI.enableSetting('encrypt');
6d6f0db0
SR
439 UI.enableSetting('shared');
440 UI.enableSetting('host');
441 UI.enableSetting('port');
442 UI.enableSetting('path');
443 UI.enableSetting('repeaterID');
cd523e8f 444 UI.updatePowerButton();
6d6f0db0
SR
445 UI.keepControlbar();
446 }
fdedbafb 447
42e3b03f
PO
448 // State change closes dialogs as they may not be relevant
449 // anymore
450 UI.closeAllPanels();
5b453ed4 451 document.getElementById('noVNC_credentials_dlg')
6d6f0db0 452 .classList.remove('noVNC_open');
6d6f0db0 453 },
29475d77 454
0e4808bf 455 showStatus(text, status_type, time) {
2b5f94fa 456 const statusElem = document.getElementById('noVNC_status');
8a7ec6ea 457
6d6f0db0
SR
458 if (typeof status_type === 'undefined') {
459 status_type = 'normal';
460 }
4e471b5b 461
d623a029
SM
462 // Don't overwrite more severe visible statuses and never
463 // errors. Only shows the first error.
d623a029
SM
464 if (statusElem.classList.contains("noVNC_open")) {
465 if (statusElem.classList.contains("noVNC_status_error")) {
b17f6c69
SM
466 return;
467 }
468 if (statusElem.classList.contains("noVNC_status_warn") &&
469 status_type === 'normal') {
470 return;
d623a029 471 }
d623a029 472 }
6d6f0db0 473
afa1f8a2
SM
474 clearTimeout(UI.statusTimeout);
475
6d6f0db0 476 switch (status_type) {
d623a029
SM
477 case 'error':
478 statusElem.classList.remove("noVNC_status_warn");
479 statusElem.classList.remove("noVNC_status_normal");
480 statusElem.classList.add("noVNC_status_error");
481 break;
6d6f0db0
SR
482 case 'warning':
483 case 'warn':
d623a029
SM
484 statusElem.classList.remove("noVNC_status_error");
485 statusElem.classList.remove("noVNC_status_normal");
6d6f0db0
SR
486 statusElem.classList.add("noVNC_status_warn");
487 break;
6d6f0db0
SR
488 case 'normal':
489 case 'info':
490 default:
d623a029
SM
491 statusElem.classList.remove("noVNC_status_error");
492 statusElem.classList.remove("noVNC_status_warn");
6d6f0db0
SR
493 statusElem.classList.add("noVNC_status_normal");
494 break;
495 }
8d7708c8 496
6d6f0db0
SR
497 statusElem.textContent = text;
498 statusElem.classList.add("noVNC_open");
8d7708c8 499
6d6f0db0
SR
500 // If no time was specified, show the status for 1.5 seconds
501 if (typeof time === 'undefined') {
502 time = 1500;
503 }
8d7708c8 504
6d6f0db0
SR
505 // Error messages do not timeout
506 if (status_type !== 'error') {
507 UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
508 }
509 },
510
0e4808bf 511 hideStatus() {
6d6f0db0
SR
512 clearTimeout(UI.statusTimeout);
513 document.getElementById('noVNC_status').classList.remove("noVNC_open");
514 },
515
0e4808bf 516 activateControlbar(event) {
6d6f0db0
SR
517 clearTimeout(UI.idleControlbarTimeout);
518 // We manipulate the anchor instead of the actual control
519 // bar in order to avoid creating new a stacking group
520 document.getElementById('noVNC_control_bar_anchor')
521 .classList.remove("noVNC_idle");
522 UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
523 },
524
0e4808bf 525 idleControlbar() {
8df281cc
SM
526 // Don't fade if a child of the control bar has focus
527 if (document.getElementById('noVNC_control_bar')
e7fa686f 528 .contains(document.activeElement) && document.hasFocus()) {
8df281cc
SM
529 UI.activateControlbar();
530 return;
531 }
532
6d6f0db0
SR
533 document.getElementById('noVNC_control_bar_anchor')
534 .classList.add("noVNC_idle");
535 },
536
0e4808bf 537 keepControlbar() {
6d6f0db0
SR
538 clearTimeout(UI.closeControlbarTimeout);
539 },
540
0e4808bf 541 openControlbar() {
6d6f0db0
SR
542 document.getElementById('noVNC_control_bar')
543 .classList.add("noVNC_open");
544 },
545
0e4808bf 546 closeControlbar() {
6d6f0db0
SR
547 UI.closeAllPanels();
548 document.getElementById('noVNC_control_bar')
549 .classList.remove("noVNC_open");
a040c402 550 UI.rfb.focus();
6d6f0db0
SR
551 },
552
0e4808bf 553 toggleControlbar() {
6d6f0db0
SR
554 if (document.getElementById('noVNC_control_bar')
555 .classList.contains("noVNC_open")) {
556 UI.closeControlbar();
557 } else {
558 UI.openControlbar();
559 }
560 },
561
0e4808bf 562 toggleControlbarSide() {
06309160
SM
563 // Temporarily disable animation, if bar is displayed, to avoid weird
564 // movement. The transitionend-event will not fire when display=none.
2b5f94fa
JD
565 const bar = document.getElementById('noVNC_control_bar');
566 const barDisplayStyle = window.getComputedStyle(bar).display;
06309160
SM
567 if (barDisplayStyle !== 'none') {
568 bar.style.transitionDuration = '0s';
651c23ec 569 bar.addEventListener('transitionend', () => bar.style.transitionDuration = '');
06309160 570 }
6d6f0db0 571
2b5f94fa 572 const anchor = document.getElementById('noVNC_control_bar_anchor');
6d6f0db0
SR
573 if (anchor.classList.contains("noVNC_right")) {
574 WebUtil.writeSetting('controlbar_pos', 'left');
575 anchor.classList.remove("noVNC_right");
576 } else {
577 WebUtil.writeSetting('controlbar_pos', 'right');
578 anchor.classList.add("noVNC_right");
579 }
ca5c74ad 580
6d6f0db0
SR
581 // Consider this a movement of the handle
582 UI.controlbarDrag = true;
583 },
4e471b5b 584
0e4808bf 585 showControlbarHint(show) {
2b5f94fa 586 const hint = document.getElementById('noVNC_control_bar_hint');
bbc1648c
SM
587 if (show) {
588 hint.classList.add("noVNC_active");
589 } else {
590 hint.classList.remove("noVNC_active");
591 }
592 },
593
0e4808bf 594 dragControlbarHandle(e) {
6d6f0db0 595 if (!UI.controlbarGrabbed) return;
38323d4d 596
2b5f94fa 597 const ptr = getPointerEvent(e);
8ee432f1 598
2b5f94fa 599 const anchor = document.getElementById('noVNC_control_bar_anchor');
6d6f0db0 600 if (ptr.clientX < (window.innerWidth * 0.1)) {
cf348b78 601 if (anchor.classList.contains("noVNC_right")) {
6d6f0db0 602 UI.toggleControlbarSide();
8ee432f1 603 }
6d6f0db0
SR
604 } else if (ptr.clientX > (window.innerWidth * 0.9)) {
605 if (!anchor.classList.contains("noVNC_right")) {
606 UI.toggleControlbarSide();
04b399e2 607 }
6d6f0db0 608 }
04b399e2 609
6d6f0db0 610 if (!UI.controlbarDrag) {
2b5f94fa 611 const dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
04b399e2 612
6d6f0db0 613 if (dragDistance < dragThreshold) return;
04b399e2 614
6d6f0db0
SR
615 UI.controlbarDrag = true;
616 }
f75e4d3c 617
2b5f94fa 618 const eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
04b399e2 619
6d6f0db0 620 UI.moveControlbarHandle(eventY);
59cd99bc 621
6d6f0db0
SR
622 e.preventDefault();
623 e.stopPropagation();
624 UI.keepControlbar();
625 UI.activateControlbar();
626 },
04b399e2 627
6d6f0db0 628 // Move the handle but don't allow any position outside the bounds
0e4808bf 629 moveControlbarHandle(viewportRelativeY) {
2b5f94fa
JD
630 const handle = document.getElementById("noVNC_control_bar_handle");
631 const handleHeight = handle.getBoundingClientRect().height;
632 const controlbarBounds = document.getElementById("noVNC_control_bar")
6d6f0db0 633 .getBoundingClientRect();
2b5f94fa 634 const margin = 10;
04b399e2 635
6d6f0db0
SR
636 // These heights need to be non-zero for the below logic to work
637 if (handleHeight === 0 || controlbarBounds.height === 0) {
638 return;
639 }
04b399e2 640
2b5f94fa 641 let newY = viewportRelativeY;
04b399e2 642
6d6f0db0
SR
643 // Check if the coordinates are outside the control bar
644 if (newY < controlbarBounds.top + margin) {
645 // Force coordinates to be below the top of the control bar
646 newY = controlbarBounds.top + margin;
04b399e2 647
6d6f0db0
SR
648 } else if (newY > controlbarBounds.top +
649 controlbarBounds.height - handleHeight - margin) {
650 // Force coordinates to be above the bottom of the control bar
651 newY = controlbarBounds.top +
652 controlbarBounds.height - handleHeight - margin;
653 }
04b399e2 654
6d6f0db0
SR
655 // Corner case: control bar too small for stable position
656 if (controlbarBounds.height < (handleHeight + margin * 2)) {
657 newY = controlbarBounds.top +
658 (controlbarBounds.height - handleHeight) / 2;
659 }
04b399e2 660
6d6f0db0 661 // The transform needs coordinates that are relative to the parent
2b5f94fa 662 const parentRelativeY = newY - controlbarBounds.top;
6d6f0db0
SR
663 handle.style.transform = "translateY(" + parentRelativeY + "px)";
664 },
665
0e4808bf 666 updateControlbarHandle() {
6d6f0db0
SR
667 // Since the control bar is fixed on the viewport and not the page,
668 // the move function expects coordinates relative the the viewport.
2b5f94fa
JD
669 const handle = document.getElementById("noVNC_control_bar_handle");
670 const handleBounds = handle.getBoundingClientRect();
6d6f0db0
SR
671 UI.moveControlbarHandle(handleBounds.top);
672 },
673
0e4808bf 674 controlbarHandleMouseUp(e) {
6d6f0db0
SR
675 if ((e.type == "mouseup") && (e.button != 0)) return;
676
677 // mouseup and mousedown on the same place toggles the controlbar
678 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
679 UI.toggleControlbar();
04b399e2
SM
680 e.preventDefault();
681 e.stopPropagation();
de315d62
PO
682 UI.keepControlbar();
683 UI.activateControlbar();
6d6f0db0
SR
684 }
685 UI.controlbarGrabbed = false;
bbc1648c 686 UI.showControlbarHint(false);
6d6f0db0
SR
687 },
688
0e4808bf 689 controlbarHandleMouseDown(e) {
6d6f0db0
SR
690 if ((e.type == "mousedown") && (e.button != 0)) return;
691
2b5f94fa 692 const ptr = getPointerEvent(e);
6d6f0db0 693
2b5f94fa
JD
694 const handle = document.getElementById("noVNC_control_bar_handle");
695 const bounds = handle.getBoundingClientRect();
6d6f0db0 696
333ad45c
SM
697 // Touch events have implicit capture
698 if (e.type === "mousedown") {
699 setCapture(handle);
700 }
701
6d6f0db0
SR
702 UI.controlbarGrabbed = true;
703 UI.controlbarDrag = false;
704
bbc1648c
SM
705 UI.showControlbarHint(true);
706
6d6f0db0
SR
707 UI.controlbarMouseDownClientY = ptr.clientY;
708 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
709 e.preventDefault();
710 e.stopPropagation();
711 UI.keepControlbar();
712 UI.activateControlbar();
713 },
714
0e4808bf 715 toggleExpander(e) {
6d6f0db0
SR
716 if (this.classList.contains("noVNC_open")) {
717 this.classList.remove("noVNC_open");
718 } else {
719 this.classList.add("noVNC_open");
720 }
721 },
575f6983 722
95dd6001 723/* ------^-------
724 * /VISUAL
725 * ==============
726 * SETTINGS
727 * ------v------*/
728
6d6f0db0 729 // Initial page load read/initialization of settings
0e4808bf 730 initSetting(name, defVal) {
6d6f0db0 731 // Check Query string followed by cookie
2b5f94fa 732 let val = WebUtil.getConfigVar(name);
6d6f0db0
SR
733 if (val === null) {
734 val = WebUtil.readSetting(name, defVal);
735 }
8ad8f15c
AW
736 WebUtil.setSetting(name, val);
737 UI.updateSetting(name);
6d6f0db0
SR
738 return val;
739 },
bbbf42bb 740
11715f20 741 // Set the new value, update and disable form control setting
0e4808bf 742 forceSetting(name, val) {
11715f20
SM
743 WebUtil.setSetting(name, val);
744 UI.updateSetting(name);
745 UI.disableSetting(name);
746 },
747
6d6f0db0
SR
748 // Update cookie and form control setting. If value is not set, then
749 // updates from control to current cookie setting.
0e4808bf 750 updateSetting(name) {
bbbf42bb 751
6d6f0db0 752 // Update the settings control
2b5f94fa 753 let value = UI.getSetting(name);
bbbf42bb 754
2b5f94fa 755 const ctrl = document.getElementById('noVNC_setting_' + name);
6d6f0db0
SR
756 if (ctrl.type === 'checkbox') {
757 ctrl.checked = value;
bbbf42bb 758
6d6f0db0 759 } else if (typeof ctrl.options !== 'undefined') {
2b5f94fa 760 for (let i = 0; i < ctrl.options.length; i += 1) {
6d6f0db0
SR
761 if (ctrl.options[i].value === value) {
762 ctrl.selectedIndex = i;
763 break;
bbbf42bb 764 }
bbbf42bb 765 }
6d6f0db0
SR
766 } else {
767 /*Weird IE9 error leads to 'null' appearring
768 in textboxes instead of ''.*/
769 if (value === null) {
770 value = "";
bbbf42bb 771 }
6d6f0db0
SR
772 ctrl.value = value;
773 }
774 },
775
776 // Save control setting to cookie
0e4808bf 777 saveSetting(name) {
2b5f94fa
JD
778 const ctrl = document.getElementById('noVNC_setting_' + name);
779 let val;
6d6f0db0
SR
780 if (ctrl.type === 'checkbox') {
781 val = ctrl.checked;
782 } else if (typeof ctrl.options !== 'undefined') {
783 val = ctrl.options[ctrl.selectedIndex].value;
784 } else {
785 val = ctrl.value;
786 }
787 WebUtil.writeSetting(name, val);
788 //Log.Debug("Setting saved '" + name + "=" + val + "'");
789 return val;
790 },
791
792 // Read form control compatible setting from cookie
0e4808bf 793 getSetting(name) {
2b5f94fa
JD
794 const ctrl = document.getElementById('noVNC_setting_' + name);
795 let val = WebUtil.readSetting(name);
6d6f0db0 796 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
942a3127 797 if (val.toString().toLowerCase() in {'0': 1, 'no': 1, 'false': 1}) {
6d6f0db0
SR
798 val = false;
799 } else {
800 val = true;
bbbf42bb 801 }
6d6f0db0
SR
802 }
803 return val;
804 },
805
806 // These helpers compensate for the lack of parent-selectors and
807 // previous-sibling-selectors in CSS which are needed when we want to
808 // disable the labels that belong to disabled input elements.
0e4808bf 809 disableSetting(name) {
2b5f94fa 810 const ctrl = document.getElementById('noVNC_setting_' + name);
6d6f0db0
SR
811 ctrl.disabled = true;
812 ctrl.label.classList.add('noVNC_disabled');
813 },
814
0e4808bf 815 enableSetting(name) {
2b5f94fa 816 const ctrl = document.getElementById('noVNC_setting_' + name);
6d6f0db0
SR
817 ctrl.disabled = false;
818 ctrl.label.classList.remove('noVNC_disabled');
819 },
24584cca 820
ed8cbe4e
PO
821/* ------^-------
822 * /SETTINGS
823 * ==============
824 * PANELS
825 * ------v------*/
826
0e4808bf 827 closeAllPanels() {
6d6f0db0 828 UI.closeSettingsPanel();
cd523e8f 829 UI.closePowerPanel();
6d6f0db0
SR
830 UI.closeClipboardPanel();
831 UI.closeExtraKeys();
832 },
ed8cbe4e
PO
833
834/* ------^-------
835 * /PANELS
836 * ==============
837 * SETTINGS (panel)
838 * ------v------*/
839
0e4808bf 840 openSettingsPanel() {
6d6f0db0
SR
841 UI.closeAllPanels();
842 UI.openControlbar();
843
844 // Refresh UI elements from saved cookies
845 UI.updateSetting('encrypt');
a49ade5f 846 UI.updateSetting('view_clip');
6d6f0db0 847 UI.updateSetting('resize');
5243cbf6 848 UI.updateSetting('quality');
479d8cef 849 UI.updateSetting('compression');
6d6f0db0
SR
850 UI.updateSetting('shared');
851 UI.updateSetting('view_only');
852 UI.updateSetting('path');
853 UI.updateSetting('repeaterID');
854 UI.updateSetting('logging');
855 UI.updateSetting('reconnect');
856 UI.updateSetting('reconnect_delay');
857
858 document.getElementById('noVNC_settings')
859 .classList.add("noVNC_open");
860 document.getElementById('noVNC_settings_button')
861 .classList.add("noVNC_selected");
862 },
863
0e4808bf 864 closeSettingsPanel() {
6d6f0db0
SR
865 document.getElementById('noVNC_settings')
866 .classList.remove("noVNC_open");
867 document.getElementById('noVNC_settings_button')
868 .classList.remove("noVNC_selected");
869 },
870
0e4808bf 871 toggleSettingsPanel() {
6d6f0db0
SR
872 if (document.getElementById('noVNC_settings')
873 .classList.contains("noVNC_open")) {
874 UI.closeSettingsPanel();
875 } else {
876 UI.openSettingsPanel();
877 }
878 },
bbbf42bb 879
95dd6001 880/* ------^-------
881 * /SETTINGS
882 * ==============
cd523e8f 883 * POWER
95dd6001 884 * ------v------*/
885
0e4808bf 886 openPowerPanel() {
6d6f0db0
SR
887 UI.closeAllPanels();
888 UI.openControlbar();
889
cd523e8f 890 document.getElementById('noVNC_power')
6d6f0db0 891 .classList.add("noVNC_open");
cd523e8f 892 document.getElementById('noVNC_power_button')
6d6f0db0
SR
893 .classList.add("noVNC_selected");
894 },
895
0e4808bf 896 closePowerPanel() {
cd523e8f 897 document.getElementById('noVNC_power')
6d6f0db0 898 .classList.remove("noVNC_open");
cd523e8f 899 document.getElementById('noVNC_power_button')
6d6f0db0
SR
900 .classList.remove("noVNC_selected");
901 },
902
0e4808bf 903 togglePowerPanel() {
cd523e8f 904 if (document.getElementById('noVNC_power')
6d6f0db0 905 .classList.contains("noVNC_open")) {
cd523e8f 906 UI.closePowerPanel();
6d6f0db0 907 } else {
cd523e8f 908 UI.openPowerPanel();
6d6f0db0
SR
909 }
910 },
ed8cbe4e 911
cd523e8f 912 // Disable/enable power button
0e4808bf 913 updatePowerButton() {
cd523e8f 914 if (UI.connected &&
747b4623
PO
915 UI.rfb.capabilities.power &&
916 !UI.rfb.viewOnly) {
cd523e8f 917 document.getElementById('noVNC_power_button')
6d6f0db0
SR
918 .classList.remove("noVNC_hidden");
919 } else {
cd523e8f 920 document.getElementById('noVNC_power_button')
6d6f0db0 921 .classList.add("noVNC_hidden");
cd523e8f
PO
922 // Close power panel if open
923 UI.closePowerPanel();
6d6f0db0
SR
924 }
925 },
bbbf42bb 926
95dd6001 927/* ------^-------
cd523e8f 928 * /POWER
95dd6001 929 * ==============
930 * CLIPBOARD
931 * ------v------*/
f8b399d7 932
0e4808bf 933 openClipboardPanel() {
6d6f0db0
SR
934 UI.closeAllPanels();
935 UI.openControlbar();
936
937 document.getElementById('noVNC_clipboard')
938 .classList.add("noVNC_open");
939 document.getElementById('noVNC_clipboard_button')
940 .classList.add("noVNC_selected");
941 },
942
0e4808bf 943 closeClipboardPanel() {
6d6f0db0
SR
944 document.getElementById('noVNC_clipboard')
945 .classList.remove("noVNC_open");
946 document.getElementById('noVNC_clipboard_button')
947 .classList.remove("noVNC_selected");
948 },
949
0e4808bf 950 toggleClipboardPanel() {
6d6f0db0
SR
951 if (document.getElementById('noVNC_clipboard')
952 .classList.contains("noVNC_open")) {
953 UI.closeClipboardPanel();
954 } else {
955 UI.openClipboardPanel();
956 }
957 },
958
0e4808bf 959 clipboardReceive(e) {
6786fd87 960 Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0, 40) + "...");
e89eef94 961 document.getElementById('noVNC_clipboard_text').value = e.detail.text;
6d6f0db0
SR
962 Log.Debug("<< UI.clipboardReceive");
963 },
964
0e4808bf 965 clipboardClear() {
6d6f0db0
SR
966 document.getElementById('noVNC_clipboard_text').value = "";
967 UI.rfb.clipboardPasteFrom("");
968 },
969
0e4808bf 970 clipboardSend() {
2b5f94fa 971 const text = document.getElementById('noVNC_clipboard_text').value;
6786fd87 972 Log.Debug(">> UI.clipboardSend: " + text.substr(0, 40) + "...");
6d6f0db0
SR
973 UI.rfb.clipboardPasteFrom(text);
974 Log.Debug("<< UI.clipboardSend");
975 },
95dd6001 976
977/* ------^-------
978 * /CLIPBOARD
979 * ==============
980 * CONNECTION
981 * ------v------*/
982
0e4808bf 983 openConnectPanel() {
6d6f0db0
SR
984 document.getElementById('noVNC_connect_dlg')
985 .classList.add("noVNC_open");
986 },
b3c932c3 987
0e4808bf 988 closeConnectPanel() {
6d6f0db0
SR
989 document.getElementById('noVNC_connect_dlg')
990 .classList.remove("noVNC_open");
991 },
b3c932c3 992
0e4808bf 993 connect(event, password) {
a4822c3a
SM
994
995 // Ignore when rfb already exists
996 if (typeof UI.rfb !== 'undefined') {
997 return;
998 }
999
2b5f94fa
JD
1000 const host = UI.getSetting('host');
1001 const port = UI.getSetting('port');
1002 const path = UI.getSetting('path');
c55f05f6 1003
6d6f0db0
SR
1004 if (typeof password === 'undefined') {
1005 password = WebUtil.getConfigVar('password');
2e735160 1006 UI.reconnect_password = password;
6d6f0db0 1007 }
044d54ed 1008
6d6f0db0
SR
1009 if (password === null) {
1010 password = undefined;
1011 }
512d3605 1012
d623a029
SM
1013 UI.hideStatus();
1014
d593483e 1015 if (!host) {
8317524c
SM
1016 Log.Error("Can't connect when host is: " + host);
1017 UI.showStatus(_("Must set host"), 'error');
6d6f0db0
SR
1018 return;
1019 }
53fc7392 1020
6d6f0db0 1021 UI.closeConnectPanel();
4102b71c 1022
f3763a01
SM
1023 UI.updateVisualState('connecting');
1024
2b5f94fa 1025 let url;
5b4e5d01
PO
1026
1027 url = UI.getSetting('encrypt') ? 'wss' : 'ws';
1028
1029 url += '://' + host;
35068204 1030 if (port) {
5b4e5d01
PO
1031 url += ':' + port;
1032 }
1033 url += '/' + path;
1034
9b84f516 1035 UI.rfb = new RFB(document.getElementById('noVNC_container'), url,
2f4516f2
PO
1036 { shared: UI.getSetting('shared'),
1037 repeaterID: UI.getSetting('repeaterID'),
1038 credentials: { password: password } });
ee5cae9f 1039 UI.rfb.addEventListener("connect", UI.connectFinished);
e89eef94
PO
1040 UI.rfb.addEventListener("disconnect", UI.disconnectFinished);
1041 UI.rfb.addEventListener("credentialsrequired", UI.credentials);
d472f3f1 1042 UI.rfb.addEventListener("securityfailure", UI.securityFailed);
651c23ec 1043 UI.rfb.addEventListener("capabilities", UI.updatePowerButton);
e89eef94
PO
1044 UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
1045 UI.rfb.addEventListener("bell", UI.bell);
e89eef94 1046 UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
9b84f516
PO
1047 UI.rfb.clipViewport = UI.getSetting('view_clip');
1048 UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
1049 UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
5243cbf6 1050 UI.rfb.qualityLevel = parseInt(UI.getSetting('quality'));
479d8cef 1051 UI.rfb.compressionLevel = parseInt(UI.getSetting('compression'));
6aed0b4d 1052 UI.rfb.showDotCursor = UI.getSetting('show_dot');
37c60935 1053
f3763a01 1054 UI.updateViewOnly(); // requires UI.rfb
6d6f0db0 1055 },
5299db1a 1056
0e4808bf 1057 disconnect() {
6d6f0db0 1058 UI.rfb.disconnect();
8e0f0088 1059
ee5cae9f
SM
1060 UI.connected = false;
1061
6d6f0db0
SR
1062 // Disable automatic reconnecting
1063 UI.inhibit_reconnect = true;
044d54ed 1064
ee5cae9f
SM
1065 UI.updateVisualState('disconnecting');
1066
6d6f0db0
SR
1067 // Don't display the connection settings until we're actually disconnected
1068 },
bbbf42bb 1069
0e4808bf 1070 reconnect() {
6d6f0db0 1071 UI.reconnect_callback = null;
044d54ed 1072
6d6f0db0
SR
1073 // if reconnect has been disabled in the meantime, do nothing.
1074 if (UI.inhibit_reconnect) {
1075 return;
1076 }
044d54ed 1077
6d6f0db0
SR
1078 UI.connect(null, UI.reconnect_password);
1079 },
044d54ed 1080
0e4808bf 1081 cancelReconnect() {
68958038
SM
1082 if (UI.reconnect_callback !== null) {
1083 clearTimeout(UI.reconnect_callback);
1084 UI.reconnect_callback = null;
1085 }
1086
1087 UI.updateVisualState('disconnected');
1088
1089 UI.openControlbar();
1090 UI.openConnectPanel();
1091 },
1092
0e4808bf 1093 connectFinished(e) {
ee5cae9f
SM
1094 UI.connected = true;
1095 UI.inhibit_reconnect = false;
ee5cae9f
SM
1096
1097 let msg;
c995c086
SM
1098 if (UI.getSetting('encrypt')) {
1099 msg = _("Connected (encrypted) to ") + UI.desktopName;
ee5cae9f 1100 } else {
c995c086 1101 msg = _("Connected (unencrypted) to ") + UI.desktopName;
ee5cae9f
SM
1102 }
1103 UI.showStatus(msg);
1104 UI.updateVisualState('connected');
1105
1106 // Do this last because it can only be used on rendered elements
9b84f516 1107 UI.rfb.focus();
ee5cae9f
SM
1108 },
1109
0e4808bf 1110 disconnectFinished(e) {
2b5f94fa 1111 const wasConnected = UI.connected;
7f1049c0 1112
ee5cae9f
SM
1113 // This variable is ideally set when disconnection starts, but
1114 // when the disconnection isn't clean or if it is initiated by
1115 // the server, we need to do it here as well since
1116 // UI.disconnect() won't be used in those cases.
1117 UI.connected = false;
1118
d1aeb435
PO
1119 UI.rfb = undefined;
1120
d472f3f1 1121 if (!e.detail.clean) {
ee5cae9f 1122 UI.updateVisualState('disconnected');
7f1049c0
SM
1123 if (wasConnected) {
1124 UI.showStatus(_("Something went wrong, connection is closed"),
1125 'error');
1126 } else {
1127 UI.showStatus(_("Failed to connect to server"), 'error');
1128 }
6d6f0db0 1129 } else if (UI.getSetting('reconnect', false) === true && !UI.inhibit_reconnect) {
ee5cae9f 1130 UI.updateVisualState('reconnecting');
044d54ed 1131
2b5f94fa 1132 const delay = parseInt(UI.getSetting('reconnect_delay'));
6d6f0db0
SR
1133 UI.reconnect_callback = setTimeout(UI.reconnect, delay);
1134 return;
ee5cae9f
SM
1135 } else {
1136 UI.updateVisualState('disconnected');
1137 UI.showStatus(_("Disconnected"), 'normal');
6d6f0db0 1138 }
044d54ed 1139
c90245da
NL
1140 document.title = PAGE_TITLE;
1141
6d6f0db0
SR
1142 UI.openControlbar();
1143 UI.openConnectPanel();
1144 },
044d54ed 1145
0e4808bf 1146 securityFailed(e) {
d472f3f1
SM
1147 let msg = "";
1148 // On security failures we might get a string with a reason
1149 // directly from the server. Note that we can't control if
1150 // this string is translated or not.
1151 if ('reason' in e.detail) {
1152 msg = _("New connection has been rejected with reason: ") +
1153 e.detail.reason;
1154 } else {
1155 msg = _("New connection has been rejected");
6d6f0db0 1156 }
d472f3f1 1157 UI.showStatus(msg, 'error');
6d6f0db0 1158 },
3bb12056 1159
7d714b15
SM
1160/* ------^-------
1161 * /CONNECTION
1162 * ==============
1163 * PASSWORD
1164 * ------v------*/
1165
0e4808bf 1166 credentials(e) {
430f00d6 1167 // FIXME: handle more types
5b453ed4
CKB
1168
1169 document.getElementById("noVNC_username_block").classList.remove("noVNC_hidden");
1170 document.getElementById("noVNC_password_block").classList.remove("noVNC_hidden");
1171
1172 let inputFocus = "none";
b8d1a8bb 1173 if (e.detail.types.indexOf("username") === -1) {
5b453ed4
CKB
1174 document.getElementById("noVNC_username_block").classList.add("noVNC_hidden");
1175 } else {
1176 inputFocus = inputFocus === "none" ? "noVNC_username_input" : inputFocus;
1177 }
b8d1a8bb 1178 if (e.detail.types.indexOf("password") === -1) {
5b453ed4
CKB
1179 document.getElementById("noVNC_password_block").classList.add("noVNC_hidden");
1180 } else {
1181 inputFocus = inputFocus === "none" ? "noVNC_password_input" : inputFocus;
1182 }
1183 document.getElementById('noVNC_credentials_dlg')
6d6f0db0 1184 .classList.add('noVNC_open');
7d714b15 1185
651c23ec 1186 setTimeout(() => document
5b453ed4 1187 .getElementById(inputFocus).focus(), 100);
7d714b15 1188
5b453ed4
CKB
1189 Log.Warn("Server asked for credentials");
1190 UI.showStatus(_("Credentials are required"), "warning");
6d6f0db0
SR
1191 },
1192
5b453ed4 1193 setCredentials(e) {
a80b5fda
PO
1194 // Prevent actually submitting the form
1195 e.preventDefault();
1196
5b453ed4
CKB
1197 let inputElemUsername = document.getElementById('noVNC_username_input');
1198 const username = inputElemUsername.value;
1199
1200 let inputElemPassword = document.getElementById('noVNC_password_input');
1201 const password = inputElemPassword.value;
c23665dd 1202 // Clear the input after reading the password
5b453ed4
CKB
1203 inputElemPassword.value = "";
1204
1205 UI.rfb.sendCredentials({ username: username, password: password });
6d6f0db0 1206 UI.reconnect_password = password;
5b453ed4 1207 document.getElementById('noVNC_credentials_dlg')
6d6f0db0 1208 .classList.remove('noVNC_open');
6d6f0db0 1209 },
58ded70d 1210
95dd6001 1211/* ------^-------
7d714b15 1212 * /PASSWORD
95dd6001 1213 * ==============
1214 * FULLSCREEN
1215 * ------v------*/
1216
0e4808bf 1217 toggleFullscreen() {
32e08195
SM
1218 if (document.fullscreenElement || // alternative standard method
1219 document.mozFullScreenElement || // currently working methods
1220 document.webkitFullscreenElement ||
1221 document.msFullscreenElement) {
1222 if (document.exitFullscreen) {
1223 document.exitFullscreen();
1224 } else if (document.mozCancelFullScreen) {
1225 document.mozCancelFullScreen();
1226 } else if (document.webkitExitFullscreen) {
1227 document.webkitExitFullscreen();
1228 } else if (document.msExitFullscreen) {
1229 document.msExitFullscreen();
6d6f0db0
SR
1230 }
1231 } else {
32e08195
SM
1232 if (document.documentElement.requestFullscreen) {
1233 document.documentElement.requestFullscreen();
1234 } else if (document.documentElement.mozRequestFullScreen) {
1235 document.documentElement.mozRequestFullScreen();
1236 } else if (document.documentElement.webkitRequestFullscreen) {
1237 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1238 } else if (document.body.msRequestFullscreen) {
1239 document.body.msRequestFullscreen();
7d1dc09a 1240 }
6d6f0db0 1241 }
6d6f0db0
SR
1242 UI.updateFullscreenButton();
1243 },
1244
0e4808bf 1245 updateFullscreenButton() {
6d6f0db0
SR
1246 if (document.fullscreenElement || // alternative standard method
1247 document.mozFullScreenElement || // currently working methods
1248 document.webkitFullscreenElement ||
1249 document.msFullscreenElement ) {
1250 document.getElementById('noVNC_fullscreen_button')
1251 .classList.add("noVNC_selected");
1252 } else {
1253 document.getElementById('noVNC_fullscreen_button')
1254 .classList.remove("noVNC_selected");
1255 }
1256 },
7d1dc09a 1257
95dd6001 1258/* ------^-------
1259 * /FULLSCREEN
1260 * ==============
1261 * RESIZE
1262 * ------v------*/
777cb7a0 1263
6d6f0db0 1264 // Apply remote resizing or local scaling
0e4808bf 1265 applyResizeMode() {
6d6f0db0 1266 if (!UI.rfb) return;
58ded70d 1267
9b84f516
PO
1268 UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
1269 UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
6d6f0db0 1270 },
bbbf42bb 1271
95dd6001 1272/* ------^-------
1273 * /RESIZE
1274 * ==============
a49ade5f 1275 * VIEW CLIPPING
95dd6001 1276 * ------v------*/
1277
4ddcc753
SM
1278 // Update viewport clipping property for the connection. The normal
1279 // case is to get the value from the setting. There are special cases
1280 // for when the viewport is scaled or when a touch device is used.
0e4808bf 1281 updateViewClip() {
6d6f0db0
SR
1282 if (!UI.rfb) return;
1283
4ddcc753 1284 const scaling = UI.getSetting('resize') === 'scale';
6d6f0db0 1285
4ddcc753
SM
1286 if (scaling) {
1287 // Can't be clipping if viewport is scaled to fit
1288 UI.forceSetting('view_clip', false);
1289 UI.rfb.clipViewport = false;
e52a278e
SM
1290 } else if (!hasScrollbarGutter) {
1291 // Some platforms have scrollbars that are difficult
1292 // to use in our case, so we always use our own panning
4ddcc753
SM
1293 UI.forceSetting('view_clip', true);
1294 UI.rfb.clipViewport = true;
1295 } else {
1296 UI.enableSetting('view_clip');
1297 UI.rfb.clipViewport = UI.getSetting('view_clip');
6d6f0db0 1298 }
8e0f0088 1299
6d6f0db0
SR
1300 // Changing the viewport may change the state of
1301 // the dragging button
1302 UI.updateViewDrag();
1303 },
1304
95dd6001 1305/* ------^-------
a49ade5f 1306 * /VIEW CLIPPING
95dd6001 1307 * ==============
1308 * VIEWDRAG
1309 * ------v------*/
1310
0e4808bf 1311 toggleViewDrag() {
6d6f0db0 1312 if (!UI.rfb) return;
bbbf42bb 1313
2bbd15cc 1314 UI.rfb.dragViewport = !UI.rfb.dragViewport;
6d6f0db0
SR
1315 UI.updateViewDrag();
1316 },
f0d9ab96 1317
0e4808bf 1318 updateViewDrag() {
6d6f0db0 1319 if (!UI.connected) return;
6244e383 1320
2b5f94fa 1321 const viewDragButton = document.getElementById('noVNC_view_drag_button');
29a0e6a8 1322
9b84f516
PO
1323 if (!UI.rfb.clipViewport && UI.rfb.dragViewport) {
1324 // We are no longer clipping the viewport. Make sure
1325 // viewport drag isn't active when it can't be used.
0460e5fd 1326 UI.rfb.dragViewport = false;
6d6f0db0
SR
1327 }
1328
0460e5fd 1329 if (UI.rfb.dragViewport) {
6d6f0db0
SR
1330 viewDragButton.classList.add("noVNC_selected");
1331 } else {
1332 viewDragButton.classList.remove("noVNC_selected");
1333 }
1334
f2d42dc3 1335 if (UI.rfb.clipViewport) {
6d6f0db0 1336 viewDragButton.classList.remove("noVNC_hidden");
6d6f0db0 1337 } else {
f2d42dc3 1338 viewDragButton.classList.add("noVNC_hidden");
6d6f0db0
SR
1339 }
1340 },
f8b399d7 1341
95dd6001 1342/* ------^-------
1343 * /VIEWDRAG
1344 * ==============
5243cbf6
PO
1345 * QUALITY
1346 * ------v------*/
1347
1348 updateQuality() {
1349 if (!UI.rfb) return;
1350
1351 UI.rfb.qualityLevel = parseInt(UI.getSetting('quality'));
1352 },
1353
1354/* ------^-------
1355 * /QUALITY
1356 * ==============
479d8cef
SM
1357 * COMPRESSION
1358 * ------v------*/
1359
1360 updateCompression() {
1361 if (!UI.rfb) return;
1362
1363 UI.rfb.compressionLevel = parseInt(UI.getSetting('compression'));
1364 },
1365
1366/* ------^-------
1367 * /COMPRESSION
1368 * ==============
95dd6001 1369 * KEYBOARD
1370 * ------v------*/
fdf21468 1371
0e4808bf 1372 showVirtualKeyboard() {
6d6f0db0 1373 if (!isTouchDevice) return;
4b30f9ce 1374
2b5f94fa 1375 const input = document.getElementById('noVNC_keyboardinput');
4b30f9ce 1376
6d6f0db0 1377 if (document.activeElement == input) return;
4b30f9ce 1378
6d6f0db0 1379 input.focus();
4b30f9ce 1380
6d6f0db0 1381 try {
2b5f94fa 1382 const l = input.value.length;
6d6f0db0
SR
1383 // Move the caret to the end
1384 input.setSelectionRange(l, l);
8727f598 1385 } catch (err) {
4a16dc51 1386 // setSelectionRange is undefined in Google Chrome
8727f598 1387 }
6d6f0db0 1388 },
4b30f9ce 1389
0e4808bf 1390 hideVirtualKeyboard() {
6d6f0db0 1391 if (!isTouchDevice) return;
4b30f9ce 1392
2b5f94fa 1393 const input = document.getElementById('noVNC_keyboardinput');
4b30f9ce 1394
6d6f0db0 1395 if (document.activeElement != input) return;
4b30f9ce 1396
6d6f0db0
SR
1397 input.blur();
1398 },
4b30f9ce 1399
0e4808bf 1400 toggleVirtualKeyboard() {
6d6f0db0
SR
1401 if (document.getElementById('noVNC_keyboard_button')
1402 .classList.contains("noVNC_selected")) {
1403 UI.hideVirtualKeyboard();
1404 } else {
1405 UI.showVirtualKeyboard();
1406 }
1407 },
bbbf42bb 1408
0e4808bf 1409 onfocusVirtualKeyboard(event) {
6d6f0db0
SR
1410 document.getElementById('noVNC_keyboard_button')
1411 .classList.add("noVNC_selected");
1d6ff4a3 1412 if (UI.rfb) {
b8dfb983 1413 UI.rfb.focusOnClick = false;
1d6ff4a3 1414 }
6d6f0db0 1415 },
fdf21468 1416
0e4808bf 1417 onblurVirtualKeyboard(event) {
6d6f0db0
SR
1418 document.getElementById('noVNC_keyboard_button')
1419 .classList.remove("noVNC_selected");
1d6ff4a3 1420 if (UI.rfb) {
b8dfb983 1421 UI.rfb.focusOnClick = true;
1d6ff4a3 1422 }
6d6f0db0 1423 },
ffcadf95 1424
0e4808bf 1425 keepVirtualKeyboard(event) {
2b5f94fa 1426 const input = document.getElementById('noVNC_keyboardinput');
ffcadf95 1427
6d6f0db0
SR
1428 // Only prevent focus change if the virtual keyboard is active
1429 if (document.activeElement != input) {
1430 return;
1431 }
ffcadf95 1432
6d6f0db0
SR
1433 // Only allow focus to move to other elements that need
1434 // focus to function properly
1435 if (event.target.form !== undefined) {
1436 switch (event.target.type) {
1437 case 'text':
1438 case 'email':
1439 case 'search':
1440 case 'password':
1441 case 'tel':
1442 case 'url':
1443 case 'textarea':
1444 case 'select-one':
1445 case 'select-multiple':
1446 return;
ffcadf95 1447 }
6d6f0db0 1448 }
ffcadf95 1449
1d6ff4a3 1450 event.preventDefault();
6d6f0db0 1451 },
bbbf42bb 1452
0e4808bf 1453 keyboardinputReset() {
2b5f94fa 1454 const kbi = document.getElementById('noVNC_keyboardinput');
6d6f0db0
SR
1455 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1456 UI.lastKeyboardinput = kbi.value;
1457 },
bbbf42bb 1458
0e4808bf 1459 keyEvent(keysym, code, down) {
867daa98
PO
1460 if (!UI.rfb) return;
1461
1462 UI.rfb.sendKey(keysym, code, down);
1463 },
1464
6d6f0db0
SR
1465 // When normal keyboard events are left uncought, use the input events from
1466 // the keyboardinput element instead and generate the corresponding key events.
1467 // This code is required since some browsers on Android are inconsistent in
1468 // sending keyCodes in the normal keyboard events when using on screen keyboards.
0e4808bf 1469 keyInput(event) {
3b8ec46f 1470
6d6f0db0 1471 if (!UI.rfb) return;
3b8ec46f 1472
2b5f94fa 1473 const newValue = event.target.value;
1138bdd4 1474
6d6f0db0
SR
1475 if (!UI.lastKeyboardinput) {
1476 UI.keyboardinputReset();
1477 }
2b5f94fa 1478 const oldValue = UI.lastKeyboardinput;
bbbf42bb 1479
2b5f94fa 1480 let newLen;
6d6f0db0
SR
1481 try {
1482 // Try to check caret position since whitespace at the end
1483 // will not be considered by value.length in some browsers
1484 newLen = Math.max(event.target.selectionStart, newValue.length);
1485 } catch (err) {
1486 // selectionStart is undefined in Google Chrome
1487 newLen = newValue.length;
1488 }
2b5f94fa 1489 const oldLen = oldValue.length;
6d6f0db0 1490
2b5f94fa
JD
1491 let inputs = newLen - oldLen;
1492 let backspaces = inputs < 0 ? -inputs : 0;
8e0f0088 1493
6d6f0db0
SR
1494 // Compare the old string with the new to account for
1495 // text-corrections or other input that modify existing text
2b5f94fa 1496 for (let i = 0; i < Math.min(oldLen, newLen); i++) {
6d6f0db0
SR
1497 if (newValue.charAt(i) != oldValue.charAt(i)) {
1498 inputs = newLen - i;
1499 backspaces = oldLen - i;
1500 break;
bbbf42bb 1501 }
6d6f0db0 1502 }
bbbf42bb 1503
6d6f0db0 1504 // Send the key events
2b5f94fa 1505 for (let i = 0; i < backspaces; i++) {
94f5cf05 1506 UI.rfb.sendKey(KeyTable.XK_BackSpace, "Backspace");
6d6f0db0 1507 }
2b5f94fa 1508 for (let i = newLen - inputs; i < newLen; i++) {
524d67f2 1509 UI.rfb.sendKey(keysyms.lookup(newValue.charCodeAt(i)));
6d6f0db0 1510 }
bbbf42bb 1511
6d6f0db0
SR
1512 // Control the text content length in the keyboardinput element
1513 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1514 UI.keyboardinputReset();
1515 } else if (newLen < 1) {
1516 // There always have to be some text in the keyboardinput
1517 // element with which backspace can interact.
1518 UI.keyboardinputReset();
1519 // This sometimes causes the keyboard to disappear for a second
1520 // but it is required for the android keyboard to recognize that
1521 // text has been added to the field
1522 event.target.blur();
1523 // This has to be ran outside of the input handler in order to work
1524 setTimeout(event.target.focus.bind(event.target), 0);
1525 } else {
1526 UI.lastKeyboardinput = newValue;
1527 }
1528 },
bbbf42bb 1529
4b30f9ce
SM
1530/* ------^-------
1531 * /KEYBOARD
1532 * ==============
1533 * EXTRA KEYS
1534 * ------v------*/
1535
0e4808bf 1536 openExtraKeys() {
6d6f0db0
SR
1537 UI.closeAllPanels();
1538 UI.openControlbar();
1539
1540 document.getElementById('noVNC_modifiers')
1541 .classList.add("noVNC_open");
1542 document.getElementById('noVNC_toggle_extra_keys_button')
1543 .classList.add("noVNC_selected");
1544 },
1545
0e4808bf 1546 closeExtraKeys() {
6d6f0db0
SR
1547 document.getElementById('noVNC_modifiers')
1548 .classList.remove("noVNC_open");
1549 document.getElementById('noVNC_toggle_extra_keys_button')
1550 .classList.remove("noVNC_selected");
1551 },
1552
0e4808bf 1553 toggleExtraKeys() {
35068204 1554 if (document.getElementById('noVNC_modifiers')
6d6f0db0
SR
1555 .classList.contains("noVNC_open")) {
1556 UI.closeExtraKeys();
1557 } else {
1558 UI.openExtraKeys();
1559 }
1560 },
1561
0e4808bf 1562 sendEsc() {
a5aa8e12 1563 UI.sendKey(KeyTable.XK_Escape, "Escape");
6d6f0db0
SR
1564 },
1565
0e4808bf 1566 sendTab() {
a5aa8e12 1567 UI.sendKey(KeyTable.XK_Tab, "Tab");
6d6f0db0
SR
1568 },
1569
0e4808bf 1570 toggleCtrl() {
2b5f94fa 1571 const btn = document.getElementById('noVNC_toggle_ctrl_button');
6d6f0db0 1572 if (btn.classList.contains("noVNC_selected")) {
a5aa8e12 1573 UI.sendKey(KeyTable.XK_Control_L, "ControlLeft", false);
6d6f0db0
SR
1574 btn.classList.remove("noVNC_selected");
1575 } else {
a5aa8e12 1576 UI.sendKey(KeyTable.XK_Control_L, "ControlLeft", true);
6d6f0db0 1577 btn.classList.add("noVNC_selected");
3e835a5d
SS
1578 }
1579 },
1580
1581 toggleWindows() {
1582 const btn = document.getElementById('noVNC_toggle_windows_button');
1583 if (btn.classList.contains("noVNC_selected")) {
a5aa8e12 1584 UI.sendKey(KeyTable.XK_Super_L, "MetaLeft", false);
3e835a5d
SS
1585 btn.classList.remove("noVNC_selected");
1586 } else {
a5aa8e12 1587 UI.sendKey(KeyTable.XK_Super_L, "MetaLeft", true);
3e835a5d 1588 btn.classList.add("noVNC_selected");
6d6f0db0
SR
1589 }
1590 },
1591
0e4808bf 1592 toggleAlt() {
2b5f94fa 1593 const btn = document.getElementById('noVNC_toggle_alt_button');
6d6f0db0 1594 if (btn.classList.contains("noVNC_selected")) {
a5aa8e12 1595 UI.sendKey(KeyTable.XK_Alt_L, "AltLeft", false);
6d6f0db0
SR
1596 btn.classList.remove("noVNC_selected");
1597 } else {
a5aa8e12 1598 UI.sendKey(KeyTable.XK_Alt_L, "AltLeft", true);
6d6f0db0
SR
1599 btn.classList.add("noVNC_selected");
1600 }
1601 },
bbbf42bb 1602
0e4808bf 1603 sendCtrlAltDel() {
6d6f0db0 1604 UI.rfb.sendCtrlAltDel();
a5aa8e12
SM
1605 // See below
1606 UI.rfb.focus();
1607 UI.idleControlbar();
1608 },
1609
1610 sendKey(keysym, code, down) {
1611 UI.rfb.sendKey(keysym, code, down);
94a01b0a
SM
1612
1613 // Move focus to the screen in order to be able to use the
1614 // keyboard right after these extra keys.
1615 // The exception is when a virtual keyboard is used, because
1616 // if we focus the screen the virtual keyboard would be closed.
1617 // In this case we focus our special virtual keyboard input
1618 // element instead.
1619 if (document.getElementById('noVNC_keyboard_button')
1620 .classList.contains("noVNC_selected")) {
1621 document.getElementById('noVNC_keyboardinput').focus();
1622 } else {
1623 UI.rfb.focus();
1624 }
a5aa8e12
SM
1625 // fade out the controlbar to highlight that
1626 // the focus has been moved to the screen
1627 UI.idleControlbar();
6d6f0db0 1628 },
bbbf42bb 1629
95dd6001 1630/* ------^-------
4b30f9ce 1631 * /EXTRA KEYS
95dd6001 1632 * ==============
1633 * MISC
1634 * ------v------*/
1635
0e4808bf 1636 setMouseButton(num) {
2b5f94fa 1637 const view_only = UI.rfb.viewOnly;
6d6f0db0 1638 if (UI.rfb && !view_only) {
747b4623 1639 UI.rfb.touchButton = num;
6d6f0db0 1640 }
95dd6001 1641
6786fd87 1642 const blist = [0, 1, 2, 4];
2b5f94fa
JD
1643 for (let b = 0; b < blist.length; b++) {
1644 const button = document.getElementById('noVNC_mouse_button' +
6d6f0db0
SR
1645 blist[b]);
1646 if (blist[b] === num && !view_only) {
1647 button.classList.remove("noVNC_hidden");
1648 } else {
1649 button.classList.add("noVNC_hidden");
ceb847b0 1650 }
6d6f0db0
SR
1651 }
1652 },
ef1e8bab 1653
0e4808bf 1654 updateViewOnly() {
e6a8eb15 1655 if (!UI.rfb) return;
747b4623 1656 UI.rfb.viewOnly = UI.getSetting('view_only');
f3763a01
SM
1657
1658 // Hide input related buttons in view only mode
1659 if (UI.rfb.viewOnly) {
1660 document.getElementById('noVNC_keyboard_button')
1661 .classList.add('noVNC_hidden');
1662 document.getElementById('noVNC_toggle_extra_keys_button')
1663 .classList.add('noVNC_hidden');
cffb42ee
SM
1664 document.getElementById('noVNC_mouse_button' + UI.rfb.touchButton)
1665 .classList.add('noVNC_hidden');
9253e178
NL
1666 document.getElementById('noVNC_clipboard_button')
1667 .classList.add('noVNC_hidden');
f3763a01
SM
1668 } else {
1669 document.getElementById('noVNC_keyboard_button')
1670 .classList.remove('noVNC_hidden');
1671 document.getElementById('noVNC_toggle_extra_keys_button')
1672 .classList.remove('noVNC_hidden');
cffb42ee
SM
1673 document.getElementById('noVNC_mouse_button' + UI.rfb.touchButton)
1674 .classList.remove('noVNC_hidden');
9253e178
NL
1675 document.getElementById('noVNC_clipboard_button')
1676 .classList.remove('noVNC_hidden');
f3763a01 1677 }
6d6f0db0
SR
1678 },
1679
4c38179d
AP
1680 updateShowDotCursor() {
1681 if (!UI.rfb) return;
1682 UI.rfb.showDotCursor = UI.getSetting('show_dot');
1683 },
1684
0e4808bf 1685 updateLogging() {
6d6f0db0
SR
1686 WebUtil.init_logging(UI.getSetting('logging'));
1687 },
1688
0e4808bf 1689 updateDesktopName(e) {
e89eef94 1690 UI.desktopName = e.detail.name;
6d6f0db0 1691 // Display the desktop name in the document title
c90245da 1692 document.title = e.detail.name + " - " + PAGE_TITLE;
6d6f0db0
SR
1693 },
1694
0e4808bf 1695 bell(e) {
6d6f0db0 1696 if (WebUtil.getConfigVar('bell', 'on') === 'on') {
2b5f94fa 1697 const promise = document.getElementById('noVNC_bell').play();
a342ed70
SM
1698 // The standards disagree on the return value here
1699 if (promise) {
651c23ec 1700 promise.catch((e) => {
d4fc89d8
SM
1701 if (e.name === "NotAllowedError") {
1702 // Ignore when the browser doesn't let us play audio.
1703 // It is common that the browsers require audio to be
1704 // initiated from a user action.
1705 } else {
1706 Log.Error("Unable to play bell: " + e);
1707 }
1708 });
a342ed70 1709 }
6d6f0db0
SR
1710 }
1711 },
63bf2ba5 1712
6d6f0db0 1713 //Helper to add options to dropdown.
0e4808bf 1714 addOption(selectbox, text, value) {
2b5f94fa 1715 const optn = document.createElement("OPTION");
6d6f0db0
SR
1716 optn.text = text;
1717 optn.value = value;
1718 selectbox.options.add(optn);
1719 },
bbbf42bb 1720
95dd6001 1721/* ------^-------
1722 * /MISC
1723 * ==============
1724 */
6d6f0db0
SR
1725};
1726
1727// Set up translations
7ded5178 1728const LINGUAS = ["cs", "de", "el", "es", "ja", "ko", "nl", "pl", "ru", "sv", "tr", "zh_CN", "zh_TW"];
6d6f0db0 1729l10n.setup(LINGUAS);
1c9b904d 1730if (l10n.language === "en" || l10n.dictionary !== undefined) {
6d6f0db0 1731 UI.prime();
1c9b904d
JD
1732} else {
1733 WebUtil.fetchJSON('app/locale/' + l10n.language + '.json')
1734 .then((translations) => { l10n.dictionary = translations; })
1735 .catch(err => Log.Error("Failed to load translations: " + err))
1736 .then(UI.prime);
6d6f0db0
SR
1737}
1738
3ae0bb09 1739export default UI;