]>
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 * Copyright (C) 2016 Pierre Ossman for Cendio AB
6 * Licensed under MPL 2.0 (see LICENSE.txt)
8 * See README.md for usage and integration instructions.
11 /* jslint white: false, browser: true */
12 /* global window, document.getElementById, Util, WebUtil, RFB, Display */
15 * import Util from "../core/util";
16 * import KeyTable from "../core/input/keysym";
17 * import RFB from "../core/rfb";
18 * import Display from "../core/display";
19 * import WebUtil from "./webutil";
27 /* [begin skip-as-module] */
28 // Load supporting scripts
30 {'core': ["base64.js", "websock.js", "des.js", "input/keysymdef.js",
31 "input/xtscancodes.js", "input/util.js", "input/devices.js",
32 "display.js", "inflator.js", "rfb.js", "input/keysym.js"]});
34 window
.onscriptsload = function () { UI
.load(); };
35 /* [end skip-as-module] */
43 hideKeyboardTimeout
: null,
45 keyboardVisible
: false,
49 rememberedClipSetting
: null,
50 lastKeyboardinput
: null,
51 defaultKeyboardinputLen
: 100,
53 // Setup rfb object, load settings from browser storage, then call
54 // UI.init to setup the UI/menus
55 load: function(callback
) {
56 WebUtil
.initSettings(UI
.start
, callback
);
59 // Render default UI and initialize settings menu
60 start: function(callback
) {
62 // Setup global variables first
63 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
64 UI
.isSafari
= (navigator
.userAgent
.indexOf('Safari') !== -1 &&
65 navigator
.userAgent
.indexOf('Chrome') === -1);
69 // Adapt the interface for touch screen devices
70 if (UI
.isTouchDevice
) {
71 document
.documentElement
.classList
.add("noVNC_touch");
72 // Remove the address bar
73 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
74 UI
.forceSetting('clip', true);
76 UI
.initSetting('clip', false);
79 // Setup and initialize event handlers
80 UI
.setupWindowEvents();
82 UI
.addControlbarHandlers();
83 UI
.addTouchSpecificHandlers();
84 UI
.addExtraKeysHandlers();
86 UI
.addConnectionControlHandlers();
87 UI
.addClipboardHandlers();
88 UI
.addSettingsHandlers();
90 // Show the connect panel on first load unless autoconnecting
92 UI
.openConnectPanel();
97 UI
.updateVisualState();
99 document
.getElementById('noVNC_setting_host').focus();
101 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
102 if (autoconnect
=== 'true' || autoconnect
== '1') {
109 if (typeof callback
=== "function") {
114 initSettings: function() {
115 // Stylesheet selection dropdown
116 var sheet
= WebUtil
.selectStylesheet();
117 var sheets
= WebUtil
.getStylesheets();
119 for (i
= 0; i
< sheets
.length
; i
+= 1) {
120 UI
.addOption(document
.getElementById('noVNC_setting_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
123 // Logging selection dropdown
124 var llevels
= ['error', 'warn', 'info', 'debug'];
125 for (i
= 0; i
< llevels
.length
; i
+= 1) {
126 UI
.addOption(document
.getElementById('noVNC_setting_logging'),llevels
[i
], llevels
[i
]);
129 // Settings with immediate effects
130 UI
.initSetting('logging', 'warn');
131 WebUtil
.init_logging(UI
.getSetting('logging'));
133 UI
.initSetting('stylesheet', 'default');
134 WebUtil
.selectStylesheet(null);
135 // call twice to get around webkit bug
136 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
138 // if port == 80 (or 443) then it won't be present and should be
140 var port
= window
.location
.port
;
142 if (window
.location
.protocol
.substring(0,5) == 'https') {
145 else if (window
.location
.protocol
.substring(0,4) == 'http') {
150 /* Populate the controls if defaults are provided in the URL */
151 UI
.initSetting('host', window
.location
.hostname
);
152 UI
.initSetting('port', port
);
153 UI
.initSetting('password', '');
154 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
155 UI
.initSetting('true_color', true);
156 UI
.initSetting('cursor', !UI
.isTouchDevice
);
157 UI
.initSetting('resize', 'off');
158 UI
.initSetting('shared', true);
159 UI
.initSetting('view_only', false);
160 UI
.initSetting('path', 'websockify');
161 UI
.initSetting('repeaterID', '');
162 UI
.initSetting('token', '');
165 setupWindowEvents: function() {
166 window
.addEventListener( 'resize', function () {
167 UI
.applyResizeMode();
172 document
.getElementById("noVNC_status")
173 .addEventListener('click', UI
.hideStatus
);
176 setupFullscreen: function() {
177 // Only show the button if fullscreen is properly supported
178 // * Safari doesn't support alphanumerical input while in fullscreen
180 (document
.documentElement
.requestFullscreen
||
181 document
.documentElement
.mozRequestFullScreen
||
182 document
.documentElement
.webkitRequestFullscreen
||
183 document
.body
.msRequestFullscreen
)) {
184 document
.getElementById('noVNC_fullscreen_button')
185 .classList
.remove("noVNC_hidden");
186 UI
.addFullscreenHandlers();
190 addControlbarHandlers: function() {
191 document
.getElementById("noVNC_view_drag_button")
192 .addEventListener('click', UI
.toggleViewDrag
);
193 document
.getElementById("noVNC_send_ctrl_alt_del_button")
194 .addEventListener('click', UI
.sendCtrlAltDel
);
197 addTouchSpecificHandlers: function() {
198 document
.getElementById("noVNC_mouse_button0")
199 .addEventListener('click', function () { UI
.setMouseButton(1); });
200 document
.getElementById("noVNC_mouse_button1")
201 .addEventListener('click', function () { UI
.setMouseButton(2); });
202 document
.getElementById("noVNC_mouse_button2")
203 .addEventListener('click', function () { UI
.setMouseButton(4); });
204 document
.getElementById("noVNC_mouse_button4")
205 .addEventListener('click', function () { UI
.setMouseButton(0); });
206 document
.getElementById("noVNC_keyboard_button")
207 .addEventListener('click', UI
.showKeyboard
);
209 document
.getElementById("noVNC_keyboardinput")
210 .addEventListener('input', UI
.keyInput
);
211 document
.getElementById("noVNC_keyboardinput")
212 .addEventListener('blur', UI
.hideKeyboard
);
213 document
.getElementById("noVNC_keyboardinput")
214 .addEventListener('submit', function () { return false; });
216 window
.addEventListener('load', UI
.keyboardinputReset
);
219 addExtraKeysHandlers: function() {
220 document
.getElementById("noVNC_toggle_extra_keys_button")
221 .addEventListener('click', UI
.toggleExtraKeys
);
222 document
.getElementById("noVNC_toggle_ctrl_button")
223 .addEventListener('click', UI
.toggleCtrl
);
224 document
.getElementById("noVNC_toggle_alt_button")
225 .addEventListener('click', UI
.toggleAlt
);
226 document
.getElementById("noVNC_send_tab_button")
227 .addEventListener('click', UI
.sendTab
);
228 document
.getElementById("noVNC_send_esc_button")
229 .addEventListener('click', UI
.sendEsc
);
232 addXvpHandlers: function() {
233 document
.getElementById("noVNC_xvp_shutdown_button")
234 .addEventListener('click', function() { UI
.rfb
.xvpShutdown(); });
235 document
.getElementById("noVNC_xvp_reboot_button")
236 .addEventListener('click', function() { UI
.rfb
.xvpReboot(); });
237 document
.getElementById("noVNC_xvp_reset_button")
238 .addEventListener('click', function() { UI
.rfb
.xvpReset(); });
239 document
.getElementById("noVNC_xvp_button")
240 .addEventListener('click', UI
.toggleXvpPanel
);
243 addConnectionControlHandlers: function() {
244 document
.getElementById("noVNC_connect_controls_button")
245 .addEventListener('click', UI
.toggleConnectPanel
);
246 document
.getElementById("noVNC_disconnect_button")
247 .addEventListener('click', UI
.disconnect
);
248 document
.getElementById("noVNC_connect_button")
249 .addEventListener('click', UI
.connect
);
251 document
.getElementById("noVNC_password_button")
252 .addEventListener('click', UI
.setPassword
);
255 addClipboardHandlers: function() {
256 document
.getElementById("noVNC_clipboard_button")
257 .addEventListener('click', UI
.toggleClipboardPanel
);
258 document
.getElementById("noVNC_clipboard_text")
259 .addEventListener('focus', UI
.displayBlur
);
260 document
.getElementById("noVNC_clipboard_text")
261 .addEventListener('blur', UI
.displayFocus
);
262 document
.getElementById("noVNC_clipboard_text")
263 .addEventListener('change', UI
.clipboardSend
);
264 document
.getElementById("noVNC_clipboard_clear_button")
265 .addEventListener('click', UI
.clipboardClear
);
268 addSettingsHandlers: function() {
269 document
.getElementById("noVNC_settings_button")
270 .addEventListener('click', UI
.toggleSettingsPanel
);
271 document
.getElementById("noVNC_settings_apply")
272 .addEventListener('click', UI
.settingsApply
);
274 document
.getElementById("noVNC_setting_resize")
275 .addEventListener('change', UI
.enableDisableViewClip
);
278 addFullscreenHandlers: function() {
279 document
.getElementById("noVNC_fullscreen_button")
280 .addEventListener('click', UI
.toggleFullscreen
);
282 window
.addEventListener('fullscreenchange', UI
.updateFullscreenButton
);
283 window
.addEventListener('mozfullscreenchange', UI
.updateFullscreenButton
);
284 window
.addEventListener('webkitfullscreenchange', UI
.updateFullscreenButton
);
285 window
.addEventListener('msfullscreenchange', UI
.updateFullscreenButton
);
288 initRFB: function() {
290 UI
.rfb
= new RFB({'target': document
.getElementById('noVNC_canvas'),
291 'onUpdateState': UI
.updateState
,
292 'onXvpInit': UI
.updateXvpButton
,
293 'onClipboard': UI
.clipboardReceive
,
294 'onFBUComplete': UI
.initialResize
,
295 'onFBResize': UI
.updateViewDrag
,
296 'onDesktopName': UI
.updateDocumentTitle
});
299 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
310 updateState: function(rfb
, state
, oldstate
, msg
) {
311 UI
.rfb_state
= state
;
313 if (typeof(msg
) !== 'undefined') {
317 // zero means no timeout
318 UI
.showStatus(msg
, 'error', 0);
324 UI
.showStatus(msg
, 'normal');
327 document
.getElementById('noVNC_password_dlg')
328 .classList
.add('noVNC_open');
329 setTimeout(function () {
330 document
.getElementById(('noVNC_password_input').focus());
333 UI
.showStatus(msg
, 'warn');
336 UI
.showStatus(msg
, 'warn');
341 UI
.updateVisualState();
344 // Disable/enable controls depending on connection state
345 updateVisualState: function() {
346 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
348 //Util.Debug(">> updateVisualState");
349 document
.getElementById('noVNC_setting_encrypt').disabled
= connected
;
350 document
.getElementById('noVNC_setting_true_color').disabled
= connected
;
351 if (Util
.browserSupportsCursorURIs()) {
352 document
.getElementById('noVNC_setting_cursor').disabled
= connected
;
354 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
355 document
.getElementById('noVNC_setting_cursor').disabled
= true;
358 UI
.enableDisableViewClip();
359 document
.getElementById('noVNC_setting_resize').disabled
= connected
;
360 document
.getElementById('noVNC_setting_shared').disabled
= connected
;
361 document
.getElementById('noVNC_setting_view_only').disabled
= connected
;
362 document
.getElementById('noVNC_setting_path').disabled
= connected
;
363 document
.getElementById('noVNC_setting_repeaterID').disabled
= connected
;
366 document
.documentElement
.classList
.add("noVNC_connected");
368 UI
.setMouseButton(1);
370 document
.documentElement
.classList
.remove("noVNC_connected");
371 UI
.updateXvpButton(0);
374 // State change disables viewport dragging.
375 // It is enabled (toggled) by direct click on the button
376 UI
.setViewDrag(false);
378 // State change also closes the password dialog
379 document
.getElementById('noVNC_password_dlg')
380 .classList
.remove('noVNC_open');
382 switch (UI
.rfb_state
) {
386 UI
.openConnectPanel();
394 //Util.Debug("<< updateVisualState");
397 showStatus: function(text
, status_type
, time
) {
398 var statusElem
= document
.getElementById('noVNC_status');
400 clearTimeout(UI
.statusTimeout
);
402 if (typeof status_type
=== 'undefined') {
403 status_type
= 'normal';
406 statusElem
.classList
.remove("noVNC_status_normal",
408 "noVNC_status_error");
410 switch (status_type
) {
413 statusElem
.classList
.add("noVNC_status_warn");
416 statusElem
.classList
.add("noVNC_status_error");
421 statusElem
.classList
.add("noVNC_status_normal");
425 statusElem
.innerHTML
= text
;
426 statusElem
.classList
.add("noVNC_open");
428 // If no time was specified, show the status for 1.5 seconds
429 if (typeof time
=== 'undefined') {
433 // A specified time of zero means no timeout
435 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
439 hideStatus: function() {
440 clearTimeout(UI
.statusTimeout
);
441 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
450 // Initial page load read/initialization of settings
451 initSetting: function(name
, defVal
) {
452 // Check Query string followed by cookie
453 var val
= WebUtil
.getConfigVar(name
);
455 val
= WebUtil
.readSetting(name
, defVal
);
457 UI
.updateSetting(name
, val
);
461 // Update cookie and form control setting. If value is not set, then
462 // updates from control to current cookie setting.
463 updateSetting: function(name
, value
) {
465 // Save the cookie for this session
466 if (typeof value
!== 'undefined') {
467 WebUtil
.writeSetting(name
, value
);
470 // Update the settings control
471 value
= UI
.getSetting(name
);
473 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
474 if (ctrl
.type
=== 'checkbox') {
475 ctrl
.checked
= value
;
477 } else if (typeof ctrl
.options
!== 'undefined') {
478 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
479 if (ctrl
.options
[i
].value
=== value
) {
480 ctrl
.selectedIndex
= i
;
485 /*Weird IE9 error leads to 'null' appearring
486 in textboxes instead of ''.*/
487 if (value
=== null) {
494 // Save control setting to cookie
495 saveSetting: function(name
) {
496 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
497 if (ctrl
.type
=== 'checkbox') {
499 } else if (typeof ctrl
.options
!== 'undefined') {
500 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
504 WebUtil
.writeSetting(name
, val
);
505 //Util.Debug("Setting saved '" + name + "=" + val + "'");
509 // Force a setting to be a certain value
510 forceSetting: function(name
, val
) {
511 UI
.updateSetting(name
, val
);
515 // Read form control compatible setting from cookie
516 getSetting: function(name
) {
517 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
518 var val
= WebUtil
.readSetting(name
);
519 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
520 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
529 // Save/apply settings when 'Apply' button is pressed
530 settingsApply: function() {
531 //Util.Debug(">> settingsApply");
532 UI
.saveSetting('encrypt');
533 UI
.saveSetting('true_color');
534 if (Util
.browserSupportsCursorURIs()) {
535 UI
.saveSetting('cursor');
538 UI
.saveSetting('resize');
540 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
541 UI
.forceSetting('clip', false);
544 UI
.saveSetting('clip');
545 UI
.saveSetting('shared');
546 UI
.saveSetting('view_only');
547 UI
.saveSetting('path');
548 UI
.saveSetting('repeaterID');
549 UI
.saveSetting('stylesheet');
550 UI
.saveSetting('logging');
552 // Settings with immediate (non-connected related) effect
553 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
554 WebUtil
.init_logging(UI
.getSetting('logging'));
557 //Util.Debug("<< settingsApply");
566 closeAllPanels: function() {
567 UI
.closeSettingsPanel();
569 UI
.closeClipboardPanel();
570 UI
.closeConnectPanel();
580 openSettingsPanel: function() {
583 UI
.updateSetting('encrypt');
584 UI
.updateSetting('true_color');
585 if (Util
.browserSupportsCursorURIs()) {
586 UI
.updateSetting('cursor');
588 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
589 document
.getElementById('noVNC_setting_cursor').disabled
= true;
591 UI
.updateSetting('clip');
592 UI
.updateSetting('resize');
593 UI
.updateSetting('shared');
594 UI
.updateSetting('view_only');
595 UI
.updateSetting('path');
596 UI
.updateSetting('repeaterID');
597 UI
.updateSetting('stylesheet');
598 UI
.updateSetting('logging');
600 document
.getElementById('noVNC_settings')
601 .classList
.add("noVNC_open");
602 document
.getElementById('noVNC_settings_button')
603 .classList
.add("noVNC_selected");
606 closeSettingsPanel: function() {
607 document
.getElementById('noVNC_settings')
608 .classList
.remove("noVNC_open");
609 document
.getElementById('noVNC_settings_button')
610 .classList
.remove("noVNC_selected");
613 // Toggle the settings menu:
614 // On open, settings are refreshed from saved cookies.
615 // On close, settings are applied
616 toggleSettingsPanel: function() {
617 if (document
.getElementById('noVNC_settings')
618 .classList
.contains("noVNC_open")) {
620 UI
.closeSettingsPanel();
622 UI
.openSettingsPanel();
632 openXvpPanel: function() {
635 document
.getElementById('noVNC_xvp')
636 .classList
.add("noVNC_open");
637 document
.getElementById('noVNC_xvp_button')
638 .classList
.add("noVNC_selected");
641 closeXvpPanel: function() {
642 document
.getElementById('noVNC_xvp')
643 .classList
.remove("noVNC_open");
644 document
.getElementById('noVNC_xvp_button')
645 .classList
.remove("noVNC_selected");
648 toggleXvpPanel: function() {
649 if (document
.getElementById('noVNC_xvp')
650 .classList
.contains("noVNC_open")) {
657 // Disable/enable XVP button
658 updateXvpButton: function(ver
) {
660 document
.getElementById('noVNC_xvp_button')
661 .classList
.remove("noVNC_hidden");
663 document
.getElementById('noVNC_xvp_button')
664 .classList
.add("noVNC_hidden");
665 // Close XVP panel if open
676 openClipboardPanel: function() {
679 document
.getElementById('noVNC_clipboard')
680 .classList
.add("noVNC_open");
681 document
.getElementById('noVNC_clipboard_button')
682 .classList
.add("noVNC_selected");
685 closeClipboardPanel: function() {
686 document
.getElementById('noVNC_clipboard')
687 .classList
.remove("noVNC_open");
688 document
.getElementById('noVNC_clipboard_button')
689 .classList
.remove("noVNC_selected");
692 toggleClipboardPanel: function() {
693 if (document
.getElementById('noVNC_clipboard')
694 .classList
.contains("noVNC_open")) {
695 UI
.closeClipboardPanel();
697 UI
.openClipboardPanel();
701 clipboardReceive: function(rfb
, text
) {
702 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
703 document
.getElementById('noVNC_clipboard_text').value
= text
;
704 Util
.Debug("<< UI.clipboardReceive");
707 clipboardClear: function() {
708 document
.getElementById('noVNC_clipboard_text').value
= "";
709 UI
.rfb
.clipboardPasteFrom("");
712 clipboardSend: function() {
713 var text
= document
.getElementById('noVNC_clipboard_text').value
;
714 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
715 UI
.rfb
.clipboardPasteFrom(text
);
716 Util
.Debug("<< UI.clipboardSend");
725 openConnectPanel: function() {
728 document
.getElementById('noVNC_connect_controls')
729 .classList
.add("noVNC_open");
730 document
.getElementById('noVNC_connect_controls_button')
731 .classList
.add("noVNC_selected");
733 document
.getElementById('noVNC_setting_host').focus();
736 closeConnectPanel: function() {
737 document
.getElementById('noVNC_connect_controls')
738 .classList
.remove("noVNC_open");
739 document
.getElementById('noVNC_connect_controls_button')
740 .classList
.remove("noVNC_selected");
742 UI
.saveSetting('host');
743 UI
.saveSetting('port');
744 UI
.saveSetting('token');
745 //UI.saveSetting('password');
748 toggleConnectPanel: function() {
749 if (document
.getElementById('noVNC_connect_controls')
750 .classList
.contains("noVNC_open")) {
751 UI
.closeConnectPanel();
753 UI
.openConnectPanel();
757 connect: function() {
760 var host
= document
.getElementById('noVNC_setting_host').value
;
761 var port
= document
.getElementById('noVNC_setting_port').value
;
762 var password
= document
.getElementById('noVNC_setting_password').value
;
763 var token
= document
.getElementById('noVNC_setting_token').value
;
764 var path
= document
.getElementById('noVNC_setting_path').value
;
766 //if token is in path then ignore the new token variable
768 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
771 if ((!host
) || (!port
)) {
772 throw new Error("Must set host and port");
775 if (!UI
.initRFB()) return;
777 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
778 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
779 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
780 UI
.rfb
.set_shared(UI
.getSetting('shared'));
781 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
782 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
784 UI
.rfb
.connect(host
, port
, password
, path
);
787 disconnect: function() {
791 // Restore the callback used for initial resize
792 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
794 // Don't display the connection settings until we're actually disconnected
797 setPassword: function() {
798 UI
.rfb
.sendPassword(document
.getElementById('noVNC_password_input').value
);
799 document
.getElementById('noVNC_password_dlg')
800 .classList
.remove('noVNC_open');
810 toggleFullscreen: function() {
811 if (document
.fullscreenElement
|| // alternative standard method
812 document
.mozFullScreenElement
|| // currently working methods
813 document
.webkitFullscreenElement
||
814 document
.msFullscreenElement
) {
815 if (document
.exitFullscreen
) {
816 document
.exitFullscreen();
817 } else if (document
.mozCancelFullScreen
) {
818 document
.mozCancelFullScreen();
819 } else if (document
.webkitExitFullscreen
) {
820 document
.webkitExitFullscreen();
821 } else if (document
.msExitFullscreen
) {
822 document
.msExitFullscreen();
825 if (document
.documentElement
.requestFullscreen
) {
826 document
.documentElement
.requestFullscreen();
827 } else if (document
.documentElement
.mozRequestFullScreen
) {
828 document
.documentElement
.mozRequestFullScreen();
829 } else if (document
.documentElement
.webkitRequestFullscreen
) {
830 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
831 } else if (document
.body
.msRequestFullscreen
) {
832 document
.body
.msRequestFullscreen();
835 UI
.enableDisableViewClip();
836 UI
.updateFullscreenButton();
839 updateFullscreenButton: function() {
840 if (document
.fullscreenElement
|| // alternative standard method
841 document
.mozFullScreenElement
|| // currently working methods
842 document
.webkitFullscreenElement
||
843 document
.msFullscreenElement
) {
844 document
.getElementById('noVNC_fullscreen_button')
845 .classList
.add("noVNC_selected");
847 document
.getElementById('noVNC_fullscreen_button')
848 .classList
.remove("noVNC_selected");
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
.showStatus("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 if (UI
.rfb_state
!== 'normal') return;
1049 // Check if viewport drag is possible. It is only possible
1050 // if the remote display is clipping the client display.
1051 if (UI
.rfb
.get_display().get_viewport() &&
1052 UI
.rfb
.get_display().clippingDisplay()) {
1056 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1059 UI
.rfb
.get_viewportDrag()) {
1060 // The size of the remote display is the same or smaller
1061 // than the client display. Make sure viewport drag isn't
1062 // active when it can't be used.
1063 UI
.rfb
.set_viewportDrag(false);
1066 if (UI
.rfb
.get_viewportDrag()) {
1067 viewDragButton
.classList
.add("noVNC_selected");
1069 viewDragButton
.classList
.remove("noVNC_selected");
1072 // Different behaviour for touch vs non-touch
1073 // The button is disabled instead of hidden on touch devices
1074 if (UI
.isTouchDevice
) {
1075 viewDragButton
.classList
.remove("noVNC_hidden");
1078 viewDragButton
.disabled
= false;
1080 viewDragButton
.disabled
= true;
1083 viewDragButton
.disabled
= false;
1086 viewDragButton
.classList
.remove("noVNC_hidden");
1088 viewDragButton
.classList
.add("noVNC_hidden");
1099 // On touch devices, show the OS keyboard
1100 showKeyboard: function() {
1101 var kbi
= document
.getElementById('noVNC_keyboardinput');
1102 var skb
= document
.getElementById('noVNC_keyboard_button');
1103 var l
= kbi
.value
.length
;
1104 if(UI
.keyboardVisible
=== false) {
1106 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
1107 catch (err
) {} // setSelectionRange is undefined in Google Chrome
1108 UI
.keyboardVisible
= true;
1109 skb
.classList
.add("noVNC_selected");
1110 } else if(UI
.keyboardVisible
=== true) {
1112 skb
.classList
.remove("noVNC_selected");
1113 UI
.keyboardVisible
= false;
1117 hideKeyboard: function() {
1118 document
.getElementById('noVNC_keyboard_button')
1119 .classList
.remove("noVNC_selected");
1120 //Weird bug in iOS if you change keyboardVisible
1121 //here it does not actually occur so next time
1122 //you click keyboard icon it doesnt work.
1123 UI
.hideKeyboardTimeout
= setTimeout(function() {
1124 UI
.keyboardVisible
= false;
1128 keepKeyboard: function() {
1129 clearTimeout(UI
.hideKeyboardTimeout
);
1130 if(UI
.keyboardVisible
=== true) {
1131 document
.getElementById('noVNC_keyboardinput').focus();
1132 document
.getElementById('noVNC_keyboard_button')
1133 .classList
.add("noVNC_selected");
1134 } else if(UI
.keyboardVisible
=== false) {
1135 document
.getElementById('noVNC_keyboardinput').blur();
1136 document
.getElementById('noVNC_keyboard_button')
1137 .classList
.remove("noVNC_selected");
1141 keyboardinputReset: function() {
1142 var kbi
= document
.getElementById('noVNC_keyboardinput');
1143 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1144 UI
.lastKeyboardinput
= kbi
.value
;
1147 // When normal keyboard events are left uncought, use the input events from
1148 // the keyboardinput element instead and generate the corresponding key events.
1149 // This code is required since some browsers on Android are inconsistent in
1150 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1151 keyInput: function(event
) {
1153 if (!UI
.rfb
) return;
1155 var newValue
= event
.target
.value
;
1157 if (!UI
.lastKeyboardinput
) {
1158 UI
.keyboardinputReset();
1160 var oldValue
= UI
.lastKeyboardinput
;
1164 // Try to check caret position since whitespace at the end
1165 // will not be considered by value.length in some browsers
1166 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1168 // selectionStart is undefined in Google Chrome
1169 newLen
= newValue
.length
;
1171 var oldLen
= oldValue
.length
;
1174 var inputs
= newLen
- oldLen
;
1176 backspaces
= -inputs
;
1181 // Compare the old string with the new to account for
1182 // text-corrections or other input that modify existing text
1184 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1185 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1186 inputs
= newLen
- i
;
1187 backspaces
= oldLen
- i
;
1192 // Send the key events
1193 for (i
= 0; i
< backspaces
; i
++) {
1194 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1196 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1197 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1200 // Control the text content length in the keyboardinput element
1201 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1202 UI
.keyboardinputReset();
1203 } else if (newLen
< 1) {
1204 // There always have to be some text in the keyboardinput
1205 // element with which backspace can interact.
1206 UI
.keyboardinputReset();
1207 // This sometimes causes the keyboard to disappear for a second
1208 // but it is required for the android keyboard to recognize that
1209 // text has been added to the field
1210 event
.target
.blur();
1211 // This has to be ran outside of the input handler in order to work
1212 setTimeout(UI
.keepKeyboard
, 0);
1214 UI
.lastKeyboardinput
= newValue
;
1218 openExtraKeys: function() {
1219 UI
.closeAllPanels();
1221 document
.getElementById('noVNC_modifiers')
1222 .classList
.add("noVNC_open");
1223 document
.getElementById('noVNC_toggle_extra_keys_button')
1224 .classList
.add("noVNC_selected");
1227 closeExtraKeys: function() {
1228 document
.getElementById('noVNC_modifiers')
1229 .classList
.remove("noVNC_open");
1230 document
.getElementById('noVNC_toggle_extra_keys_button')
1231 .classList
.remove("noVNC_selected");
1234 toggleExtraKeys: function() {
1236 if(document
.getElementById('noVNC_modifiers')
1237 .classList
.contains("noVNC_open")) {
1238 UI
.closeExtraKeys();
1244 sendEsc: function() {
1246 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1249 sendTab: function() {
1251 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1254 toggleCtrl: function() {
1256 var btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1257 if (btn
.classList
.contains("noVNC_selected")) {
1258 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1259 btn
.classList
.remove("noVNC_selected");
1261 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1262 btn
.classList
.add("noVNC_selected");
1266 toggleAlt: function() {
1268 var btn
= document
.getElementById('noVNC_toggle_alt_button');
1269 if (btn
.classList
.contains("noVNC_selected")) {
1270 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1271 btn
.classList
.remove("noVNC_selected");
1273 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1274 btn
.classList
.add("noVNC_selected");
1278 sendCtrlAltDel: function() {
1279 UI
.rfb
.sendCtrlAltDel();
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
.classList
.remove("noVNC_hidden");
1299 button
.classList
.add("noVNC_hidden");
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
);
1337 /* [module] UI.load(); */
1340 /* [module] export default UI; */