]>
git.proxmox.com Git - mirror_novnc.git/blob - app/ui.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2016 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, document.getElementById, Util, WebUtil, RFB, Display */
14 * import Util from "../core/util";
15 * import KeyTable from "../core/input/keysym";
16 * import RFB from "../core/rfb";
17 * import Display from "../core/display";
18 * import WebUtil from "./webutil";
26 /* [begin skip-as-module] */
27 // Load supporting scripts
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"]});
33 window
.onscriptsload = function () { UI
.load(); };
34 /* [end skip-as-module] */
41 popupStatusTimeout
: null,
42 hideKeyboardTimeout
: null,
45 connSettingsOpen
: false,
47 keyboardVisible
: false,
48 extraKeysVisible
: false,
52 rememberedClipSetting
: null,
53 lastKeyboardinput
: null,
54 defaultKeyboardinputLen
: 100,
59 // Setup rfb object, load settings from browser storage, then call
60 // UI.init to setup the UI/menus
61 load: function(callback
) {
62 WebUtil
.initSettings(UI
.start
, callback
);
65 // Render default UI and initialize settings menu
66 start: function(callback
) {
68 // Setup global variables first
69 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
70 UI
.isSafari
= (navigator
.userAgent
.indexOf('Safari') !== -1 &&
71 navigator
.userAgent
.indexOf('Chrome') === -1);
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";
80 // Remove the address bar
81 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
82 UI
.forceSetting('clip', true);
84 UI
.initSetting('clip', false);
87 // Setup and initialize event handlers
88 UI
.setupWindowEvents();
90 UI
.addControlbarHandlers();
91 UI
.addTouchSpecificHandlers();
93 UI
.addConnectionControlHandlers();
94 UI
.addClipboardHandlers();
95 UI
.addSettingsHandlers();
97 // Show the connect panel on first load unless autoconnecting
98 if (!autoconnect
&& !UI
.connSettingsOpen
)
99 UI
.toggleConnectPanel();
104 UI
.updateVisualState();
106 document
.getElementById('noVNC_setting_host').focus();
108 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
109 if (autoconnect
=== 'true' || autoconnect
== '1') {
116 if (typeof callback
=== "function") {
121 initSettings: function() {
122 // Stylesheet selection dropdown
123 var sheet
= WebUtil
.selectStylesheet();
124 var sheets
= WebUtil
.getStylesheets();
126 for (i
= 0; i
< sheets
.length
; i
+= 1) {
127 UI
.addOption(document
.getElementById('noVNC_setting_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
130 // Logging selection dropdown
131 var llevels
= ['error', 'warn', 'info', 'debug'];
132 for (i
= 0; i
< llevels
.length
; i
+= 1) {
133 UI
.addOption(document
.getElementById('noVNC_setting_logging'),llevels
[i
], llevels
[i
]);
136 // Settings with immediate effects
137 UI
.initSetting('logging', 'warn');
138 WebUtil
.init_logging(UI
.getSetting('logging'));
140 UI
.initSetting('stylesheet', 'default');
141 WebUtil
.selectStylesheet(null);
142 // call twice to get around webkit bug
143 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
145 // if port == 80 (or 443) then it won't be present and should be
147 var port
= window
.location
.port
;
149 if (window
.location
.protocol
.substring(0,5) == 'https') {
152 else if (window
.location
.protocol
.substring(0,4) == 'http') {
157 /* Populate the controls if defaults are provided in the URL */
158 UI
.initSetting('host', window
.location
.hostname
);
159 UI
.initSetting('port', port
);
160 UI
.initSetting('password', '');
161 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
162 UI
.initSetting('true_color', true);
163 UI
.initSetting('cursor', !UI
.isTouchDevice
);
164 UI
.initSetting('resize', 'off');
165 UI
.initSetting('shared', true);
166 UI
.initSetting('view_only', false);
167 UI
.initSetting('path', 'websockify');
168 UI
.initSetting('repeaterID', '');
169 UI
.initSetting('token', '');
172 setupWindowEvents: function() {
173 window
.addEventListener( 'resize', function () {
174 UI
.applyResizeMode();
181 setupFullscreen: function() {
182 // Only show the button if fullscreen is properly supported
183 // * Safari doesn't support alphanumerical input while in fullscreen
185 (document
.documentElement
.requestFullscreen
||
186 document
.documentElement
.mozRequestFullScreen
||
187 document
.documentElement
.webkitRequestFullscreen
||
188 document
.body
.msRequestFullscreen
)) {
189 document
.getElementById('noVNC_fullscreen_button').style
.display
= "inline";
190 UI
.addFullscreenHandlers();
194 addControlbarHandlers: function() {
195 document
.getElementById("noVNC_view_drag_button")
196 .addEventListener('click', UI
.toggleViewDrag
);
197 document
.getElementById("noVNC_sendCtrlAltDel_button")
198 .addEventListener('click', UI
.sendCtrlAltDel
);
199 document
.getElementById("noVNC_status")
200 .addEventListener('click', UI
.popupStatus
);
201 document
.getElementById("noVNC_popup_status")
202 .addEventListener('click', UI
.closePopup
);
205 addTouchSpecificHandlers: function() {
206 document
.getElementById("noVNC_mouse_button0")
207 .addEventListener('click', function () { UI
.setMouseButton(1); });
208 document
.getElementById("noVNC_mouse_button1")
209 .addEventListener('click', function () { UI
.setMouseButton(2); });
210 document
.getElementById("noVNC_mouse_button2")
211 .addEventListener('click', function () { UI
.setMouseButton(4); });
212 document
.getElementById("noVNC_mouse_button4")
213 .addEventListener('click', function () { UI
.setMouseButton(0); });
214 document
.getElementById("noVNC_keyboard_button")
215 .addEventListener('click', UI
.showKeyboard
);
217 document
.getElementById("noVNC_keyboardinput")
218 .addEventListener('input', UI
.keyInput
);
219 document
.getElementById("noVNC_keyboardinput")
220 .addEventListener('blur', UI
.hideKeyboard
);
221 document
.getElementById("noVNC_keyboardinput")
222 .addEventListener('submit', function () { return false; });
224 window
.addEventListener('load', UI
.keyboardinputReset
);
226 document
.getElementById("noVNC_toggleExtraKeys_button")
227 .addEventListener('click', UI
.toggleExtraKeys
);
228 document
.getElementById("noVNC_toggleCtrl_button")
229 .addEventListener('click', UI
.toggleCtrl
);
230 document
.getElementById("noVNC_toggleAlt_button")
231 .addEventListener('click', UI
.toggleAlt
);
232 document
.getElementById("noVNC_sendTab_button")
233 .addEventListener('click', UI
.sendTab
);
234 document
.getElementById("noVNC_sendEsc_button")
235 .addEventListener('click', UI
.sendEsc
);
238 addXvpHandlers: function() {
239 document
.getElementById("noVNC_xvpShutdown_button")
240 .addEventListener('click', function() { UI
.rfb
.xvpShutdown(); });
241 document
.getElementById("noVNC_xvpReboot_button")
242 .addEventListener('click', function() { UI
.rfb
.xvpReboot(); });
243 document
.getElementById("noVNC_xvpReset_button")
244 .addEventListener('click', function() { UI
.rfb
.xvpReset(); });
245 document
.getElementById("noVNC_toggleXvp_button")
246 .addEventListener('click', UI
.toggleXvpPanel
);
249 addConnectionControlHandlers: function() {
250 document
.getElementById("noVNC_connectPanel_button")
251 .addEventListener('click', UI
.toggleConnectPanel
);
252 document
.getElementById("noVNC_disconnect_button")
253 .addEventListener('click', UI
.disconnect
);
254 document
.getElementById("noVNC_connect_button")
255 .addEventListener('click', UI
.connect
);
258 addClipboardHandlers: function() {
259 document
.getElementById("noVNC_clipboard_button")
260 .addEventListener('click', UI
.toggleClipboardPanel
);
261 document
.getElementById("noVNC_clipboard_text")
262 .addEventListener('focus', UI
.displayBlur
);
263 document
.getElementById("noVNC_clipboard_text")
264 .addEventListener('blur', UI
.displayFocus
);
265 document
.getElementById("noVNC_clipboard_text")
266 .addEventListener('change', UI
.clipboardSend
);
267 document
.getElementById("noVNC_clipboard_clear_button")
268 .addEventListener('click', UI
.clipboardClear
);
271 addSettingsHandlers: function() {
272 document
.getElementById("noVNC_settings_button")
273 .addEventListener('click', UI
.toggleSettingsPanel
);
274 document
.getElementById("noVNC_settings_apply")
275 .addEventListener('click', UI
.settingsApply
);
277 document
.getElementById("noVNC_setting_resize")
278 .addEventListener('change', UI
.enableDisableViewClip
);
281 addFullscreenHandlers: function() {
282 document
.getElementById("noVNC_fullscreen_button")
283 .addEventListener('click', UI
.toggleFullscreen
);
285 window
.addEventListener('fullscreenchange', UI
.updateFullscreenButton
);
286 window
.addEventListener('mozfullscreenchange', UI
.updateFullscreenButton
);
287 window
.addEventListener('webkitfullscreenchange', UI
.updateFullscreenButton
);
288 window
.addEventListener('msfullscreenchange', UI
.updateFullscreenButton
);
291 initRFB: function() {
293 UI
.rfb
= new RFB({'target': document
.getElementById('noVNC_canvas'),
294 'onUpdateState': UI
.updateState
,
295 'onXvpInit': UI
.updateXvpButton
,
296 'onClipboard': UI
.clipboardReceive
,
297 'onFBUComplete': UI
.initialResize
,
298 'onFBResize': UI
.updateViewDrag
,
299 'onDesktopName': UI
.updateDocumentTitle
});
302 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
313 updateState: function(rfb
, state
, oldstate
, msg
) {
314 UI
.rfb_state
= state
;
319 klass
= "noVNC_status_error";
322 klass
= "noVNC_status_normal";
325 document
.getElementById('noVNC_logo').style
.display
= "block";
326 document
.getElementById('noVNC_screen').style
.display
= "none";
329 klass
= "noVNC_status_normal";
332 UI
.toggleConnectPanel();
334 document
.getElementById('noVNC_connect_button').value
= "Send Password";
335 document
.getElementById('noVNC_connect_button').onclick
= UI
.setPassword
;
336 document
.getElementById('noVNC_setting_password').focus();
338 klass
= "noVNC_status_warn";
341 klass
= "noVNC_status_warn";
345 if (typeof(msg
) !== 'undefined') {
346 document
.getElementById('noVNC_control_bar').setAttribute("class", klass
);
347 document
.getElementById('noVNC_status').innerHTML
= msg
;
350 UI
.updateVisualState();
353 // Disable/enable controls depending on connection state
354 updateVisualState: function() {
355 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
357 //Util.Debug(">> updateVisualState");
358 document
.getElementById('noVNC_setting_encrypt').disabled
= connected
;
359 document
.getElementById('noVNC_setting_true_color').disabled
= connected
;
360 if (Util
.browserSupportsCursorURIs()) {
361 document
.getElementById('noVNC_setting_cursor').disabled
= connected
;
363 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
364 document
.getElementById('noVNC_setting_cursor').disabled
= true;
367 UI
.enableDisableViewClip();
368 document
.getElementById('noVNC_setting_resize').disabled
= connected
;
369 document
.getElementById('noVNC_setting_shared').disabled
= connected
;
370 document
.getElementById('noVNC_setting_view_only').disabled
= connected
;
371 document
.getElementById('noVNC_setting_path').disabled
= connected
;
372 document
.getElementById('noVNC_setting_repeaterID').disabled
= connected
;
376 UI
.setMouseButton(1);
377 document
.getElementById('noVNC_clipboard_button').style
.display
= "inline";
378 document
.getElementById('noVNC_keyboard_button').style
.display
= "inline";
379 document
.getElementById('noVNC_extra_keys').style
.display
= "";
380 document
.getElementById('noVNC_sendCtrlAltDel_button').style
.display
= "inline";
382 UI
.hideMouseButton();
383 document
.getElementById('noVNC_clipboard_button').style
.display
= "none";
384 document
.getElementById('noVNC_keyboard_button').style
.display
= "none";
385 document
.getElementById('noVNC_extra_keys').style
.display
= "none";
386 document
.getElementById('noVNC_sendCtrlAltDel_button').style
.display
= "none";
387 UI
.updateXvpButton(0);
390 // State change disables viewport dragging.
391 // It is enabled (toggled) by direct click on the button
392 UI
.setViewDrag(false);
394 switch (UI
.rfb_state
) {
398 document
.getElementById('noVNC_connectPanel_button').style
.display
= "";
399 document
.getElementById('noVNC_disconnect_button').style
.display
= "none";
400 UI
.connSettingsOpen
= false;
401 UI
.toggleConnectPanel();
404 document
.getElementById('noVNC_connectPanel_button').style
.display
= "";
405 document
.getElementById('noVNC_disconnect_button').style
.display
= "none";
408 document
.getElementById('noVNC_connectPanel_button').style
.display
= "none";
409 document
.getElementById('noVNC_disconnect_button').style
.display
= "";
413 //Util.Debug("<< updateVisualState");
416 popupStatus: function(text
) {
417 var psp
= document
.getElementById('noVNC_popup_status');
419 clearTimeout(UI
.popupStatusTimeout
);
421 if (typeof text
=== 'string') {
422 psp
.innerHTML
= text
;
424 psp
.innerHTML
= document
.getElementById('noVNC_status').innerHTML
;
426 psp
.style
.display
= "block";
427 psp
.style
.left
= window
.innerWidth
/2 -
428 parseInt(window
.getComputedStyle(psp
).width
)/2 -30 + "px";
430 // Show the popup for a maximum of 1.5 seconds
431 UI
.popupStatusTimeout
= setTimeout(UI
.closePopup
, 1500);
434 closePopup: function() {
435 clearTimeout(UI
.popupStatusTimeout
);
436 document
.getElementById('noVNC_popup_status').style
.display
= "none";
445 // Initial page load read/initialization of settings
446 initSetting: function(name
, defVal
) {
447 // Check Query string followed by cookie
448 var val
= WebUtil
.getConfigVar(name
);
450 val
= WebUtil
.readSetting(name
, defVal
);
452 UI
.updateSetting(name
, val
);
456 // Update cookie and form control setting. If value is not set, then
457 // updates from control to current cookie setting.
458 updateSetting: function(name
, value
) {
460 // Save the cookie for this session
461 if (typeof value
!== 'undefined') {
462 WebUtil
.writeSetting(name
, value
);
465 // Update the settings control
466 value
= UI
.getSetting(name
);
468 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
469 if (ctrl
.type
=== 'checkbox') {
470 ctrl
.checked
= value
;
472 } else if (typeof ctrl
.options
!== 'undefined') {
473 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
474 if (ctrl
.options
[i
].value
=== value
) {
475 ctrl
.selectedIndex
= i
;
480 /*Weird IE9 error leads to 'null' appearring
481 in textboxes instead of ''.*/
482 if (value
=== null) {
489 // Save control setting to cookie
490 saveSetting: function(name
) {
491 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
492 if (ctrl
.type
=== 'checkbox') {
494 } else if (typeof ctrl
.options
!== 'undefined') {
495 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
499 WebUtil
.writeSetting(name
, val
);
500 //Util.Debug("Setting saved '" + name + "=" + val + "'");
504 // Force a setting to be a certain value
505 forceSetting: function(name
, val
) {
506 UI
.updateSetting(name
, val
);
510 // Read form control compatible setting from cookie
511 getSetting: function(name
) {
512 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
513 var val
= WebUtil
.readSetting(name
);
514 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
515 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
524 // Save/apply settings when 'Apply' button is pressed
525 settingsApply: function() {
526 //Util.Debug(">> settingsApply");
527 UI
.saveSetting('encrypt');
528 UI
.saveSetting('true_color');
529 if (Util
.browserSupportsCursorURIs()) {
530 UI
.saveSetting('cursor');
533 UI
.saveSetting('resize');
535 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
536 UI
.forceSetting('clip', false);
539 UI
.saveSetting('clip');
540 UI
.saveSetting('shared');
541 UI
.saveSetting('view_only');
542 UI
.saveSetting('path');
543 UI
.saveSetting('repeaterID');
544 UI
.saveSetting('stylesheet');
545 UI
.saveSetting('logging');
547 // Settings with immediate (non-connected related) effect
548 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
549 WebUtil
.init_logging(UI
.getSetting('logging'));
552 //Util.Debug("<< settingsApply");
556 openSettingsMenu: function() {
557 // Close clipboard panel if open
558 if (UI
.clipboardOpen
=== true) {
559 UI
.toggleClipboardPanel();
561 // Close connection settings if open
562 if (UI
.connSettingsOpen
=== true) {
563 UI
.toggleConnectPanel();
565 // Close XVP panel if open
566 if (UI
.xvpOpen
=== true) {
569 document
.getElementById('noVNC_settings').style
.display
= "block";
570 document
.getElementById('noVNC_settings_button').className
= "noVNC_status_button_selected";
571 UI
.settingsOpen
= true;
574 // Close menu (without applying settings)
575 closeSettingsMenu: function() {
576 document
.getElementById('noVNC_settings').style
.display
= "none";
577 document
.getElementById('noVNC_settings_button').className
= "noVNC_status_button";
578 UI
.settingsOpen
= false;
581 // Toggle the settings menu:
582 // On open, settings are refreshed from saved cookies.
583 // On close, settings are applied
584 toggleSettingsPanel: function() {
585 if (UI
.settingsOpen
) {
587 UI
.closeSettingsMenu();
589 UI
.updateSetting('encrypt');
590 UI
.updateSetting('true_color');
591 if (Util
.browserSupportsCursorURIs()) {
592 UI
.updateSetting('cursor');
594 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
595 document
.getElementById('noVNC_setting_cursor').disabled
= true;
597 UI
.updateSetting('clip');
598 UI
.updateSetting('resize');
599 UI
.updateSetting('shared');
600 UI
.updateSetting('view_only');
601 UI
.updateSetting('path');
602 UI
.updateSetting('repeaterID');
603 UI
.updateSetting('stylesheet');
604 UI
.updateSetting('logging');
606 UI
.openSettingsMenu();
616 // Show the XVP panel
617 toggleXvpPanel: function() {
618 // Close settings if open
619 if (UI
.settingsOpen
=== true) {
621 UI
.closeSettingsMenu();
623 // Close connection settings if open
624 if (UI
.connSettingsOpen
=== true) {
625 UI
.toggleConnectPanel();
627 // Close clipboard panel if open
628 if (UI
.clipboardOpen
=== true) {
629 UI
.toggleClipboardPanel();
632 if (UI
.xvpOpen
=== true) {
633 document
.getElementById('noVNC_xvp').style
.display
= "none";
634 document
.getElementById('noVNC_toggleXvp_button').className
= "noVNC_status_button";
637 document
.getElementById('noVNC_xvp').style
.display
= "block";
638 document
.getElementById('noVNC_toggleXvp_button').className
= "noVNC_status_button_selected";
643 // Disable/enable XVP button
644 updateXvpButton: function(ver
) {
646 document
.getElementById('noVNC_toggleXvp_button').style
.display
= 'inline';
648 document
.getElementById('noVNC_toggleXvp_button').style
.display
= 'none';
649 // Close XVP panel if open
650 if (UI
.xvpOpen
=== true) {
662 // Show the clipboard panel
663 toggleClipboardPanel: function() {
664 // Close settings if open
665 if (UI
.settingsOpen
=== true) {
667 UI
.closeSettingsMenu();
669 // Close connection settings if open
670 if (UI
.connSettingsOpen
=== true) {
671 UI
.toggleConnectPanel();
673 // Close XVP panel if open
674 if (UI
.xvpOpen
=== true) {
677 // Toggle Clipboard Panel
678 if (UI
.clipboardOpen
=== true) {
679 document
.getElementById('noVNC_clipboard').style
.display
= "none";
680 document
.getElementById('noVNC_clipboard_button').className
= "noVNC_status_button";
681 UI
.clipboardOpen
= false;
683 document
.getElementById('noVNC_clipboard').style
.display
= "block";
684 document
.getElementById('noVNC_clipboard_button').className
= "noVNC_status_button_selected";
685 UI
.clipboardOpen
= true;
689 clipboardReceive: function(rfb
, text
) {
690 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
691 document
.getElementById('noVNC_clipboard_text').value
= text
;
692 Util
.Debug("<< UI.clipboardReceive");
695 clipboardClear: function() {
696 document
.getElementById('noVNC_clipboard_text').value
= "";
697 UI
.rfb
.clipboardPasteFrom("");
700 clipboardSend: function() {
701 var text
= document
.getElementById('noVNC_clipboard_text').value
;
702 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
703 UI
.rfb
.clipboardPasteFrom(text
);
704 Util
.Debug("<< UI.clipboardSend");
713 // Show the connection settings panel/menu
714 toggleConnectPanel: function() {
715 // Close connection settings if open
716 if (UI
.settingsOpen
=== true) {
718 UI
.closeSettingsMenu();
719 document
.getElementById('noVNC_connectPanel_button').className
= "noVNC_status_button";
721 // Close clipboard panel if open
722 if (UI
.clipboardOpen
=== true) {
723 UI
.toggleClipboardPanel();
725 // Close XVP panel if open
726 if (UI
.xvpOpen
=== true) {
730 // Toggle Connection Panel
731 if (UI
.connSettingsOpen
=== true) {
732 document
.getElementById('noVNC_controls').style
.display
= "none";
733 document
.getElementById('noVNC_connectPanel_button').className
= "noVNC_status_button";
734 UI
.connSettingsOpen
= false;
735 UI
.saveSetting('host');
736 UI
.saveSetting('port');
737 UI
.saveSetting('token');
738 //UI.saveSetting('password');
740 document
.getElementById('noVNC_controls').style
.display
= "block";
741 document
.getElementById('noVNC_connectPanel_button').className
= "noVNC_status_button_selected";
742 UI
.connSettingsOpen
= true;
743 document
.getElementById('noVNC_setting_host').focus();
747 connect: function() {
748 UI
.closeSettingsMenu();
749 UI
.toggleConnectPanel();
751 var host
= document
.getElementById('noVNC_setting_host').value
;
752 var port
= document
.getElementById('noVNC_setting_port').value
;
753 var password
= document
.getElementById('noVNC_setting_password').value
;
754 var token
= document
.getElementById('noVNC_setting_token').value
;
755 var path
= document
.getElementById('noVNC_setting_path').value
;
757 //if token is in path then ignore the new token variable
759 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
762 if ((!host
) || (!port
)) {
763 throw new Error("Must set host and port");
766 if (!UI
.initRFB()) return;
768 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
769 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
770 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
771 UI
.rfb
.set_shared(UI
.getSetting('shared'));
772 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
773 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
775 UI
.rfb
.connect(host
, port
, password
, path
);
778 setTimeout(UI
.setBarPosition
, 100);
779 document
.getElementById('noVNC_logo').style
.display
= "none";
780 document
.getElementById('noVNC_screen').style
.display
= "inline";
783 disconnect: function() {
784 UI
.closeSettingsMenu();
787 // Restore the callback used for initial resize
788 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
790 document
.getElementById('noVNC_logo').style
.display
= "block";
791 document
.getElementById('noVNC_screen').style
.display
= "none";
793 // Don't display the connection settings until we're actually disconnected
796 setPassword: function() {
797 UI
.rfb
.sendPassword(document
.getElementById('noVNC_setting_password').value
);
798 //Reset connect button.
799 document
.getElementById('noVNC_connect_button').value
= "Connect";
800 document
.getElementById('noVNC_connect_button').onclick
= UI
.connect
;
801 //Hide connection panel.
802 UI
.toggleConnectPanel();
812 toggleFullscreen: function() {
813 if (document
.fullscreenElement
|| // alternative standard method
814 document
.mozFullScreenElement
|| // currently working methods
815 document
.webkitFullscreenElement
||
816 document
.msFullscreenElement
) {
817 if (document
.exitFullscreen
) {
818 document
.exitFullscreen();
819 } else if (document
.mozCancelFullScreen
) {
820 document
.mozCancelFullScreen();
821 } else if (document
.webkitExitFullscreen
) {
822 document
.webkitExitFullscreen();
823 } else if (document
.msExitFullscreen
) {
824 document
.msExitFullscreen();
827 if (document
.documentElement
.requestFullscreen
) {
828 document
.documentElement
.requestFullscreen();
829 } else if (document
.documentElement
.mozRequestFullScreen
) {
830 document
.documentElement
.mozRequestFullScreen();
831 } else if (document
.documentElement
.webkitRequestFullscreen
) {
832 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
833 } else if (document
.body
.msRequestFullscreen
) {
834 document
.body
.msRequestFullscreen();
837 UI
.enableDisableViewClip();
838 UI
.updateFullscreenButton();
841 updateFullscreenButton: function() {
842 if (document
.fullscreenElement
|| // alternative standard method
843 document
.mozFullScreenElement
|| // currently working methods
844 document
.webkitFullscreenElement
||
845 document
.msFullscreenElement
) {
846 document
.getElementById('noVNC_fullscreen_button').className
= "noVNC_status_button_selected";
848 document
.getElementById('noVNC_fullscreen_button').className
= "noVNC_status_button";
858 // Apply remote resizing or local scaling
859 applyResizeMode: function() {
862 var screen
= UI
.screenSize();
864 if (screen
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
866 var display
= UI
.rfb
.get_display();
867 var resizeMode
= UI
.getSetting('resize');
869 if (resizeMode
=== 'remote') {
871 // Request changing the resolution of the remote display to
872 // the size of the local browser viewport.
874 // In order to not send multiple requests before the browser-resize
875 // is finished we wait 0.5 seconds before sending the request.
876 clearTimeout(UI
.resizeTimeout
);
877 UI
.resizeTimeout
= setTimeout(function(){
879 // Limit the viewport to the size of the browser window
880 display
.set_maxWidth(screen
.w
);
881 display
.set_maxHeight(screen
.h
);
883 Util
.Debug('Attempting requestDesktopSize(' +
884 screen
.w
+ ', ' + screen
.h
+ ')');
886 // Request a remote size covering the viewport
887 UI
.rfb
.requestDesktopSize(screen
.w
, screen
.h
);
890 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
891 var downscaleOnly
= resizeMode
=== 'downscale';
892 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
893 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
894 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
899 // The screen is always the same size as the available viewport
900 // in the browser window minus the height of the control bar
901 screenSize: function() {
902 var screen
= document
.getElementById('noVNC_screen');
904 // Hide the scrollbars until the size is calculated
905 screen
.style
.overflow
= "hidden";
907 var pos
= Util
.getPosition(screen
);
911 screen
.style
.overflow
= "visible";
913 if (isNaN(w
) || isNaN(h
)) {
920 // Normally we only apply the current resize mode after a window resize
921 // event. This means that when a new connection is opened, there is no
922 // resize mode active.
923 // We have to wait until the first FBU because this is where the client
924 // will find the supported encodings of the server. Some calls later in
925 // the chain is dependant on knowing the server-capabilities.
926 initialResize: function(rfb
, fbu
) {
927 UI
.applyResizeMode();
928 // After doing this once, we remove the callback.
929 UI
.rfb
.set_onFBUComplete(function() { });
938 // Set and configure viewport clipping
939 setViewClip: function(clip
) {
940 UI
.updateSetting('clip', clip
);
944 // Update parameters that depend on the clip setting
945 updateViewClip: function() {
951 var display
= UI
.rfb
.get_display();
952 var cur_clip
= display
.get_viewport();
953 var new_clip
= UI
.getSetting('clip');
955 if (cur_clip
!== new_clip
) {
956 display
.set_viewport(new_clip
);
959 var size
= UI
.screenSize();
961 if (new_clip
&& size
) {
962 // When clipping is enabled, the screen is limited to
963 // the size of the browser window.
964 display
.set_maxWidth(size
.w
);
965 display
.set_maxHeight(size
.h
);
967 var screen
= document
.getElementById('noVNC_screen');
968 var canvas
= document
.getElementById('noVNC_canvas');
970 // Hide potential scrollbars that can skew the position
971 screen
.style
.overflow
= "hidden";
973 // The x position marks the left margin of the canvas,
974 // remove the margin from both sides to keep it centered.
975 var new_w
= size
.w
- (2 * Util
.getPosition(canvas
).x
);
977 screen
.style
.overflow
= "visible";
979 display
.viewportChangeSize(new_w
, size
.h
);
981 // Disable max dimensions
982 display
.set_maxWidth(0);
983 display
.set_maxHeight(0);
984 display
.viewportChangeSize();
988 // Handle special cases where clipping is forced on/off or locked
989 enableDisableViewClip: function() {
990 var resizeSetting
= document
.getElementById('noVNC_setting_resize');
991 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
994 // Safari auto-hides the scrollbars which makes them
995 // impossible to use in most cases
996 UI
.setViewClip(true);
997 document
.getElementById('noVNC_setting_clip').disabled
= true;
998 } else if (resizeSetting
.value
=== 'downscale' || resizeSetting
.value
=== 'scale') {
999 // Disable clipping if we are scaling
1000 UI
.setViewClip(false);
1001 document
.getElementById('noVNC_setting_clip').disabled
= true;
1002 } else if (document
.msFullscreenElement
) {
1003 // The browser is IE and we are in fullscreen mode.
1004 // - We need to force clipping while in fullscreen since
1005 // scrollbars doesn't work.
1006 UI
.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1007 UI
.rememberedClipSetting
= UI
.getSetting('clip');
1008 UI
.setViewClip(true);
1009 document
.getElementById('noVNC_setting_clip').disabled
= true;
1010 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
1011 // Restore view clip to what it was before fullscreen on IE
1012 UI
.setViewClip(UI
.rememberedClipSetting
);
1013 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1015 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1016 if (UI
.isTouchDevice
) {
1017 UI
.setViewClip(true);
1028 toggleViewDrag: function() {
1029 if (!UI
.rfb
) return;
1031 var drag
= UI
.rfb
.get_viewportDrag();
1032 UI
.setViewDrag(!drag
);
1035 // Set the view drag mode which moves the viewport on mouse drags
1036 setViewDrag: function(drag
) {
1037 if (!UI
.rfb
) return;
1039 UI
.rfb
.set_viewportDrag(drag
);
1041 UI
.updateViewDrag();
1044 updateViewDrag: function() {
1045 var clipping
= false;
1047 // Check if viewport drag is possible. It is only possible
1048 // if the remote display is clipping the client display.
1049 if (UI
.rfb_state
=== 'normal' &&
1050 UI
.rfb
.get_display().get_viewport() &&
1051 UI
.rfb
.get_display().clippingDisplay()) {
1055 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1057 if (UI
.rfb_state
!== 'normal') {
1058 // Always hide when not connected
1059 viewDragButton
.style
.display
= "none";
1062 UI
.rfb
.get_viewportDrag()) {
1063 // The size of the remote display is the same or smaller
1064 // than the client display. Make sure viewport drag isn't
1065 // active when it can't be used.
1066 UI
.rfb
.set_viewportDrag(false);
1069 if (UI
.rfb
.get_viewportDrag()) {
1070 viewDragButton
.className
= "noVNC_status_button_selected";
1072 viewDragButton
.className
= "noVNC_status_button";
1075 // Different behaviour for touch vs non-touch
1076 // The button is disabled instead of hidden on touch devices
1077 if (UI
.isTouchDevice
) {
1078 viewDragButton
.style
.display
= "inline";
1081 viewDragButton
.disabled
= false;
1083 viewDragButton
.disabled
= true;
1086 viewDragButton
.disabled
= false;
1089 viewDragButton
.style
.display
= "inline";
1091 viewDragButton
.style
.display
= "none";
1103 // On touch devices, show the OS keyboard
1104 showKeyboard: function() {
1105 var kbi
= document
.getElementById('noVNC_keyboardinput');
1106 var skb
= document
.getElementById('noVNC_keyboard_button');
1107 var l
= kbi
.value
.length
;
1108 if(UI
.keyboardVisible
=== false) {
1110 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
1111 catch (err
) {} // setSelectionRange is undefined in Google Chrome
1112 UI
.keyboardVisible
= true;
1113 skb
.className
= "noVNC_status_button_selected";
1114 } else if(UI
.keyboardVisible
=== true) {
1116 skb
.className
= "noVNC_status_button";
1117 UI
.keyboardVisible
= false;
1121 hideKeyboard: function() {
1122 document
.getElementById('noVNC_keyboard_button').className
= "noVNC_status_button";
1123 //Weird bug in iOS if you change keyboardVisible
1124 //here it does not actually occur so next time
1125 //you click keyboard icon it doesnt work.
1126 UI
.hideKeyboardTimeout
= setTimeout(function() {
1127 UI
.keyboardVisible
= false;
1131 keepKeyboard: function() {
1132 clearTimeout(UI
.hideKeyboardTimeout
);
1133 if(UI
.keyboardVisible
=== true) {
1134 document
.getElementById('noVNC_keyboardinput').focus();
1135 document
.getElementById('noVNC_keyboard_button').className
= "noVNC_status_button_selected";
1136 } else if(UI
.keyboardVisible
=== false) {
1137 document
.getElementById('noVNC_keyboardinput').blur();
1138 document
.getElementById('noVNC_keyboard_button').className
= "noVNC_status_button";
1142 keyboardinputReset: function() {
1143 var kbi
= document
.getElementById('noVNC_keyboardinput');
1144 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1145 UI
.lastKeyboardinput
= kbi
.value
;
1148 // When normal keyboard events are left uncought, use the input events from
1149 // the keyboardinput element instead and generate the corresponding key events.
1150 // This code is required since some browsers on Android are inconsistent in
1151 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1152 keyInput: function(event
) {
1154 if (!UI
.rfb
) return;
1156 var newValue
= event
.target
.value
;
1158 if (!UI
.lastKeyboardinput
) {
1159 UI
.keyboardinputReset();
1161 var oldValue
= UI
.lastKeyboardinput
;
1165 // Try to check caret position since whitespace at the end
1166 // will not be considered by value.length in some browsers
1167 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1169 // selectionStart is undefined in Google Chrome
1170 newLen
= newValue
.length
;
1172 var oldLen
= oldValue
.length
;
1175 var inputs
= newLen
- oldLen
;
1177 backspaces
= -inputs
;
1182 // Compare the old string with the new to account for
1183 // text-corrections or other input that modify existing text
1185 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1186 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1187 inputs
= newLen
- i
;
1188 backspaces
= oldLen
- i
;
1193 // Send the key events
1194 for (i
= 0; i
< backspaces
; i
++) {
1195 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1197 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1198 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1201 // Control the text content length in the keyboardinput element
1202 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1203 UI
.keyboardinputReset();
1204 } else if (newLen
< 1) {
1205 // There always have to be some text in the keyboardinput
1206 // element with which backspace can interact.
1207 UI
.keyboardinputReset();
1208 // This sometimes causes the keyboard to disappear for a second
1209 // but it is required for the android keyboard to recognize that
1210 // text has been added to the field
1211 event
.target
.blur();
1212 // This has to be ran outside of the input handler in order to work
1213 setTimeout(UI
.keepKeyboard
, 0);
1215 UI
.lastKeyboardinput
= newValue
;
1219 toggleExtraKeys: function() {
1221 if(UI
.extraKeysVisible
=== false) {
1222 document
.getElementById('noVNC_toggleCtrl_button').style
.display
= "inline";
1223 document
.getElementById('noVNC_toggleAlt_button').style
.display
= "inline";
1224 document
.getElementById('noVNC_sendTab_button').style
.display
= "inline";
1225 document
.getElementById('noVNC_sendEsc_button').style
.display
= "inline";
1226 document
.getElementById('noVNC_toggleExtraKeys_button').className
= "noVNC_status_button_selected";
1227 UI
.extraKeysVisible
= true;
1228 } else if(UI
.extraKeysVisible
=== true) {
1229 document
.getElementById('noVNC_toggleCtrl_button').style
.display
= "";
1230 document
.getElementById('noVNC_toggleAlt_button').style
.display
= "";
1231 document
.getElementById('noVNC_sendTab_button').style
.display
= "";
1232 document
.getElementById('noVNC_sendEsc_button').style
.display
= "";
1233 document
.getElementById('noVNC_toggleExtraKeys_button').className
= "noVNC_status_button";
1234 UI
.extraKeysVisible
= false;
1238 sendEsc: function() {
1240 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1243 sendTab: function() {
1245 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1248 toggleCtrl: function() {
1250 if(UI
.ctrlOn
=== false) {
1251 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1252 document
.getElementById('noVNC_toggleCtrl_button').className
= "noVNC_status_button_selected";
1254 } else if(UI
.ctrlOn
=== true) {
1255 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1256 document
.getElementById('noVNC_toggleCtrl_button').className
= "noVNC_status_button";
1261 toggleAlt: function() {
1263 if(UI
.altOn
=== false) {
1264 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1265 document
.getElementById('noVNC_toggleAlt_button').className
= "noVNC_status_button_selected";
1267 } else if(UI
.altOn
=== true) {
1268 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1269 document
.getElementById('noVNC_toggleAlt_button').className
= "noVNC_status_button";
1274 sendCtrlAltDel: function() {
1275 UI
.rfb
.sendCtrlAltDel();
1284 hideMouseButton: function() {
1285 UI
.setMouseButton(-1);
1288 setMouseButton: function(num
) {
1290 UI
.rfb
.get_mouse().set_touchButton(num
);
1293 var blist
= [0, 1,2,4];
1294 for (var b
= 0; b
< blist
.length
; b
++) {
1295 var button
= document
.getElementById('noVNC_mouse_button' + blist
[b
]);
1296 if (blist
[b
] === num
) {
1297 button
.style
.display
= "";
1299 button
.style
.display
= "none";
1304 displayBlur: function() {
1305 if (!UI
.rfb
) return;
1307 UI
.rfb
.get_keyboard().set_focused(false);
1308 UI
.rfb
.get_mouse().set_focused(false);
1311 displayFocus: function() {
1312 if (!UI
.rfb
) return;
1314 UI
.rfb
.get_keyboard().set_focused(true);
1315 UI
.rfb
.get_mouse().set_focused(true);
1318 // Display the desktop name in the document title
1319 updateDocumentTitle: function(rfb
, name
) {
1320 document
.title
= name
+ " - noVNC";
1323 //Helper to add options to dropdown.
1324 addOption: function(selectbox
, text
, value
) {
1325 var optn
= document
.createElement("OPTION");
1328 selectbox
.options
.add(optn
);
1331 setBarPosition: function() {
1332 document
.getElementById('noVNC_control_bar').style
.top
= (window
.pageYOffset
) + 'px';
1333 document
.getElementById('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
1335 var vncwidth
= document
.getElementById('noVNC_container').style
.offsetWidth
;
1336 document
.getElementById('noVNC_control_bar').style
.width
= vncwidth
+ 'px';
1345 /* [module] UI.load(); */
1348 /* [module] export default UI; */