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