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