]>
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 // Show mouse selector buttons on touch screen devices
70 if (UI
.isTouchDevice
) {
71 // Show mobile buttons
72 document
.getElementById('noVNC_mobile_buttons')
73 .classList
.remove("noVNC_hidden");
75 // Remove the address bar
76 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
77 UI
.forceSetting('clip', true);
79 UI
.initSetting('clip', false);
82 // Setup and initialize event handlers
83 UI
.setupWindowEvents();
85 UI
.addControlbarHandlers();
86 UI
.addTouchSpecificHandlers();
88 UI
.addConnectionControlHandlers();
89 UI
.addClipboardHandlers();
90 UI
.addSettingsHandlers();
92 // Show the connect panel on first load unless autoconnecting
94 UI
.openConnectPanel();
100 UI
.updateVisualState();
102 document
.getElementById('noVNC_setting_host').focus();
104 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
105 if (autoconnect
=== 'true' || autoconnect
== '1') {
112 if (typeof callback
=== "function") {
117 initSettings: function() {
118 // Stylesheet selection dropdown
119 var sheet
= WebUtil
.selectStylesheet();
120 var sheets
= WebUtil
.getStylesheets();
122 for (i
= 0; i
< sheets
.length
; i
+= 1) {
123 UI
.addOption(document
.getElementById('noVNC_setting_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
126 // Logging selection dropdown
127 var llevels
= ['error', 'warn', 'info', 'debug'];
128 for (i
= 0; i
< llevels
.length
; i
+= 1) {
129 UI
.addOption(document
.getElementById('noVNC_setting_logging'),llevels
[i
], llevels
[i
]);
132 // Settings with immediate effects
133 UI
.initSetting('logging', 'warn');
134 WebUtil
.init_logging(UI
.getSetting('logging'));
136 UI
.initSetting('stylesheet', 'default');
137 WebUtil
.selectStylesheet(null);
138 // call twice to get around webkit bug
139 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
141 // if port == 80 (or 443) then it won't be present and should be
143 var port
= window
.location
.port
;
145 if (window
.location
.protocol
.substring(0,5) == 'https') {
148 else if (window
.location
.protocol
.substring(0,4) == 'http') {
153 /* Populate the controls if defaults are provided in the URL */
154 UI
.initSetting('host', window
.location
.hostname
);
155 UI
.initSetting('port', port
);
156 UI
.initSetting('password', '');
157 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
158 UI
.initSetting('true_color', true);
159 UI
.initSetting('cursor', !UI
.isTouchDevice
);
160 UI
.initSetting('resize', 'off');
161 UI
.initSetting('shared', true);
162 UI
.initSetting('view_only', false);
163 UI
.initSetting('path', 'websockify');
164 UI
.initSetting('repeaterID', '');
165 UI
.initSetting('token', '');
168 setupWindowEvents: function() {
169 window
.addEventListener( 'resize', function () {
170 UI
.applyResizeMode();
176 document
.getElementById("noVNC_status")
177 .addEventListener('click', UI
.hideStatus
);
180 setupFullscreen: function() {
181 // Only show the button if fullscreen is properly supported
182 // * Safari doesn't support alphanumerical input while in fullscreen
184 (document
.documentElement
.requestFullscreen
||
185 document
.documentElement
.mozRequestFullScreen
||
186 document
.documentElement
.webkitRequestFullscreen
||
187 document
.body
.msRequestFullscreen
)) {
188 document
.getElementById('noVNC_fullscreen_button')
189 .classList
.remove("noVNC_hidden");
190 UI
.addFullscreenHandlers();
194 addControlbarHandlers: function() {
195 document
.getElementById("noVNC_view_drag_button")
196 .addEventListener('click', UI
.toggleViewDrag
);
197 document
.getElementById("noVNC_send_ctrl_alt_del_button")
198 .addEventListener('click', UI
.sendCtrlAltDel
);
201 addTouchSpecificHandlers: function() {
202 document
.getElementById("noVNC_mouse_button0")
203 .addEventListener('click', function () { UI
.setMouseButton(1); });
204 document
.getElementById("noVNC_mouse_button1")
205 .addEventListener('click', function () { UI
.setMouseButton(2); });
206 document
.getElementById("noVNC_mouse_button2")
207 .addEventListener('click', function () { UI
.setMouseButton(4); });
208 document
.getElementById("noVNC_mouse_button4")
209 .addEventListener('click', function () { UI
.setMouseButton(0); });
210 document
.getElementById("noVNC_keyboard_button")
211 .addEventListener('click', UI
.showKeyboard
);
213 document
.getElementById("noVNC_keyboardinput")
214 .addEventListener('input', UI
.keyInput
);
215 document
.getElementById("noVNC_keyboardinput")
216 .addEventListener('blur', UI
.hideKeyboard
);
217 document
.getElementById("noVNC_keyboardinput")
218 .addEventListener('submit', function () { return false; });
220 window
.addEventListener('load', UI
.keyboardinputReset
);
222 document
.getElementById("noVNC_toggle_extra_keys_button")
223 .addEventListener('click', UI
.toggleExtraKeys
);
224 document
.getElementById("noVNC_toggle_ctrl_button")
225 .addEventListener('click', UI
.toggleCtrl
);
226 document
.getElementById("noVNC_toggle_alt_button")
227 .addEventListener('click', UI
.toggleAlt
);
228 document
.getElementById("noVNC_send_tab_button")
229 .addEventListener('click', UI
.sendTab
);
230 document
.getElementById("noVNC_send_esc_button")
231 .addEventListener('click', UI
.sendEsc
);
234 addXvpHandlers: function() {
235 document
.getElementById("noVNC_xvp_shutdown_button")
236 .addEventListener('click', function() { UI
.rfb
.xvpShutdown(); });
237 document
.getElementById("noVNC_xvp_reboot_button")
238 .addEventListener('click', function() { UI
.rfb
.xvpReboot(); });
239 document
.getElementById("noVNC_xvp_reset_button")
240 .addEventListener('click', function() { UI
.rfb
.xvpReset(); });
241 document
.getElementById("noVNC_xvp_button")
242 .addEventListener('click', UI
.toggleXvpPanel
);
245 addConnectionControlHandlers: function() {
246 document
.getElementById("noVNC_connect_controls_button")
247 .addEventListener('click', UI
.toggleConnectPanel
);
248 document
.getElementById("noVNC_disconnect_button")
249 .addEventListener('click', UI
.disconnect
);
250 document
.getElementById("noVNC_connect_button")
251 .addEventListener('click', UI
.connect
);
254 addClipboardHandlers: function() {
255 document
.getElementById("noVNC_clipboard_button")
256 .addEventListener('click', UI
.toggleClipboardPanel
);
257 document
.getElementById("noVNC_clipboard_text")
258 .addEventListener('focus', UI
.displayBlur
);
259 document
.getElementById("noVNC_clipboard_text")
260 .addEventListener('blur', UI
.displayFocus
);
261 document
.getElementById("noVNC_clipboard_text")
262 .addEventListener('change', UI
.clipboardSend
);
263 document
.getElementById("noVNC_clipboard_clear_button")
264 .addEventListener('click', UI
.clipboardClear
);
267 addSettingsHandlers: function() {
268 document
.getElementById("noVNC_settings_button")
269 .addEventListener('click', UI
.toggleSettingsPanel
);
270 document
.getElementById("noVNC_settings_apply")
271 .addEventListener('click', UI
.settingsApply
);
273 document
.getElementById("noVNC_setting_resize")
274 .addEventListener('change', UI
.enableDisableViewClip
);
277 addFullscreenHandlers: function() {
278 document
.getElementById("noVNC_fullscreen_button")
279 .addEventListener('click', UI
.toggleFullscreen
);
281 window
.addEventListener('fullscreenchange', UI
.updateFullscreenButton
);
282 window
.addEventListener('mozfullscreenchange', UI
.updateFullscreenButton
);
283 window
.addEventListener('webkitfullscreenchange', UI
.updateFullscreenButton
);
284 window
.addEventListener('msfullscreenchange', UI
.updateFullscreenButton
);
287 initRFB: function() {
289 UI
.rfb
= new RFB({'target': document
.getElementById('noVNC_canvas'),
290 'onUpdateState': UI
.updateState
,
291 'onXvpInit': UI
.updateXvpButton
,
292 'onClipboard': UI
.clipboardReceive
,
293 'onFBUComplete': UI
.initialResize
,
294 'onFBResize': UI
.updateViewDrag
,
295 'onDesktopName': UI
.updateDocumentTitle
});
298 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
309 updateState: function(rfb
, state
, oldstate
, msg
) {
310 UI
.rfb_state
= state
;
316 klass
= "noVNC_status_error";
317 timeout
= 0; // zero means no timeout
320 klass
= "noVNC_status_normal";
325 klass
= "noVNC_status_normal";
328 UI
.toggleConnectPanel();
330 document
.getElementById('noVNC_connect_button').value
= "Send Password";
331 document
.getElementById('noVNC_connect_button').onclick
= UI
.setPassword
;
332 document
.getElementById('noVNC_setting_password').focus();
334 klass
= "noVNC_status_warn";
337 klass
= "noVNC_status_warn";
341 if (typeof(msg
) !== 'undefined') {
342 document
.getElementById('noVNC_status')
343 .classList
.remove("noVNC_status_normal",
345 "noVNC_status_error");
346 document
.getElementById('noVNC_status').classList
.add(klass
);
347 UI
.showStatus(msg
, timeout
);
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
;
375 document
.getElementById('noVNC_logo')
376 .classList
.add("noVNC_hidden");
377 document
.getElementById('noVNC_screen')
378 .classList
.remove("noVNC_hidden");
380 UI
.setMouseButton(1);
381 document
.getElementById('noVNC_clipboard_button')
382 .classList
.remove("noVNC_hidden");
383 document
.getElementById('noVNC_keyboard_button')
384 .classList
.remove("noVNC_hidden");
385 document
.getElementById('noVNC_extra_keys')
386 .classList
.remove("noVNC_hidden");
387 document
.getElementById('noVNC_send_ctrl_alt_del_button')
388 .classList
.remove("noVNC_hidden");
390 document
.getElementById('noVNC_logo')
391 .classList
.remove("noVNC_hidden");
392 document
.getElementById('noVNC_screen')
393 .classList
.add("noVNC_hidden");
394 UI
.hideMouseButton();
395 document
.getElementById('noVNC_clipboard_button')
396 .classList
.add("noVNC_hidden");
397 document
.getElementById('noVNC_keyboard_button')
398 .classList
.add("noVNC_hidden");
399 document
.getElementById('noVNC_extra_keys')
400 .classList
.add("noVNC_hidden");
401 document
.getElementById('noVNC_send_ctrl_alt_del_button')
402 .classList
.add("noVNC_hidden");
403 UI
.updateXvpButton(0);
406 // State change disables viewport dragging.
407 // It is enabled (toggled) by direct click on the button
408 UI
.setViewDrag(false);
410 switch (UI
.rfb_state
) {
414 document
.getElementById('noVNC_connect_controls_button')
415 .classList
.remove("noVNC_hidden");
416 document
.getElementById('noVNC_disconnect_button')
417 .classList
.add("noVNC_hidden");
418 UI
.openConnectPanel();
421 document
.getElementById('noVNC_connect_controls_button')
422 .classList
.remove("noVNC_hidden");
423 document
.getElementById('noVNC_disconnect_button')
424 .classList
.add("noVNC_hidden");
427 document
.getElementById('noVNC_connect_controls_button')
428 .classList
.add("noVNC_hidden");
429 document
.getElementById('noVNC_disconnect_button')
430 .classList
.remove("noVNC_hidden");
434 //Util.Debug("<< updateVisualState");
437 showStatus: function(text
, time
) {
438 var statusElem
= document
.getElementById('noVNC_status');
440 clearTimeout(UI
.statusTimeout
);
442 statusElem
.innerHTML
= text
;
443 statusElem
.classList
.add("noVNC_open");
445 // If no time was specified, show the status for 1.5 seconds
446 if (typeof time
=== 'undefined') {
450 // A specified time of zero means no timeout
452 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
456 hideStatus: function() {
457 clearTimeout(UI
.statusTimeout
);
458 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
467 // Initial page load read/initialization of settings
468 initSetting: function(name
, defVal
) {
469 // Check Query string followed by cookie
470 var val
= WebUtil
.getConfigVar(name
);
472 val
= WebUtil
.readSetting(name
, defVal
);
474 UI
.updateSetting(name
, val
);
478 // Update cookie and form control setting. If value is not set, then
479 // updates from control to current cookie setting.
480 updateSetting: function(name
, value
) {
482 // Save the cookie for this session
483 if (typeof value
!== 'undefined') {
484 WebUtil
.writeSetting(name
, value
);
487 // Update the settings control
488 value
= UI
.getSetting(name
);
490 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
491 if (ctrl
.type
=== 'checkbox') {
492 ctrl
.checked
= value
;
494 } else if (typeof ctrl
.options
!== 'undefined') {
495 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
496 if (ctrl
.options
[i
].value
=== value
) {
497 ctrl
.selectedIndex
= i
;
502 /*Weird IE9 error leads to 'null' appearring
503 in textboxes instead of ''.*/
504 if (value
=== null) {
511 // Save control setting to cookie
512 saveSetting: function(name
) {
513 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
514 if (ctrl
.type
=== 'checkbox') {
516 } else if (typeof ctrl
.options
!== 'undefined') {
517 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
521 WebUtil
.writeSetting(name
, val
);
522 //Util.Debug("Setting saved '" + name + "=" + val + "'");
526 // Force a setting to be a certain value
527 forceSetting: function(name
, val
) {
528 UI
.updateSetting(name
, val
);
532 // Read form control compatible setting from cookie
533 getSetting: function(name
) {
534 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
535 var val
= WebUtil
.readSetting(name
);
536 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
537 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
546 // Save/apply settings when 'Apply' button is pressed
547 settingsApply: function() {
548 //Util.Debug(">> settingsApply");
549 UI
.saveSetting('encrypt');
550 UI
.saveSetting('true_color');
551 if (Util
.browserSupportsCursorURIs()) {
552 UI
.saveSetting('cursor');
555 UI
.saveSetting('resize');
557 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
558 UI
.forceSetting('clip', false);
561 UI
.saveSetting('clip');
562 UI
.saveSetting('shared');
563 UI
.saveSetting('view_only');
564 UI
.saveSetting('path');
565 UI
.saveSetting('repeaterID');
566 UI
.saveSetting('stylesheet');
567 UI
.saveSetting('logging');
569 // Settings with immediate (non-connected related) effect
570 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
571 WebUtil
.init_logging(UI
.getSetting('logging'));
574 //Util.Debug("<< settingsApply");
583 closeAllPanels: function() {
584 UI
.closeSettingsPanel();
586 UI
.closeClipboardPanel();
587 UI
.closeConnectPanel();
596 openSettingsPanel: function() {
599 UI
.updateSetting('encrypt');
600 UI
.updateSetting('true_color');
601 if (Util
.browserSupportsCursorURIs()) {
602 UI
.updateSetting('cursor');
604 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
605 document
.getElementById('noVNC_setting_cursor').disabled
= true;
607 UI
.updateSetting('clip');
608 UI
.updateSetting('resize');
609 UI
.updateSetting('shared');
610 UI
.updateSetting('view_only');
611 UI
.updateSetting('path');
612 UI
.updateSetting('repeaterID');
613 UI
.updateSetting('stylesheet');
614 UI
.updateSetting('logging');
616 document
.getElementById('noVNC_settings')
617 .classList
.add("noVNC_open");
618 document
.getElementById('noVNC_settings_button')
619 .classList
.add("noVNC_selected");
622 closeSettingsPanel: function() {
623 document
.getElementById('noVNC_settings')
624 .classList
.remove("noVNC_open");
625 document
.getElementById('noVNC_settings_button')
626 .classList
.remove("noVNC_selected");
629 // Toggle the settings menu:
630 // On open, settings are refreshed from saved cookies.
631 // On close, settings are applied
632 toggleSettingsPanel: function() {
633 if (document
.getElementById('noVNC_settings')
634 .classList
.contains("noVNC_open")) {
636 UI
.closeSettingsPanel();
638 UI
.openSettingsPanel();
648 openXvpPanel: function() {
651 document
.getElementById('noVNC_xvp')
652 .classList
.add("noVNC_open");
653 document
.getElementById('noVNC_xvp_button')
654 .classList
.add("noVNC_selected");
657 closeXvpPanel: function() {
658 document
.getElementById('noVNC_xvp')
659 .classList
.remove("noVNC_open");
660 document
.getElementById('noVNC_xvp_button')
661 .classList
.remove("noVNC_selected");
664 toggleXvpPanel: function() {
665 if (document
.getElementById('noVNC_xvp')
666 .classList
.contains("noVNC_open")) {
673 // Disable/enable XVP button
674 updateXvpButton: function(ver
) {
676 document
.getElementById('noVNC_xvp_button')
677 .classList
.remove("noVNC_hidden");
679 document
.getElementById('noVNC_xvp_button')
680 .classList
.add("noVNC_hidden");
681 // Close XVP panel if open
692 openClipboardPanel: function() {
695 document
.getElementById('noVNC_clipboard')
696 .classList
.add("noVNC_open");
697 document
.getElementById('noVNC_clipboard_button')
698 .classList
.add("noVNC_selected");
701 closeClipboardPanel: function() {
702 document
.getElementById('noVNC_clipboard')
703 .classList
.remove("noVNC_open");
704 document
.getElementById('noVNC_clipboard_button')
705 .classList
.remove("noVNC_selected");
708 toggleClipboardPanel: function() {
709 if (document
.getElementById('noVNC_clipboard')
710 .classList
.contains("noVNC_open")) {
711 UI
.closeClipboardPanel();
713 UI
.openClipboardPanel();
717 clipboardReceive: function(rfb
, text
) {
718 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
719 document
.getElementById('noVNC_clipboard_text').value
= text
;
720 Util
.Debug("<< UI.clipboardReceive");
723 clipboardClear: function() {
724 document
.getElementById('noVNC_clipboard_text').value
= "";
725 UI
.rfb
.clipboardPasteFrom("");
728 clipboardSend: function() {
729 var text
= document
.getElementById('noVNC_clipboard_text').value
;
730 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
731 UI
.rfb
.clipboardPasteFrom(text
);
732 Util
.Debug("<< UI.clipboardSend");
741 openConnectPanel: function() {
744 document
.getElementById('noVNC_connect_controls')
745 .classList
.add("noVNC_open");
746 document
.getElementById('noVNC_connect_controls_button')
747 .classList
.add("noVNC_selected");
749 document
.getElementById('noVNC_setting_host').focus();
752 closeConnectPanel: function() {
753 document
.getElementById('noVNC_connect_controls')
754 .classList
.remove("noVNC_open");
755 document
.getElementById('noVNC_connect_controls_button')
756 .classList
.remove("noVNC_selected");
758 UI
.saveSetting('host');
759 UI
.saveSetting('port');
760 UI
.saveSetting('token');
761 //UI.saveSetting('password');
764 toggleConnectPanel: function() {
765 if (document
.getElementById('noVNC_connect_controls')
766 .classList
.contains("noVNC_open")) {
767 UI
.closeConnectPanel();
769 UI
.openConnectPanel();
773 connect: function() {
776 var host
= document
.getElementById('noVNC_setting_host').value
;
777 var port
= document
.getElementById('noVNC_setting_port').value
;
778 var password
= document
.getElementById('noVNC_setting_password').value
;
779 var token
= document
.getElementById('noVNC_setting_token').value
;
780 var path
= document
.getElementById('noVNC_setting_path').value
;
782 //if token is in path then ignore the new token variable
784 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
787 if ((!host
) || (!port
)) {
788 throw new Error("Must set host and port");
791 if (!UI
.initRFB()) return;
793 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
794 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
795 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
796 UI
.rfb
.set_shared(UI
.getSetting('shared'));
797 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
798 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
800 UI
.rfb
.connect(host
, port
, password
, path
);
803 setTimeout(UI
.setBarPosition
, 100);
806 disconnect: function() {
810 // Restore the callback used for initial resize
811 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
813 // Don't display the connection settings until we're actually disconnected
816 setPassword: function() {
817 UI
.rfb
.sendPassword(document
.getElementById('noVNC_setting_password').value
);
818 //Reset connect button.
819 document
.getElementById('noVNC_connect_button').value
= "Connect";
820 document
.getElementById('noVNC_connect_button').onclick
= UI
.connect
;
821 //Hide connection panel.
822 UI
.toggleConnectPanel();
832 toggleFullscreen: function() {
833 if (document
.fullscreenElement
|| // alternative standard method
834 document
.mozFullScreenElement
|| // currently working methods
835 document
.webkitFullscreenElement
||
836 document
.msFullscreenElement
) {
837 if (document
.exitFullscreen
) {
838 document
.exitFullscreen();
839 } else if (document
.mozCancelFullScreen
) {
840 document
.mozCancelFullScreen();
841 } else if (document
.webkitExitFullscreen
) {
842 document
.webkitExitFullscreen();
843 } else if (document
.msExitFullscreen
) {
844 document
.msExitFullscreen();
847 if (document
.documentElement
.requestFullscreen
) {
848 document
.documentElement
.requestFullscreen();
849 } else if (document
.documentElement
.mozRequestFullScreen
) {
850 document
.documentElement
.mozRequestFullScreen();
851 } else if (document
.documentElement
.webkitRequestFullscreen
) {
852 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
853 } else if (document
.body
.msRequestFullscreen
) {
854 document
.body
.msRequestFullscreen();
857 UI
.enableDisableViewClip();
858 UI
.updateFullscreenButton();
861 updateFullscreenButton: function() {
862 if (document
.fullscreenElement
|| // alternative standard method
863 document
.mozFullScreenElement
|| // currently working methods
864 document
.webkitFullscreenElement
||
865 document
.msFullscreenElement
) {
866 document
.getElementById('noVNC_fullscreen_button')
867 .classList
.add("noVNC_selected");
869 document
.getElementById('noVNC_fullscreen_button')
870 .classList
.remove("noVNC_selected");
880 // Apply remote resizing or local scaling
881 applyResizeMode: function() {
884 var screen
= UI
.screenSize();
886 if (screen
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
888 var display
= UI
.rfb
.get_display();
889 var resizeMode
= UI
.getSetting('resize');
891 if (resizeMode
=== 'remote') {
893 // Request changing the resolution of the remote display to
894 // the size of the local browser viewport.
896 // In order to not send multiple requests before the browser-resize
897 // is finished we wait 0.5 seconds before sending the request.
898 clearTimeout(UI
.resizeTimeout
);
899 UI
.resizeTimeout
= setTimeout(function(){
901 // Limit the viewport to the size of the browser window
902 display
.set_maxWidth(screen
.w
);
903 display
.set_maxHeight(screen
.h
);
905 Util
.Debug('Attempting requestDesktopSize(' +
906 screen
.w
+ ', ' + screen
.h
+ ')');
908 // Request a remote size covering the viewport
909 UI
.rfb
.requestDesktopSize(screen
.w
, screen
.h
);
912 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
913 var downscaleOnly
= resizeMode
=== 'downscale';
914 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
915 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
916 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
921 // The screen is always the same size as the available viewport
922 // in the browser window minus the height of the control bar
923 screenSize: function() {
924 var screen
= document
.getElementById('noVNC_screen');
926 // Hide the scrollbars until the size is calculated
927 screen
.style
.overflow
= "hidden";
929 var pos
= Util
.getPosition(screen
);
933 screen
.style
.overflow
= "visible";
935 if (isNaN(w
) || isNaN(h
)) {
942 // Normally we only apply the current resize mode after a window resize
943 // event. This means that when a new connection is opened, there is no
944 // resize mode active.
945 // We have to wait until the first FBU because this is where the client
946 // will find the supported encodings of the server. Some calls later in
947 // the chain is dependant on knowing the server-capabilities.
948 initialResize: function(rfb
, fbu
) {
949 UI
.applyResizeMode();
950 // After doing this once, we remove the callback.
951 UI
.rfb
.set_onFBUComplete(function() { });
960 // Set and configure viewport clipping
961 setViewClip: function(clip
) {
962 UI
.updateSetting('clip', clip
);
966 // Update parameters that depend on the clip setting
967 updateViewClip: function() {
973 var display
= UI
.rfb
.get_display();
974 var cur_clip
= display
.get_viewport();
975 var new_clip
= UI
.getSetting('clip');
977 if (cur_clip
!== new_clip
) {
978 display
.set_viewport(new_clip
);
981 var size
= UI
.screenSize();
983 if (new_clip
&& size
) {
984 // When clipping is enabled, the screen is limited to
985 // the size of the browser window.
986 display
.set_maxWidth(size
.w
);
987 display
.set_maxHeight(size
.h
);
989 var screen
= document
.getElementById('noVNC_screen');
990 var canvas
= document
.getElementById('noVNC_canvas');
992 // Hide potential scrollbars that can skew the position
993 screen
.style
.overflow
= "hidden";
995 // The x position marks the left margin of the canvas,
996 // remove the margin from both sides to keep it centered.
997 var new_w
= size
.w
- (2 * Util
.getPosition(canvas
).x
);
999 screen
.style
.overflow
= "visible";
1001 display
.viewportChangeSize(new_w
, size
.h
);
1003 // Disable max dimensions
1004 display
.set_maxWidth(0);
1005 display
.set_maxHeight(0);
1006 display
.viewportChangeSize();
1010 // Handle special cases where clipping is forced on/off or locked
1011 enableDisableViewClip: function() {
1012 var resizeSetting
= document
.getElementById('noVNC_setting_resize');
1013 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
1016 // Safari auto-hides the scrollbars which makes them
1017 // impossible to use in most cases
1018 UI
.setViewClip(true);
1019 document
.getElementById('noVNC_setting_clip').disabled
= true;
1020 } else if (resizeSetting
.value
=== 'downscale' || resizeSetting
.value
=== 'scale') {
1021 // Disable clipping if we are scaling
1022 UI
.setViewClip(false);
1023 document
.getElementById('noVNC_setting_clip').disabled
= true;
1024 } else if (document
.msFullscreenElement
) {
1025 // The browser is IE and we are in fullscreen mode.
1026 // - We need to force clipping while in fullscreen since
1027 // scrollbars doesn't work.
1028 UI
.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1029 UI
.rememberedClipSetting
= UI
.getSetting('clip');
1030 UI
.setViewClip(true);
1031 document
.getElementById('noVNC_setting_clip').disabled
= true;
1032 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
1033 // Restore view clip to what it was before fullscreen on IE
1034 UI
.setViewClip(UI
.rememberedClipSetting
);
1035 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1037 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1038 if (UI
.isTouchDevice
) {
1039 UI
.setViewClip(true);
1050 toggleViewDrag: function() {
1051 if (!UI
.rfb
) return;
1053 var drag
= UI
.rfb
.get_viewportDrag();
1054 UI
.setViewDrag(!drag
);
1057 // Set the view drag mode which moves the viewport on mouse drags
1058 setViewDrag: function(drag
) {
1059 if (!UI
.rfb
) return;
1061 UI
.rfb
.set_viewportDrag(drag
);
1063 UI
.updateViewDrag();
1066 updateViewDrag: function() {
1067 var clipping
= false;
1069 // Check if viewport drag is possible. It is only possible
1070 // if the remote display is clipping the client display.
1071 if (UI
.rfb_state
=== 'normal' &&
1072 UI
.rfb
.get_display().get_viewport() &&
1073 UI
.rfb
.get_display().clippingDisplay()) {
1077 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1079 if (UI
.rfb_state
!== 'normal') {
1080 // Always hide when not connected
1081 viewDragButton
.classList
.add("noVNC_hidden");
1084 UI
.rfb
.get_viewportDrag()) {
1085 // The size of the remote display is the same or smaller
1086 // than the client display. Make sure viewport drag isn't
1087 // active when it can't be used.
1088 UI
.rfb
.set_viewportDrag(false);
1091 if (UI
.rfb
.get_viewportDrag()) {
1092 viewDragButton
.classList
.add("noVNC_selected");
1094 viewDragButton
.classList
.remove("noVNC_selected");
1097 // Different behaviour for touch vs non-touch
1098 // The button is disabled instead of hidden on touch devices
1099 if (UI
.isTouchDevice
) {
1100 viewDragButton
.classList
.remove("noVNC_hidden");
1103 viewDragButton
.disabled
= false;
1105 viewDragButton
.disabled
= true;
1108 viewDragButton
.disabled
= false;
1111 viewDragButton
.classList
.remove("noVNC_hidden");
1113 viewDragButton
.classList
.add("noVNC_hidden");
1125 // On touch devices, show the OS keyboard
1126 showKeyboard: function() {
1127 var kbi
= document
.getElementById('noVNC_keyboardinput');
1128 var skb
= document
.getElementById('noVNC_keyboard_button');
1129 var l
= kbi
.value
.length
;
1130 if(UI
.keyboardVisible
=== false) {
1132 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
1133 catch (err
) {} // setSelectionRange is undefined in Google Chrome
1134 UI
.keyboardVisible
= true;
1135 skb
.classList
.add("noVNC_selected");
1136 } else if(UI
.keyboardVisible
=== true) {
1138 skb
.classList
.remove("noVNC_selected");
1139 UI
.keyboardVisible
= false;
1143 hideKeyboard: function() {
1144 document
.getElementById('noVNC_keyboard_button')
1145 .classList
.remove("noVNC_selected");
1146 //Weird bug in iOS if you change keyboardVisible
1147 //here it does not actually occur so next time
1148 //you click keyboard icon it doesnt work.
1149 UI
.hideKeyboardTimeout
= setTimeout(function() {
1150 UI
.keyboardVisible
= false;
1154 keepKeyboard: function() {
1155 clearTimeout(UI
.hideKeyboardTimeout
);
1156 if(UI
.keyboardVisible
=== true) {
1157 document
.getElementById('noVNC_keyboardinput').focus();
1158 document
.getElementById('noVNC_keyboard_button')
1159 .classList
.add("noVNC_selected");
1160 } else if(UI
.keyboardVisible
=== false) {
1161 document
.getElementById('noVNC_keyboardinput').blur();
1162 document
.getElementById('noVNC_keyboard_button')
1163 .classList
.remove("noVNC_selected");
1167 keyboardinputReset: function() {
1168 var kbi
= document
.getElementById('noVNC_keyboardinput');
1169 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1170 UI
.lastKeyboardinput
= kbi
.value
;
1173 // When normal keyboard events are left uncought, use the input events from
1174 // the keyboardinput element instead and generate the corresponding key events.
1175 // This code is required since some browsers on Android are inconsistent in
1176 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1177 keyInput: function(event
) {
1179 if (!UI
.rfb
) return;
1181 var newValue
= event
.target
.value
;
1183 if (!UI
.lastKeyboardinput
) {
1184 UI
.keyboardinputReset();
1186 var oldValue
= UI
.lastKeyboardinput
;
1190 // Try to check caret position since whitespace at the end
1191 // will not be considered by value.length in some browsers
1192 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1194 // selectionStart is undefined in Google Chrome
1195 newLen
= newValue
.length
;
1197 var oldLen
= oldValue
.length
;
1200 var inputs
= newLen
- oldLen
;
1202 backspaces
= -inputs
;
1207 // Compare the old string with the new to account for
1208 // text-corrections or other input that modify existing text
1210 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1211 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1212 inputs
= newLen
- i
;
1213 backspaces
= oldLen
- i
;
1218 // Send the key events
1219 for (i
= 0; i
< backspaces
; i
++) {
1220 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1222 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1223 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1226 // Control the text content length in the keyboardinput element
1227 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1228 UI
.keyboardinputReset();
1229 } else if (newLen
< 1) {
1230 // There always have to be some text in the keyboardinput
1231 // element with which backspace can interact.
1232 UI
.keyboardinputReset();
1233 // This sometimes causes the keyboard to disappear for a second
1234 // but it is required for the android keyboard to recognize that
1235 // text has been added to the field
1236 event
.target
.blur();
1237 // This has to be ran outside of the input handler in order to work
1238 setTimeout(UI
.keepKeyboard
, 0);
1240 UI
.lastKeyboardinput
= newValue
;
1244 openExtraKeys: function() {
1245 document
.getElementById('noVNC_modifiers')
1246 .classList
.add("noVNC_open");
1247 document
.getElementById('noVNC_toggle_extra_keys_button')
1248 .classList
.add("noVNC_selected");
1251 closeExtraKeys: function() {
1252 document
.getElementById('noVNC_modifiers')
1253 .classList
.remove("noVNC_open");
1254 document
.getElementById('noVNC_toggle_extra_keys_button')
1255 .classList
.remove("noVNC_selected");
1258 toggleExtraKeys: function() {
1260 if(document
.getElementById('noVNC_modifiers')
1261 .classList
.contains("noVNC_open")) {
1262 UI
.closeExtraKeys();
1268 sendEsc: function() {
1270 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1273 sendTab: function() {
1275 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1278 toggleCtrl: function() {
1280 var btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1281 if (btn
.classList
.contains("noVNC_selected")) {
1282 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1283 btn
.classList
.remove("noVNC_selected");
1285 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1286 btn
.classList
.add("noVNC_selected");
1290 toggleAlt: function() {
1292 var btn
= document
.getElementById('noVNC_toggle_alt_button');
1293 if (btn
.classList
.contains("noVNC_selected")) {
1294 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1295 btn
.classList
.remove("noVNC_selected");
1297 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1298 btn
.classList
.add("noVNC_selected");
1302 sendCtrlAltDel: function() {
1303 UI
.rfb
.sendCtrlAltDel();
1312 hideMouseButton: function() {
1313 UI
.setMouseButton(-1);
1316 setMouseButton: function(num
) {
1318 UI
.rfb
.get_mouse().set_touchButton(num
);
1321 var blist
= [0, 1,2,4];
1322 for (var b
= 0; b
< blist
.length
; b
++) {
1323 var button
= document
.getElementById('noVNC_mouse_button' + blist
[b
]);
1324 if (blist
[b
] === num
) {
1325 button
.classList
.remove("noVNC_hidden");
1327 button
.classList
.add("noVNC_hidden");
1332 displayBlur: function() {
1333 if (!UI
.rfb
) return;
1335 UI
.rfb
.get_keyboard().set_focused(false);
1336 UI
.rfb
.get_mouse().set_focused(false);
1339 displayFocus: function() {
1340 if (!UI
.rfb
) return;
1342 UI
.rfb
.get_keyboard().set_focused(true);
1343 UI
.rfb
.get_mouse().set_focused(true);
1346 // Display the desktop name in the document title
1347 updateDocumentTitle: function(rfb
, name
) {
1348 document
.title
= name
+ " - noVNC";
1351 //Helper to add options to dropdown.
1352 addOption: function(selectbox
, text
, value
) {
1353 var optn
= document
.createElement("OPTION");
1356 selectbox
.options
.add(optn
);
1359 setBarPosition: function() {
1360 document
.getElementById('noVNC_control_bar').style
.top
= (window
.pageYOffset
) + 'px';
1361 document
.getElementById('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
1363 var vncwidth
= document
.getElementById('noVNC_container').style
.offsetWidth
;
1364 document
.getElementById('noVNC_control_bar').style
.width
= vncwidth
+ 'px';
1373 /* [module] UI.load(); */
1376 /* [module] export default UI; */