]>
git.proxmox.com Git - mirror_novnc.git/blob - include/ui.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2011 Joel Martin
4 * Licensed under LGPL-3 (see LICENSE.txt)
6 * See README.md for usage and integration instructions.
10 /*jslint white: false, browser: true */
11 /*global window, $D, Util, WebUtil, RFB, Display */
16 ConnSettingsOpen
: true,
19 // Render default UI and initialize settings menu
21 var html
= '', i
, sheet
, sheets
, llevels
;
23 // Stylesheet selection dropdown
24 sheet
= WebUtil
.selectStylesheet();
25 sheets
= WebUtil
.getStylesheets();
26 for (i
= 0; i
< sheets
.length
; i
+= 1) {
27 addOption($D('noVNC_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
30 // Logging selection dropdown
31 llevels
= ['error', 'warn', 'info', 'debug'];
32 for (i
= 0; i
< llevels
.length
; i
+= 1) {
33 addOption($D('noVNC_logging'),llevels
[i
], llevels
[i
]);
36 // Settings with immediate effects
37 UI
.initSetting('logging', 'warn');
38 WebUtil
.init_logging(UI
.getSetting('logging'));
39 UI
.initSetting('stylesheet', 'default');
41 WebUtil
.selectStylesheet(null); // call twice to get around webkit bug
42 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
44 /* Populate the controls if defaults are provided in the URL */
45 UI
.initSetting('host', '');
46 UI
.initSetting('port', '');
47 UI
.initSetting('password', '');
48 UI
.initSetting('encrypt', false);
49 UI
.initSetting('true_color', true);
50 UI
.initSetting('cursor', false);
51 UI
.initSetting('shared', true);
52 UI
.initSetting('connectTimeout', 2);
54 UI
.rfb
= RFB({'target': $D('noVNC_canvas'),
55 'onUpdateState': UI
.updateState
,
56 'onClipboard': UI
.clipReceive
});
58 // Unfocus clipboard when over the VNC area
59 //$D('VNC_screen').onmousemove = function () {
60 // var keyboard = UI.rfb.get_keyboard();
61 // if ((! keyboard) || (! keyboard.get_focused())) {
62 // $D('VNC_clipboard_text').blur();
66 // Show mouse selector buttons on touch screen devices
67 if ('ontouchstart' in document
.documentElement
) {
68 $D('noVNC_mobile_buttons').style
.display
= "inline";
70 window
.scrollTo(0, 1);
73 //iOS Safari does not support CSS position:fixed. This detects iOS devices and enables javascript workaround.
74 if((navigator
.userAgent
.match(/iPhone/i)) || (navigator
.userAgent
.match(/iPod/i)) || (navigator
.userAgent
.match(/iPad/i))) {
82 // Read form control compatible setting from cookie
83 getSetting: function(name
) {
84 var val
, ctrl
= $D('noVNC_' + name
);
85 val
= WebUtil
.readCookie(name
);
86 if (ctrl
.type
=== 'checkbox') {
87 if (val
.toLowerCase() in {'0':1, 'no':1, 'false':1}) {
96 // Update cookie and form control setting. If value is not set, then
97 // updates from control to current cookie setting.
98 updateSetting: function(name
, value
) {
100 var i
, ctrl
= $D('noVNC_' + name
);
101 // Save the cookie for this session
102 if (typeof value
!== 'undefined') {
103 WebUtil
.createCookie(name
, value
);
110 // Update the settings control
111 value
= UI
.getSetting(name
);
113 if (ctrl
.type
=== 'checkbox') {
114 ctrl
.checked
= value
;
116 } else if (typeof ctrl
.options
!== 'undefined') {
117 for (i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
118 if (ctrl
.options
[i
].value
=== value
) {
119 ctrl
.selectedIndex
= i
;
124 /*Weird IE9 error leads to 'null' appearring in textboxes instead of ''.*/
133 // Save control setting to cookie
134 saveSetting: function(name
) {
135 var val
, ctrl
= $D('noVNC_' + name
);
136 if (ctrl
.type
=== 'checkbox') {
138 } else if (typeof ctrl
.options
!== 'undefined') {
139 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
143 WebUtil
.createCookie(name
, val
);
144 //Util.Debug("Setting saved '" + name + "=" + val + "'");
148 // Initial page load read/initialization of settings
149 initSetting: function(name
, defVal
) {
152 // Check Query string followed by cookie
153 val
= WebUtil
.getQueryVar(name
);
155 val
= WebUtil
.readCookie(name
, defVal
);
157 UI
.updateSetting(name
, val
);
158 //Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
163 // Toggle the settings menu:
164 // On open, settings are refreshed from saved cookies.
165 // On close, settings are applied
166 clickSettingsMenu: function() {
167 if (UI
.settingsOpen
) {
170 UI
.closeSettingsMenu();
172 UI
.updateSetting('encrypt');
173 UI
.updateSetting('true_color');
174 if (UI
.rfb
.get_display().get_cursor_uri()) {
175 UI
.updateSetting('cursor');
177 UI
.updateSetting('cursor', false);
178 $D('noVNC_cursor').disabled
= true;
180 UI
.updateSetting('shared');
181 UI
.updateSetting('connectTimeout');
182 UI
.updateSetting('stylesheet');
183 UI
.updateSetting('logging');
185 UI
.openSettingsMenu();
190 openSettingsMenu: function() {
191 if(UI
.clipboardOpen
== true)
195 //Close connection settings if open
196 if(UI
.ConnSettingsOpen
== true) {
197 connectPanelbutton();
199 $D('noVNC_Settings').style
.display
= "block";
200 UI
.settingsOpen
= true;
203 // Close menu (without applying settings)
204 closeSettingsMenu: function() {
205 $D('noVNC_Settings').style
.display
= "none";
206 UI
.settingsOpen
= false;
209 // Disable/enable controls depending on connection state
210 settingsDisabled: function(disabled
, rfb
) {
211 //Util.Debug(">> settingsDisabled");
212 $D('noVNC_encrypt').disabled
= disabled
;
213 $D('noVNC_true_color').disabled
= disabled
;
214 if (rfb
&& rfb
.get_display() && rfb
.get_display().get_cursor_uri()) {
215 $D('noVNC_cursor').disabled
= disabled
;
217 UI
.updateSetting('cursor', false);
218 $D('noVNC_cursor').disabled
= true;
220 $D('noVNC_shared').disabled
= disabled
;
221 $D('noVNC_connectTimeout').disabled
= disabled
;
222 //Util.Debug("<< settingsDisabled");
225 // Save/apply settings when 'Apply' button is pressed
226 settingsApply: function() {
227 //Util.Debug(">> settingsApply");
228 UI
.saveSetting('encrypt');
229 UI
.saveSetting('true_color');
230 if (UI
.rfb
.get_display().get_cursor_uri()) {
231 UI
.saveSetting('cursor');
233 UI
.saveSetting('shared');
234 UI
.saveSetting('connectTimeout');
235 UI
.saveSetting('stylesheet');
236 UI
.saveSetting('logging');
238 // Settings with immediate (non-connected related) effect
239 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
240 WebUtil
.init_logging(UI
.getSetting('logging'));
241 //Util.Debug("<< settingsApply");
246 setPassword: function() {
247 UI
.rfb
.sendPassword($D('noVNC_password').value
);
251 sendCtrlAltDel: function() {
252 UI
.rfb
.sendCtrlAltDel();
255 setMouseButton: function(num
) {
256 var b
, blist
= [1,2,4], button
,
257 mouse
= UI
.rfb
.get_mouse();
259 if (typeof num
=== 'undefined') {
261 num
= mouse
.get_touchButton();
262 } else if (num
=== mouse
.get_touchButton()) {
263 // Set all buttons off (no clicks)
264 mouse
.set_touchButton(0);
267 // Turn on one button
268 mouse
.set_touchButton(num
);
271 for (b
= 0; b
< blist
.length
; b
++) {
272 button
= $D('noVNC_mouse_button' + blist
[b
]);
273 if (blist
[b
] === num
) {
274 button
.style
.backgroundColor
= "black";
275 button
.style
.color
= "lightgray";
277 button
.style
.backgroundColor
= "";
278 button
.style
.color
= "";
283 updateState: function(rfb
, state
, oldstate
, msg
) {
284 var s
, sb
, c
, cad
, klass
;
285 s
= $D('noVNC_status');
286 sb
= $D('noVNC_status_bar');
287 c
= $D('noVNC_connect_button');
288 cad
= $D('sendCtrlAltDelButton');
294 UI
.settingsDisabled(true, rfb
);
295 klass
= "noVNC_status_error";
298 c
.value
= "Disconnect";
299 c
.onclick
= UI
.disconnect
;
301 cad
.disabled
= false;
302 UI
.settingsDisabled(true, rfb
);
303 klass
= "noVNC_status_normal";
306 $D('noVNC_defaultScreen').style
.display
= "block";
309 c
.onclick
= UI
.connect
;
313 UI
.settingsDisabled(false, rfb
);
314 klass
= "noVNC_status_normal";
317 c
.value
= "Send Password";
318 c
.onclick
= UI
.setPassword
;
322 UI
.settingsDisabled(true, rfb
);
323 klass
= "noVNC_status_warn";
328 UI
.settingsDisabled(true, rfb
);
329 klass
= "noVNC_status_warn";
333 if (typeof(msg
) !== 'undefined') {
334 s
.setAttribute("class", klass
);
335 sb
.setAttribute("class", klass
);
341 clipReceive: function(rfb
, text
) {
342 Util
.Debug(">> UI.clipReceive: " + text
.substr(0,40) + "...");
343 $D('noVNC_clipboard_text').value
= text
;
344 Util
.Debug("<< UI.clipReceive");
348 connect: function() {
349 var host
, port
, password
;
351 UI
.closeSettingsMenu();
352 connectPanelbutton();
353 host
= $D('noVNC_host').value
;
354 port
= $D('noVNC_port').value
;
355 password
= $D('noVNC_password').value
;
356 if ((!host
) || (!port
)) {
357 throw("Must set host and port");
360 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
361 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
362 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
363 UI
.rfb
.set_shared(UI
.getSetting('shared'));
364 UI
.rfb
.set_connectTimeout(UI
.getSetting('connectTimeout'));
366 UI
.rfb
.connect(host
, port
, password
);
368 setTimeout("setBarPosition()",100);
369 $D('noVNC_defaultScreen').style
.display
= "none";
372 disconnect: function() {
373 UI
.closeSettingsMenu();
375 $D('noVNC_defaultScreen').style
.display
= "block";
376 UI
.openSettingsMenu();
379 displayBlur: function() {
380 UI
.rfb
.get_keyboard().set_focused(false);
381 UI
.rfb
.get_mouse().set_focused(false);
384 displayFocus: function() {
385 UI
.rfb
.get_keyboard().set_focused(true);
386 UI
.rfb
.get_mouse().set_focused(true);
389 clipClear: function() {
390 $D('noVNC_clipboard_text').value
= "";
391 UI
.rfb
.clipboardPasteFrom("");
394 clipSend: function() {
395 var text
= $D('noVNC_clipboard_text').value
;
396 Util
.Debug(">> UI.clipSend: " + text
.substr(0,40) + "...");
397 UI
.rfb
.clipboardPasteFrom(text
);
398 Util
.Debug("<< UI.clipSend");
401 showClipboard: function() {
402 //Close settings if open
403 if(UI
.settingsOpen
== true) {
404 UI
.closeSettingsMenu();
406 //Close connection settings if open
407 if(UI
.ConnSettingsOpen
== true) {
408 connectPanelbutton();
410 //Toggle Connection Panel
411 if(UI
.clipboardOpen
== true)
412 { $D('noVNC_clipboard').style
.display
= "none";
413 UI
.clipboardOpen
= false;
415 $D('noVNC_clipboard').style
.display
= "block";
416 UI
.clipboardOpen
= true;
422 function connectPanelbutton() {
423 //Close connection settings if open
424 if(UI
.settingsOpen
== true) {
425 UI
.closeSettingsMenu();
427 if(UI
.clipboardOpen
== true)
432 //Toggle Connection Panel
433 if(UI
.ConnSettingsOpen
== true)
435 $D('noVNC_controls').style
.display
= "none";
436 UI
.ConnSettingsOpen
= false;
438 $D('noVNC_controls').style
.display
= "block";
439 UI
.ConnSettingsOpen
= true;
443 function showkeyboard(){
444 //Get Current Scroll Position
445 var scrollx
= (document
.all
)?document
.body
.scrollLeft
:window
.pageXOffset
;
446 var scrolly
= (document
.all
)?document
.body
.scrollTop
:window
.pageYOffset
;
449 //Stop browser zooming on textbox.
451 $D('keyboardinput').focus();
452 scroll(scrollx
,scrolly
);
457 function zoomDisable(){
458 //Change viewport meta data to disable zooming.
459 changeViewportMeta("user-scalable=0");
462 function zoomEnable(){
463 //Change viewport meta data to enable user zooming.
464 changeViewportMeta("user-scalable=1");
467 function changeViewportMeta(newattributes
) {
469 // First, get the array of meta-tag elements
470 var metatags
= document
.getElementsByTagName("meta");
472 // Update only the Viewport meta tag
473 for (var cnt
= 0; cnt
< metatags
.length
; cnt
++)
476 var name
= metatags
[cnt
].getAttribute("name");
477 var content
= metatags
[cnt
].getAttribute("content");
479 // Update the Viewport meta tag
480 if (metatags
[cnt
].getAttribute("name") == "viewport")
481 metatags
[cnt
].setAttribute("content", newattributes
);
485 //iOS < Version 5 does not support position fixed. Javascript workaround:
486 function setOnscroll() {
487 window
.onscroll = function() {
492 function setResize() {
493 window
.onResize = function() {
498 function setBarPosition() {
499 $D('VNC_mobile_bar').style
.top
= (window
.pageYOffset
) + 'px';
500 $D('VNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
502 $D('VNC_mobile_buttons_right').style
.right
= 0 + 'px';
504 var vncwidth
= $('#VNC_screen').width();
505 $D('VNC_mobile_bar').style
.width
= vncwidth
+ 'px';
508 //Helper to add options to dropdown.
509 function addOption(selectbox
,text
,value
)
511 var optn
= document
.createElement("OPTION");
514 selectbox
.options
.add(optn
);