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