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