]>
git.proxmox.com Git - mirror_novnc.git/blob - include/ui.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2015 Samuel Mannehed for Cendio AB
5 * Licensed under MPL 2.0 (see LICENSE.txt)
7 * See README.md for usage and integration instructions.
10 /* jslint white: false, browser: true */
11 /* global window, $D, Util, WebUtil, RFB, Display */
20 // Load supporting scripts
21 window
.onscriptsload = function () { UI
.load(); };
22 Util
.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
23 "keysymdef.js", "keyboard.js", "input.js", "display.js",
24 "jsunzip.js", "rfb.js", "keysym.js"]);
30 connSettingsOpen
: false,
31 popupStatusTimeout
: null,
33 keyboardVisible
: false,
34 hideKeyboardTimeout
: null,
35 lastKeyboardinput
: null,
36 defaultKeyboardinputLen
: 100,
37 extraKeysVisible
: false,
42 // Setup rfb object, load settings from browser storage, then call
43 // UI.init to setup the UI/menus
44 load: function (callback
) {
45 WebUtil
.initSettings(UI
.start
, callback
);
48 // Render default UI and initialize settings menu
49 start: function(callback
) {
50 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
52 // Stylesheet selection dropdown
53 var sheet
= WebUtil
.selectStylesheet();
54 var sheets
= WebUtil
.getStylesheets();
56 for (i
= 0; i
< sheets
.length
; i
+= 1) {
57 UI
.addOption($D('noVNC_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
60 // Logging selection dropdown
61 var llevels
= ['error', 'warn', 'info', 'debug'];
62 for (i
= 0; i
< llevels
.length
; i
+= 1) {
63 UI
.addOption($D('noVNC_logging'),llevels
[i
], llevels
[i
]);
66 // Settings with immediate effects
67 UI
.initSetting('logging', 'warn');
68 WebUtil
.init_logging(UI
.getSetting('logging'));
70 UI
.initSetting('stylesheet', 'default');
71 WebUtil
.selectStylesheet(null);
72 // call twice to get around webkit bug
73 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
75 // if port == 80 (or 443) then it won't be present and should be
77 var port
= window
.location
.port
;
79 if (window
.location
.protocol
.substring(0,5) == 'https') {
82 else if (window
.location
.protocol
.substring(0,4) == 'http') {
87 /* Populate the controls if defaults are provided in the URL */
88 UI
.initSetting('host', window
.location
.hostname
);
89 UI
.initSetting('port', port
);
90 UI
.initSetting('password', '');
91 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
92 UI
.initSetting('true_color', true);
93 UI
.initSetting('cursor', !UI
.isTouchDevice
);
94 UI
.initSetting('resize', 'off');
95 UI
.initSetting('shared', true);
96 UI
.initSetting('view_only', false);
97 UI
.initSetting('path', 'websockify');
98 UI
.initSetting('repeaterID', '');
100 var autoconnect
= WebUtil
.getQueryVar('autoconnect', false);
101 if (autoconnect
=== 'true' || autoconnect
== '1') {
108 UI
.updateVisualState();
110 $D('noVNC_host').focus();
112 // Show mouse selector buttons on touch screen devices
113 if (UI
.isTouchDevice
) {
114 // Show mobile buttons
115 $D('noVNC_mobile_buttons').style
.display
= "inline";
117 // Remove the address bar
118 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
119 UI
.forceSetting('clip', true);
121 UI
.initSetting('clip', false);
127 Util
.addEvent(window
, 'resize', function () {
134 Util
.addEvent(window
, 'load', UI
.keyboardinputReset
);
136 Util
.addEvent(window
, 'beforeunload', function () {
137 if (UI
.rfb
&& UI
.rfb_state
=== 'normal') {
138 return "You are currently connected.";
142 // Show description by default when hosted at for kanaka.github.com
143 if (location
.host
=== "kanaka.github.io") {
144 // Open the description dialog
145 $D('noVNC_description').style
.display
= "block";
147 // Show the connect panel on first load unless autoconnecting
148 if (autoconnect
=== UI
.connSettingsOpen
) {
149 UI
.toggleConnectPanel();
153 // Add mouse event click/focus/blur event handlers to the UI
154 UI
.addMouseHandlers();
156 if (typeof callback
=== "function") {
161 initRFB: function () {
163 UI
.rfb
= new RFB({'target': $D('noVNC_canvas'),
164 'onUpdateState': UI
.updateState
,
165 'onXvpInit': UI
.updateXvpVisualState
,
166 'onClipboard': UI
.clipReceive
,
167 'onFBUComplete': UI
.FBUComplete
,
168 'onFBResize': UI
.updateViewDrag
,
169 'onDesktopName': UI
.updateDocumentTitle
});
172 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
177 addMouseHandlers: function() {
178 // Setup interface handlers that can't be inline
179 $D("noVNC_view_drag_button").onclick
= UI
.toggleViewDrag
;
180 $D("noVNC_mouse_button0").onclick = function () { UI
.setMouseButton(1); };
181 $D("noVNC_mouse_button1").onclick = function () { UI
.setMouseButton(2); };
182 $D("noVNC_mouse_button2").onclick = function () { UI
.setMouseButton(4); };
183 $D("noVNC_mouse_button4").onclick = function () { UI
.setMouseButton(0); };
184 $D("showKeyboard").onclick
= UI
.showKeyboard
;
186 $D("keyboardinput").oninput
= UI
.keyInput
;
187 $D("keyboardinput").onblur
= UI
.keyInputBlur
;
188 $D("keyboardinput").onsubmit = function () { return false; };
190 $D("showExtraKeysButton").onclick
= UI
.showExtraKeys
;
191 $D("toggleCtrlButton").onclick
= UI
.toggleCtrl
;
192 $D("toggleAltButton").onclick
= UI
.toggleAlt
;
193 $D("sendTabButton").onclick
= UI
.sendTab
;
194 $D("sendEscButton").onclick
= UI
.sendEsc
;
196 $D("sendCtrlAltDelButton").onclick
= UI
.sendCtrlAltDel
;
197 $D("xvpShutdownButton").onclick
= UI
.xvpShutdown
;
198 $D("xvpRebootButton").onclick
= UI
.xvpReboot
;
199 $D("xvpResetButton").onclick
= UI
.xvpReset
;
200 $D("noVNC_status").onclick
= UI
.togglePopupStatus
;
201 $D("noVNC_popup_status").onclick
= UI
.togglePopupStatus
;
202 $D("xvpButton").onclick
= UI
.toggleXvpPanel
;
203 $D("clipboardButton").onclick
= UI
.toggleClipboardPanel
;
204 $D("settingsButton").onclick
= UI
.toggleSettingsPanel
;
205 $D("connectButton").onclick
= UI
.toggleConnectPanel
;
206 $D("disconnectButton").onclick
= UI
.disconnect
;
207 $D("descriptionButton").onclick
= UI
.toggleConnectPanel
;
209 $D("noVNC_clipboard_text").onfocus
= UI
.displayBlur
;
210 $D("noVNC_clipboard_text").onblur
= UI
.displayFocus
;
211 $D("noVNC_clipboard_text").onchange
= UI
.clipSend
;
212 $D("noVNC_clipboard_clear_button").onclick
= UI
.clipClear
;
214 $D("noVNC_settings_menu").onmouseover
= UI
.displayBlur
;
215 $D("noVNC_settings_menu").onmouseover
= UI
.displayFocus
;
216 $D("noVNC_apply").onclick
= UI
.settingsApply
;
218 $D("noVNC_connect_button").onclick
= UI
.connect
;
220 $D("noVNC_resize").onchange = function () {
221 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
222 UI
.enableDisableViewClip(connected
);
226 onresize: function (callback
) {
229 var size
= UI
.getCanvasLimit();
231 if (size
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
232 var display
= UI
.rfb
.get_display();
233 var scaleType
= UI
.getSetting('resize');
234 if (scaleType
=== 'remote') {
235 // use remote resizing
237 // When the local window has been resized, wait until the size remains
238 // the same for 0.5 seconds before sending the request for changing
239 // the resolution of the session
240 clearTimeout(resizeTimeout
);
241 resizeTimeout
= setTimeout(function(){
242 display
.set_maxWidth(size
.w
);
243 display
.set_maxHeight(size
.h
);
244 Util
.Debug('Attempting setDesktopSize(' +
245 size
.w
+ ', ' + size
.h
+ ')');
246 UI
.rfb
.setDesktopSize(size
.w
, size
.h
);
248 } else if (scaleType
=== 'scale' || scaleType
=== 'downscale') {
251 var downscaleOnly
= scaleType
=== 'downscale';
252 var scaleRatio
= display
.autoscale(size
.w
, size
.h
, downscaleOnly
);
253 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
254 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
259 getCanvasLimit: function () {
260 var container
= $D('noVNC_container');
262 // Hide the scrollbars until the size is calculated
263 container
.style
.overflow
= "hidden";
265 var pos
= Util
.getPosition(container
);
269 container
.style
.overflow
= "visible";
271 if (isNaN(w
) || isNaN(h
)) {
278 // Read form control compatible setting from cookie
279 getSetting: function(name
) {
280 var ctrl
= $D('noVNC_' + name
);
281 var val
= WebUtil
.readSetting(name
);
282 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
283 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
292 // Update cookie and form control setting. If value is not set, then
293 // updates from control to current cookie setting.
294 updateSetting: function(name
, value
) {
296 // Save the cookie for this session
297 if (typeof value
!== 'undefined') {
298 WebUtil
.writeSetting(name
, value
);
301 // Update the settings control
302 value
= UI
.getSetting(name
);
304 var ctrl
= $D('noVNC_' + name
);
305 if (ctrl
.type
=== 'checkbox') {
306 ctrl
.checked
= value
;
308 } else if (typeof ctrl
.options
!== 'undefined') {
309 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
310 if (ctrl
.options
[i
].value
=== value
) {
311 ctrl
.selectedIndex
= i
;
316 /*Weird IE9 error leads to 'null' appearring
317 in textboxes instead of ''.*/
318 if (value
=== null) {
325 // Save control setting to cookie
326 saveSetting: function(name
) {
327 var val
, ctrl
= $D('noVNC_' + name
);
328 if (ctrl
.type
=== 'checkbox') {
330 } else if (typeof ctrl
.options
!== 'undefined') {
331 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
335 WebUtil
.writeSetting(name
, val
);
336 //Util.Debug("Setting saved '" + name + "=" + val + "'");
340 // Initial page load read/initialization of settings
341 initSetting: function(name
, defVal
) {
342 // Check Query string followed by cookie
343 var val
= WebUtil
.getQueryVar(name
);
345 val
= WebUtil
.readSetting(name
, defVal
);
347 UI
.updateSetting(name
, val
);
351 // Force a setting to be a certain value
352 forceSetting: function(name
, val
) {
353 UI
.updateSetting(name
, val
);
358 // Show the popup status
359 togglePopupStatus: function(text
) {
360 var psp
= $D('noVNC_popup_status');
362 var closePopup = function() { psp
.style
.display
= "none"; };
364 if (window
.getComputedStyle(psp
).display
=== 'none') {
365 if (typeof text
=== 'string') {
366 psp
.innerHTML
= text
;
368 psp
.innerHTML
= $D('noVNC_status').innerHTML
;
370 psp
.style
.display
= "block";
371 psp
.style
.left
= window
.innerWidth
/2 -
372 parseInt(window
.getComputedStyle(psp
).width
)/2 -30 + "px";
374 // Show the popup for a maximum of 1.5 seconds
375 UI
.popupStatusTimeout
= setTimeout(function() { closePopup(); }, 1500);
377 clearTimeout(UI
.popupStatusTimeout
);
382 // Show the XVP panel
383 toggleXvpPanel: function() {
384 // Close the description panel
385 $D('noVNC_description').style
.display
= "none";
386 // Close settings if open
387 if (UI
.settingsOpen
=== true) {
389 UI
.closeSettingsMenu();
391 // Close connection settings if open
392 if (UI
.connSettingsOpen
=== true) {
393 UI
.toggleConnectPanel();
395 // Close clipboard panel if open
396 if (UI
.clipboardOpen
=== true) {
397 UI
.toggleClipboardPanel();
400 if (UI
.xvpOpen
=== true) {
401 $D('noVNC_xvp').style
.display
= "none";
402 $D('xvpButton').className
= "noVNC_status_button";
405 $D('noVNC_xvp').style
.display
= "block";
406 $D('xvpButton').className
= "noVNC_status_button_selected";
411 // Show the clipboard panel
412 toggleClipboardPanel: function() {
413 // Close the description panel
414 $D('noVNC_description').style
.display
= "none";
415 // Close settings if open
416 if (UI
.settingsOpen
=== true) {
418 UI
.closeSettingsMenu();
420 // Close connection settings if open
421 if (UI
.connSettingsOpen
=== true) {
422 UI
.toggleConnectPanel();
424 // Close XVP panel if open
425 if (UI
.xvpOpen
=== true) {
428 // Toggle Clipboard Panel
429 if (UI
.clipboardOpen
=== true) {
430 $D('noVNC_clipboard').style
.display
= "none";
431 $D('clipboardButton').className
= "noVNC_status_button";
432 UI
.clipboardOpen
= false;
434 $D('noVNC_clipboard').style
.display
= "block";
435 $D('clipboardButton').className
= "noVNC_status_button_selected";
436 UI
.clipboardOpen
= true;
440 // Show the connection settings panel/menu
441 toggleConnectPanel: function() {
442 // Close the description panel
443 $D('noVNC_description').style
.display
= "none";
444 // Close connection settings if open
445 if (UI
.settingsOpen
=== true) {
447 UI
.closeSettingsMenu();
448 $D('connectButton').className
= "noVNC_status_button";
450 // Close clipboard panel if open
451 if (UI
.clipboardOpen
=== true) {
452 UI
.toggleClipboardPanel();
454 // Close XVP panel if open
455 if (UI
.xvpOpen
=== true) {
459 // Toggle Connection Panel
460 if (UI
.connSettingsOpen
=== true) {
461 $D('noVNC_controls').style
.display
= "none";
462 $D('connectButton').className
= "noVNC_status_button";
463 UI
.connSettingsOpen
= false;
464 UI
.saveSetting('host');
465 UI
.saveSetting('port');
466 //UI.saveSetting('password');
468 $D('noVNC_controls').style
.display
= "block";
469 $D('connectButton').className
= "noVNC_status_button_selected";
470 UI
.connSettingsOpen
= true;
471 $D('noVNC_host').focus();
475 // Toggle the settings menu:
476 // On open, settings are refreshed from saved cookies.
477 // On close, settings are applied
478 toggleSettingsPanel: function() {
479 // Close the description panel
480 $D('noVNC_description').style
.display
= "none";
481 if (UI
.settingsOpen
) {
483 UI
.closeSettingsMenu();
485 UI
.updateSetting('encrypt');
486 UI
.updateSetting('true_color');
487 if (Util
.browserSupportsCursorURIs()) {
488 UI
.updateSetting('cursor');
490 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
491 $D('noVNC_cursor').disabled
= true;
493 UI
.updateSetting('clip');
494 UI
.updateSetting('resize');
495 UI
.updateSetting('shared');
496 UI
.updateSetting('view_only');
497 UI
.updateSetting('path');
498 UI
.updateSetting('repeaterID');
499 UI
.updateSetting('stylesheet');
500 UI
.updateSetting('logging');
502 UI
.openSettingsMenu();
507 openSettingsMenu: function() {
508 // Close the description panel
509 $D('noVNC_description').style
.display
= "none";
510 // Close clipboard panel if open
511 if (UI
.clipboardOpen
=== true) {
512 UI
.toggleClipboardPanel();
514 // Close connection settings if open
515 if (UI
.connSettingsOpen
=== true) {
516 UI
.toggleConnectPanel();
518 // Close XVP panel if open
519 if (UI
.xvpOpen
=== true) {
522 $D('noVNC_settings').style
.display
= "block";
523 $D('settingsButton').className
= "noVNC_status_button_selected";
524 UI
.settingsOpen
= true;
527 // Close menu (without applying settings)
528 closeSettingsMenu: function() {
529 $D('noVNC_settings').style
.display
= "none";
530 $D('settingsButton').className
= "noVNC_status_button";
531 UI
.settingsOpen
= false;
534 // Save/apply settings when 'Apply' button is pressed
535 settingsApply: function() {
536 //Util.Debug(">> settingsApply");
537 UI
.saveSetting('encrypt');
538 UI
.saveSetting('true_color');
539 if (Util
.browserSupportsCursorURIs()) {
540 UI
.saveSetting('cursor');
543 UI
.saveSetting('resize');
545 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
546 UI
.forceSetting('clip', false);
549 UI
.saveSetting('clip');
550 UI
.saveSetting('shared');
551 UI
.saveSetting('view_only');
552 UI
.saveSetting('path');
553 UI
.saveSetting('repeaterID');
554 UI
.saveSetting('stylesheet');
555 UI
.saveSetting('logging');
557 // Settings with immediate (non-connected related) effect
558 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
559 WebUtil
.init_logging(UI
.getSetting('logging'));
562 //Util.Debug("<< settingsApply");
567 setPassword: function() {
568 UI
.rfb
.sendPassword($D('noVNC_password').value
);
569 //Reset connect button.
570 $D('noVNC_connect_button').value
= "Connect";
571 $D('noVNC_connect_button').onclick
= UI
.Connect
;
572 //Hide connection panel.
573 UI
.toggleConnectPanel();
577 sendCtrlAltDel: function() {
578 UI
.rfb
.sendCtrlAltDel();
581 xvpShutdown: function() {
582 UI
.rfb
.xvpShutdown();
585 xvpReboot: function() {
589 xvpReset: function() {
593 setMouseButton: function(num
) {
594 if (typeof num
=== 'undefined') {
595 // Disable mouse buttons
599 UI
.rfb
.get_mouse().set_touchButton(num
);
602 var blist
= [0, 1,2,4];
603 for (var b
= 0; b
< blist
.length
; b
++) {
604 var button
= $D('noVNC_mouse_button' + blist
[b
]);
605 if (blist
[b
] === num
) {
606 button
.style
.display
= "";
608 button
.style
.display
= "none";
613 updateState: function(rfb
, state
, oldstate
, msg
) {
614 UI
.rfb_state
= state
;
619 klass
= "noVNC_status_error";
622 klass
= "noVNC_status_normal";
625 $D('noVNC_logo').style
.display
= "block";
626 $D('noVNC_container').style
.display
= "none";
629 klass
= "noVNC_status_normal";
632 UI
.toggleConnectPanel();
634 $D('noVNC_connect_button').value
= "Send Password";
635 $D('noVNC_connect_button').onclick
= UI
.setPassword
;
636 $D('noVNC_password').focus();
638 klass
= "noVNC_status_warn";
641 klass
= "noVNC_status_warn";
645 if (typeof(msg
) !== 'undefined') {
646 $D('noVNC-control-bar').setAttribute("class", klass
);
647 $D('noVNC_status').innerHTML
= msg
;
650 UI
.updateVisualState();
653 // Disable/enable controls depending on connection state
654 updateVisualState: function() {
655 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
657 //Util.Debug(">> updateVisualState");
658 $D('noVNC_encrypt').disabled
= connected
;
659 $D('noVNC_true_color').disabled
= connected
;
660 if (Util
.browserSupportsCursorURIs()) {
661 $D('noVNC_cursor').disabled
= connected
;
663 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
664 $D('noVNC_cursor').disabled
= true;
667 UI
.enableDisableViewClip(connected
);
668 $D('noVNC_resize').disabled
= connected
;
669 $D('noVNC_shared').disabled
= connected
;
670 $D('noVNC_view_only').disabled
= connected
;
671 $D('noVNC_path').disabled
= connected
;
672 $D('noVNC_repeaterID').disabled
= connected
;
676 UI
.setMouseButton(1);
677 $D('clipboardButton').style
.display
= "inline";
678 $D('showKeyboard').style
.display
= "inline";
679 $D('noVNC_extra_keys').style
.display
= "";
680 $D('sendCtrlAltDelButton').style
.display
= "inline";
683 $D('clipboardButton').style
.display
= "none";
684 $D('showKeyboard').style
.display
= "none";
685 $D('noVNC_extra_keys').style
.display
= "none";
686 $D('sendCtrlAltDelButton').style
.display
= "none";
687 UI
.updateXvpVisualState(0);
690 // State change disables viewport dragging.
691 // It is enabled (toggled) by direct click on the button
692 UI
.updateViewDrag(false);
694 switch (UI
.rfb_state
) {
698 $D('connectButton').style
.display
= "";
699 $D('disconnectButton').style
.display
= "none";
700 UI
.connSettingsOpen
= false;
701 UI
.toggleConnectPanel();
704 $D('connectButton').style
.display
= "";
705 $D('disconnectButton').style
.display
= "none";
708 $D('connectButton').style
.display
= "none";
709 $D('disconnectButton').style
.display
= "";
713 //Util.Debug("<< updateVisualState");
716 // Disable/enable XVP button
717 updateXvpVisualState: function(ver
) {
719 $D('xvpButton').style
.display
= 'inline';
721 $D('xvpButton').style
.display
= 'none';
722 // Close XVP panel if open
723 if (UI
.xvpOpen
=== true) {
729 // This resize can not be done until we know from the first Frame Buffer Update
730 // if it is supported or not.
731 // The resize is needed to make sure the server desktop size is updated to the
732 // corresponding size of the current local window when reconnecting to an
734 FBUComplete: function(rfb
, fbu
) {
736 UI
.rfb
.set_onFBUComplete(function() { });
739 // Display the desktop name in the document title
740 updateDocumentTitle: function(rfb
, name
) {
741 document
.title
= name
+ " - noVNC";
744 clipReceive: function(rfb
, text
) {
745 Util
.Debug(">> UI.clipReceive: " + text
.substr(0,40) + "...");
746 $D('noVNC_clipboard_text').value
= text
;
747 Util
.Debug("<< UI.clipReceive");
750 connect: function() {
751 UI
.closeSettingsMenu();
752 UI
.toggleConnectPanel();
754 var host
= $D('noVNC_host').value
;
755 var port
= $D('noVNC_port').value
;
756 var password
= $D('noVNC_password').value
;
757 var path
= $D('noVNC_path').value
;
758 if ((!host
) || (!port
)) {
759 throw new Error("Must set host and port");
762 if (!UI
.initRFB()) return;
764 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
765 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
766 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
767 UI
.rfb
.set_shared(UI
.getSetting('shared'));
768 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
769 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
771 UI
.rfb
.connect(host
, port
, password
, path
);
774 setTimeout(UI
.setBarPosition
, 100);
775 $D('noVNC_logo').style
.display
= "none";
776 $D('noVNC_container').style
.display
= "inline";
779 disconnect: function() {
780 UI
.closeSettingsMenu();
783 // Restore the callback used for initial resize
784 UI
.rfb
.set_onFBUComplete(UI
.FBUComplete
);
786 $D('noVNC_logo').style
.display
= "block";
787 $D('noVNC_container').style
.display
= "none";
789 // Don't display the connection settings until we're actually disconnected
792 displayBlur: function() {
795 UI
.rfb
.get_keyboard().set_focused(false);
796 UI
.rfb
.get_mouse().set_focused(false);
799 displayFocus: function() {
802 UI
.rfb
.get_keyboard().set_focused(true);
803 UI
.rfb
.get_mouse().set_focused(true);
806 clipClear: function() {
807 $D('noVNC_clipboard_text').value
= "";
808 UI
.rfb
.clipboardPasteFrom("");
811 clipSend: function() {
812 var text
= $D('noVNC_clipboard_text').value
;
813 Util
.Debug(">> UI.clipSend: " + text
.substr(0,40) + "...");
814 UI
.rfb
.clipboardPasteFrom(text
);
815 Util
.Debug("<< UI.clipSend");
818 // Set and configure viewport clipping
819 setViewClip: function(clip
) {
822 display
= UI
.rfb
.get_display();
827 var cur_clip
= display
.get_viewport();
829 if (typeof(clip
) !== 'boolean') {
830 // Use current setting
831 clip
= UI
.getSetting('clip');
834 if (clip
&& !cur_clip
) {
836 UI
.updateSetting('clip', true);
837 } else if (!clip
&& cur_clip
) {
839 UI
.updateSetting('clip', false);
840 display
.set_viewport(false);
841 // Disable max dimensions
842 display
.set_maxWidth(0);
843 display
.set_maxHeight(0);
844 display
.viewportChangeSize();
846 if (UI
.getSetting('clip')) {
847 // If clipping, update clipping settings
848 display
.set_viewport(true);
850 var size
= UI
.getCanvasLimit();
852 display
.set_maxWidth(size
.w
);
853 display
.set_maxHeight(size
.h
);
855 // Hide potential scrollbars that can skew the position
856 $D('noVNC_container').style
.overflow
= "hidden";
858 // The x position marks the left margin of the canvas,
859 // remove the margin from both sides to keep it centered
860 var new_w
= size
.w
- (2 * Util
.getPosition($D('noVNC_canvas')).x
);
862 $D('noVNC_container').style
.overflow
= "visible";
864 display
.viewportChangeSize(new_w
, size
.h
);
869 // Handle special cases where clipping is forced on/off or locked
870 enableDisableViewClip: function (connected
) {
871 var resizeElem
= $D('noVNC_resize');
872 if (resizeElem
.value
=== 'downscale' || resizeElem
.value
=== 'scale') {
873 UI
.forceSetting('clip', false);
874 $D('noVNC_clip').disabled
= true;
876 $D('noVNC_clip').disabled
= connected
|| UI
.isTouchDevice
;
877 if (UI
.isTouchDevice
) {
878 UI
.forceSetting('clip', true);
883 // Update the viewport drag/move button
884 updateViewDrag: function(drag
) {
887 var vmb
= $D('noVNC_view_drag_button');
889 // Check if viewport drag is possible
890 if (UI
.rfb_state
=== 'normal' &&
891 UI
.rfb
.get_display().get_viewport() &&
892 UI
.rfb
.get_display().clippingDisplay()) {
894 // Show and enable the drag button
895 vmb
.style
.display
= "inline";
896 vmb
.disabled
= false;
899 // The VNC content is the same size as
900 // or smaller than the display
902 if (UI
.rfb
.get_viewportDrag
) {
903 // Turn off viewport drag when it's
904 // active since it can't be used here
905 vmb
.className
= "noVNC_status_button";
906 UI
.rfb
.set_viewportDrag(false);
909 // Disable or hide the drag button
910 if (UI
.rfb_state
=== 'normal' && UI
.isTouchDevice
) {
911 vmb
.style
.display
= "inline";
914 vmb
.style
.display
= "none";
919 if (typeof(drag
) !== "undefined" &&
920 typeof(drag
) !== "object") {
922 vmb
.className
= "noVNC_status_button_selected";
923 UI
.rfb
.set_viewportDrag(true);
925 vmb
.className
= "noVNC_status_button";
926 UI
.rfb
.set_viewportDrag(false);
931 toggleViewDrag: function() {
934 var vmb
= $D('noVNC_view_drag_button');
935 if (UI
.rfb
.get_viewportDrag()) {
936 vmb
.className
= "noVNC_status_button";
937 UI
.rfb
.set_viewportDrag(false);
939 vmb
.className
= "noVNC_status_button_selected";
940 UI
.rfb
.set_viewportDrag(true);
944 // On touch devices, show the OS keyboard
945 showKeyboard: function() {
946 var kbi
= $D('keyboardinput');
947 var skb
= $D('showKeyboard');
948 var l
= kbi
.value
.length
;
949 if(UI
.keyboardVisible
=== false) {
951 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
952 catch (err
) {} // setSelectionRange is undefined in Google Chrome
953 UI
.keyboardVisible
= true;
954 skb
.className
= "noVNC_status_button_selected";
955 } else if(UI
.keyboardVisible
=== true) {
957 skb
.className
= "noVNC_status_button";
958 UI
.keyboardVisible
= false;
962 keepKeyboard: function() {
963 clearTimeout(UI
.hideKeyboardTimeout
);
964 if(UI
.keyboardVisible
=== true) {
965 $D('keyboardinput').focus();
966 $D('showKeyboard').className
= "noVNC_status_button_selected";
967 } else if(UI
.keyboardVisible
=== false) {
968 $D('keyboardinput').blur();
969 $D('showKeyboard').className
= "noVNC_status_button";
973 keyboardinputReset: function() {
974 var kbi
= $D('keyboardinput');
975 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
976 UI
.lastKeyboardinput
= kbi
.value
;
979 // When normal keyboard events are left uncought, use the input events from
980 // the keyboardinput element instead and generate the corresponding key events.
981 // This code is required since some browsers on Android are inconsistent in
982 // sending keyCodes in the normal keyboard events when using on screen keyboards.
983 keyInput: function(event
) {
987 var newValue
= event
.target
.value
;
989 if (!UI
.lastKeyboardinput
) {
990 UI
.keyboardinputReset();
992 var oldValue
= UI
.lastKeyboardinput
;
996 // Try to check caret position since whitespace at the end
997 // will not be considered by value.length in some browsers
998 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1000 // selectionStart is undefined in Google Chrome
1001 newLen
= newValue
.length
;
1003 var oldLen
= oldValue
.length
;
1006 var inputs
= newLen
- oldLen
;
1008 backspaces
= -inputs
;
1013 // Compare the old string with the new to account for
1014 // text-corrections or other input that modify existing text
1016 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1017 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1018 inputs
= newLen
- i
;
1019 backspaces
= oldLen
- i
;
1024 // Send the key events
1025 for (i
= 0; i
< backspaces
; i
++) {
1026 UI
.rfb
.sendKey(XK_BackSpace
);
1028 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1029 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1032 // Control the text content length in the keyboardinput element
1033 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1034 UI
.keyboardinputReset();
1035 } else if (newLen
< 1) {
1036 // There always have to be some text in the keyboardinput
1037 // element with which backspace can interact.
1038 UI
.keyboardinputReset();
1039 // This sometimes causes the keyboard to disappear for a second
1040 // but it is required for the android keyboard to recognize that
1041 // text has been added to the field
1042 event
.target
.blur();
1043 // This has to be ran outside of the input handler in order to work
1044 setTimeout(function() { UI
.keepKeyboard(); }, 0);
1046 UI
.lastKeyboardinput
= newValue
;
1050 keyInputBlur: function() {
1051 $D('showKeyboard').className
= "noVNC_status_button";
1052 //Weird bug in iOS if you change keyboardVisible
1053 //here it does not actually occur so next time
1054 //you click keyboard icon it doesnt work.
1055 UI
.hideKeyboardTimeout
= setTimeout(function() { UI
.setKeyboard(); },100);
1058 showExtraKeys: function() {
1060 if(UI
.extraKeysVisible
=== false) {
1061 $D('toggleCtrlButton').style
.display
= "inline";
1062 $D('toggleAltButton').style
.display
= "inline";
1063 $D('sendTabButton').style
.display
= "inline";
1064 $D('sendEscButton').style
.display
= "inline";
1065 $D('showExtraKeysButton').className
= "noVNC_status_button_selected";
1066 UI
.extraKeysVisible
= true;
1067 } else if(UI
.extraKeysVisible
=== true) {
1068 $D('toggleCtrlButton').style
.display
= "";
1069 $D('toggleAltButton').style
.display
= "";
1070 $D('sendTabButton').style
.display
= "";
1071 $D('sendEscButton').style
.display
= "";
1072 $D('showExtraKeysButton').className
= "noVNC_status_button";
1073 UI
.extraKeysVisible
= false;
1077 toggleCtrl: function() {
1079 if(UI
.ctrlOn
=== false) {
1080 UI
.rfb
.sendKey(XK_Control_L
, true);
1081 $D('toggleCtrlButton').className
= "noVNC_status_button_selected";
1083 } else if(UI
.ctrlOn
=== true) {
1084 UI
.rfb
.sendKey(XK_Control_L
, false);
1085 $D('toggleCtrlButton').className
= "noVNC_status_button";
1090 toggleAlt: function() {
1092 if(UI
.altOn
=== false) {
1093 UI
.rfb
.sendKey(XK_Alt_L
, true);
1094 $D('toggleAltButton').className
= "noVNC_status_button_selected";
1096 } else if(UI
.altOn
=== true) {
1097 UI
.rfb
.sendKey(XK_Alt_L
, false);
1098 $D('toggleAltButton').className
= "noVNC_status_button";
1103 sendTab: function() {
1105 UI
.rfb
.sendKey(XK_Tab
);
1108 sendEsc: function() {
1110 UI
.rfb
.sendKey(XK_Escape
);
1113 setKeyboard: function() {
1114 UI
.keyboardVisible
= false;
1117 //Helper to add options to dropdown.
1118 addOption: function(selectbox
, text
, value
) {
1119 var optn
= document
.createElement("OPTION");
1122 selectbox
.options
.add(optn
);
1125 setBarPosition: function() {
1126 $D('noVNC-control-bar').style
.top
= (window
.pageYOffset
) + 'px';
1127 $D('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
1129 var vncwidth
= $D('noVNC_screen').style
.offsetWidth
;
1130 $D('noVNC-control-bar').style
.width
= vncwidth
+ 'px';