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