]> git.proxmox.com Git - mirror_novnc.git/blame - app/ui.js
Merge branch 'babel' of https://github.com/CendioOssman/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';
ef64917a
SM
11import { isTouchDevice, isSafari, isIOS, isAndroid, dragThreshold }
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
15c7b7a6
JD
64 WebUtil.fetchJSON('../package.json')
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
319 document.getElementById("noVNC_password_button")
320 .addEventListener('click', UI.setPassword);
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
6d6f0db0
SR
443 document.getElementById('noVNC_password_dlg')
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 450 clearTimeout(UI.statusTimeout);
29475d77 451
6d6f0db0
SR
452 if (typeof status_type === 'undefined') {
453 status_type = 'normal';
454 }
4e471b5b 455
d623a029
SM
456 // Don't overwrite more severe visible statuses and never
457 // errors. Only shows the first error.
458 let visible_status_type = 'none';
459 if (statusElem.classList.contains("noVNC_open")) {
460 if (statusElem.classList.contains("noVNC_status_error")) {
461 visible_status_type = 'error';
462 } else if (statusElem.classList.contains("noVNC_status_warn")) {
463 visible_status_type = 'warn';
464 } else {
465 visible_status_type = 'normal';
466 }
467 }
468 if (visible_status_type === 'error' ||
469 (visible_status_type === 'warn' && status_type === 'normal')) {
470 return;
471 }
6d6f0db0
SR
472
473 switch (status_type) {
d623a029
SM
474 case 'error':
475 statusElem.classList.remove("noVNC_status_warn");
476 statusElem.classList.remove("noVNC_status_normal");
477 statusElem.classList.add("noVNC_status_error");
478 break;
6d6f0db0
SR
479 case 'warning':
480 case 'warn':
d623a029
SM
481 statusElem.classList.remove("noVNC_status_error");
482 statusElem.classList.remove("noVNC_status_normal");
6d6f0db0
SR
483 statusElem.classList.add("noVNC_status_warn");
484 break;
6d6f0db0
SR
485 case 'normal':
486 case 'info':
487 default:
d623a029
SM
488 statusElem.classList.remove("noVNC_status_error");
489 statusElem.classList.remove("noVNC_status_warn");
6d6f0db0
SR
490 statusElem.classList.add("noVNC_status_normal");
491 break;
492 }
8d7708c8 493
6d6f0db0
SR
494 statusElem.textContent = text;
495 statusElem.classList.add("noVNC_open");
8d7708c8 496
6d6f0db0
SR
497 // If no time was specified, show the status for 1.5 seconds
498 if (typeof time === 'undefined') {
499 time = 1500;
500 }
8d7708c8 501
6d6f0db0
SR
502 // Error messages do not timeout
503 if (status_type !== 'error') {
504 UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
505 }
506 },
507
0e4808bf 508 hideStatus() {
6d6f0db0
SR
509 clearTimeout(UI.statusTimeout);
510 document.getElementById('noVNC_status').classList.remove("noVNC_open");
511 },
512
0e4808bf 513 activateControlbar(event) {
6d6f0db0
SR
514 clearTimeout(UI.idleControlbarTimeout);
515 // We manipulate the anchor instead of the actual control
516 // bar in order to avoid creating new a stacking group
517 document.getElementById('noVNC_control_bar_anchor')
518 .classList.remove("noVNC_idle");
519 UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
520 },
521
0e4808bf 522 idleControlbar() {
6d6f0db0
SR
523 document.getElementById('noVNC_control_bar_anchor')
524 .classList.add("noVNC_idle");
525 },
526
0e4808bf 527 keepControlbar() {
6d6f0db0
SR
528 clearTimeout(UI.closeControlbarTimeout);
529 },
530
0e4808bf 531 openControlbar() {
6d6f0db0
SR
532 document.getElementById('noVNC_control_bar')
533 .classList.add("noVNC_open");
534 },
535
0e4808bf 536 closeControlbar() {
6d6f0db0
SR
537 UI.closeAllPanels();
538 document.getElementById('noVNC_control_bar')
539 .classList.remove("noVNC_open");
540 },
541
0e4808bf 542 toggleControlbar() {
6d6f0db0
SR
543 if (document.getElementById('noVNC_control_bar')
544 .classList.contains("noVNC_open")) {
545 UI.closeControlbar();
546 } else {
547 UI.openControlbar();
548 }
549 },
550
0e4808bf 551 toggleControlbarSide() {
06309160
SM
552 // Temporarily disable animation, if bar is displayed, to avoid weird
553 // movement. The transitionend-event will not fire when display=none.
2b5f94fa
JD
554 const bar = document.getElementById('noVNC_control_bar');
555 const barDisplayStyle = window.getComputedStyle(bar).display;
06309160
SM
556 if (barDisplayStyle !== 'none') {
557 bar.style.transitionDuration = '0s';
651c23ec 558 bar.addEventListener('transitionend', () => bar.style.transitionDuration = '');
06309160 559 }
6d6f0db0 560
2b5f94fa 561 const anchor = document.getElementById('noVNC_control_bar_anchor');
6d6f0db0
SR
562 if (anchor.classList.contains("noVNC_right")) {
563 WebUtil.writeSetting('controlbar_pos', 'left');
564 anchor.classList.remove("noVNC_right");
565 } else {
566 WebUtil.writeSetting('controlbar_pos', 'right');
567 anchor.classList.add("noVNC_right");
568 }
ca5c74ad 569
6d6f0db0
SR
570 // Consider this a movement of the handle
571 UI.controlbarDrag = true;
572 },
4e471b5b 573
0e4808bf 574 showControlbarHint(show) {
2b5f94fa 575 const hint = document.getElementById('noVNC_control_bar_hint');
bbc1648c
SM
576 if (show) {
577 hint.classList.add("noVNC_active");
578 } else {
579 hint.classList.remove("noVNC_active");
580 }
581 },
582
0e4808bf 583 dragControlbarHandle(e) {
6d6f0db0 584 if (!UI.controlbarGrabbed) return;
38323d4d 585
2b5f94fa 586 const ptr = getPointerEvent(e);
8ee432f1 587
2b5f94fa 588 const anchor = document.getElementById('noVNC_control_bar_anchor');
6d6f0db0 589 if (ptr.clientX < (window.innerWidth * 0.1)) {
cf348b78 590 if (anchor.classList.contains("noVNC_right")) {
6d6f0db0 591 UI.toggleControlbarSide();
8ee432f1 592 }
6d6f0db0
SR
593 } else if (ptr.clientX > (window.innerWidth * 0.9)) {
594 if (!anchor.classList.contains("noVNC_right")) {
595 UI.toggleControlbarSide();
04b399e2 596 }
6d6f0db0 597 }
04b399e2 598
6d6f0db0 599 if (!UI.controlbarDrag) {
2b5f94fa 600 const dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
04b399e2 601
6d6f0db0 602 if (dragDistance < dragThreshold) return;
04b399e2 603
6d6f0db0
SR
604 UI.controlbarDrag = true;
605 }
f75e4d3c 606
2b5f94fa 607 const eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
04b399e2 608
6d6f0db0 609 UI.moveControlbarHandle(eventY);
59cd99bc 610
6d6f0db0
SR
611 e.preventDefault();
612 e.stopPropagation();
613 UI.keepControlbar();
614 UI.activateControlbar();
615 },
04b399e2 616
6d6f0db0 617 // Move the handle but don't allow any position outside the bounds
0e4808bf 618 moveControlbarHandle(viewportRelativeY) {
2b5f94fa
JD
619 const handle = document.getElementById("noVNC_control_bar_handle");
620 const handleHeight = handle.getBoundingClientRect().height;
621 const controlbarBounds = document.getElementById("noVNC_control_bar")
6d6f0db0 622 .getBoundingClientRect();
2b5f94fa 623 const margin = 10;
04b399e2 624
6d6f0db0
SR
625 // These heights need to be non-zero for the below logic to work
626 if (handleHeight === 0 || controlbarBounds.height === 0) {
627 return;
628 }
04b399e2 629
2b5f94fa 630 let newY = viewportRelativeY;
04b399e2 631
6d6f0db0
SR
632 // Check if the coordinates are outside the control bar
633 if (newY < controlbarBounds.top + margin) {
634 // Force coordinates to be below the top of the control bar
635 newY = controlbarBounds.top + margin;
04b399e2 636
6d6f0db0
SR
637 } else if (newY > controlbarBounds.top +
638 controlbarBounds.height - handleHeight - margin) {
639 // Force coordinates to be above the bottom of the control bar
640 newY = controlbarBounds.top +
641 controlbarBounds.height - handleHeight - margin;
642 }
04b399e2 643
6d6f0db0
SR
644 // Corner case: control bar too small for stable position
645 if (controlbarBounds.height < (handleHeight + margin * 2)) {
646 newY = controlbarBounds.top +
647 (controlbarBounds.height - handleHeight) / 2;
648 }
04b399e2 649
6d6f0db0 650 // The transform needs coordinates that are relative to the parent
2b5f94fa 651 const parentRelativeY = newY - controlbarBounds.top;
6d6f0db0
SR
652 handle.style.transform = "translateY(" + parentRelativeY + "px)";
653 },
654
0e4808bf 655 updateControlbarHandle() {
6d6f0db0
SR
656 // Since the control bar is fixed on the viewport and not the page,
657 // the move function expects coordinates relative the the viewport.
2b5f94fa
JD
658 const handle = document.getElementById("noVNC_control_bar_handle");
659 const handleBounds = handle.getBoundingClientRect();
6d6f0db0
SR
660 UI.moveControlbarHandle(handleBounds.top);
661 },
662
0e4808bf 663 controlbarHandleMouseUp(e) {
6d6f0db0
SR
664 if ((e.type == "mouseup") && (e.button != 0)) return;
665
666 // mouseup and mousedown on the same place toggles the controlbar
667 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
668 UI.toggleControlbar();
04b399e2
SM
669 e.preventDefault();
670 e.stopPropagation();
de315d62
PO
671 UI.keepControlbar();
672 UI.activateControlbar();
6d6f0db0
SR
673 }
674 UI.controlbarGrabbed = false;
bbc1648c 675 UI.showControlbarHint(false);
6d6f0db0
SR
676 },
677
0e4808bf 678 controlbarHandleMouseDown(e) {
6d6f0db0
SR
679 if ((e.type == "mousedown") && (e.button != 0)) return;
680
2b5f94fa 681 const ptr = getPointerEvent(e);
6d6f0db0 682
2b5f94fa
JD
683 const handle = document.getElementById("noVNC_control_bar_handle");
684 const bounds = handle.getBoundingClientRect();
6d6f0db0 685
333ad45c
SM
686 // Touch events have implicit capture
687 if (e.type === "mousedown") {
688 setCapture(handle);
689 }
690
6d6f0db0
SR
691 UI.controlbarGrabbed = true;
692 UI.controlbarDrag = false;
693
bbc1648c
SM
694 UI.showControlbarHint(true);
695
6d6f0db0
SR
696 UI.controlbarMouseDownClientY = ptr.clientY;
697 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
698 e.preventDefault();
699 e.stopPropagation();
700 UI.keepControlbar();
701 UI.activateControlbar();
702 },
703
0e4808bf 704 toggleExpander(e) {
6d6f0db0
SR
705 if (this.classList.contains("noVNC_open")) {
706 this.classList.remove("noVNC_open");
707 } else {
708 this.classList.add("noVNC_open");
709 }
710 },
575f6983 711
95dd6001 712/* ------^-------
713 * /VISUAL
714 * ==============
715 * SETTINGS
716 * ------v------*/
717
6d6f0db0 718 // Initial page load read/initialization of settings
0e4808bf 719 initSetting(name, defVal) {
6d6f0db0 720 // Check Query string followed by cookie
2b5f94fa 721 let val = WebUtil.getConfigVar(name);
6d6f0db0
SR
722 if (val === null) {
723 val = WebUtil.readSetting(name, defVal);
724 }
8ad8f15c
AW
725 WebUtil.setSetting(name, val);
726 UI.updateSetting(name);
6d6f0db0
SR
727 return val;
728 },
bbbf42bb 729
11715f20 730 // Set the new value, update and disable form control setting
0e4808bf 731 forceSetting(name, val) {
11715f20
SM
732 WebUtil.setSetting(name, val);
733 UI.updateSetting(name);
734 UI.disableSetting(name);
735 },
736
6d6f0db0
SR
737 // Update cookie and form control setting. If value is not set, then
738 // updates from control to current cookie setting.
0e4808bf 739 updateSetting(name) {
bbbf42bb 740
6d6f0db0 741 // Update the settings control
2b5f94fa 742 let value = UI.getSetting(name);
bbbf42bb 743
2b5f94fa 744 const ctrl = document.getElementById('noVNC_setting_' + name);
6d6f0db0
SR
745 if (ctrl.type === 'checkbox') {
746 ctrl.checked = value;
bbbf42bb 747
6d6f0db0 748 } else if (typeof ctrl.options !== 'undefined') {
2b5f94fa 749 for (let i = 0; i < ctrl.options.length; i += 1) {
6d6f0db0
SR
750 if (ctrl.options[i].value === value) {
751 ctrl.selectedIndex = i;
752 break;
bbbf42bb 753 }
bbbf42bb 754 }
6d6f0db0
SR
755 } else {
756 /*Weird IE9 error leads to 'null' appearring
757 in textboxes instead of ''.*/
758 if (value === null) {
759 value = "";
bbbf42bb 760 }
6d6f0db0
SR
761 ctrl.value = value;
762 }
763 },
764
765 // Save control setting to cookie
0e4808bf 766 saveSetting(name) {
2b5f94fa
JD
767 const ctrl = document.getElementById('noVNC_setting_' + name);
768 let val;
6d6f0db0
SR
769 if (ctrl.type === 'checkbox') {
770 val = ctrl.checked;
771 } else if (typeof ctrl.options !== 'undefined') {
772 val = ctrl.options[ctrl.selectedIndex].value;
773 } else {
774 val = ctrl.value;
775 }
776 WebUtil.writeSetting(name, val);
777 //Log.Debug("Setting saved '" + name + "=" + val + "'");
778 return val;
779 },
780
781 // Read form control compatible setting from cookie
0e4808bf 782 getSetting(name) {
2b5f94fa
JD
783 const ctrl = document.getElementById('noVNC_setting_' + name);
784 let val = WebUtil.readSetting(name);
6d6f0db0 785 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
942a3127 786 if (val.toString().toLowerCase() in {'0': 1, 'no': 1, 'false': 1}) {
6d6f0db0
SR
787 val = false;
788 } else {
789 val = true;
bbbf42bb 790 }
6d6f0db0
SR
791 }
792 return val;
793 },
794
795 // These helpers compensate for the lack of parent-selectors and
796 // previous-sibling-selectors in CSS which are needed when we want to
797 // disable the labels that belong to disabled input elements.
0e4808bf 798 disableSetting(name) {
2b5f94fa 799 const ctrl = document.getElementById('noVNC_setting_' + name);
6d6f0db0
SR
800 ctrl.disabled = true;
801 ctrl.label.classList.add('noVNC_disabled');
802 },
803
0e4808bf 804 enableSetting(name) {
2b5f94fa 805 const ctrl = document.getElementById('noVNC_setting_' + name);
6d6f0db0
SR
806 ctrl.disabled = false;
807 ctrl.label.classList.remove('noVNC_disabled');
808 },
24584cca 809
ed8cbe4e
PO
810/* ------^-------
811 * /SETTINGS
812 * ==============
813 * PANELS
814 * ------v------*/
815
0e4808bf 816 closeAllPanels() {
6d6f0db0 817 UI.closeSettingsPanel();
cd523e8f 818 UI.closePowerPanel();
6d6f0db0
SR
819 UI.closeClipboardPanel();
820 UI.closeExtraKeys();
821 },
ed8cbe4e
PO
822
823/* ------^-------
824 * /PANELS
825 * ==============
826 * SETTINGS (panel)
827 * ------v------*/
828
0e4808bf 829 openSettingsPanel() {
6d6f0db0
SR
830 UI.closeAllPanels();
831 UI.openControlbar();
832
833 // Refresh UI elements from saved cookies
834 UI.updateSetting('encrypt');
a49ade5f 835 UI.updateSetting('view_clip');
6d6f0db0
SR
836 UI.updateSetting('resize');
837 UI.updateSetting('shared');
838 UI.updateSetting('view_only');
839 UI.updateSetting('path');
840 UI.updateSetting('repeaterID');
841 UI.updateSetting('logging');
842 UI.updateSetting('reconnect');
843 UI.updateSetting('reconnect_delay');
844
845 document.getElementById('noVNC_settings')
846 .classList.add("noVNC_open");
847 document.getElementById('noVNC_settings_button')
848 .classList.add("noVNC_selected");
849 },
850
0e4808bf 851 closeSettingsPanel() {
6d6f0db0
SR
852 document.getElementById('noVNC_settings')
853 .classList.remove("noVNC_open");
854 document.getElementById('noVNC_settings_button')
855 .classList.remove("noVNC_selected");
856 },
857
0e4808bf 858 toggleSettingsPanel() {
6d6f0db0
SR
859 if (document.getElementById('noVNC_settings')
860 .classList.contains("noVNC_open")) {
861 UI.closeSettingsPanel();
862 } else {
863 UI.openSettingsPanel();
864 }
865 },
bbbf42bb 866
95dd6001 867/* ------^-------
868 * /SETTINGS
869 * ==============
cd523e8f 870 * POWER
95dd6001 871 * ------v------*/
872
0e4808bf 873 openPowerPanel() {
6d6f0db0
SR
874 UI.closeAllPanels();
875 UI.openControlbar();
876
cd523e8f 877 document.getElementById('noVNC_power')
6d6f0db0 878 .classList.add("noVNC_open");
cd523e8f 879 document.getElementById('noVNC_power_button')
6d6f0db0
SR
880 .classList.add("noVNC_selected");
881 },
882
0e4808bf 883 closePowerPanel() {
cd523e8f 884 document.getElementById('noVNC_power')
6d6f0db0 885 .classList.remove("noVNC_open");
cd523e8f 886 document.getElementById('noVNC_power_button')
6d6f0db0
SR
887 .classList.remove("noVNC_selected");
888 },
889
0e4808bf 890 togglePowerPanel() {
cd523e8f 891 if (document.getElementById('noVNC_power')
6d6f0db0 892 .classList.contains("noVNC_open")) {
cd523e8f 893 UI.closePowerPanel();
6d6f0db0 894 } else {
cd523e8f 895 UI.openPowerPanel();
6d6f0db0
SR
896 }
897 },
ed8cbe4e 898
cd523e8f 899 // Disable/enable power button
0e4808bf 900 updatePowerButton() {
cd523e8f 901 if (UI.connected &&
747b4623
PO
902 UI.rfb.capabilities.power &&
903 !UI.rfb.viewOnly) {
cd523e8f 904 document.getElementById('noVNC_power_button')
6d6f0db0
SR
905 .classList.remove("noVNC_hidden");
906 } else {
cd523e8f 907 document.getElementById('noVNC_power_button')
6d6f0db0 908 .classList.add("noVNC_hidden");
cd523e8f
PO
909 // Close power panel if open
910 UI.closePowerPanel();
6d6f0db0
SR
911 }
912 },
bbbf42bb 913
95dd6001 914/* ------^-------
cd523e8f 915 * /POWER
95dd6001 916 * ==============
917 * CLIPBOARD
918 * ------v------*/
f8b399d7 919
0e4808bf 920 openClipboardPanel() {
6d6f0db0
SR
921 UI.closeAllPanels();
922 UI.openControlbar();
923
924 document.getElementById('noVNC_clipboard')
925 .classList.add("noVNC_open");
926 document.getElementById('noVNC_clipboard_button')
927 .classList.add("noVNC_selected");
928 },
929
0e4808bf 930 closeClipboardPanel() {
6d6f0db0
SR
931 document.getElementById('noVNC_clipboard')
932 .classList.remove("noVNC_open");
933 document.getElementById('noVNC_clipboard_button')
934 .classList.remove("noVNC_selected");
935 },
936
0e4808bf 937 toggleClipboardPanel() {
6d6f0db0
SR
938 if (document.getElementById('noVNC_clipboard')
939 .classList.contains("noVNC_open")) {
940 UI.closeClipboardPanel();
941 } else {
942 UI.openClipboardPanel();
943 }
944 },
945
0e4808bf 946 clipboardReceive(e) {
6786fd87 947 Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0, 40) + "...");
e89eef94 948 document.getElementById('noVNC_clipboard_text').value = e.detail.text;
6d6f0db0
SR
949 Log.Debug("<< UI.clipboardReceive");
950 },
951
0e4808bf 952 clipboardClear() {
6d6f0db0
SR
953 document.getElementById('noVNC_clipboard_text').value = "";
954 UI.rfb.clipboardPasteFrom("");
955 },
956
0e4808bf 957 clipboardSend() {
2b5f94fa 958 const text = document.getElementById('noVNC_clipboard_text').value;
6786fd87 959 Log.Debug(">> UI.clipboardSend: " + text.substr(0, 40) + "...");
6d6f0db0
SR
960 UI.rfb.clipboardPasteFrom(text);
961 Log.Debug("<< UI.clipboardSend");
962 },
95dd6001 963
964/* ------^-------
965 * /CLIPBOARD
966 * ==============
967 * CONNECTION
968 * ------v------*/
969
0e4808bf 970 openConnectPanel() {
6d6f0db0
SR
971 document.getElementById('noVNC_connect_dlg')
972 .classList.add("noVNC_open");
973 },
b3c932c3 974
0e4808bf 975 closeConnectPanel() {
6d6f0db0
SR
976 document.getElementById('noVNC_connect_dlg')
977 .classList.remove("noVNC_open");
978 },
b3c932c3 979
0e4808bf 980 connect(event, password) {
a4822c3a
SM
981
982 // Ignore when rfb already exists
983 if (typeof UI.rfb !== 'undefined') {
984 return;
985 }
986
2b5f94fa
JD
987 const host = UI.getSetting('host');
988 const port = UI.getSetting('port');
989 const path = UI.getSetting('path');
c55f05f6 990
6d6f0db0
SR
991 if (typeof password === 'undefined') {
992 password = WebUtil.getConfigVar('password');
2e735160 993 UI.reconnect_password = password;
6d6f0db0 994 }
044d54ed 995
6d6f0db0
SR
996 if (password === null) {
997 password = undefined;
998 }
512d3605 999
d623a029
SM
1000 UI.hideStatus();
1001
d593483e 1002 if (!host) {
8317524c
SM
1003 Log.Error("Can't connect when host is: " + host);
1004 UI.showStatus(_("Must set host"), 'error');
6d6f0db0
SR
1005 return;
1006 }
53fc7392 1007
6d6f0db0
SR
1008 UI.closeAllPanels();
1009 UI.closeConnectPanel();
4102b71c 1010
f3763a01
SM
1011 UI.updateVisualState('connecting');
1012
2b5f94fa 1013 let url;
5b4e5d01
PO
1014
1015 url = UI.getSetting('encrypt') ? 'wss' : 'ws';
1016
1017 url += '://' + host;
35068204 1018 if (port) {
5b4e5d01
PO
1019 url += ':' + port;
1020 }
1021 url += '/' + path;
1022
9b84f516 1023 UI.rfb = new RFB(document.getElementById('noVNC_container'), url,
2f4516f2
PO
1024 { shared: UI.getSetting('shared'),
1025 repeaterID: UI.getSetting('repeaterID'),
1026 credentials: { password: password } });
ee5cae9f 1027 UI.rfb.addEventListener("connect", UI.connectFinished);
e89eef94
PO
1028 UI.rfb.addEventListener("disconnect", UI.disconnectFinished);
1029 UI.rfb.addEventListener("credentialsrequired", UI.credentials);
d472f3f1 1030 UI.rfb.addEventListener("securityfailure", UI.securityFailed);
651c23ec 1031 UI.rfb.addEventListener("capabilities", UI.updatePowerButton);
e89eef94
PO
1032 UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
1033 UI.rfb.addEventListener("bell", UI.bell);
e89eef94 1034 UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
9b84f516
PO
1035 UI.rfb.clipViewport = UI.getSetting('view_clip');
1036 UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
1037 UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
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
6d6f0db0
SR
1155 document.getElementById('noVNC_password_dlg')
1156 .classList.add('noVNC_open');
7d714b15 1157
651c23ec
JD
1158 setTimeout(() => document
1159 .getElementById('noVNC_password_input').focus(), 100);
7d714b15 1160
8317524c
SM
1161 Log.Warn("Server asked for a password");
1162 UI.showStatus(_("Password is required"), "warning");
6d6f0db0
SR
1163 },
1164
0e4808bf 1165 setPassword(e) {
a80b5fda
PO
1166 // Prevent actually submitting the form
1167 e.preventDefault();
1168
2b5f94fa
JD
1169 const inputElem = document.getElementById('noVNC_password_input');
1170 const password = inputElem.value;
c23665dd
SM
1171 // Clear the input after reading the password
1172 inputElem.value = "";
430f00d6 1173 UI.rfb.sendCredentials({ password: password });
6d6f0db0
SR
1174 UI.reconnect_password = password;
1175 document.getElementById('noVNC_password_dlg')
1176 .classList.remove('noVNC_open');
6d6f0db0 1177 },
58ded70d 1178
95dd6001 1179/* ------^-------
7d714b15 1180 * /PASSWORD
95dd6001 1181 * ==============
1182 * FULLSCREEN
1183 * ------v------*/
1184
0e4808bf 1185 toggleFullscreen() {
32e08195
SM
1186 if (document.fullscreenElement || // alternative standard method
1187 document.mozFullScreenElement || // currently working methods
1188 document.webkitFullscreenElement ||
1189 document.msFullscreenElement) {
1190 if (document.exitFullscreen) {
1191 document.exitFullscreen();
1192 } else if (document.mozCancelFullScreen) {
1193 document.mozCancelFullScreen();
1194 } else if (document.webkitExitFullscreen) {
1195 document.webkitExitFullscreen();
1196 } else if (document.msExitFullscreen) {
1197 document.msExitFullscreen();
6d6f0db0
SR
1198 }
1199 } else {
32e08195
SM
1200 if (document.documentElement.requestFullscreen) {
1201 document.documentElement.requestFullscreen();
1202 } else if (document.documentElement.mozRequestFullScreen) {
1203 document.documentElement.mozRequestFullScreen();
1204 } else if (document.documentElement.webkitRequestFullscreen) {
1205 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1206 } else if (document.body.msRequestFullscreen) {
1207 document.body.msRequestFullscreen();
7d1dc09a 1208 }
6d6f0db0 1209 }
6d6f0db0
SR
1210 UI.updateFullscreenButton();
1211 },
1212
0e4808bf 1213 updateFullscreenButton() {
6d6f0db0
SR
1214 if (document.fullscreenElement || // alternative standard method
1215 document.mozFullScreenElement || // currently working methods
1216 document.webkitFullscreenElement ||
1217 document.msFullscreenElement ) {
1218 document.getElementById('noVNC_fullscreen_button')
1219 .classList.add("noVNC_selected");
1220 } else {
1221 document.getElementById('noVNC_fullscreen_button')
1222 .classList.remove("noVNC_selected");
1223 }
1224 },
7d1dc09a 1225
95dd6001 1226/* ------^-------
1227 * /FULLSCREEN
1228 * ==============
1229 * RESIZE
1230 * ------v------*/
777cb7a0 1231
6d6f0db0 1232 // Apply remote resizing or local scaling
0e4808bf 1233 applyResizeMode() {
6d6f0db0 1234 if (!UI.rfb) return;
58ded70d 1235
9b84f516
PO
1236 UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
1237 UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
6d6f0db0 1238 },
bbbf42bb 1239
95dd6001 1240/* ------^-------
1241 * /RESIZE
1242 * ==============
a49ade5f 1243 * VIEW CLIPPING
95dd6001 1244 * ------v------*/
1245
4ddcc753
SM
1246 // Update viewport clipping property for the connection. The normal
1247 // case is to get the value from the setting. There are special cases
1248 // for when the viewport is scaled or when a touch device is used.
0e4808bf 1249 updateViewClip() {
6d6f0db0
SR
1250 if (!UI.rfb) return;
1251
4ddcc753 1252 const scaling = UI.getSetting('resize') === 'scale';
6d6f0db0 1253
4ddcc753
SM
1254 if (scaling) {
1255 // Can't be clipping if viewport is scaled to fit
1256 UI.forceSetting('view_clip', false);
1257 UI.rfb.clipViewport = false;
ef64917a
SM
1258 } else if (isIOS() || isAndroid()) {
1259 // iOS and Android usually have shit scrollbars
4ddcc753
SM
1260 UI.forceSetting('view_clip', true);
1261 UI.rfb.clipViewport = true;
1262 } else {
1263 UI.enableSetting('view_clip');
1264 UI.rfb.clipViewport = UI.getSetting('view_clip');
6d6f0db0 1265 }
8e0f0088 1266
6d6f0db0
SR
1267 // Changing the viewport may change the state of
1268 // the dragging button
1269 UI.updateViewDrag();
1270 },
1271
95dd6001 1272/* ------^-------
a49ade5f 1273 * /VIEW CLIPPING
95dd6001 1274 * ==============
1275 * VIEWDRAG
1276 * ------v------*/
1277
0e4808bf 1278 toggleViewDrag() {
6d6f0db0 1279 if (!UI.rfb) return;
bbbf42bb 1280
2bbd15cc 1281 UI.rfb.dragViewport = !UI.rfb.dragViewport;
6d6f0db0
SR
1282 UI.updateViewDrag();
1283 },
f0d9ab96 1284
0e4808bf 1285 updateViewDrag() {
6d6f0db0 1286 if (!UI.connected) return;
6244e383 1287
2b5f94fa 1288 const viewDragButton = document.getElementById('noVNC_view_drag_button');
29a0e6a8 1289
9b84f516
PO
1290 if (!UI.rfb.clipViewport && UI.rfb.dragViewport) {
1291 // We are no longer clipping the viewport. Make sure
1292 // viewport drag isn't active when it can't be used.
0460e5fd 1293 UI.rfb.dragViewport = false;
6d6f0db0
SR
1294 }
1295
0460e5fd 1296 if (UI.rfb.dragViewport) {
6d6f0db0
SR
1297 viewDragButton.classList.add("noVNC_selected");
1298 } else {
1299 viewDragButton.classList.remove("noVNC_selected");
1300 }
1301
f2d42dc3 1302 if (UI.rfb.clipViewport) {
6d6f0db0 1303 viewDragButton.classList.remove("noVNC_hidden");
6d6f0db0 1304 } else {
f2d42dc3 1305 viewDragButton.classList.add("noVNC_hidden");
6d6f0db0
SR
1306 }
1307 },
f8b399d7 1308
95dd6001 1309/* ------^-------
1310 * /VIEWDRAG
1311 * ==============
1312 * KEYBOARD
1313 * ------v------*/
fdf21468 1314
0e4808bf 1315 showVirtualKeyboard() {
6d6f0db0 1316 if (!isTouchDevice) return;
4b30f9ce 1317
2b5f94fa 1318 const input = document.getElementById('noVNC_keyboardinput');
4b30f9ce 1319
6d6f0db0 1320 if (document.activeElement == input) return;
4b30f9ce 1321
6d6f0db0 1322 input.focus();
4b30f9ce 1323
6d6f0db0 1324 try {
2b5f94fa 1325 const l = input.value.length;
6d6f0db0
SR
1326 // Move the caret to the end
1327 input.setSelectionRange(l, l);
8727f598 1328 } catch (err) {
4a16dc51 1329 // setSelectionRange is undefined in Google Chrome
8727f598 1330 }
6d6f0db0 1331 },
4b30f9ce 1332
0e4808bf 1333 hideVirtualKeyboard() {
6d6f0db0 1334 if (!isTouchDevice) return;
4b30f9ce 1335
2b5f94fa 1336 const input = document.getElementById('noVNC_keyboardinput');
4b30f9ce 1337
6d6f0db0 1338 if (document.activeElement != input) return;
4b30f9ce 1339
6d6f0db0
SR
1340 input.blur();
1341 },
4b30f9ce 1342
0e4808bf 1343 toggleVirtualKeyboard() {
6d6f0db0
SR
1344 if (document.getElementById('noVNC_keyboard_button')
1345 .classList.contains("noVNC_selected")) {
1346 UI.hideVirtualKeyboard();
1347 } else {
1348 UI.showVirtualKeyboard();
1349 }
1350 },
bbbf42bb 1351
0e4808bf 1352 onfocusVirtualKeyboard(event) {
6d6f0db0
SR
1353 document.getElementById('noVNC_keyboard_button')
1354 .classList.add("noVNC_selected");
1d6ff4a3 1355 if (UI.rfb) {
b8dfb983 1356 UI.rfb.focusOnClick = false;
1d6ff4a3 1357 }
6d6f0db0 1358 },
fdf21468 1359
0e4808bf 1360 onblurVirtualKeyboard(event) {
6d6f0db0
SR
1361 document.getElementById('noVNC_keyboard_button')
1362 .classList.remove("noVNC_selected");
1d6ff4a3 1363 if (UI.rfb) {
b8dfb983 1364 UI.rfb.focusOnClick = true;
1d6ff4a3 1365 }
6d6f0db0 1366 },
ffcadf95 1367
0e4808bf 1368 keepVirtualKeyboard(event) {
2b5f94fa 1369 const input = document.getElementById('noVNC_keyboardinput');
ffcadf95 1370
6d6f0db0
SR
1371 // Only prevent focus change if the virtual keyboard is active
1372 if (document.activeElement != input) {
1373 return;
1374 }
ffcadf95 1375
6d6f0db0
SR
1376 // Only allow focus to move to other elements that need
1377 // focus to function properly
1378 if (event.target.form !== undefined) {
1379 switch (event.target.type) {
1380 case 'text':
1381 case 'email':
1382 case 'search':
1383 case 'password':
1384 case 'tel':
1385 case 'url':
1386 case 'textarea':
1387 case 'select-one':
1388 case 'select-multiple':
1389 return;
ffcadf95 1390 }
6d6f0db0 1391 }
ffcadf95 1392
1d6ff4a3 1393 event.preventDefault();
6d6f0db0 1394 },
bbbf42bb 1395
0e4808bf 1396 keyboardinputReset() {
2b5f94fa 1397 const kbi = document.getElementById('noVNC_keyboardinput');
6d6f0db0
SR
1398 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1399 UI.lastKeyboardinput = kbi.value;
1400 },
bbbf42bb 1401
0e4808bf 1402 keyEvent(keysym, code, down) {
867daa98
PO
1403 if (!UI.rfb) return;
1404
1405 UI.rfb.sendKey(keysym, code, down);
1406 },
1407
6d6f0db0
SR
1408 // When normal keyboard events are left uncought, use the input events from
1409 // the keyboardinput element instead and generate the corresponding key events.
1410 // This code is required since some browsers on Android are inconsistent in
1411 // sending keyCodes in the normal keyboard events when using on screen keyboards.
0e4808bf 1412 keyInput(event) {
3b8ec46f 1413
6d6f0db0 1414 if (!UI.rfb) return;
3b8ec46f 1415
2b5f94fa 1416 const newValue = event.target.value;
1138bdd4 1417
6d6f0db0
SR
1418 if (!UI.lastKeyboardinput) {
1419 UI.keyboardinputReset();
1420 }
2b5f94fa 1421 const oldValue = UI.lastKeyboardinput;
bbbf42bb 1422
2b5f94fa 1423 let newLen;
6d6f0db0
SR
1424 try {
1425 // Try to check caret position since whitespace at the end
1426 // will not be considered by value.length in some browsers
1427 newLen = Math.max(event.target.selectionStart, newValue.length);
1428 } catch (err) {
1429 // selectionStart is undefined in Google Chrome
1430 newLen = newValue.length;
1431 }
2b5f94fa 1432 const oldLen = oldValue.length;
6d6f0db0 1433
2b5f94fa
JD
1434 let inputs = newLen - oldLen;
1435 let backspaces = inputs < 0 ? -inputs : 0;
8e0f0088 1436
6d6f0db0
SR
1437 // Compare the old string with the new to account for
1438 // text-corrections or other input that modify existing text
2b5f94fa 1439 for (let i = 0; i < Math.min(oldLen, newLen); i++) {
6d6f0db0
SR
1440 if (newValue.charAt(i) != oldValue.charAt(i)) {
1441 inputs = newLen - i;
1442 backspaces = oldLen - i;
1443 break;
bbbf42bb 1444 }
6d6f0db0 1445 }
bbbf42bb 1446
6d6f0db0 1447 // Send the key events
2b5f94fa 1448 for (let i = 0; i < backspaces; i++) {
94f5cf05 1449 UI.rfb.sendKey(KeyTable.XK_BackSpace, "Backspace");
6d6f0db0 1450 }
2b5f94fa 1451 for (let i = newLen - inputs; i < newLen; i++) {
524d67f2 1452 UI.rfb.sendKey(keysyms.lookup(newValue.charCodeAt(i)));
6d6f0db0 1453 }
bbbf42bb 1454
6d6f0db0
SR
1455 // Control the text content length in the keyboardinput element
1456 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1457 UI.keyboardinputReset();
1458 } else if (newLen < 1) {
1459 // There always have to be some text in the keyboardinput
1460 // element with which backspace can interact.
1461 UI.keyboardinputReset();
1462 // This sometimes causes the keyboard to disappear for a second
1463 // but it is required for the android keyboard to recognize that
1464 // text has been added to the field
1465 event.target.blur();
1466 // This has to be ran outside of the input handler in order to work
1467 setTimeout(event.target.focus.bind(event.target), 0);
1468 } else {
1469 UI.lastKeyboardinput = newValue;
1470 }
1471 },
bbbf42bb 1472
4b30f9ce
SM
1473/* ------^-------
1474 * /KEYBOARD
1475 * ==============
1476 * EXTRA KEYS
1477 * ------v------*/
1478
0e4808bf 1479 openExtraKeys() {
6d6f0db0
SR
1480 UI.closeAllPanels();
1481 UI.openControlbar();
1482
1483 document.getElementById('noVNC_modifiers')
1484 .classList.add("noVNC_open");
1485 document.getElementById('noVNC_toggle_extra_keys_button')
1486 .classList.add("noVNC_selected");
1487 },
1488
0e4808bf 1489 closeExtraKeys() {
6d6f0db0
SR
1490 document.getElementById('noVNC_modifiers')
1491 .classList.remove("noVNC_open");
1492 document.getElementById('noVNC_toggle_extra_keys_button')
1493 .classList.remove("noVNC_selected");
1494 },
1495
0e4808bf 1496 toggleExtraKeys() {
35068204 1497 if (document.getElementById('noVNC_modifiers')
6d6f0db0
SR
1498 .classList.contains("noVNC_open")) {
1499 UI.closeExtraKeys();
1500 } else {
1501 UI.openExtraKeys();
1502 }
1503 },
1504
0e4808bf 1505 sendEsc() {
a5aa8e12 1506 UI.sendKey(KeyTable.XK_Escape, "Escape");
6d6f0db0
SR
1507 },
1508
0e4808bf 1509 sendTab() {
a5aa8e12 1510 UI.sendKey(KeyTable.XK_Tab, "Tab");
6d6f0db0
SR
1511 },
1512
0e4808bf 1513 toggleCtrl() {
2b5f94fa 1514 const btn = document.getElementById('noVNC_toggle_ctrl_button');
6d6f0db0 1515 if (btn.classList.contains("noVNC_selected")) {
a5aa8e12 1516 UI.sendKey(KeyTable.XK_Control_L, "ControlLeft", false);
6d6f0db0
SR
1517 btn.classList.remove("noVNC_selected");
1518 } else {
a5aa8e12 1519 UI.sendKey(KeyTable.XK_Control_L, "ControlLeft", true);
6d6f0db0 1520 btn.classList.add("noVNC_selected");
3e835a5d
SS
1521 }
1522 },
1523
1524 toggleWindows() {
1525 const btn = document.getElementById('noVNC_toggle_windows_button');
1526 if (btn.classList.contains("noVNC_selected")) {
a5aa8e12 1527 UI.sendKey(KeyTable.XK_Super_L, "MetaLeft", false);
3e835a5d
SS
1528 btn.classList.remove("noVNC_selected");
1529 } else {
a5aa8e12 1530 UI.sendKey(KeyTable.XK_Super_L, "MetaLeft", true);
3e835a5d 1531 btn.classList.add("noVNC_selected");
6d6f0db0
SR
1532 }
1533 },
1534
0e4808bf 1535 toggleAlt() {
2b5f94fa 1536 const btn = document.getElementById('noVNC_toggle_alt_button');
6d6f0db0 1537 if (btn.classList.contains("noVNC_selected")) {
a5aa8e12 1538 UI.sendKey(KeyTable.XK_Alt_L, "AltLeft", false);
6d6f0db0
SR
1539 btn.classList.remove("noVNC_selected");
1540 } else {
a5aa8e12 1541 UI.sendKey(KeyTable.XK_Alt_L, "AltLeft", true);
6d6f0db0
SR
1542 btn.classList.add("noVNC_selected");
1543 }
1544 },
bbbf42bb 1545
0e4808bf 1546 sendCtrlAltDel() {
6d6f0db0 1547 UI.rfb.sendCtrlAltDel();
a5aa8e12
SM
1548 // See below
1549 UI.rfb.focus();
1550 UI.idleControlbar();
1551 },
1552
1553 sendKey(keysym, code, down) {
1554 UI.rfb.sendKey(keysym, code, down);
94a01b0a
SM
1555
1556 // Move focus to the screen in order to be able to use the
1557 // keyboard right after these extra keys.
1558 // The exception is when a virtual keyboard is used, because
1559 // if we focus the screen the virtual keyboard would be closed.
1560 // In this case we focus our special virtual keyboard input
1561 // element instead.
1562 if (document.getElementById('noVNC_keyboard_button')
1563 .classList.contains("noVNC_selected")) {
1564 document.getElementById('noVNC_keyboardinput').focus();
1565 } else {
1566 UI.rfb.focus();
1567 }
a5aa8e12
SM
1568 // fade out the controlbar to highlight that
1569 // the focus has been moved to the screen
1570 UI.idleControlbar();
6d6f0db0 1571 },
bbbf42bb 1572
95dd6001 1573/* ------^-------
4b30f9ce 1574 * /EXTRA KEYS
95dd6001 1575 * ==============
1576 * MISC
1577 * ------v------*/
1578
0e4808bf 1579 setMouseButton(num) {
2b5f94fa 1580 const view_only = UI.rfb.viewOnly;
6d6f0db0 1581 if (UI.rfb && !view_only) {
747b4623 1582 UI.rfb.touchButton = num;
6d6f0db0 1583 }
95dd6001 1584
6786fd87 1585 const blist = [0, 1, 2, 4];
2b5f94fa
JD
1586 for (let b = 0; b < blist.length; b++) {
1587 const button = document.getElementById('noVNC_mouse_button' +
6d6f0db0
SR
1588 blist[b]);
1589 if (blist[b] === num && !view_only) {
1590 button.classList.remove("noVNC_hidden");
1591 } else {
1592 button.classList.add("noVNC_hidden");
ceb847b0 1593 }
6d6f0db0
SR
1594 }
1595 },
ef1e8bab 1596
0e4808bf 1597 updateViewOnly() {
e6a8eb15 1598 if (!UI.rfb) return;
747b4623 1599 UI.rfb.viewOnly = UI.getSetting('view_only');
f3763a01
SM
1600
1601 // Hide input related buttons in view only mode
1602 if (UI.rfb.viewOnly) {
1603 document.getElementById('noVNC_keyboard_button')
1604 .classList.add('noVNC_hidden');
1605 document.getElementById('noVNC_toggle_extra_keys_button')
1606 .classList.add('noVNC_hidden');
cffb42ee
SM
1607 document.getElementById('noVNC_mouse_button' + UI.rfb.touchButton)
1608 .classList.add('noVNC_hidden');
f3763a01
SM
1609 } else {
1610 document.getElementById('noVNC_keyboard_button')
1611 .classList.remove('noVNC_hidden');
1612 document.getElementById('noVNC_toggle_extra_keys_button')
1613 .classList.remove('noVNC_hidden');
cffb42ee
SM
1614 document.getElementById('noVNC_mouse_button' + UI.rfb.touchButton)
1615 .classList.remove('noVNC_hidden');
f3763a01 1616 }
6d6f0db0
SR
1617 },
1618
4c38179d
AP
1619 updateShowDotCursor() {
1620 if (!UI.rfb) return;
1621 UI.rfb.showDotCursor = UI.getSetting('show_dot');
1622 },
1623
0e4808bf 1624 updateLogging() {
6d6f0db0
SR
1625 WebUtil.init_logging(UI.getSetting('logging'));
1626 },
1627
0e4808bf 1628 updateDesktopName(e) {
e89eef94 1629 UI.desktopName = e.detail.name;
6d6f0db0 1630 // Display the desktop name in the document title
c90245da 1631 document.title = e.detail.name + " - " + PAGE_TITLE;
6d6f0db0
SR
1632 },
1633
0e4808bf 1634 bell(e) {
6d6f0db0 1635 if (WebUtil.getConfigVar('bell', 'on') === 'on') {
2b5f94fa 1636 const promise = document.getElementById('noVNC_bell').play();
a342ed70
SM
1637 // The standards disagree on the return value here
1638 if (promise) {
651c23ec 1639 promise.catch((e) => {
d4fc89d8
SM
1640 if (e.name === "NotAllowedError") {
1641 // Ignore when the browser doesn't let us play audio.
1642 // It is common that the browsers require audio to be
1643 // initiated from a user action.
1644 } else {
1645 Log.Error("Unable to play bell: " + e);
1646 }
1647 });
a342ed70 1648 }
6d6f0db0
SR
1649 }
1650 },
63bf2ba5 1651
6d6f0db0 1652 //Helper to add options to dropdown.
0e4808bf 1653 addOption(selectbox, text, value) {
2b5f94fa 1654 const optn = document.createElement("OPTION");
6d6f0db0
SR
1655 optn.text = text;
1656 optn.value = value;
1657 selectbox.options.add(optn);
1658 },
bbbf42bb 1659
95dd6001 1660/* ------^-------
1661 * /MISC
1662 * ==============
1663 */
6d6f0db0
SR
1664};
1665
1666// Set up translations
7ded5178 1667const LINGUAS = ["cs", "de", "el", "es", "ja", "ko", "nl", "pl", "ru", "sv", "tr", "zh_CN", "zh_TW"];
6d6f0db0 1668l10n.setup(LINGUAS);
1c9b904d 1669if (l10n.language === "en" || l10n.dictionary !== undefined) {
6d6f0db0 1670 UI.prime();
1c9b904d
JD
1671} else {
1672 WebUtil.fetchJSON('app/locale/' + l10n.language + '.json')
1673 .then((translations) => { l10n.dictionary = translations; })
1674 .catch(err => Log.Error("Failed to load translations: " + err))
1675 .then(UI.prime);
6d6f0db0
SR
1676}
1677
3ae0bb09 1678export default UI;