]> git.proxmox.com Git - mirror_novnc.git/blame - app/ui.js
Separate init functions from event handling setup
[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');
424 UI.addSettingChangeHandler('clip');
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_resize').disabled = UI.connected;
509 document.getElementById('noVNC_setting_shared').disabled = UI.connected;
510 document.getElementById('noVNC_setting_view_only').disabled = UI.connected;
575f6983
PO
511 document.getElementById('noVNC_setting_host').disabled = UI.connected;
512 document.getElementById('noVNC_setting_port').disabled = UI.connected;
3bb12056
SM
513 document.getElementById('noVNC_setting_path').disabled = UI.connected;
514 document.getElementById('noVNC_setting_repeaterID').disabled = UI.connected;
044d54ed
AN
515 document.getElementById('noVNC_setting_reconnect').disabled = UI.connected;
516 document.getElementById('noVNC_setting_reconnect_delay').disabled = UI.connected;
fdedbafb 517
3bb12056 518 if (UI.connected) {
f0d9ab96 519 UI.updateViewClip();
29475d77 520 UI.setMouseButton(1);
3f93a385
SM
521
522 // Hide the controlbar after 2 seconds
523 UI.closeControlbarTimeout = setTimeout(UI.closeControlbar, 2000);
29475d77 524 } else {
9e45354e 525 UI.updateXvpButton(0);
7520ba52 526 UI.keepControlbar();
29475d77 527 }
fdedbafb 528
eef91bf9
SM
529 // Hide input related buttons in view only mode
530 if (UI.rfb && UI.rfb.get_view_only()) {
531 document.getElementById('noVNC_keyboard_button')
532 .classList.add('noVNC_hidden');
533 document.getElementById('noVNC_toggle_extra_keys_button')
534 .classList.add('noVNC_hidden');
535 } else {
536 document.getElementById('noVNC_keyboard_button')
537 .classList.remove('noVNC_hidden');
538 document.getElementById('noVNC_toggle_extra_keys_button')
539 .classList.remove('noVNC_hidden');
540 }
541
29475d77 542 // State change disables viewport dragging.
543 // It is enabled (toggled) by direct click on the button
f0d9ab96 544 UI.setViewDrag(false);
29475d77 545
8a7ec6ea
SM
546 // State change also closes the password dialog
547 document.getElementById('noVNC_password_dlg')
548 .classList.remove('noVNC_open');
549
29475d77 550 //Util.Debug("<< updateVisualState");
551 },
552
8d7708c8 553 showStatus: function(text, status_type, time) {
ca5c74ad 554 var statusElem = document.getElementById('noVNC_status');
4e471b5b 555
ca5c74ad 556 clearTimeout(UI.statusTimeout);
4e471b5b 557
8d7708c8
SM
558 if (typeof status_type === 'undefined') {
559 status_type = 'normal';
560 }
561
562 statusElem.classList.remove("noVNC_status_normal",
563 "noVNC_status_warn",
564 "noVNC_status_error");
565
566 switch (status_type) {
567 case 'warning':
568 case 'warn':
569 statusElem.classList.add("noVNC_status_warn");
570 break;
571 case 'error':
572 statusElem.classList.add("noVNC_status_error");
573 break;
574 case 'normal':
575 case 'info':
576 default:
577 statusElem.classList.add("noVNC_status_normal");
578 break;
579 }
580
6048299a 581 statusElem.textContent = text;
ca5c74ad 582 statusElem.classList.add("noVNC_open");
583
584 // If no time was specified, show the status for 1.5 seconds
585 if (typeof time === 'undefined') {
586 time = 1500;
fdedbafb 587 }
4e471b5b 588
74a4a2b4
SM
589 // Error messages do not timeout
590 if (status_type !== 'error') {
ca5c74ad 591 UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
592 }
fdedbafb 593 },
594
ca5c74ad 595 hideStatus: function() {
596 clearTimeout(UI.statusTimeout);
597 document.getElementById('noVNC_status').classList.remove("noVNC_open");
4e471b5b 598 },
599
a7127fee
SM
600 notification: function (rfb, msg, level, options) {
601 UI.showStatus(msg, level);
602 },
603
3f93a385
SM
604 activateControlbar: function(event) {
605 clearTimeout(UI.idleControlbarTimeout);
728b5d9e
PO
606 // We manipulate the anchor instead of the actual control
607 // bar in order to avoid creating new a stacking group
608 document.getElementById('noVNC_control_bar_anchor')
609 .classList.remove("noVNC_idle");
3f93a385 610 UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
728b5d9e
PO
611 },
612
613 idleControlbar: function() {
614 document.getElementById('noVNC_control_bar_anchor')
615 .classList.add("noVNC_idle");
616 },
617
3f93a385
SM
618 keepControlbar: function() {
619 clearTimeout(UI.closeControlbarTimeout);
620 },
621
38323d4d
PO
622 openControlbar: function() {
623 document.getElementById('noVNC_control_bar')
624 .classList.add("noVNC_open");
625 },
626
627 closeControlbar: function() {
628 UI.closeAllPanels();
629 document.getElementById('noVNC_control_bar')
630 .classList.remove("noVNC_open");
631 },
632
633 toggleControlbar: function() {
634 if (document.getElementById('noVNC_control_bar')
635 .classList.contains("noVNC_open")) {
636 UI.closeControlbar();
637 } else {
638 UI.openControlbar();
639 }
640 },
641
8ee432f1
PO
642 toggleControlbarSide: function () {
643 // Temporarily disable animation to avoid weird movement
644 var bar = document.getElementById('noVNC_control_bar');
645 bar.style.transitionDuration = '0s';
646 bar.addEventListener('transitionend', function () { this.style.transitionDuration = ""; });
647
648 var anchor = document.getElementById('noVNC_control_bar_anchor');
cf348b78
PO
649 if (anchor.classList.contains("noVNC_right")) {
650 WebUtil.writeSetting('controlbar_pos', 'left');
651 anchor.classList.remove("noVNC_right");
652 } else {
653 WebUtil.writeSetting('controlbar_pos', 'right');
654 anchor.classList.add("noVNC_right");
655 }
8ee432f1
PO
656
657 // Consider this a movement of the handle
658 UI.controlbarDrag = true;
659 },
660
04b399e2
SM
661 dragControlbarHandle: function (e) {
662 if (!UI.controlbarGrabbed) return;
663
664 var ptr = Util.getPointerEvent(e);
665
8ee432f1
PO
666 var anchor = document.getElementById('noVNC_control_bar_anchor');
667 if (ptr.clientX < (window.innerWidth * 0.1)) {
668 if (anchor.classList.contains("noVNC_right")) {
669 UI.toggleControlbarSide();
670 }
671 } else if (ptr.clientX > (window.innerWidth * 0.9)) {
672 if (!anchor.classList.contains("noVNC_right")) {
673 UI.toggleControlbarSide();
674 }
675 }
676
04b399e2
SM
677 if (!UI.controlbarDrag) {
678 // The goal is to trigger on a certain physical width, the
679 // devicePixelRatio brings us a bit closer but is not optimal.
680 var dragThreshold = 10 * (window.devicePixelRatio || 1);
681 var dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
682
683 if (dragDistance < dragThreshold) return;
684
685 UI.controlbarDrag = true;
686 }
687
688 var eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
689
690 UI.moveControlbarHandle(eventY);
691
692 e.preventDefault();
693 e.stopPropagation();
de315d62
PO
694 UI.keepControlbar();
695 UI.activateControlbar();
04b399e2
SM
696 },
697
698 // Move the handle but don't allow any position outside the bounds
59cd99bc 699 moveControlbarHandle: function (viewportRelativeY) {
04b399e2 700 var handle = document.getElementById("noVNC_control_bar_handle");
59cd99bc
SM
701 var handleHeight = handle.getBoundingClientRect().height;
702 var controlbarBounds = document.getElementById("noVNC_control_bar")
703 .getBoundingClientRect();
04b399e2
SM
704 var margin = 10;
705
f75e4d3c
SM
706 // These heights need to be non-zero for the below logic to work
707 if (handleHeight === 0 || controlbarBounds.height === 0) {
708 return;
709 }
710
59cd99bc 711 var newY = viewportRelativeY;
04b399e2 712
59cd99bc
SM
713 // Check if the coordinates are outside the control bar
714 if (newY < controlbarBounds.top + margin) {
715 // Force coordinates to be below the top of the control bar
716 newY = controlbarBounds.top + margin;
717
718 } else if (newY > controlbarBounds.top +
719 controlbarBounds.height - handleHeight - margin) {
720 // Force coordinates to be above the bottom of the control bar
721 newY = controlbarBounds.top +
722 controlbarBounds.height - handleHeight - margin;
04b399e2
SM
723 }
724
725 // Corner case: control bar too small for stable position
726 if (controlbarBounds.height < (handleHeight + margin * 2)) {
59cd99bc
SM
727 newY = controlbarBounds.top +
728 (controlbarBounds.height - handleHeight) / 2;
04b399e2
SM
729 }
730
59cd99bc
SM
731 // The transform needs coordinates that are relative to the parent
732 var parentRelativeY = newY - controlbarBounds.top;
733 handle.style.transform = "translateY(" + parentRelativeY + "px)";
04b399e2
SM
734 },
735
736 updateControlbarHandle: function () {
59cd99bc
SM
737 // Since the control bar is fixed on the viewport and not the page,
738 // the move function expects coordinates relative the the viewport.
04b399e2 739 var handle = document.getElementById("noVNC_control_bar_handle");
59cd99bc
SM
740 var handleBounds = handle.getBoundingClientRect();
741 UI.moveControlbarHandle(handleBounds.top);
04b399e2
SM
742 },
743
744 controlbarHandleMouseUp: function(e) {
1c1cc1d0 745 if ((e.type == "mouseup") && (e.button != 0)) return;
04b399e2
SM
746
747 // mouseup and mousedown on the same place toggles the controlbar
748 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
749 UI.toggleControlbar();
750 e.preventDefault();
751 e.stopPropagation();
de315d62
PO
752 UI.keepControlbar();
753 UI.activateControlbar();
04b399e2
SM
754 }
755 UI.controlbarGrabbed = false;
756 },
757
758 controlbarHandleMouseDown: function(e) {
1c1cc1d0 759 if ((e.type == "mousedown") && (e.button != 0)) return;
04b399e2
SM
760
761 var ptr = Util.getPointerEvent(e);
762
763 var handle = document.getElementById("noVNC_control_bar_handle");
764 var bounds = handle.getBoundingClientRect();
765
766 WebUtil.setCapture(handle);
767 UI.controlbarGrabbed = true;
768 UI.controlbarDrag = false;
769
770 UI.controlbarMouseDownClientY = ptr.clientY;
771 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
772 e.preventDefault();
773 e.stopPropagation();
de315d62
PO
774 UI.keepControlbar();
775 UI.activateControlbar();
04b399e2
SM
776 },
777
575f6983
PO
778 toggleExpander: function(e) {
779 if (this.classList.contains("noVNC_open")) {
780 this.classList.remove("noVNC_open");
781 } else {
782 this.classList.add("noVNC_open");
783 }
784 },
785
95dd6001 786/* ------^-------
787 * /VISUAL
788 * ==============
789 * SETTINGS
790 * ------v------*/
791
45c70c9e 792 // Initial page load read/initialization of settings
793 initSetting: function(name, defVal) {
794 // Check Query string followed by cookie
795 var val = WebUtil.getConfigVar(name);
796 if (val === null) {
797 val = WebUtil.readSetting(name, defVal);
bbbf42bb 798 }
45c70c9e 799 UI.updateSetting(name, val);
bbbf42bb
SR
800 return val;
801 },
802
803 // Update cookie and form control setting. If value is not set, then
804 // updates from control to current cookie setting.
805 updateSetting: function(name, value) {
806
807 // Save the cookie for this session
808 if (typeof value !== 'undefined') {
809 WebUtil.writeSetting(name, value);
810 }
811
812 // Update the settings control
813 value = UI.getSetting(name);
814
ae510306 815 var ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
816 if (ctrl.type === 'checkbox') {
817 ctrl.checked = value;
818
819 } else if (typeof ctrl.options !== 'undefined') {
820 for (var i = 0; i < ctrl.options.length; i += 1) {
821 if (ctrl.options[i].value === value) {
822 ctrl.selectedIndex = i;
823 break;
824 }
825 }
826 } else {
827 /*Weird IE9 error leads to 'null' appearring
828 in textboxes instead of ''.*/
829 if (value === null) {
830 value = "";
831 }
832 ctrl.value = value;
833 }
834 },
835
836 // Save control setting to cookie
837 saveSetting: function(name) {
ae510306 838 var val, ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
839 if (ctrl.type === 'checkbox') {
840 val = ctrl.checked;
841 } else if (typeof ctrl.options !== 'undefined') {
842 val = ctrl.options[ctrl.selectedIndex].value;
843 } else {
844 val = ctrl.value;
845 }
846 WebUtil.writeSetting(name, val);
847 //Util.Debug("Setting saved '" + name + "=" + val + "'");
848 return val;
849 },
850
bbbf42bb
SR
851 // Force a setting to be a certain value
852 forceSetting: function(name, val) {
853 UI.updateSetting(name, val);
854 return val;
855 },
856
45c70c9e 857 // Read form control compatible setting from cookie
858 getSetting: function(name) {
ae510306 859 var ctrl = document.getElementById('noVNC_setting_' + name);
45c70c9e 860 var val = WebUtil.readSetting(name);
861 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
862 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
863 val = false;
4f19e5c6 864 } else {
45c70c9e 865 val = true;
4f19e5c6 866 }
bbbf42bb 867 }
bbbf42bb 868 return val;
bbbf42bb
SR
869 },
870
ed8cbe4e
PO
871/* ------^-------
872 * /SETTINGS
873 * ==============
874 * PANELS
875 * ------v------*/
876
877 closeAllPanels: function() {
878 UI.closeSettingsPanel();
879 UI.closeXvpPanel();
880 UI.closeClipboardPanel();
fb7c3b3b 881 UI.closeExtraKeys();
ed8cbe4e
PO
882 },
883
884/* ------^-------
885 * /PANELS
886 * ==============
887 * SETTINGS (panel)
888 * ------v------*/
889
890 openSettingsPanel: function() {
891 UI.closeAllPanels();
38323d4d 892 UI.openControlbar();
ed8cbe4e 893
dceda586 894 // Refresh UI elements from saved cookies
ed8cbe4e
PO
895 UI.updateSetting('encrypt');
896 UI.updateSetting('true_color');
897 if (Util.browserSupportsCursorURIs()) {
898 UI.updateSetting('cursor');
899 } else {
bea2b3fd 900 UI.updateSetting('cursor', !Util.isTouchDevice);
ed8cbe4e 901 document.getElementById('noVNC_setting_cursor').disabled = true;
bbbf42bb 902 }
ed8cbe4e
PO
903 UI.updateSetting('clip');
904 UI.updateSetting('resize');
905 UI.updateSetting('shared');
906 UI.updateSetting('view_only');
907 UI.updateSetting('path');
908 UI.updateSetting('repeaterID');
ed8cbe4e 909 UI.updateSetting('logging');
044d54ed
AN
910 UI.updateSetting('reconnect');
911 UI.updateSetting('reconnect_delay');
ed8cbe4e 912
e40978c7
PO
913 document.getElementById('noVNC_settings')
914 .classList.add("noVNC_open");
915 document.getElementById('noVNC_settings_button')
d9e86214 916 .classList.add("noVNC_selected");
45c70c9e 917 },
bbbf42bb 918
ed8cbe4e 919 closeSettingsPanel: function() {
e40978c7
PO
920 document.getElementById('noVNC_settings')
921 .classList.remove("noVNC_open");
922 document.getElementById('noVNC_settings_button')
d9e86214 923 .classList.remove("noVNC_selected");
bbbf42bb
SR
924 },
925
bbbf42bb 926 toggleSettingsPanel: function() {
ed8cbe4e
PO
927 if (document.getElementById('noVNC_settings')
928 .classList.contains("noVNC_open")) {
ed8cbe4e 929 UI.closeSettingsPanel();
bbbf42bb 930 } else {
ed8cbe4e 931 UI.openSettingsPanel();
bbbf42bb
SR
932 }
933 },
934
95dd6001 935/* ------^-------
936 * /SETTINGS
937 * ==============
938 * XVP
939 * ------v------*/
940
ed8cbe4e
PO
941 openXvpPanel: function() {
942 UI.closeAllPanels();
38323d4d 943 UI.openControlbar();
ed8cbe4e
PO
944
945 document.getElementById('noVNC_xvp')
946 .classList.add("noVNC_open");
947 document.getElementById('noVNC_xvp_button')
948 .classList.add("noVNC_selected");
949 },
950
951 closeXvpPanel: function() {
952 document.getElementById('noVNC_xvp')
953 .classList.remove("noVNC_open");
954 document.getElementById('noVNC_xvp_button')
955 .classList.remove("noVNC_selected");
956 },
957
bbbf42bb 958 toggleXvpPanel: function() {
ed8cbe4e
PO
959 if (document.getElementById('noVNC_xvp')
960 .classList.contains("noVNC_open")) {
961 UI.closeXvpPanel();
bbbf42bb 962 } else {
ed8cbe4e 963 UI.openXvpPanel();
bbbf42bb 964 }
bbbf42bb
SR
965 },
966
967 // Disable/enable XVP button
9e45354e 968 updateXvpButton: function(ver) {
eef91bf9 969 if (ver >= 1 && !UI.rfb.get_view_only()) {
a49d9298 970 document.getElementById('noVNC_xvp_button')
e40978c7 971 .classList.remove("noVNC_hidden");
bbbf42bb 972 } else {
a49d9298 973 document.getElementById('noVNC_xvp_button')
e40978c7 974 .classList.add("noVNC_hidden");
bbbf42bb 975 // Close XVP panel if open
ed8cbe4e 976 UI.closeXvpPanel();
bbbf42bb
SR
977 }
978 },
979
95dd6001 980/* ------^-------
981 * /XVP
982 * ==============
983 * CLIPBOARD
984 * ------v------*/
f8b399d7 985
ed8cbe4e
PO
986 openClipboardPanel: function() {
987 UI.closeAllPanels();
38323d4d 988 UI.openControlbar();
ed8cbe4e
PO
989
990 document.getElementById('noVNC_clipboard')
991 .classList.add("noVNC_open");
992 document.getElementById('noVNC_clipboard_button')
993 .classList.add("noVNC_selected");
994 },
995
996 closeClipboardPanel: function() {
997 document.getElementById('noVNC_clipboard')
998 .classList.remove("noVNC_open");
999 document.getElementById('noVNC_clipboard_button')
1000 .classList.remove("noVNC_selected");
1001 },
1002
bbbf42bb 1003 toggleClipboardPanel: function() {
ed8cbe4e
PO
1004 if (document.getElementById('noVNC_clipboard')
1005 .classList.contains("noVNC_open")) {
1006 UI.closeClipboardPanel();
bbbf42bb 1007 } else {
ed8cbe4e 1008 UI.openClipboardPanel();
bbbf42bb 1009 }
bbbf42bb
SR
1010 },
1011
4d26f58e 1012 clipboardReceive: function(rfb, text) {
1013 Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "...");
ae510306 1014 document.getElementById('noVNC_clipboard_text').value = text;
4d26f58e 1015 Util.Debug("<< UI.clipboardReceive");
95dd6001 1016 },
1017
4d26f58e 1018 clipboardClear: function() {
ae510306 1019 document.getElementById('noVNC_clipboard_text').value = "";
95dd6001 1020 UI.rfb.clipboardPasteFrom("");
1021 },
1022
4d26f58e 1023 clipboardSend: function() {
ae510306 1024 var text = document.getElementById('noVNC_clipboard_text').value;
4d26f58e 1025 Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "...");
95dd6001 1026 UI.rfb.clipboardPasteFrom(text);
4d26f58e 1027 Util.Debug("<< UI.clipboardSend");
95dd6001 1028 },
1029
1030/* ------^-------
1031 * /CLIPBOARD
1032 * ==============
1033 * CONNECTION
1034 * ------v------*/
1035
b3c932c3
PO
1036 openConnectPanel: function() {
1037 document.getElementById('noVNC_connect_dlg')
1038 .classList.add("noVNC_open");
1039 },
1040
1041 closeConnectPanel: function() {
1042 document.getElementById('noVNC_connect_dlg')
1043 .classList.remove("noVNC_open");
1044 },
1045
044d54ed 1046 connect: function(event, password) {
dceda586
SM
1047 var host = UI.getSetting('host');
1048 var port = UI.getSetting('port');
1049 var path = UI.getSetting('path');
c55f05f6 1050
044d54ed
AN
1051 if (typeof password === 'undefined') {
1052 password = WebUtil.getConfigVar('password');
1053 }
1054
512d3605
PO
1055 if (password === null) {
1056 password = undefined;
1057 }
1058
bbbf42bb 1059 if ((!host) || (!port)) {
f28e248d 1060 var msg = _("Must set host and port");
7d20158b
SM
1061 Util.Error(msg);
1062 UI.showStatus(msg, 'error');
3bb12056 1063 return;
bbbf42bb 1064 }
53fc7392 1065
d9fc1c7b 1066 if (!UI.initRFB()) return;
58ded70d 1067
4102b71c 1068 UI.closeAllPanels();
b3c932c3 1069 UI.closeConnectPanel();
4102b71c 1070
bbbf42bb
SR
1071 UI.rfb.set_encrypt(UI.getSetting('encrypt'));
1072 UI.rfb.set_true_color(UI.getSetting('true_color'));
1073 UI.rfb.set_local_cursor(UI.getSetting('cursor'));
1074 UI.rfb.set_shared(UI.getSetting('shared'));
1075 UI.rfb.set_view_only(UI.getSetting('view_only'));
1076 UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
53fc7392 1077
bbbf42bb 1078 UI.rfb.connect(host, port, password, path);
bbbf42bb 1079 },
5299db1a 1080
bbbf42bb 1081 disconnect: function() {
ed8cbe4e 1082 UI.closeAllPanels();
bbbf42bb 1083 UI.rfb.disconnect();
8e0f0088 1084
044d54ed
AN
1085 // Disable automatic reconnecting
1086 UI.inhibit_reconnect = true;
1087
f8b399d7 1088 // Restore the callback used for initial resize
ab81ddf5 1089 UI.rfb.set_onFBUComplete(UI.initialResize);
f8b399d7 1090
e543525f 1091 // Don't display the connection settings until we're actually disconnected
bbbf42bb
SR
1092 },
1093
044d54ed
AN
1094 reconnect: function() {
1095 UI.reconnect_callback = null;
1096
1097 // if reconnect has been disabled in the meantime, do nothing.
1098 if (UI.inhibit_reconnect) {
1099 return;
1100 }
1101
1102 UI.connect(null, UI.reconnect_password);
1103 },
1104
3bb12056
SM
1105 disconnectFinished: function (rfb, reason) {
1106 if (typeof reason !== 'undefined') {
1107 UI.showStatus(reason, 'error');
044d54ed
AN
1108 } else if (UI.getSetting('reconnect', false) === true && !UI.inhibit_reconnect) {
1109 document.getElementById("noVNC_transition_text").textContent = _("Reconnecting...");
1110 document.documentElement.classList.add("noVNC_reconnecting");
1111
1112 var delay = parseInt(UI.getSetting('reconnect_delay'));
1113 UI.reconnect_callback = setTimeout(UI.reconnect, delay);
1114 return;
3bb12056 1115 }
044d54ed
AN
1116
1117 UI.openControlbar();
1118 UI.openConnectPanel();
1119 },
1120
1121 cancelReconnect: function() {
1122 if (UI.reconnect_callback !== null) {
1123 clearTimeout(UI.reconnect_callback);
1124 UI.reconnect_callback = null;
1125 }
1126
1127 document.documentElement.classList.remove("noVNC_reconnecting");
512d3605 1128 UI.openControlbar();
b3c932c3 1129 UI.openConnectPanel();
3bb12056
SM
1130 },
1131
7d714b15
SM
1132/* ------^-------
1133 * /CONNECTION
1134 * ==============
1135 * PASSWORD
1136 * ------v------*/
1137
1138 passwordRequired: function(rfb, msg) {
1139
1140 document.getElementById('noVNC_password_dlg')
1141 .classList.add('noVNC_open');
1142
1143 setTimeout(function () {
1144 document.getElementById('noVNC_password_input').focus();
1145 }, 100);
1146
1147 if (typeof msg === 'undefined') {
45729def 1148 msg = _("Password is required");
7d714b15 1149 }
4770b0c3 1150 Util.Warn(msg);
3bb12056 1151 UI.showStatus(msg, "warning");
7d714b15
SM
1152 },
1153
95dd6001 1154 setPassword: function() {
044d54ed
AN
1155 var password = document.getElementById('noVNC_password_input').value;
1156 UI.rfb.sendPassword(password);
1157 UI.reconnect_password = password;
8a7ec6ea
SM
1158 document.getElementById('noVNC_password_dlg')
1159 .classList.remove('noVNC_open');
95dd6001 1160 return false;
1161 },
58ded70d 1162
95dd6001 1163/* ------^-------
7d714b15 1164 * /PASSWORD
95dd6001 1165 * ==============
1166 * FULLSCREEN
1167 * ------v------*/
1168
7d1dc09a 1169 toggleFullscreen: function() {
1170 if (document.fullscreenElement || // alternative standard method
1171 document.mozFullScreenElement || // currently working methods
1172 document.webkitFullscreenElement ||
a6357e82 1173 document.msFullscreenElement) {
7d1dc09a 1174 if (document.exitFullscreen) {
1175 document.exitFullscreen();
1176 } else if (document.mozCancelFullScreen) {
1177 document.mozCancelFullScreen();
1178 } else if (document.webkitExitFullscreen) {
1179 document.webkitExitFullscreen();
1180 } else if (document.msExitFullscreen) {
1181 document.msExitFullscreen();
1182 }
1183 } else {
1184 if (document.documentElement.requestFullscreen) {
1185 document.documentElement.requestFullscreen();
1186 } else if (document.documentElement.mozRequestFullScreen) {
1187 document.documentElement.mozRequestFullScreen();
1188 } else if (document.documentElement.webkitRequestFullscreen) {
1189 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1190 } else if (document.body.msRequestFullscreen) {
1191 document.body.msRequestFullscreen();
1192 }
1193 }
a6357e82 1194 UI.enableDisableViewClip();
7d1dc09a 1195 UI.updateFullscreenButton();
bbbf42bb
SR
1196 },
1197
7d1dc09a 1198 updateFullscreenButton: function() {
1199 if (document.fullscreenElement || // alternative standard method
1200 document.mozFullScreenElement || // currently working methods
1201 document.webkitFullscreenElement ||
1202 document.msFullscreenElement ) {
a49d9298 1203 document.getElementById('noVNC_fullscreen_button')
d9e86214 1204 .classList.add("noVNC_selected");
7d1dc09a 1205 } else {
a49d9298 1206 document.getElementById('noVNC_fullscreen_button')
d9e86214 1207 .classList.remove("noVNC_selected");
7d1dc09a 1208 }
1209 },
1210
95dd6001 1211/* ------^-------
1212 * /FULLSCREEN
1213 * ==============
1214 * RESIZE
1215 * ------v------*/
777cb7a0 1216
1217 // Apply remote resizing or local scaling
0bd2cbac 1218 applyResizeMode: function() {
58ded70d
SR
1219 if (!UI.rfb) return;
1220
777cb7a0 1221 var screen = UI.screenSize();
1222
3bb12056 1223 if (screen && UI.connected && UI.rfb.get_display()) {
777cb7a0 1224
1225 var display = UI.rfb.get_display();
1226 var resizeMode = UI.getSetting('resize');
1227
1228 if (resizeMode === 'remote') {
1229
1230 // Request changing the resolution of the remote display to
1231 // the size of the local browser viewport.
1232
1233 // In order to not send multiple requests before the browser-resize
1234 // is finished we wait 0.5 seconds before sending the request.
1235 clearTimeout(UI.resizeTimeout);
1236 UI.resizeTimeout = setTimeout(function(){
777cb7a0 1237 // Request a remote size covering the viewport
7ae53db9
SM
1238 if (UI.rfb.requestDesktopSize(screen.w, screen.h)) {
1239 Util.Debug('Requested new desktop size: ' +
1240 screen.w + 'x' + screen.h);
1241 }
777cb7a0 1242 }, 500);
1243
1244 } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
1245 var downscaleOnly = resizeMode === 'downscale';
1246 var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
ceb847b0
SM
1247
1248 if (!UI.rfb.get_view_only()) {
1249 UI.rfb.get_mouse().set_scale(scaleRatio);
1250 Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
1251 }
777cb7a0 1252 }
1253 }
bbbf42bb
SR
1254 },
1255
2e5cae1b 1256 // Gets the the size of the available viewport in the browser window
0bd2cbac 1257 screenSize: function() {
ae510306 1258 var screen = document.getElementById('noVNC_screen');
777cb7a0 1259
1260 // Hide the scrollbars until the size is calculated
1261 screen.style.overflow = "hidden";
1262
1263 var pos = Util.getPosition(screen);
1264 var w = pos.width;
1265 var h = pos.height;
1266
1267 screen.style.overflow = "visible";
1268
1269 if (isNaN(w) || isNaN(h)) {
1270 return false;
1271 } else {
1272 return {w: w, h: h};
1273 }
bbbf42bb
SR
1274 },
1275
777cb7a0 1276 // Normally we only apply the current resize mode after a window resize
1277 // event. This means that when a new connection is opened, there is no
1278 // resize mode active.
1279 // We have to wait until the first FBU because this is where the client
1280 // will find the supported encodings of the server. Some calls later in
1281 // the chain is dependant on knowing the server-capabilities.
1282 initialResize: function(rfb, fbu) {
1283 UI.applyResizeMode();
1284 // After doing this once, we remove the callback.
1285 UI.rfb.set_onFBUComplete(function() { });
bbbf42bb
SR
1286 },
1287
95dd6001 1288/* ------^-------
1289 * /RESIZE
1290 * ==============
1291 * CLIPPING
1292 * ------v------*/
1293
30bfff81 1294 // Set and configure viewport clipping
bbbf42bb 1295 setViewClip: function(clip) {
f0d9ab96 1296 UI.updateSetting('clip', clip);
1297 UI.updateViewClip();
1298 },
1299
1300 // Update parameters that depend on the clip setting
1301 updateViewClip: function() {
1c1cc1d0 1302 if (!UI.rfb) return;
8e0f0088 1303
f0d9ab96 1304 var display = UI.rfb.get_display();
bbbf42bb 1305 var cur_clip = display.get_viewport();
f0d9ab96 1306 var new_clip = UI.getSetting('clip');
bbbf42bb 1307
f0d9ab96 1308 if (cur_clip !== new_clip) {
1309 display.set_viewport(new_clip);
bbbf42bb 1310 }
8e0f0088 1311
f0d9ab96 1312 var size = UI.screenSize();
fdedbafb 1313
f0d9ab96 1314 if (new_clip && size) {
1315 // When clipping is enabled, the screen is limited to
1316 // the size of the browser window.
adf345fd 1317 display.viewportChangeSize(size.w, size.h);
bbbf42bb
SR
1318 }
1319 },
1320
30bfff81 1321 // Handle special cases where clipping is forced on/off or locked
0bd2cbac 1322 enableDisableViewClip: function() {
dceda586 1323 var resizeSetting = UI.getSetting('resize');
a6357e82 1324
f620259b 1325 if (UI.isSafari) {
1326 // Safari auto-hides the scrollbars which makes them
1327 // impossible to use in most cases
1328 UI.setViewClip(true);
ae510306 1329 document.getElementById('noVNC_setting_clip').disabled = true;
dceda586 1330 } else if (resizeSetting === 'downscale' || resizeSetting === 'scale') {
a6357e82 1331 // Disable clipping if we are scaling
dceda586 1332 UI.forceSetting('clip', false);
a6357e82 1333 UI.setViewClip(false);
ae510306 1334 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1335 } else if (document.msFullscreenElement) {
1336 // The browser is IE and we are in fullscreen mode.
1337 // - We need to force clipping while in fullscreen since
1338 // scrollbars doesn't work.
f28e248d
PO
1339 var msg = _("Forcing clipping mode since " +
1340 "scrollbars aren't supported " +
1341 "by IE in fullscreen");
7d20158b
SM
1342 Util.Debug(msg);
1343 UI.showStatus(msg);
a6357e82 1344 UI.rememberedClipSetting = UI.getSetting('clip');
1345 UI.setViewClip(true);
ae510306 1346 document.getElementById('noVNC_setting_clip').disabled = true;
8473635a
SM
1347 } else if (document.body.msRequestFullscreen &&
1348 UI.rememberedClipSetting !== null) {
a6357e82 1349 // Restore view clip to what it was before fullscreen on IE
1350 UI.setViewClip(UI.rememberedClipSetting);
3bb12056 1351 document.getElementById('noVNC_setting_clip').disabled =
bea2b3fd 1352 UI.connected || Util.isTouchDevice;
30bfff81 1353 } else {
3bb12056 1354 document.getElementById('noVNC_setting_clip').disabled =
bea2b3fd 1355 UI.connected || Util.isTouchDevice;
1356 if (Util.isTouchDevice) {
a6357e82 1357 UI.setViewClip(true);
30bfff81 1358 }
1359 }
1360 },
1361
95dd6001 1362/* ------^-------
1363 * /CLIPPING
1364 * ==============
1365 * VIEWDRAG
1366 * ------v------*/
1367
f0d9ab96 1368 toggleViewDrag: function() {
58ded70d 1369 if (!UI.rfb) return;
bbbf42bb 1370
f0d9ab96 1371 var drag = UI.rfb.get_viewportDrag();
1372 UI.setViewDrag(!drag);
1373 },
1374
1375 // Set the view drag mode which moves the viewport on mouse drags
1376 setViewDrag: function(drag) {
1377 if (!UI.rfb) return;
1378
1379 UI.rfb.set_viewportDrag(drag);
1380
1381 UI.updateViewDrag();
1382 },
1383
1384 updateViewDrag: function() {
1385 var clipping = false;
bbbf42bb 1386
3bb12056 1387 if (!UI.connected) return;
6244e383 1388
e00698fe 1389 // Check if viewport drag is possible. It is only possible
1390 // if the remote display is clipping the client display.
6244e383 1391 if (UI.rfb.get_display().get_viewport() &&
fdedbafb 1392 UI.rfb.get_display().clippingDisplay()) {
f0d9ab96 1393 clipping = true;
1394 }
31ddaa1c 1395
f0d9ab96 1396 var viewDragButton = document.getElementById('noVNC_view_drag_button');
29a0e6a8 1397
6244e383
PO
1398 if (!clipping &&
1399 UI.rfb.get_viewportDrag()) {
1400 // The size of the remote display is the same or smaller
1401 // than the client display. Make sure viewport drag isn't
1402 // active when it can't be used.
1403 UI.rfb.set_viewportDrag(false);
1404 }
1405
1406 if (UI.rfb.get_viewportDrag()) {
1407 viewDragButton.classList.add("noVNC_selected");
31ddaa1c 1408 } else {
6244e383
PO
1409 viewDragButton.classList.remove("noVNC_selected");
1410 }
31ddaa1c 1411
6244e383
PO
1412 // Different behaviour for touch vs non-touch
1413 // The button is disabled instead of hidden on touch devices
bea2b3fd 1414 if (Util.isTouchDevice) {
6244e383
PO
1415 viewDragButton.classList.remove("noVNC_hidden");
1416
1417 if (clipping) {
1418 viewDragButton.disabled = false;
31ddaa1c 1419 } else {
6244e383 1420 viewDragButton.disabled = true;
31ddaa1c 1421 }
6244e383
PO
1422 } else {
1423 viewDragButton.disabled = false;
29a0e6a8 1424
6244e383 1425 if (clipping) {
e40978c7 1426 viewDragButton.classList.remove("noVNC_hidden");
f0d9ab96 1427 } else {
6244e383 1428 viewDragButton.classList.add("noVNC_hidden");
f0d9ab96 1429 }
f8b399d7 1430 }
1431 },
1432
95dd6001 1433/* ------^-------
1434 * /VIEWDRAG
1435 * ==============
1436 * KEYBOARD
1437 * ------v------*/
fdf21468 1438
4b30f9ce 1439 showVirtualKeyboard: function() {
bea2b3fd 1440 if (!Util.isTouchDevice) return;
4b30f9ce
SM
1441
1442 var input = document.getElementById('noVNC_keyboardinput');
1443
1c1cc1d0 1444 if (document.activeElement == input) return;
4b30f9ce 1445
4b30f9ce
SM
1446 input.focus();
1447
1448 try {
1449 var l = input.value.length;
1450 // Move the caret to the end
1451 input.setSelectionRange(l, l);
1452 } catch (err) {} // setSelectionRange is undefined in Google Chrome
1453 },
1454
1455 hideVirtualKeyboard: function() {
bea2b3fd 1456 if (!Util.isTouchDevice) return;
4b30f9ce
SM
1457
1458 var input = document.getElementById('noVNC_keyboardinput');
1459
1c1cc1d0 1460 if (document.activeElement != input) return;
4b30f9ce
SM
1461
1462 input.blur();
1463 },
1464
1465 toggleVirtualKeyboard: function () {
ffcadf95
PO
1466 if (document.getElementById('noVNC_keyboard_button')
1467 .classList.contains("noVNC_selected")) {
4b30f9ce
SM
1468 UI.hideVirtualKeyboard();
1469 } else {
1470 UI.showVirtualKeyboard();
bbbf42bb
SR
1471 }
1472 },
1473
ffcadf95
PO
1474 onfocusVirtualKeyboard: function(event) {
1475 document.getElementById('noVNC_keyboard_button')
1476 .classList.add("noVNC_selected");
fdf21468 1477 },
1478
ffcadf95
PO
1479 onblurVirtualKeyboard: function(event) {
1480 document.getElementById('noVNC_keyboard_button')
1481 .classList.remove("noVNC_selected");
1482 },
1483
1484 keepVirtualKeyboard: function(event) {
1485 var input = document.getElementById('noVNC_keyboardinput');
1486
1487 // Only prevent focus change if the virtual keyboard is active
1488 if (document.activeElement != input) {
1489 return;
bbbf42bb 1490 }
ffcadf95
PO
1491
1492 // Allow clicking on links
1983baf5 1493 if (event.target.tagName === "A") {
ffcadf95
PO
1494 return;
1495 }
1496
1497 // And form elements, except standard noVNC buttons
1498 if ((event.target.form !== undefined) &&
1499 !event.target.classList.contains("noVNC_button")) {
1500 return;
1501 }
1502
1503 event.preventDefault();
bbbf42bb
SR
1504 },
1505
1506 keyboardinputReset: function() {
ae510306 1507 var kbi = document.getElementById('noVNC_keyboardinput');
bbbf42bb
SR
1508 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1509 UI.lastKeyboardinput = kbi.value;
1510 },
1511
1512 // When normal keyboard events are left uncought, use the input events from
1513 // the keyboardinput element instead and generate the corresponding key events.
1514 // This code is required since some browsers on Android are inconsistent in
1515 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1516 keyInput: function(event) {
3b8ec46f 1517
58ded70d 1518 if (!UI.rfb) return;
3b8ec46f 1519
bbbf42bb 1520 var newValue = event.target.value;
1138bdd4 1521
1522 if (!UI.lastKeyboardinput) {
1523 UI.keyboardinputReset();
1524 }
cb3e4deb 1525 var oldValue = UI.lastKeyboardinput;
bbbf42bb
SR
1526
1527 var newLen;
1528 try {
1529 // Try to check caret position since whitespace at the end
1530 // will not be considered by value.length in some browsers
1531 newLen = Math.max(event.target.selectionStart, newValue.length);
1532 } catch (err) {
1533 // selectionStart is undefined in Google Chrome
1534 newLen = newValue.length;
1535 }
1536 var oldLen = oldValue.length;
1537
1538 var backspaces;
1539 var inputs = newLen - oldLen;
1540 if (inputs < 0) {
1541 backspaces = -inputs;
1542 } else {
1543 backspaces = 0;
1544 }
8e0f0088 1545
bbbf42bb
SR
1546 // Compare the old string with the new to account for
1547 // text-corrections or other input that modify existing text
1548 var i;
1549 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1550 if (newValue.charAt(i) != oldValue.charAt(i)) {
1551 inputs = newLen - i;
1552 backspaces = oldLen - i;
1553 break;
1554 }
1555 }
1556
1557 // Send the key events
1558 for (i = 0; i < backspaces; i++) {
ae510306 1559 UI.rfb.sendKey(KeyTable.XK_BackSpace);
bbbf42bb
SR
1560 }
1561 for (i = newLen - inputs; i < newLen; i++) {
0b96eddf 1562 UI.rfb.sendKey(keysyms.fromUnicode(newValue.charCodeAt(i)).keysym);
bbbf42bb
SR
1563 }
1564
1565 // Control the text content length in the keyboardinput element
1566 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1567 UI.keyboardinputReset();
1568 } else if (newLen < 1) {
1569 // There always have to be some text in the keyboardinput
1570 // element with which backspace can interact.
1571 UI.keyboardinputReset();
1572 // This sometimes causes the keyboard to disappear for a second
1573 // but it is required for the android keyboard to recognize that
1574 // text has been added to the field
1575 event.target.blur();
1576 // This has to be ran outside of the input handler in order to work
ffcadf95 1577 setTimeout(event.target.focus.bind(event.target), 0);
bbbf42bb
SR
1578 } else {
1579 UI.lastKeyboardinput = newValue;
1580 }
1581 },
1582
4b30f9ce
SM
1583/* ------^-------
1584 * /KEYBOARD
1585 * ==============
1586 * EXTRA KEYS
1587 * ------v------*/
1588
ed8cbe4e 1589 openExtraKeys: function() {
fb7c3b3b 1590 UI.closeAllPanels();
38323d4d 1591 UI.openControlbar();
fb7c3b3b 1592
ed8cbe4e
PO
1593 document.getElementById('noVNC_modifiers')
1594 .classList.add("noVNC_open");
1595 document.getElementById('noVNC_toggle_extra_keys_button')
1596 .classList.add("noVNC_selected");
1597 },
1598
1599 closeExtraKeys: function() {
1600 document.getElementById('noVNC_modifiers')
1601 .classList.remove("noVNC_open");
1602 document.getElementById('noVNC_toggle_extra_keys_button')
1603 .classList.remove("noVNC_selected");
1604 },
1605
cd611a53 1606 toggleExtraKeys: function() {
ed8cbe4e
PO
1607 if(document.getElementById('noVNC_modifiers')
1608 .classList.contains("noVNC_open")) {
1609 UI.closeExtraKeys();
1610 } else {
1611 UI.openExtraKeys();
bbbf42bb
SR
1612 }
1613 },
1614
fdf21468 1615 sendEsc: function() {
ae510306 1616 UI.rfb.sendKey(KeyTable.XK_Escape);
fdf21468 1617 },
1618
1619 sendTab: function() {
ae510306 1620 UI.rfb.sendKey(KeyTable.XK_Tab);
fdf21468 1621 },
1622
bbbf42bb 1623 toggleCtrl: function() {
b0c6d3c6 1624 var btn = document.getElementById('noVNC_toggle_ctrl_button');
1625 if (btn.classList.contains("noVNC_selected")) {
ae510306 1626 UI.rfb.sendKey(KeyTable.XK_Control_L, false);
b0c6d3c6 1627 btn.classList.remove("noVNC_selected");
1628 } else {
1629 UI.rfb.sendKey(KeyTable.XK_Control_L, true);
1630 btn.classList.add("noVNC_selected");
bbbf42bb
SR
1631 }
1632 },
1633
1634 toggleAlt: function() {
b0c6d3c6 1635 var btn = document.getElementById('noVNC_toggle_alt_button');
1636 if (btn.classList.contains("noVNC_selected")) {
ae510306 1637 UI.rfb.sendKey(KeyTable.XK_Alt_L, false);
b0c6d3c6 1638 btn.classList.remove("noVNC_selected");
1639 } else {
1640 UI.rfb.sendKey(KeyTable.XK_Alt_L, true);
1641 btn.classList.add("noVNC_selected");
bbbf42bb
SR
1642 }
1643 },
1644
fdf21468 1645 sendCtrlAltDel: function() {
1646 UI.rfb.sendCtrlAltDel();
bbbf42bb
SR
1647 },
1648
95dd6001 1649/* ------^-------
4b30f9ce 1650 * /EXTRA KEYS
95dd6001 1651 * ==============
1652 * MISC
1653 * ------v------*/
1654
1655 setMouseButton: function(num) {
eef91bf9
SM
1656 var view_only = UI.rfb.get_view_only();
1657 if (UI.rfb && !view_only) {
95dd6001 1658 UI.rfb.get_mouse().set_touchButton(num);
1659 }
1660
1661 var blist = [0, 1,2,4];
1662 for (var b = 0; b < blist.length; b++) {
eef91bf9
SM
1663 var button = document.getElementById('noVNC_mouse_button' +
1664 blist[b]);
1665 if (blist[b] === num && !view_only) {
e40978c7 1666 button.classList.remove("noVNC_hidden");
95dd6001 1667 } else {
e40978c7 1668 button.classList.add("noVNC_hidden");
95dd6001 1669 }
1670 }
1671 },
1672
1673 displayBlur: function() {
ceb847b0
SM
1674 if (UI.rfb && !UI.rfb.get_view_only()) {
1675 UI.rfb.get_keyboard().set_focused(false);
1676 UI.rfb.get_mouse().set_focused(false);
1677 }
95dd6001 1678 },
1679
1680 displayFocus: function() {
ceb847b0
SM
1681 if (UI.rfb && !UI.rfb.get_view_only()) {
1682 UI.rfb.get_keyboard().set_focused(true);
1683 UI.rfb.get_mouse().set_focused(true);
1684 }
bbbf42bb
SR
1685 },
1686
aa905475
SM
1687 updateLogging: function() {
1688 WebUtil.init_logging(UI.getSetting('logging'));
1689 },
1690
18d21e36
PO
1691 updateSessionSize: function(rfb, width, height) {
1692 UI.updateViewClip();
1693 UI.updateViewDrag();
1694 },
1695
3bb12056
SM
1696 updateDesktopName: function(rfb, name) {
1697 UI.desktopName = name;
1698 // Display the desktop name in the document title
95dd6001 1699 document.title = name + " - noVNC";
bbbf42bb
SR
1700 },
1701
63bf2ba5
PO
1702 bell: function(rfb) {
1703 if (WebUtil.getConfigVar('bell', 'on') === 'on') {
1704 document.getElementById('noVNC_bell').play();
1705 }
1706 },
1707
bbbf42bb
SR
1708 //Helper to add options to dropdown.
1709 addOption: function(selectbox, text, value) {
1710 var optn = document.createElement("OPTION");
1711 optn.text = text;
1712 optn.value = value;
1713 selectbox.options.add(optn);
1714 },
1715
95dd6001 1716/* ------^-------
1717 * /MISC
1718 * ==============
1719 */
bbbf42bb 1720 };
ae510306
SR
1721
1722 /* [module] UI.load(); */
bbbf42bb 1723})();
ae510306
SR
1724
1725/* [module] export default UI; */