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