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