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