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