]> git.proxmox.com Git - mirror_novnc.git/blame - app/ui.js
Remove beforeunload handler
[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
1d728ace 5 * Licensed under MPL 2.0 (see LICENSE.txt)
15046f00
JM
6 *
7 * See README.md for usage and integration instructions.
8 */
43cf7bd8 9
bbbf42bb 10/* jslint white: false, browser: true */
ae510306
SR
11/* global window, document.getElementById, Util, WebUtil, RFB, Display */
12
13/* [module]
14 * import Util from "../core/util";
bd5340c7 15 * import KeyTable from "../core/input/keysym";
ae510306
SR
16 * import RFB from "../core/rfb";
17 * import Display from "../core/display";
18 * import WebUtil from "./webutil";
19 */
bbbf42bb
SR
20
21var UI;
22
23(function () {
24 "use strict";
25
ae510306 26 /* [begin skip-as-module] */
bbbf42bb 27 // Load supporting scripts
72bdd06e 28 WebUtil.load_scripts(
bd5340c7
SR
29 {'core': ["base64.js", "websock.js", "des.js", "input/keysymdef.js",
30 "input/xtscancodes.js", "input/util.js", "input/devices.js",
31 "display.js", "inflator.js", "rfb.js", "input/keysym.js"]});
ae510306 32
bbbf42bb 33 window.onscriptsload = function () { UI.load(); };
ae510306 34 /* [end skip-as-module] */
bbbf42bb 35
bd6874e0 36 UI = {
bbbf42bb 37
045d9224 38 rfb_state: 'loaded',
529c64e1 39
045d9224 40 resizeTimeout: null,
529c64e1 41 popupStatusTimeout: null,
42 hideKeyboardTimeout: null,
43
045d9224 44 settingsOpen: false,
45 connSettingsOpen: false,
bbbf42bb
SR
46 clipboardOpen: false,
47 keyboardVisible: false,
9d16e512 48 extraKeysVisible: false,
529c64e1 49
bbbf42bb 50 isTouchDevice: false,
f620259b 51 isSafari: false,
a6357e82 52 rememberedClipSetting: null,
529c64e1 53 lastKeyboardinput: null,
54 defaultKeyboardinputLen: 100,
55
e961641f
SM
56 ctrlOn: false,
57 altOn: false,
bbbf42bb
SR
58
59 // Setup rfb object, load settings from browser storage, then call
60 // UI.init to setup the UI/menus
0bd2cbac 61 load: function(callback) {
bbbf42bb
SR
62 WebUtil.initSettings(UI.start, callback);
63 },
64
65 // Render default UI and initialize settings menu
66 start: function(callback) {
0f6af1e3 67
68 // Setup global variables first
bbbf42bb 69 UI.isTouchDevice = 'ontouchstart' in document.documentElement;
0f6af1e3 70 UI.isSafari = (navigator.userAgent.indexOf('Safari') !== -1 &&
71 navigator.userAgent.indexOf('Chrome') === -1);
72
73 UI.initSettings();
74
75 // Show mouse selector buttons on touch screen devices
76 if (UI.isTouchDevice) {
77 // Show mobile buttons
78 document.getElementById('noVNC_mobile_buttons').style.display = "inline";
79 UI.setMouseButton();
80 // Remove the address bar
81 setTimeout(function() { window.scrollTo(0, 1); }, 100);
82 UI.forceSetting('clip', true);
83 } else {
84 UI.initSetting('clip', false);
85 }
86
87 // Setup and initialize event handlers
88 UI.setupWindowEvents();
89 UI.setupFullscreen();
90 UI.addControlbarHandlers();
91 UI.addTouchSpecificHandlers();
92 UI.addXvpHandlers();
93 UI.addConnectionControlHandlers();
94 UI.addClipboardHandlers();
95 UI.addSettingsHandlers();
bbbf42bb 96
0f6af1e3 97 // Show description by default when hosted at for kanaka.github.com
98 if (location.host === "kanaka.github.io") {
99 // Open the description dialog
100 document.getElementById('noVNC_description').style.display = "block";
101 } else {
102 // Show the connect panel on first load unless autoconnecting
103 if (autoconnect === UI.connSettingsOpen) {
104 UI.toggleConnectPanel();
105 }
106 }
107
108 UI.setViewClip();
109 UI.setBarPosition();
110
111 UI.updateVisualState();
112
113 document.getElementById('noVNC_setting_host').focus();
114
115 var autoconnect = WebUtil.getConfigVar('autoconnect', false);
116 if (autoconnect === 'true' || autoconnect == '1') {
117 autoconnect = true;
118 UI.connect();
119 } else {
120 autoconnect = false;
121 }
122
123 if (typeof callback === "function") {
124 callback(UI.rfb);
125 }
126 },
127
128 initSettings: function() {
bbbf42bb
SR
129 // Stylesheet selection dropdown
130 var sheet = WebUtil.selectStylesheet();
131 var sheets = WebUtil.getStylesheets();
132 var i;
133 for (i = 0; i < sheets.length; i += 1) {
ae510306 134 UI.addOption(document.getElementById('noVNC_setting_stylesheet'),sheets[i].title, sheets[i].title);
bbbf42bb
SR
135 }
136
137 // Logging selection dropdown
138 var llevels = ['error', 'warn', 'info', 'debug'];
139 for (i = 0; i < llevels.length; i += 1) {
ae510306 140 UI.addOption(document.getElementById('noVNC_setting_logging'),llevels[i], llevels[i]);
bbbf42bb
SR
141 }
142
143 // Settings with immediate effects
144 UI.initSetting('logging', 'warn');
145 WebUtil.init_logging(UI.getSetting('logging'));
146
147 UI.initSetting('stylesheet', 'default');
148 WebUtil.selectStylesheet(null);
149 // call twice to get around webkit bug
150 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
151
152 // if port == 80 (or 443) then it won't be present and should be
153 // set manually
154 var port = window.location.port;
155 if (!port) {
156 if (window.location.protocol.substring(0,5) == 'https') {
157 port = 443;
158 }
159 else if (window.location.protocol.substring(0,4) == 'http') {
160 port = 80;
161 }
162 }
163
164 /* Populate the controls if defaults are provided in the URL */
165 UI.initSetting('host', window.location.hostname);
166 UI.initSetting('port', port);
167 UI.initSetting('password', '');
168 UI.initSetting('encrypt', (window.location.protocol === "https:"));
169 UI.initSetting('true_color', true);
170 UI.initSetting('cursor', !UI.isTouchDevice);
8b46c0de 171 UI.initSetting('resize', 'off');
bbbf42bb
SR
172 UI.initSetting('shared', true);
173 UI.initSetting('view_only', false);
174 UI.initSetting('path', 'websockify');
175 UI.initSetting('repeaterID', '');
c55f05f6 176 UI.initSetting('token', '');
0f6af1e3 177 },
bbbf42bb 178
0f6af1e3 179 setupWindowEvents: function() {
180 window.addEventListener( 'resize', function () {
777cb7a0 181 UI.applyResizeMode();
f8b399d7 182 UI.setViewClip();
31ddaa1c 183 UI.updateViewDrag();
fdedbafb 184 UI.setBarPosition();
f8b399d7 185 } );
bbbf42bb
SR
186 },
187
0f6af1e3 188 setupFullscreen: function() {
189 // Only show the button if fullscreen is properly supported
190 // * Safari doesn't support alphanumerical input while in fullscreen
191 if (!UI.isSafari &&
192 (document.documentElement.requestFullscreen ||
193 document.documentElement.mozRequestFullScreen ||
194 document.documentElement.webkitRequestFullscreen ||
195 document.body.msRequestFullscreen)) {
196 document.getElementById('noVNC_fullscreen_button').style.display = "inline";
197 UI.addFullscreenHandlers();
d9fc1c7b 198 }
e543525f
SR
199 },
200
0f6af1e3 201 addControlbarHandlers: function() {
d7f79071 202 document.getElementById("noVNC_view_drag_button")
203 .addEventListener('click', UI.toggleViewDrag);
0f6af1e3 204 document.getElementById("noVNC_sendCtrlAltDel_button")
205 .addEventListener('click', UI.sendCtrlAltDel);
206 document.getElementById("noVNC_status")
207 .addEventListener('click', UI.popupStatus);
208 document.getElementById("noVNC_popup_status")
209 .addEventListener('click', UI.closePopup);
210 document.getElementById("noVNC_description_button")
211 .addEventListener('click', UI.toggleConnectPanel);
212 },
213
214 addTouchSpecificHandlers: function() {
d7f79071 215 document.getElementById("noVNC_mouse_button0")
216 .addEventListener('click', function () { UI.setMouseButton(1); });
217 document.getElementById("noVNC_mouse_button1")
218 .addEventListener('click', function () { UI.setMouseButton(2); });
219 document.getElementById("noVNC_mouse_button2")
220 .addEventListener('click', function () { UI.setMouseButton(4); });
221 document.getElementById("noVNC_mouse_button4")
222 .addEventListener('click', function () { UI.setMouseButton(0); });
223 document.getElementById("noVNC_keyboard_button")
224 .addEventListener('click', UI.showKeyboard);
225
226 document.getElementById("noVNC_keyboardinput")
227 .addEventListener('input', UI.keyInput);
228 document.getElementById("noVNC_keyboardinput")
229 .addEventListener('blur', UI.hideKeyboard);
230 document.getElementById("noVNC_keyboardinput")
231 .addEventListener('submit', function () { return false; });
232
0f6af1e3 233 window.addEventListener('load', UI.keyboardinputReset);
234
d7f79071 235 document.getElementById("noVNC_toggleExtraKeys_button")
236 .addEventListener('click', UI.toggleExtraKeys);
237 document.getElementById("noVNC_toggleCtrl_button")
238 .addEventListener('click', UI.toggleCtrl);
239 document.getElementById("noVNC_toggleAlt_button")
240 .addEventListener('click', UI.toggleAlt);
241 document.getElementById("noVNC_sendTab_button")
242 .addEventListener('click', UI.sendTab);
243 document.getElementById("noVNC_sendEsc_button")
244 .addEventListener('click', UI.sendEsc);
0f6af1e3 245 },
d7f79071 246
0f6af1e3 247 addXvpHandlers: function() {
d7f79071 248 document.getElementById("noVNC_xvpShutdown_button")
249 .addEventListener('click', function() { UI.rfb.xvpShutdown(); });
250 document.getElementById("noVNC_xvpReboot_button")
251 .addEventListener('click', function() { UI.rfb.xvpReboot(); });
252 document.getElementById("noVNC_xvpReset_button")
253 .addEventListener('click', function() { UI.rfb.xvpReset(); });
d7f79071 254 document.getElementById("noVNC_toggleXvp_button")
255 .addEventListener('click', UI.toggleXvpPanel);
0f6af1e3 256 },
257
258 addConnectionControlHandlers: function() {
d7f79071 259 document.getElementById("noVNC_connectPanel_button")
260 .addEventListener('click', UI.toggleConnectPanel);
261 document.getElementById("noVNC_disconnect_button")
262 .addEventListener('click', UI.disconnect);
0f6af1e3 263 document.getElementById("noVNC_connect_button")
264 .addEventListener('click', UI.connect);
265 },
d7f79071 266
0f6af1e3 267 addClipboardHandlers: function() {
268 document.getElementById("noVNC_clipboard_button")
269 .addEventListener('click', UI.toggleClipboardPanel);
d7f79071 270 document.getElementById("noVNC_clipboard_text")
271 .addEventListener('focus', UI.displayBlur);
272 document.getElementById("noVNC_clipboard_text")
273 .addEventListener('blur', UI.displayFocus);
274 document.getElementById("noVNC_clipboard_text")
275 .addEventListener('change', UI.clipboardSend);
276 document.getElementById("noVNC_clipboard_clear_button")
277 .addEventListener('click', UI.clipboardClear);
0f6af1e3 278 },
d7f79071 279
0f6af1e3 280 addSettingsHandlers: function() {
281 document.getElementById("noVNC_settings_button")
282 .addEventListener('click', UI.toggleSettingsPanel);
d7f79071 283 document.getElementById("noVNC_settings_apply")
284 .addEventListener('click', UI.settingsApply);
285
d7f79071 286 document.getElementById("noVNC_setting_resize")
287 .addEventListener('change', UI.enableDisableViewClip);
bbbf42bb
SR
288 },
289
0f6af1e3 290 addFullscreenHandlers: function() {
291 document.getElementById("noVNC_fullscreen_button")
292 .addEventListener('click', UI.toggleFullscreen);
293
294 window.addEventListener('fullscreenchange', UI.updateFullscreenButton);
295 window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton);
296 window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton);
297 window.addEventListener('msfullscreenchange', UI.updateFullscreenButton);
298 },
299
300 initRFB: function() {
301 try {
302 UI.rfb = new RFB({'target': document.getElementById('noVNC_canvas'),
303 'onUpdateState': UI.updateState,
304 'onXvpInit': UI.updateXvpButton,
305 'onClipboard': UI.clipboardReceive,
306 'onFBUComplete': UI.initialResize,
307 'onFBResize': UI.updateViewDrag,
308 'onDesktopName': UI.updateDocumentTitle});
309 return true;
310 } catch (exc) {
311 UI.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
312 return false;
313 }
314 },
315
95dd6001 316/* ------^-------
317 * /INIT
318 * ==============
319 * VISUAL
320 * ------v------*/
58ded70d 321
29475d77 322 updateState: function(rfb, state, oldstate, msg) {
323 UI.rfb_state = state;
324 var klass;
325 switch (state) {
326 case 'failed':
327 case 'fatal':
328 klass = "noVNC_status_error";
329 break;
330 case 'normal':
331 klass = "noVNC_status_normal";
332 break;
333 case 'disconnected':
ae510306
SR
334 document.getElementById('noVNC_logo').style.display = "block";
335 document.getElementById('noVNC_screen').style.display = "none";
29475d77 336 /* falls through */
337 case 'loaded':
338 klass = "noVNC_status_normal";
339 break;
340 case 'password':
341 UI.toggleConnectPanel();
fdedbafb 342
ae510306
SR
343 document.getElementById('noVNC_connect_button').value = "Send Password";
344 document.getElementById('noVNC_connect_button').onclick = UI.setPassword;
345 document.getElementById('noVNC_setting_password').focus();
fdedbafb 346
29475d77 347 klass = "noVNC_status_warn";
348 break;
349 default:
350 klass = "noVNC_status_warn";
351 break;
352 }
fdedbafb 353
29475d77 354 if (typeof(msg) !== 'undefined') {
ae510306
SR
355 document.getElementById('noVNC_control_bar').setAttribute("class", klass);
356 document.getElementById('noVNC_status').innerHTML = msg;
fdedbafb 357 }
29475d77 358
359 UI.updateVisualState();
fdedbafb 360 },
361
29475d77 362 // Disable/enable controls depending on connection state
363 updateVisualState: function() {
364 var connected = UI.rfb && UI.rfb_state === 'normal';
fdedbafb 365
29475d77 366 //Util.Debug(">> updateVisualState");
ae510306
SR
367 document.getElementById('noVNC_setting_encrypt').disabled = connected;
368 document.getElementById('noVNC_setting_true_color').disabled = connected;
29475d77 369 if (Util.browserSupportsCursorURIs()) {
ae510306 370 document.getElementById('noVNC_setting_cursor').disabled = connected;
29475d77 371 } else {
372 UI.updateSetting('cursor', !UI.isTouchDevice);
ae510306 373 document.getElementById('noVNC_setting_cursor').disabled = true;
29475d77 374 }
fdedbafb 375
29475d77 376 UI.enableDisableViewClip();
ae510306
SR
377 document.getElementById('noVNC_setting_resize').disabled = connected;
378 document.getElementById('noVNC_setting_shared').disabled = connected;
379 document.getElementById('noVNC_setting_view_only').disabled = connected;
380 document.getElementById('noVNC_setting_path').disabled = connected;
381 document.getElementById('noVNC_setting_repeaterID').disabled = connected;
fdedbafb 382
29475d77 383 if (connected) {
384 UI.setViewClip();
385 UI.setMouseButton(1);
ae510306
SR
386 document.getElementById('noVNC_clipboard_button').style.display = "inline";
387 document.getElementById('noVNC_keyboard_button').style.display = "inline";
388 document.getElementById('noVNC_extra_keys').style.display = "";
389 document.getElementById('noVNC_sendCtrlAltDel_button').style.display = "inline";
29475d77 390 } else {
391 UI.setMouseButton();
ae510306
SR
392 document.getElementById('noVNC_clipboard_button').style.display = "none";
393 document.getElementById('noVNC_keyboard_button').style.display = "none";
394 document.getElementById('noVNC_extra_keys').style.display = "none";
395 document.getElementById('noVNC_sendCtrlAltDel_button').style.display = "none";
9e45354e 396 UI.updateXvpButton(0);
29475d77 397 }
fdedbafb 398
29475d77 399 // State change disables viewport dragging.
400 // It is enabled (toggled) by direct click on the button
401 UI.updateViewDrag(false);
402
403 switch (UI.rfb_state) {
404 case 'fatal':
405 case 'failed':
406 case 'disconnected':
ae510306
SR
407 document.getElementById('noVNC_connectPanel_button').style.display = "";
408 document.getElementById('noVNC_disconnect_button').style.display = "none";
29475d77 409 UI.connSettingsOpen = false;
410 UI.toggleConnectPanel();
411 break;
412 case 'loaded':
ae510306
SR
413 document.getElementById('noVNC_connectPanel_button').style.display = "";
414 document.getElementById('noVNC_disconnect_button').style.display = "none";
29475d77 415 break;
416 default:
ae510306
SR
417 document.getElementById('noVNC_connectPanel_button').style.display = "none";
418 document.getElementById('noVNC_disconnect_button').style.display = "";
29475d77 419 break;
420 }
421
422 //Util.Debug("<< updateVisualState");
423 },
424
4e471b5b 425 popupStatus: function(text) {
ae510306 426 var psp = document.getElementById('noVNC_popup_status');
4e471b5b 427
428 clearTimeout(UI.popupStatusTimeout);
429
430 if (typeof text === 'string') {
431 psp.innerHTML = text;
fdedbafb 432 } else {
ae510306 433 psp.innerHTML = document.getElementById('noVNC_status').innerHTML;
fdedbafb 434 }
4e471b5b 435 psp.style.display = "block";
436 psp.style.left = window.innerWidth/2 -
437 parseInt(window.getComputedStyle(psp).width)/2 -30 + "px";
438
439 // Show the popup for a maximum of 1.5 seconds
67685d07 440 UI.popupStatusTimeout = setTimeout(UI.closePopup, 1500);
fdedbafb 441 },
442
4e471b5b 443 closePopup: function() {
444 clearTimeout(UI.popupStatusTimeout);
ae510306 445 document.getElementById('noVNC_popup_status').style.display = "none";
4e471b5b 446 },
447
95dd6001 448/* ------^-------
449 * /VISUAL
450 * ==============
451 * SETTINGS
452 * ------v------*/
453
45c70c9e 454 // Initial page load read/initialization of settings
455 initSetting: function(name, defVal) {
456 // Check Query string followed by cookie
457 var val = WebUtil.getConfigVar(name);
458 if (val === null) {
459 val = WebUtil.readSetting(name, defVal);
bbbf42bb 460 }
45c70c9e 461 UI.updateSetting(name, val);
bbbf42bb
SR
462 return val;
463 },
464
465 // Update cookie and form control setting. If value is not set, then
466 // updates from control to current cookie setting.
467 updateSetting: function(name, value) {
468
469 // Save the cookie for this session
470 if (typeof value !== 'undefined') {
471 WebUtil.writeSetting(name, value);
472 }
473
474 // Update the settings control
475 value = UI.getSetting(name);
476
ae510306 477 var ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
478 if (ctrl.type === 'checkbox') {
479 ctrl.checked = value;
480
481 } else if (typeof ctrl.options !== 'undefined') {
482 for (var i = 0; i < ctrl.options.length; i += 1) {
483 if (ctrl.options[i].value === value) {
484 ctrl.selectedIndex = i;
485 break;
486 }
487 }
488 } else {
489 /*Weird IE9 error leads to 'null' appearring
490 in textboxes instead of ''.*/
491 if (value === null) {
492 value = "";
493 }
494 ctrl.value = value;
495 }
496 },
497
498 // Save control setting to cookie
499 saveSetting: function(name) {
ae510306 500 var val, ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
501 if (ctrl.type === 'checkbox') {
502 val = ctrl.checked;
503 } else if (typeof ctrl.options !== 'undefined') {
504 val = ctrl.options[ctrl.selectedIndex].value;
505 } else {
506 val = ctrl.value;
507 }
508 WebUtil.writeSetting(name, val);
509 //Util.Debug("Setting saved '" + name + "=" + val + "'");
510 return val;
511 },
512
bbbf42bb
SR
513 // Force a setting to be a certain value
514 forceSetting: function(name, val) {
515 UI.updateSetting(name, val);
516 return val;
517 },
518
45c70c9e 519 // Read form control compatible setting from cookie
520 getSetting: function(name) {
ae510306 521 var ctrl = document.getElementById('noVNC_setting_' + name);
45c70c9e 522 var val = WebUtil.readSetting(name);
523 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
524 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
525 val = false;
4f19e5c6 526 } else {
45c70c9e 527 val = true;
4f19e5c6 528 }
bbbf42bb 529 }
bbbf42bb 530 return val;
bbbf42bb
SR
531 },
532
95dd6001 533 // Save/apply settings when 'Apply' button is pressed
534 settingsApply: function() {
535 //Util.Debug(">> settingsApply");
536 UI.saveSetting('encrypt');
537 UI.saveSetting('true_color');
538 if (Util.browserSupportsCursorURIs()) {
539 UI.saveSetting('cursor');
bbbf42bb 540 }
bbbf42bb 541
95dd6001 542 UI.saveSetting('resize');
bbbf42bb 543
95dd6001 544 if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
545 UI.forceSetting('clip', false);
7d1dc09a 546 }
7d1dc09a 547
95dd6001 548 UI.saveSetting('clip');
549 UI.saveSetting('shared');
550 UI.saveSetting('view_only');
551 UI.saveSetting('path');
552 UI.saveSetting('repeaterID');
553 UI.saveSetting('stylesheet');
554 UI.saveSetting('logging');
555
556 // Settings with immediate (non-connected related) effect
557 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
558 WebUtil.init_logging(UI.getSetting('logging'));
559 UI.setViewClip();
560 UI.updateViewDrag();
561 //Util.Debug("<< settingsApply");
7d1dc09a 562 },
563
45c70c9e 564 // Open menu
565 openSettingsMenu: function() {
bbbf42bb 566 // Close the description panel
ae510306 567 document.getElementById('noVNC_description').style.display = "none";
bbbf42bb
SR
568 // Close clipboard panel if open
569 if (UI.clipboardOpen === true) {
570 UI.toggleClipboardPanel();
571 }
45c70c9e 572 // Close connection settings if open
573 if (UI.connSettingsOpen === true) {
574 UI.toggleConnectPanel();
575 }
bbbf42bb
SR
576 // Close XVP panel if open
577 if (UI.xvpOpen === true) {
578 UI.toggleXvpPanel();
579 }
ae510306
SR
580 document.getElementById('noVNC_settings').style.display = "block";
581 document.getElementById('noVNC_settings_button').className = "noVNC_status_button_selected";
45c70c9e 582 UI.settingsOpen = true;
583 },
bbbf42bb 584
45c70c9e 585 // Close menu (without applying settings)
586 closeSettingsMenu: function() {
ae510306
SR
587 document.getElementById('noVNC_settings').style.display = "none";
588 document.getElementById('noVNC_settings_button').className = "noVNC_status_button";
45c70c9e 589 UI.settingsOpen = false;
bbbf42bb
SR
590 },
591
592 // Toggle the settings menu:
593 // On open, settings are refreshed from saved cookies.
594 // On close, settings are applied
595 toggleSettingsPanel: function() {
596 // Close the description panel
ae510306 597 document.getElementById('noVNC_description').style.display = "none";
bbbf42bb
SR
598 if (UI.settingsOpen) {
599 UI.settingsApply();
600 UI.closeSettingsMenu();
601 } else {
602 UI.updateSetting('encrypt');
603 UI.updateSetting('true_color');
58ded70d 604 if (Util.browserSupportsCursorURIs()) {
bbbf42bb
SR
605 UI.updateSetting('cursor');
606 } else {
607 UI.updateSetting('cursor', !UI.isTouchDevice);
ae510306 608 document.getElementById('noVNC_setting_cursor').disabled = true;
bbbf42bb
SR
609 }
610 UI.updateSetting('clip');
f8b399d7 611 UI.updateSetting('resize');
bbbf42bb
SR
612 UI.updateSetting('shared');
613 UI.updateSetting('view_only');
614 UI.updateSetting('path');
615 UI.updateSetting('repeaterID');
616 UI.updateSetting('stylesheet');
617 UI.updateSetting('logging');
618
619 UI.openSettingsMenu();
620 }
621 },
622
95dd6001 623/* ------^-------
624 * /SETTINGS
625 * ==============
626 * XVP
627 * ------v------*/
628
bbbf42bb
SR
629 // Show the XVP panel
630 toggleXvpPanel: function() {
bbbf42bb 631 // Close the description panel
ae510306 632 document.getElementById('noVNC_description').style.display = "none";
bbbf42bb
SR
633 // Close settings if open
634 if (UI.settingsOpen === true) {
635 UI.settingsApply();
636 UI.closeSettingsMenu();
bbbf42bb
SR
637 }
638 // Close connection settings if open
639 if (UI.connSettingsOpen === true) {
640 UI.toggleConnectPanel();
641 }
bbbf42bb
SR
642 // Close clipboard panel if open
643 if (UI.clipboardOpen === true) {
644 UI.toggleClipboardPanel();
bbbf42bb 645 }
bbbf42bb
SR
646 // Toggle XVP panel
647 if (UI.xvpOpen === true) {
ae510306
SR
648 document.getElementById('noVNC_xvp').style.display = "none";
649 document.getElementById('noVNC_toggleXvp_button').className = "noVNC_status_button";
bbbf42bb
SR
650 UI.xvpOpen = false;
651 } else {
ae510306
SR
652 document.getElementById('noVNC_xvp').style.display = "block";
653 document.getElementById('noVNC_toggleXvp_button').className = "noVNC_status_button_selected";
bbbf42bb 654 UI.xvpOpen = true;
bbbf42bb 655 }
bbbf42bb
SR
656 },
657
658 // Disable/enable XVP button
9e45354e 659 updateXvpButton: function(ver) {
bbbf42bb 660 if (ver >= 1) {
ae510306 661 document.getElementById('noVNC_toggleXvp_button').style.display = 'inline';
bbbf42bb 662 } else {
ae510306 663 document.getElementById('noVNC_toggleXvp_button').style.display = 'none';
bbbf42bb
SR
664 // Close XVP panel if open
665 if (UI.xvpOpen === true) {
666 UI.toggleXvpPanel();
667 }
668 }
669 },
670
95dd6001 671/* ------^-------
672 * /XVP
673 * ==============
674 * CLIPBOARD
675 * ------v------*/
f8b399d7 676
bbbf42bb
SR
677 // Show the clipboard panel
678 toggleClipboardPanel: function() {
679 // Close the description panel
ae510306 680 document.getElementById('noVNC_description').style.display = "none";
bbbf42bb
SR
681 // Close settings if open
682 if (UI.settingsOpen === true) {
683 UI.settingsApply();
684 UI.closeSettingsMenu();
685 }
686 // Close connection settings if open
687 if (UI.connSettingsOpen === true) {
688 UI.toggleConnectPanel();
689 }
bbbf42bb
SR
690 // Close XVP panel if open
691 if (UI.xvpOpen === true) {
692 UI.toggleXvpPanel();
693 }
694 // Toggle Clipboard Panel
695 if (UI.clipboardOpen === true) {
ae510306
SR
696 document.getElementById('noVNC_clipboard').style.display = "none";
697 document.getElementById('noVNC_clipboard_button').className = "noVNC_status_button";
bbbf42bb
SR
698 UI.clipboardOpen = false;
699 } else {
ae510306
SR
700 document.getElementById('noVNC_clipboard').style.display = "block";
701 document.getElementById('noVNC_clipboard_button').className = "noVNC_status_button_selected";
bbbf42bb
SR
702 UI.clipboardOpen = true;
703 }
bbbf42bb
SR
704 },
705
4d26f58e 706 clipboardReceive: function(rfb, text) {
707 Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "...");
ae510306 708 document.getElementById('noVNC_clipboard_text').value = text;
4d26f58e 709 Util.Debug("<< UI.clipboardReceive");
95dd6001 710 },
711
4d26f58e 712 clipboardClear: function() {
ae510306 713 document.getElementById('noVNC_clipboard_text').value = "";
95dd6001 714 UI.rfb.clipboardPasteFrom("");
715 },
716
4d26f58e 717 clipboardSend: function() {
ae510306 718 var text = document.getElementById('noVNC_clipboard_text').value;
4d26f58e 719 Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "...");
95dd6001 720 UI.rfb.clipboardPasteFrom(text);
4d26f58e 721 Util.Debug("<< UI.clipboardSend");
95dd6001 722 },
723
724/* ------^-------
725 * /CLIPBOARD
726 * ==============
727 * CONNECTION
728 * ------v------*/
729
ab81ddf5 730 // Show the connection settings panel/menu
731 toggleConnectPanel: function() {
732 // Close the description panel
ae510306 733 document.getElementById('noVNC_description').style.display = "none";
ab81ddf5 734 // Close connection settings if open
735 if (UI.settingsOpen === true) {
736 UI.settingsApply();
737 UI.closeSettingsMenu();
ae510306 738 document.getElementById('noVNC_connectPanel_button').className = "noVNC_status_button";
ab81ddf5 739 }
740 // Close clipboard panel if open
741 if (UI.clipboardOpen === true) {
742 UI.toggleClipboardPanel();
743 }
744 // Close XVP panel if open
745 if (UI.xvpOpen === true) {
746 UI.toggleXvpPanel();
747 }
748
749 // Toggle Connection Panel
750 if (UI.connSettingsOpen === true) {
ae510306
SR
751 document.getElementById('noVNC_controls').style.display = "none";
752 document.getElementById('noVNC_connectPanel_button').className = "noVNC_status_button";
ab81ddf5 753 UI.connSettingsOpen = false;
754 UI.saveSetting('host');
755 UI.saveSetting('port');
756 UI.saveSetting('token');
757 //UI.saveSetting('password');
758 } else {
ae510306
SR
759 document.getElementById('noVNC_controls').style.display = "block";
760 document.getElementById('noVNC_connectPanel_button').className = "noVNC_status_button_selected";
ab81ddf5 761 UI.connSettingsOpen = true;
ae510306 762 document.getElementById('noVNC_setting_host').focus();
ab81ddf5 763 }
bbbf42bb
SR
764 },
765
766 connect: function() {
767 UI.closeSettingsMenu();
c506a481 768 UI.toggleConnectPanel();
f00b1e37 769
ae510306
SR
770 var host = document.getElementById('noVNC_setting_host').value;
771 var port = document.getElementById('noVNC_setting_port').value;
772 var password = document.getElementById('noVNC_setting_password').value;
773 var token = document.getElementById('noVNC_setting_token').value;
774 var path = document.getElementById('noVNC_setting_path').value;
c55f05f6
MXPN
775
776 //if token is in path then ignore the new token variable
777 if (token) {
778 path = WebUtil.injectParamIfMissing(path, "token", token);
779 }
780
bbbf42bb
SR
781 if ((!host) || (!port)) {
782 throw new Error("Must set host and port");
783 }
53fc7392 784
d9fc1c7b 785 if (!UI.initRFB()) return;
58ded70d 786
bbbf42bb
SR
787 UI.rfb.set_encrypt(UI.getSetting('encrypt'));
788 UI.rfb.set_true_color(UI.getSetting('true_color'));
789 UI.rfb.set_local_cursor(UI.getSetting('cursor'));
790 UI.rfb.set_shared(UI.getSetting('shared'));
791 UI.rfb.set_view_only(UI.getSetting('view_only'));
792 UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
53fc7392 793
bbbf42bb 794 UI.rfb.connect(host, port, password, path);
8e0f0088 795
bbbf42bb 796 //Close dialog.
67685d07 797 setTimeout(UI.setBarPosition, 100);
ae510306
SR
798 document.getElementById('noVNC_logo').style.display = "none";
799 document.getElementById('noVNC_screen').style.display = "inline";
bbbf42bb 800 },
5299db1a 801
bbbf42bb
SR
802 disconnect: function() {
803 UI.closeSettingsMenu();
804 UI.rfb.disconnect();
8e0f0088 805
f8b399d7 806 // Restore the callback used for initial resize
ab81ddf5 807 UI.rfb.set_onFBUComplete(UI.initialResize);
f8b399d7 808
ae510306
SR
809 document.getElementById('noVNC_logo').style.display = "block";
810 document.getElementById('noVNC_screen').style.display = "none";
fdedbafb 811
e543525f 812 // Don't display the connection settings until we're actually disconnected
bbbf42bb
SR
813 },
814
95dd6001 815 setPassword: function() {
ae510306 816 UI.rfb.sendPassword(document.getElementById('noVNC_setting_password').value);
95dd6001 817 //Reset connect button.
ae510306
SR
818 document.getElementById('noVNC_connect_button').value = "Connect";
819 document.getElementById('noVNC_connect_button').onclick = UI.connect;
95dd6001 820 //Hide connection panel.
821 UI.toggleConnectPanel();
822 return false;
823 },
58ded70d 824
95dd6001 825/* ------^-------
826 * /CONNECTION
827 * ==============
828 * FULLSCREEN
829 * ------v------*/
830
7d1dc09a 831 toggleFullscreen: function() {
832 if (document.fullscreenElement || // alternative standard method
833 document.mozFullScreenElement || // currently working methods
834 document.webkitFullscreenElement ||
a6357e82 835 document.msFullscreenElement) {
7d1dc09a 836 if (document.exitFullscreen) {
837 document.exitFullscreen();
838 } else if (document.mozCancelFullScreen) {
839 document.mozCancelFullScreen();
840 } else if (document.webkitExitFullscreen) {
841 document.webkitExitFullscreen();
842 } else if (document.msExitFullscreen) {
843 document.msExitFullscreen();
844 }
845 } else {
846 if (document.documentElement.requestFullscreen) {
847 document.documentElement.requestFullscreen();
848 } else if (document.documentElement.mozRequestFullScreen) {
849 document.documentElement.mozRequestFullScreen();
850 } else if (document.documentElement.webkitRequestFullscreen) {
851 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
852 } else if (document.body.msRequestFullscreen) {
853 document.body.msRequestFullscreen();
854 }
855 }
a6357e82 856 UI.enableDisableViewClip();
7d1dc09a 857 UI.updateFullscreenButton();
bbbf42bb
SR
858 },
859
7d1dc09a 860 updateFullscreenButton: function() {
861 if (document.fullscreenElement || // alternative standard method
862 document.mozFullScreenElement || // currently working methods
863 document.webkitFullscreenElement ||
864 document.msFullscreenElement ) {
ae510306 865 document.getElementById('noVNC_fullscreen_button').className = "noVNC_status_button_selected";
7d1dc09a 866 } else {
ae510306 867 document.getElementById('noVNC_fullscreen_button').className = "noVNC_status_button";
7d1dc09a 868 }
869 },
870
95dd6001 871/* ------^-------
872 * /FULLSCREEN
873 * ==============
874 * RESIZE
875 * ------v------*/
777cb7a0 876
877 // Apply remote resizing or local scaling
0bd2cbac 878 applyResizeMode: function() {
58ded70d
SR
879 if (!UI.rfb) return;
880
777cb7a0 881 var screen = UI.screenSize();
882
883 if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
884
885 var display = UI.rfb.get_display();
886 var resizeMode = UI.getSetting('resize');
887
888 if (resizeMode === 'remote') {
889
890 // Request changing the resolution of the remote display to
891 // the size of the local browser viewport.
892
893 // In order to not send multiple requests before the browser-resize
894 // is finished we wait 0.5 seconds before sending the request.
895 clearTimeout(UI.resizeTimeout);
896 UI.resizeTimeout = setTimeout(function(){
897
898 // Limit the viewport to the size of the browser window
899 display.set_maxWidth(screen.w);
900 display.set_maxHeight(screen.h);
901
5fd3f88e 902 Util.Debug('Attempting requestDesktopSize(' +
777cb7a0 903 screen.w + ', ' + screen.h + ')');
904
905 // Request a remote size covering the viewport
5fd3f88e 906 UI.rfb.requestDesktopSize(screen.w, screen.h);
777cb7a0 907 }, 500);
908
909 } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
910 var downscaleOnly = resizeMode === 'downscale';
911 var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
912 UI.rfb.get_mouse().set_scale(scaleRatio);
913 Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
914 }
915 }
bbbf42bb
SR
916 },
917
777cb7a0 918 // The screen is always the same size as the available viewport
919 // in the browser window minus the height of the control bar
0bd2cbac 920 screenSize: function() {
ae510306 921 var screen = document.getElementById('noVNC_screen');
777cb7a0 922
923 // Hide the scrollbars until the size is calculated
924 screen.style.overflow = "hidden";
925
926 var pos = Util.getPosition(screen);
927 var w = pos.width;
928 var h = pos.height;
929
930 screen.style.overflow = "visible";
931
932 if (isNaN(w) || isNaN(h)) {
933 return false;
934 } else {
935 return {w: w, h: h};
936 }
bbbf42bb
SR
937 },
938
777cb7a0 939 // Normally we only apply the current resize mode after a window resize
940 // event. This means that when a new connection is opened, there is no
941 // resize mode active.
942 // We have to wait until the first FBU because this is where the client
943 // will find the supported encodings of the server. Some calls later in
944 // the chain is dependant on knowing the server-capabilities.
945 initialResize: function(rfb, fbu) {
946 UI.applyResizeMode();
947 // After doing this once, we remove the callback.
948 UI.rfb.set_onFBUComplete(function() { });
bbbf42bb
SR
949 },
950
95dd6001 951/* ------^-------
952 * /RESIZE
953 * ==============
954 * CLIPPING
955 * ------v------*/
956
30bfff81 957 // Set and configure viewport clipping
bbbf42bb
SR
958 setViewClip: function(clip) {
959 var display;
960 if (UI.rfb) {
961 display = UI.rfb.get_display();
962 } else {
a6357e82 963 UI.forceSetting('clip', clip);
bbbf42bb
SR
964 return;
965 }
8e0f0088 966
bbbf42bb
SR
967 var cur_clip = display.get_viewport();
968
969 if (typeof(clip) !== 'boolean') {
970 // Use current setting
971 clip = UI.getSetting('clip');
972 }
8e0f0088 973
bbbf42bb
SR
974 if (clip && !cur_clip) {
975 // Turn clipping on
976 UI.updateSetting('clip', true);
977 } else if (!clip && cur_clip) {
978 // Turn clipping off
979 UI.updateSetting('clip', false);
980 display.set_viewport(false);
31ddaa1c 981 // Disable max dimensions
fdedbafb 982 display.set_maxWidth(0);
983 display.set_maxHeight(0);
f8b399d7 984 display.viewportChangeSize();
bbbf42bb
SR
985 }
986 if (UI.getSetting('clip')) {
987 // If clipping, update clipping settings
bbbf42bb 988 display.set_viewport(true);
fdedbafb 989
553864e8 990 var size = UI.screenSize();
fdedbafb 991 if (size) {
992 display.set_maxWidth(size.w);
993 display.set_maxHeight(size.h);
994
995 // Hide potential scrollbars that can skew the position
ae510306 996 document.getElementById('noVNC_screen').style.overflow = "hidden";
fdedbafb 997
998 // The x position marks the left margin of the canvas,
999 // remove the margin from both sides to keep it centered
ae510306 1000 var new_w = size.w - (2 * Util.getPosition(document.getElementById('noVNC_canvas')).x);
fdedbafb 1001
ae510306 1002 document.getElementById('noVNC_screen').style.overflow = "visible";
fdedbafb 1003
1004 display.viewportChangeSize(new_w, size.h);
1005 }
bbbf42bb
SR
1006 }
1007 },
1008
30bfff81 1009 // Handle special cases where clipping is forced on/off or locked
0bd2cbac 1010 enableDisableViewClip: function() {
ae510306 1011 var resizeSetting = document.getElementById('noVNC_setting_resize');
a6357e82 1012 var connected = UI.rfb && UI.rfb_state === 'normal';
1013
f620259b 1014 if (UI.isSafari) {
1015 // Safari auto-hides the scrollbars which makes them
1016 // impossible to use in most cases
1017 UI.setViewClip(true);
ae510306 1018 document.getElementById('noVNC_setting_clip').disabled = true;
682fd02b 1019 } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') {
a6357e82 1020 // Disable clipping if we are scaling
1021 UI.setViewClip(false);
ae510306 1022 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1023 } else if (document.msFullscreenElement) {
1024 // The browser is IE and we are in fullscreen mode.
1025 // - We need to force clipping while in fullscreen since
1026 // scrollbars doesn't work.
4e471b5b 1027 UI.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
a6357e82 1028 UI.rememberedClipSetting = UI.getSetting('clip');
1029 UI.setViewClip(true);
ae510306 1030 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1031 } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
1032 // Restore view clip to what it was before fullscreen on IE
1033 UI.setViewClip(UI.rememberedClipSetting);
ae510306 1034 document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
30bfff81 1035 } else {
ae510306 1036 document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
30bfff81 1037 if (UI.isTouchDevice) {
a6357e82 1038 UI.setViewClip(true);
30bfff81 1039 }
1040 }
1041 },
1042
95dd6001 1043/* ------^-------
1044 * /CLIPPING
1045 * ==============
1046 * VIEWDRAG
1047 * ------v------*/
1048
e00698fe 1049 // Update the viewport drag state
31ddaa1c 1050 updateViewDrag: function(drag) {
58ded70d 1051 if (!UI.rfb) return;
bbbf42bb 1052
ae510306 1053 var viewDragButton = document.getElementById('noVNC_view_drag_button');
bbbf42bb 1054
e00698fe 1055 // Check if viewport drag is possible. It is only possible
1056 // if the remote display is clipping the client display.
f8b399d7 1057 if (UI.rfb_state === 'normal' &&
1058 UI.rfb.get_display().get_viewport() &&
fdedbafb 1059 UI.rfb.get_display().clippingDisplay()) {
31ddaa1c 1060
e00698fe 1061 viewDragButton.style.display = "inline";
1062 viewDragButton.disabled = false;
29a0e6a8 1063
31ddaa1c 1064 } else {
e00698fe 1065 // The size of the remote display is the same or smaller
1066 // than the client display. Make sure viewport drag isn't
1067 // active when it can't be used.
31ddaa1c 1068 if (UI.rfb.get_viewportDrag) {
e00698fe 1069 viewDragButton.className = "noVNC_status_button";
31ddaa1c 1070 UI.rfb.set_viewportDrag(false);
1071 }
1072
e00698fe 1073 // The button is disabled instead of hidden on touch devices
31ddaa1c 1074 if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
e00698fe 1075 viewDragButton.style.display = "inline";
1076 viewDragButton.disabled = true;
31ddaa1c 1077 } else {
e00698fe 1078 viewDragButton.style.display = "none";
31ddaa1c 1079 }
1080 return;
1081 }
1082
1083 if (typeof(drag) !== "undefined" &&
1084 typeof(drag) !== "object") {
1085 if (drag) {
e00698fe 1086 viewDragButton.className = "noVNC_status_button_selected";
31ddaa1c 1087 UI.rfb.set_viewportDrag(true);
1088 } else {
e00698fe 1089 viewDragButton.className = "noVNC_status_button";
31ddaa1c 1090 UI.rfb.set_viewportDrag(false);
1091 }
1092 }
1093 },
29a0e6a8 1094
31ddaa1c 1095 toggleViewDrag: function() {
1096 if (!UI.rfb) return;
1097
ae510306 1098 var viewDragButton = document.getElementById('noVNC_view_drag_button');
31ddaa1c 1099 if (UI.rfb.get_viewportDrag()) {
e00698fe 1100 viewDragButton.className = "noVNC_status_button";
31ddaa1c 1101 UI.rfb.set_viewportDrag(false);
f8b399d7 1102 } else {
e00698fe 1103 viewDragButton.className = "noVNC_status_button_selected";
31ddaa1c 1104 UI.rfb.set_viewportDrag(true);
f8b399d7 1105 }
1106 },
1107
95dd6001 1108/* ------^-------
1109 * /VIEWDRAG
1110 * ==============
1111 * KEYBOARD
1112 * ------v------*/
fdf21468 1113
bbbf42bb
SR
1114 // On touch devices, show the OS keyboard
1115 showKeyboard: function() {
ae510306
SR
1116 var kbi = document.getElementById('noVNC_keyboardinput');
1117 var skb = document.getElementById('noVNC_keyboard_button');
bbbf42bb
SR
1118 var l = kbi.value.length;
1119 if(UI.keyboardVisible === false) {
1120 kbi.focus();
1121 try { kbi.setSelectionRange(l, l); } // Move the caret to the end
1122 catch (err) {} // setSelectionRange is undefined in Google Chrome
1123 UI.keyboardVisible = true;
1124 skb.className = "noVNC_status_button_selected";
1125 } else if(UI.keyboardVisible === true) {
1126 kbi.blur();
1127 skb.className = "noVNC_status_button";
1128 UI.keyboardVisible = false;
1129 }
1130 },
1131
fdf21468 1132 hideKeyboard: function() {
ae510306 1133 document.getElementById('noVNC_keyboard_button').className = "noVNC_status_button";
fdf21468 1134 //Weird bug in iOS if you change keyboardVisible
1135 //here it does not actually occur so next time
1136 //you click keyboard icon it doesnt work.
1137 UI.hideKeyboardTimeout = setTimeout(function() {
1138 UI.keyboardVisible = false;
1139 },100);
1140 },
1141
bbbf42bb
SR
1142 keepKeyboard: function() {
1143 clearTimeout(UI.hideKeyboardTimeout);
1144 if(UI.keyboardVisible === true) {
ae510306
SR
1145 document.getElementById('noVNC_keyboardinput').focus();
1146 document.getElementById('noVNC_keyboard_button').className = "noVNC_status_button_selected";
bbbf42bb 1147 } else if(UI.keyboardVisible === false) {
ae510306
SR
1148 document.getElementById('noVNC_keyboardinput').blur();
1149 document.getElementById('noVNC_keyboard_button').className = "noVNC_status_button";
bbbf42bb
SR
1150 }
1151 },
1152
1153 keyboardinputReset: function() {
ae510306 1154 var kbi = document.getElementById('noVNC_keyboardinput');
bbbf42bb
SR
1155 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1156 UI.lastKeyboardinput = kbi.value;
1157 },
1158
1159 // When normal keyboard events are left uncought, use the input events from
1160 // the keyboardinput element instead and generate the corresponding key events.
1161 // This code is required since some browsers on Android are inconsistent in
1162 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1163 keyInput: function(event) {
3b8ec46f 1164
58ded70d 1165 if (!UI.rfb) return;
3b8ec46f 1166
bbbf42bb 1167 var newValue = event.target.value;
1138bdd4 1168
1169 if (!UI.lastKeyboardinput) {
1170 UI.keyboardinputReset();
1171 }
cb3e4deb 1172 var oldValue = UI.lastKeyboardinput;
bbbf42bb
SR
1173
1174 var newLen;
1175 try {
1176 // Try to check caret position since whitespace at the end
1177 // will not be considered by value.length in some browsers
1178 newLen = Math.max(event.target.selectionStart, newValue.length);
1179 } catch (err) {
1180 // selectionStart is undefined in Google Chrome
1181 newLen = newValue.length;
1182 }
1183 var oldLen = oldValue.length;
1184
1185 var backspaces;
1186 var inputs = newLen - oldLen;
1187 if (inputs < 0) {
1188 backspaces = -inputs;
1189 } else {
1190 backspaces = 0;
1191 }
8e0f0088 1192
bbbf42bb
SR
1193 // Compare the old string with the new to account for
1194 // text-corrections or other input that modify existing text
1195 var i;
1196 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1197 if (newValue.charAt(i) != oldValue.charAt(i)) {
1198 inputs = newLen - i;
1199 backspaces = oldLen - i;
1200 break;
1201 }
1202 }
1203
1204 // Send the key events
1205 for (i = 0; i < backspaces; i++) {
ae510306 1206 UI.rfb.sendKey(KeyTable.XK_BackSpace);
bbbf42bb
SR
1207 }
1208 for (i = newLen - inputs; i < newLen; i++) {
1209 UI.rfb.sendKey(newValue.charCodeAt(i));
1210 }
1211
1212 // Control the text content length in the keyboardinput element
1213 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1214 UI.keyboardinputReset();
1215 } else if (newLen < 1) {
1216 // There always have to be some text in the keyboardinput
1217 // element with which backspace can interact.
1218 UI.keyboardinputReset();
1219 // This sometimes causes the keyboard to disappear for a second
1220 // but it is required for the android keyboard to recognize that
1221 // text has been added to the field
1222 event.target.blur();
1223 // This has to be ran outside of the input handler in order to work
67685d07 1224 setTimeout(UI.keepKeyboard, 0);
bbbf42bb
SR
1225 } else {
1226 UI.lastKeyboardinput = newValue;
1227 }
1228 },
1229
cd611a53 1230 toggleExtraKeys: function() {
bbbf42bb
SR
1231 UI.keepKeyboard();
1232 if(UI.extraKeysVisible === false) {
ae510306
SR
1233 document.getElementById('noVNC_toggleCtrl_button').style.display = "inline";
1234 document.getElementById('noVNC_toggleAlt_button').style.display = "inline";
1235 document.getElementById('noVNC_sendTab_button').style.display = "inline";
1236 document.getElementById('noVNC_sendEsc_button').style.display = "inline";
1237 document.getElementById('noVNC_toggleExtraKeys_button').className = "noVNC_status_button_selected";
bbbf42bb
SR
1238 UI.extraKeysVisible = true;
1239 } else if(UI.extraKeysVisible === true) {
ae510306
SR
1240 document.getElementById('noVNC_toggleCtrl_button').style.display = "";
1241 document.getElementById('noVNC_toggleAlt_button').style.display = "";
1242 document.getElementById('noVNC_sendTab_button').style.display = "";
1243 document.getElementById('noVNC_sendEsc_button').style.display = "";
1244 document.getElementById('noVNC_toggleExtraKeys_button').className = "noVNC_status_button";
bbbf42bb
SR
1245 UI.extraKeysVisible = false;
1246 }
1247 },
1248
fdf21468 1249 sendEsc: function() {
1250 UI.keepKeyboard();
ae510306 1251 UI.rfb.sendKey(KeyTable.XK_Escape);
fdf21468 1252 },
1253
1254 sendTab: function() {
1255 UI.keepKeyboard();
ae510306 1256 UI.rfb.sendKey(KeyTable.XK_Tab);
fdf21468 1257 },
1258
bbbf42bb
SR
1259 toggleCtrl: function() {
1260 UI.keepKeyboard();
1261 if(UI.ctrlOn === false) {
ae510306
SR
1262 UI.rfb.sendKey(KeyTable.XK_Control_L, true);
1263 document.getElementById('noVNC_toggleCtrl_button').className = "noVNC_status_button_selected";
bbbf42bb
SR
1264 UI.ctrlOn = true;
1265 } else if(UI.ctrlOn === true) {
ae510306
SR
1266 UI.rfb.sendKey(KeyTable.XK_Control_L, false);
1267 document.getElementById('noVNC_toggleCtrl_button').className = "noVNC_status_button";
bbbf42bb
SR
1268 UI.ctrlOn = false;
1269 }
1270 },
1271
1272 toggleAlt: function() {
1273 UI.keepKeyboard();
1274 if(UI.altOn === false) {
ae510306
SR
1275 UI.rfb.sendKey(KeyTable.XK_Alt_L, true);
1276 document.getElementById('noVNC_toggleAlt_button').className = "noVNC_status_button_selected";
bbbf42bb
SR
1277 UI.altOn = true;
1278 } else if(UI.altOn === true) {
ae510306
SR
1279 UI.rfb.sendKey(KeyTable.XK_Alt_L, false);
1280 document.getElementById('noVNC_toggleAlt_button').className = "noVNC_status_button";
bbbf42bb
SR
1281 UI.altOn = false;
1282 }
1283 },
1284
fdf21468 1285 sendCtrlAltDel: function() {
1286 UI.rfb.sendCtrlAltDel();
bbbf42bb
SR
1287 },
1288
95dd6001 1289/* ------^-------
1290 * /KEYBOARD
1291 * ==============
1292 * MISC
1293 * ------v------*/
1294
1295 setMouseButton: function(num) {
1296 if (typeof num === 'undefined') {
1297 // Disable mouse buttons
1298 num = -1;
1299 }
1300 if (UI.rfb) {
1301 UI.rfb.get_mouse().set_touchButton(num);
1302 }
1303
1304 var blist = [0, 1,2,4];
1305 for (var b = 0; b < blist.length; b++) {
ae510306 1306 var button = document.getElementById('noVNC_mouse_button' + blist[b]);
95dd6001 1307 if (blist[b] === num) {
1308 button.style.display = "";
1309 } else {
1310 button.style.display = "none";
1311 }
1312 }
1313 },
1314
1315 displayBlur: function() {
1316 if (!UI.rfb) return;
1317
1318 UI.rfb.get_keyboard().set_focused(false);
1319 UI.rfb.get_mouse().set_focused(false);
1320 },
1321
1322 displayFocus: function() {
1323 if (!UI.rfb) return;
1324
1325 UI.rfb.get_keyboard().set_focused(true);
1326 UI.rfb.get_mouse().set_focused(true);
bbbf42bb
SR
1327 },
1328
95dd6001 1329 // Display the desktop name in the document title
1330 updateDocumentTitle: function(rfb, name) {
1331 document.title = name + " - noVNC";
bbbf42bb
SR
1332 },
1333
bbbf42bb
SR
1334 //Helper to add options to dropdown.
1335 addOption: function(selectbox, text, value) {
1336 var optn = document.createElement("OPTION");
1337 optn.text = text;
1338 optn.value = value;
1339 selectbox.options.add(optn);
1340 },
1341
1342 setBarPosition: function() {
ae510306
SR
1343 document.getElementById('noVNC_control_bar').style.top = (window.pageYOffset) + 'px';
1344 document.getElementById('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
bbbf42bb 1345
ae510306
SR
1346 var vncwidth = document.getElementById('noVNC_container').style.offsetWidth;
1347 document.getElementById('noVNC_control_bar').style.width = vncwidth + 'px';
bbbf42bb
SR
1348 }
1349
95dd6001 1350/* ------^-------
1351 * /MISC
1352 * ==============
1353 */
bbbf42bb 1354 };
ae510306
SR
1355
1356 /* [module] UI.load(); */
bbbf42bb 1357})();
ae510306
SR
1358
1359/* [module] export default UI; */