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