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