]>
git.proxmox.com Git - mirror_novnc.git/blob - include/ui.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2013 Samuel Mannehed for Cendio AB
5 * Licensed under MPL 2.0 (see LICENSE.txt)
7 * See README.md for usage and integration instructions.
11 /*jslint white: false, browser: true */
12 /*global window, $D, Util, WebUtil, RFB, Display */
14 // Load supporting scripts
15 window
.onscriptsload = function () { UI
.load(); };
16 Util
.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
17 "input.js", "display.js", "jsunzip.js", "rfb.js"]);
23 connSettingsOpen
: false,
24 popupStatusOpen
: false,
26 keyboardVisible
: false,
28 // Setup rfb object, load settings from browser storage, then call
29 // UI.init to setup the UI/menus
30 load: function (callback
) {
31 WebUtil
.initSettings(UI
.start
, callback
);
34 // Render default UI and initialize settings menu
35 start: function(callback
) {
36 var html
= '', i
, sheet
, sheets
, llevels
, port
;
38 // Stylesheet selection dropdown
39 sheet
= WebUtil
.selectStylesheet();
40 sheets
= WebUtil
.getStylesheets();
41 for (i
= 0; i
< sheets
.length
; i
+= 1) {
42 UI
.addOption($D('noVNC_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
45 // Logging selection dropdown
46 llevels
= ['error', 'warn', 'info', 'debug'];
47 for (i
= 0; i
< llevels
.length
; i
+= 1) {
48 UI
.addOption($D('noVNC_logging'),llevels
[i
], llevels
[i
]);
51 // Settings with immediate effects
52 UI
.initSetting('logging', 'warn');
53 WebUtil
.init_logging(UI
.getSetting('logging'));
55 UI
.initSetting('stylesheet', 'default');
56 WebUtil
.selectStylesheet(null);
57 // call twice to get around webkit bug
58 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
60 // if port == 80 (or 443) then it won't be present and should be
62 port
= window
.location
.port
;
64 if (window
.location
.protocol
.substring(0,5) == 'https') {
67 else if (window
.location
.protocol
.substring(0,4) == 'http') {
72 /* Populate the controls if defaults are provided in the URL */
73 UI
.initSetting('host', window
.location
.hostname
);
74 UI
.initSetting('port', port
);
75 UI
.initSetting('password', '');
76 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
77 UI
.initSetting('true_color', true);
78 UI
.initSetting('cursor', false);
79 UI
.initSetting('shared', true);
80 UI
.initSetting('view_only', false);
81 UI
.initSetting('connectTimeout', 2);
82 UI
.initSetting('path', 'websockify');
83 UI
.initSetting('repeaterID', '');
85 UI
.rfb
= RFB({'target': $D('noVNC_canvas'),
86 'onUpdateState': UI
.updateState
,
87 'onClipboard': UI
.clipReceive
});
88 UI
.updateVisualState();
90 // Unfocus clipboard when over the VNC area
91 //$D('VNC_screen').onmousemove = function () {
92 // var keyboard = UI.rfb.get_keyboard();
93 // if ((! keyboard) || (! keyboard.get_focused())) {
94 // $D('VNC_clipboard_text').blur();
98 // Show mouse selector buttons on touch screen devices
99 if ('ontouchstart' in document
.documentElement
) {
100 // Show mobile buttons
101 $D('noVNC_mobile_buttons').style
.display
= "inline";
103 // Remove the address bar
104 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
105 UI
.forceSetting('clip', true);
106 $D('noVNC_clip').disabled
= true;
108 UI
.initSetting('clip', false);
111 //iOS Safari does not support CSS position:fixed.
112 //This detects iOS devices and enables javascript workaround.
113 if ((navigator
.userAgent
.match(/iPhone/i)) ||
114 (navigator
.userAgent
.match(/iPod/i)) ||
115 (navigator
.userAgent
.match(/iPad/i))) {
121 $D('noVNC_host').focus();
124 Util
.addEvent(window
, 'resize', UI
.setViewClip
);
126 Util
.addEvent(window
, 'beforeunload', function () {
127 if (UI
.rfb_state
=== 'normal') {
128 return "You are currently connected.";
132 // Show description by default when hosted at for kanaka.github.com
133 if (location
.host
=== "kanaka.github.com") {
134 // Open the description dialog
135 $D('noVNC_description').style
.display
= "block";
137 // Open the connect panel on first load
138 UI
.toggleConnectPanel();
141 // Add mouse event click/focus/blur event handlers to the UI
142 UI
.addMouseHandlers();
144 if (typeof callback
=== "function") {
149 addMouseHandlers: function() {
150 // Setup interface handlers that can't be inline
151 $D("noVNC_view_drag_button").onclick
= UI
.setViewDrag
;
152 $D("noVNC_mouse_button0").onclick = function () { UI
.setMouseButton(1); };
153 $D("noVNC_mouse_button1").onclick = function () { UI
.setMouseButton(2); };
154 $D("noVNC_mouse_button2").onclick = function () { UI
.setMouseButton(4); };
155 $D("noVNC_mouse_button4").onclick = function () { UI
.setMouseButton(0); };
156 $D("showKeyboard").onclick
= UI
.showKeyboard
;
157 //$D("keyboardinput").onkeydown = function (event) { onKeyDown(event); };
158 $D("keyboardinput").onblur
= UI
.keyInputBlur
;
160 $D("sendCtrlAltDelButton").onclick
= UI
.sendCtrlAltDel
;
161 $D("noVNC_status").onclick
= UI
.togglePopupStatusPanel
;
162 $D("noVNC_popup_status_panel").onclick
= UI
.togglePopupStatusPanel
;
163 $D("clipboardButton").onclick
= UI
.toggleClipboardPanel
;
164 $D("settingsButton").onclick
= UI
.toggleSettingsPanel
;
165 $D("connectButton").onclick
= UI
.toggleConnectPanel
;
166 $D("disconnectButton").onclick
= UI
.disconnect
;
167 $D("descriptionButton").onclick
= UI
.toggleConnectPanel
;
169 $D("noVNC_clipboard_text").onfocus
= UI
.displayBlur
;
170 $D("noVNC_clipboard_text").onblur
= UI
.displayFocus
;
171 $D("noVNC_clipboard_text").onchange
= UI
.clipSend
;
172 $D("noVNC_clipboard_clear_button").onclick
= UI
.clipClear
;
174 $D("noVNC_settings_menu").onmouseover
= UI
.displayBlur
;
175 $D("noVNC_settings_menu").onmouseover
= UI
.displayFocus
;
176 $D("noVNC_apply").onclick
= UI
.settingsApply
;
178 $D("noVNC_connect_button").onclick
= UI
.connect
;
181 // Read form control compatible setting from cookie
182 getSetting: function(name
) {
183 var val
, ctrl
= $D('noVNC_' + name
);
184 val
= WebUtil
.readSetting(name
);
185 if (val
!== null && ctrl
.type
=== 'checkbox') {
186 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
195 // Update cookie and form control setting. If value is not set, then
196 // updates from control to current cookie setting.
197 updateSetting: function(name
, value
) {
199 var i
, ctrl
= $D('noVNC_' + name
);
200 // Save the cookie for this session
201 if (typeof value
!== 'undefined') {
202 WebUtil
.writeSetting(name
, value
);
205 // Update the settings control
206 value
= UI
.getSetting(name
);
208 if (ctrl
.type
=== 'checkbox') {
209 ctrl
.checked
= value
;
211 } else if (typeof ctrl
.options
!== 'undefined') {
212 for (i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
213 if (ctrl
.options
[i
].value
=== value
) {
214 ctrl
.selectedIndex
= i
;
219 /*Weird IE9 error leads to 'null' appearring
220 in textboxes instead of ''.*/
221 if (value
=== null) {
228 // Save control setting to cookie
229 saveSetting: function(name
) {
230 var val
, ctrl
= $D('noVNC_' + name
);
231 if (ctrl
.type
=== 'checkbox') {
233 } else if (typeof ctrl
.options
!== 'undefined') {
234 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
238 WebUtil
.writeSetting(name
, val
);
239 //Util.Debug("Setting saved '" + name + "=" + val + "'");
243 // Initial page load read/initialization of settings
244 initSetting: function(name
, defVal
) {
247 // Check Query string followed by cookie
248 val
= WebUtil
.getQueryVar(name
);
250 val
= WebUtil
.readSetting(name
, defVal
);
252 UI
.updateSetting(name
, val
);
253 //Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
257 // Force a setting to be a certain value
258 forceSetting: function(name
, val
) {
259 UI
.updateSetting(name
, val
);
264 // Show the popup status panel
265 togglePopupStatusPanel: function() {
266 var psp
= $D('noVNC_popup_status_panel');
267 if (UI
.popupStatusOpen
=== true) {
268 psp
.style
.display
= "none";
269 UI
.popupStatusOpen
= false;
271 psp
.innerHTML
= $D('noVNC_status').innerHTML
;
272 psp
.style
.display
= "block";
273 psp
.style
.left
= window
.innerWidth
/2 -
274 parseInt(window
.getComputedStyle(psp
, false).width
)/2 -30 + "px";
275 UI
.popupStatusOpen
= true;
279 // Show the clipboard panel
280 toggleClipboardPanel: function() {
281 // Close the description panel
282 $D('noVNC_description').style
.display
= "none";
283 // Close settings if open
284 if (UI
.settingsOpen
=== true) {
286 UI
.closeSettingsMenu();
288 // Close connection settings if open
289 if (UI
.connSettingsOpen
=== true) {
290 UI
.toggleConnectPanel();
292 // Close popup status panel if open
293 if (UI
.popupStatusOpen
=== true) {
294 UI
.togglePopupStatusPanel();
296 // Toggle Clipboard Panel
297 if (UI
.clipboardOpen
=== true) {
298 $D('noVNC_clipboard').style
.display
= "none";
299 $D('clipboardButton').className
= "noVNC_status_button";
300 UI
.clipboardOpen
= false;
302 $D('noVNC_clipboard').style
.display
= "block";
303 $D('clipboardButton').className
= "noVNC_status_button_selected";
304 UI
.clipboardOpen
= true;
308 // Show the connection settings panel/menu
309 toggleConnectPanel: function() {
310 // Close the description panel
311 $D('noVNC_description').style
.display
= "none";
312 // Close connection settings if open
313 if (UI
.settingsOpen
=== true) {
315 UI
.closeSettingsMenu();
316 $D('connectButton').className
= "noVNC_status_button";
318 // Close clipboard panel if open
319 if (UI
.clipboardOpen
=== true) {
320 UI
.toggleClipboardPanel();
322 // Close popup status panel if open
323 if (UI
.popupStatusOpen
=== true) {
324 UI
.togglePopupStatusPanel();
327 // Toggle Connection Panel
328 if (UI
.connSettingsOpen
=== true) {
329 $D('noVNC_controls').style
.display
= "none";
330 $D('connectButton').className
= "noVNC_status_button";
331 UI
.connSettingsOpen
= false;
332 UI
.saveSetting('host');
333 UI
.saveSetting('port');
334 //UI.saveSetting('password');
336 $D('noVNC_controls').style
.display
= "block";
337 $D('connectButton').className
= "noVNC_status_button_selected";
338 UI
.connSettingsOpen
= true;
339 $D('noVNC_host').focus();
343 // Toggle the settings menu:
344 // On open, settings are refreshed from saved cookies.
345 // On close, settings are applied
346 toggleSettingsPanel: function() {
347 // Close the description panel
348 $D('noVNC_description').style
.display
= "none";
349 if (UI
.settingsOpen
) {
351 UI
.closeSettingsMenu();
353 UI
.updateSetting('encrypt');
354 UI
.updateSetting('true_color');
355 if (UI
.rfb
.get_display().get_cursor_uri()) {
356 UI
.updateSetting('cursor');
358 UI
.updateSetting('cursor', false);
359 $D('noVNC_cursor').disabled
= true;
361 UI
.updateSetting('clip');
362 UI
.updateSetting('shared');
363 UI
.updateSetting('view_only');
364 UI
.updateSetting('connectTimeout');
365 UI
.updateSetting('path');
366 UI
.updateSetting('repeaterID');
367 UI
.updateSetting('stylesheet');
368 UI
.updateSetting('logging');
370 UI
.openSettingsMenu();
375 openSettingsMenu: function() {
376 // Close the description panel
377 $D('noVNC_description').style
.display
= "none";
378 // Close clipboard panel if open
379 if (UI
.clipboardOpen
=== true) {
380 UI
.toggleClipboardPanel();
382 // Close connection settings if open
383 if (UI
.connSettingsOpen
=== true) {
384 UI
.toggleConnectPanel();
386 // Close popup status panel if open
387 if (UI
.popupStatusOpen
=== true) {
388 UI
.togglePopupStatusPanel();
390 $D('noVNC_settings').style
.display
= "block";
391 $D('settingsButton').className
= "noVNC_status_button_selected";
392 UI
.settingsOpen
= true;
395 // Close menu (without applying settings)
396 closeSettingsMenu: function() {
397 $D('noVNC_settings').style
.display
= "none";
398 $D('settingsButton').className
= "noVNC_status_button";
399 UI
.settingsOpen
= false;
402 // Save/apply settings when 'Apply' button is pressed
403 settingsApply: function() {
404 //Util.Debug(">> settingsApply");
405 UI
.saveSetting('encrypt');
406 UI
.saveSetting('true_color');
407 if (UI
.rfb
.get_display().get_cursor_uri()) {
408 UI
.saveSetting('cursor');
410 UI
.saveSetting('clip');
411 UI
.saveSetting('shared');
412 UI
.saveSetting('view_only');
413 UI
.saveSetting('connectTimeout');
414 UI
.saveSetting('path');
415 UI
.saveSetting('repeaterID');
416 UI
.saveSetting('stylesheet');
417 UI
.saveSetting('logging');
419 // Settings with immediate (non-connected related) effect
420 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
421 WebUtil
.init_logging(UI
.getSetting('logging'));
423 UI
.setViewDrag(UI
.rfb
.get_viewportDrag());
424 //Util.Debug("<< settingsApply");
429 setPassword: function() {
430 UI
.rfb
.sendPassword($D('noVNC_password').value
);
431 //Reset connect button.
432 $D('noVNC_connect_button').value
= "Connect";
433 $D('noVNC_connect_button').onclick
= UI
.Connect
;
434 //Hide connection panel.
435 UI
.toggleConnectPanel();
439 sendCtrlAltDel: function() {
440 UI
.rfb
.sendCtrlAltDel();
443 setMouseButton: function(num
) {
444 var b
, blist
= [0, 1,2,4], button
;
446 if (typeof num
=== 'undefined') {
447 // Disable mouse buttons
451 UI
.rfb
.get_mouse().set_touchButton(num
);
454 for (b
= 0; b
< blist
.length
; b
++) {
455 button
= $D('noVNC_mouse_button' + blist
[b
]);
456 if (blist
[b
] === num
) {
457 button
.style
.display
= "";
459 button
.style
.display
= "none";
461 button.style.backgroundColor = "black";
462 button.style.color = "lightgray";
463 button.style.backgroundColor = "";
464 button.style.color = "";
470 updateState: function(rfb
, state
, oldstate
, msg
) {
471 var s
, sb
, c
, d
, cad
, vd
, klass
;
472 UI
.rfb_state
= state
;
476 klass
= "noVNC_status_error";
479 klass
= "noVNC_status_normal";
482 $D('noVNC_logo').style
.display
= "block";
485 klass
= "noVNC_status_normal";
488 UI
.toggleConnectPanel();
490 $D('noVNC_connect_button').value
= "Send Password";
491 $D('noVNC_connect_button').onclick
= UI
.setPassword
;
492 $D('noVNC_password').focus();
494 klass
= "noVNC_status_warn";
497 klass
= "noVNC_status_warn";
501 if (typeof(msg
) !== 'undefined') {
502 $D('noVNC-control-bar').setAttribute("class", klass
);
503 $D('noVNC_status').innerHTML
= msg
;
506 UI
.updateVisualState();
509 // Disable/enable controls depending on connection state
510 updateVisualState: function() {
511 var connected
= UI
.rfb_state
=== 'normal' ? true : false;
513 //Util.Debug(">> updateVisualState");
514 $D('noVNC_encrypt').disabled
= connected
;
515 $D('noVNC_true_color').disabled
= connected
;
516 if (UI
.rfb
&& UI
.rfb
.get_display() &&
517 UI
.rfb
.get_display().get_cursor_uri()) {
518 $D('noVNC_cursor').disabled
= connected
;
520 UI
.updateSetting('cursor', false);
521 $D('noVNC_cursor').disabled
= true;
523 $D('noVNC_shared').disabled
= connected
;
524 $D('noVNC_view_only').disabled
= connected
;
525 $D('noVNC_connectTimeout').disabled
= connected
;
526 $D('noVNC_path').disabled
= connected
;
527 $D('noVNC_repeaterID').disabled
= connected
;
531 UI
.setMouseButton(1);
532 $D('clipboardButton').style
.display
= "inline";
533 $D('showKeyboard').style
.display
= "inline";
534 $D('sendCtrlAltDelButton').style
.display
= "inline";
537 $D('clipboardButton').style
.display
= "none";
538 $D('showKeyboard').style
.display
= "none";
539 $D('sendCtrlAltDelButton').style
.display
= "none";
541 // State change disables viewport dragging.
542 // It is enabled (toggled) by direct click on the button
543 UI
.setViewDrag(false);
545 switch (UI
.rfb_state
) {
550 $D('connectButton').style
.display
= "";
551 $D('disconnectButton').style
.display
= "none";
554 $D('connectButton').style
.display
= "none";
555 $D('disconnectButton').style
.display
= "";
559 //Util.Debug("<< updateVisualState");
563 clipReceive: function(rfb
, text
) {
564 Util
.Debug(">> UI.clipReceive: " + text
.substr(0,40) + "...");
565 $D('noVNC_clipboard_text').value
= text
;
566 Util
.Debug("<< UI.clipReceive");
570 connect: function() {
571 var host
, port
, password
, path
;
573 UI
.closeSettingsMenu();
574 UI
.toggleConnectPanel();
576 host
= $D('noVNC_host').value
;
577 port
= $D('noVNC_port').value
;
578 password
= $D('noVNC_password').value
;
579 path
= $D('noVNC_path').value
;
580 if ((!host
) || (!port
)) {
581 throw("Must set host and port");
584 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
585 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
586 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
587 UI
.rfb
.set_shared(UI
.getSetting('shared'));
588 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
589 UI
.rfb
.set_connectTimeout(UI
.getSetting('connectTimeout'));
590 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
592 UI
.rfb
.connect(host
, port
, password
, path
);
595 setTimeout(UI
.setBarPosition
, 100);
596 $D('noVNC_logo').style
.display
= "none";
599 disconnect: function() {
600 UI
.closeSettingsMenu();
603 $D('noVNC_logo').style
.display
= "block";
604 UI
.connSettingsOpen
= false;
605 UI
.toggleConnectPanel();
608 displayBlur: function() {
609 UI
.rfb
.get_keyboard().set_focused(false);
610 UI
.rfb
.get_mouse().set_focused(false);
613 displayFocus: function() {
614 UI
.rfb
.get_keyboard().set_focused(true);
615 UI
.rfb
.get_mouse().set_focused(true);
618 clipClear: function() {
619 $D('noVNC_clipboard_text').value
= "";
620 UI
.rfb
.clipboardPasteFrom("");
623 clipSend: function() {
624 var text
= $D('noVNC_clipboard_text').value
;
625 Util
.Debug(">> UI.clipSend: " + text
.substr(0,40) + "...");
626 UI
.rfb
.clipboardPasteFrom(text
);
627 Util
.Debug("<< UI.clipSend");
631 // Enable/disable and configure viewport clipping
632 setViewClip: function(clip
) {
633 var display
, cur_clip
, pos
, new_w
, new_h
;
636 display
= UI
.rfb
.get_display();
641 cur_clip
= display
.get_viewport();
643 if (typeof(clip
) !== 'boolean') {
644 // Use current setting
645 clip
= UI
.getSetting('clip');
648 if (clip
&& !cur_clip
) {
650 UI
.updateSetting('clip', true);
651 } else if (!clip
&& cur_clip
) {
653 UI
.updateSetting('clip', false);
654 display
.set_viewport(false);
655 $D('noVNC_canvas').style
.position
= 'static';
656 display
.viewportChange();
658 if (UI
.getSetting('clip')) {
659 // If clipping, update clipping settings
660 $D('noVNC_canvas').style
.position
= 'absolute';
661 pos
= Util
.getPosition($D('noVNC_canvas'));
662 new_w
= window
.innerWidth
- pos
.x
;
663 new_h
= window
.innerHeight
- pos
.y
;
664 display
.set_viewport(true);
665 display
.viewportChange(0, 0, new_w
, new_h
);
669 // Toggle/set/unset the viewport drag/move button
670 setViewDrag: function(drag
) {
671 var vmb
= $D('noVNC_view_drag_button');
672 if (!UI
.rfb
) { return; }
674 if (UI
.rfb_state
=== 'normal' &&
675 UI
.rfb
.get_display().get_viewport()) {
676 vmb
.style
.display
= "inline";
678 vmb
.style
.display
= "none";
681 if (typeof(drag
) === "undefined" ||
682 typeof(drag
) === "object") {
683 // If not specified, then toggle
684 drag
= !UI
.rfb
.get_viewportDrag();
687 vmb
.className
= "noVNC_status_button_selected";
688 UI
.rfb
.set_viewportDrag(true);
690 vmb
.className
= "noVNC_status_button";
691 UI
.rfb
.set_viewportDrag(false);
695 // On touch devices, show the OS keyboard
696 showKeyboard: function() {
697 if(UI
.keyboardVisible
=== false) {
698 $D('keyboardinput').focus();
699 UI
.keyboardVisible
= true;
700 $D('showKeyboard').className
= "noVNC_status_button_selected";
701 } else if(UI
.keyboardVisible
=== true) {
702 $D('keyboardinput').blur();
703 $D('showKeyboard').className
= "noVNC_status_button";
704 UI
.keyboardVisible
= false;
708 keyInputBlur: function() {
709 $D('showKeyboard').className
= "noVNC_status_button";
710 //Weird bug in iOS if you change keyboardVisible
711 //here it does not actually occur so next time
712 //you click keyboard icon it doesnt work.
713 setTimeout(function() { UI
.setKeyboard(); },100);
716 setKeyboard: function() {
717 UI
.keyboardVisible
= false;
720 // iOS < Version 5 does not support position fixed. Javascript workaround:
721 setOnscroll: function() {
722 window
.onscroll = function() {
727 setResize: function () {
728 window
.onResize = function() {
733 //Helper to add options to dropdown.
734 addOption: function(selectbox
,text
,value
)
736 var optn
= document
.createElement("OPTION");
739 selectbox
.options
.add(optn
);
742 setBarPosition: function() {
743 $D('noVNC-control-bar').style
.top
= (window
.pageYOffset
) + 'px';
744 $D('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
746 var vncwidth
= $D('noVNC_screen').style
.offsetWidth
;
747 $D('noVNC-control-bar').style
.width
= vncwidth
+ 'px';