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