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