ui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*jslint white: false */
  10. /*global $, Util, RFB, Canvas, VNC_uri_prefix, Element, Fx */
  11. var UI = {
  12. settingsOpen : false,
  13. // Render default UI and initialize settings menu
  14. load: function(target) {
  15. var html = '', i, sheet, sheets, llevels;
  16. /* Populate the 'target' DOM element with default UI */
  17. if (!target) { target = 'vnc'; }
  18. if ((!document.createElement('canvas').getContext) &&
  19. window.ActiveXObject) {
  20. // Suggest Chrome frame for Internet Explorer users
  21. html += '<center><div style="text-align: left; width: 400px">';
  22. html += ' You are using a version of Internet Explorer ';
  23. html += ' that does not have HTML5 Canvas support. ';
  24. html += ' To use noVNC you must use a browser with HTML5 ';
  25. html += ' Canvas support or install ';
  26. html += ' <a href="http://google.com/chromeframe" target="cframe">';
  27. html += ' Google Chrome Frame.</a>';
  28. html += '</div></center>';
  29. $(target).innerHTML = html;
  30. return;
  31. }
  32. html += '<div id="VNC_controls">';
  33. html += ' <ul>';
  34. html += ' <li>Host: <input id="VNC_host"></li>';
  35. html += ' <li>Port: <input id="VNC_port"></li>';
  36. html += ' <li>Password: <input id="VNC_password"';
  37. html += ' type="password"></li>';
  38. html += ' <li><input id="VNC_connect_button" type="button"';
  39. html += ' value="Loading" disabled></li>';
  40. html += ' </ul>';
  41. html += '</div>';
  42. html += '<div id="VNC_screen">';
  43. html += ' <div id="VNC_status_bar" class="VNC_status_bar" style="margin-top: 0px;">';
  44. html += ' <table border=0 width=100%><tr>';
  45. html += ' <td><div id="VNC_status">Loading</div></td>';
  46. html += ' <td width=1%><div class="VNC_buttons_right">';
  47. html += ' <input type=button class="VNC_status_button" value="Settings"';
  48. html += ' id="menuButton"';
  49. html += ' onclick="UI.clickSettingsMenu();">';
  50. html += ' <span id="VNC_settings_menu"';
  51. html += ' onmouseover="UI.canvasBlur();"';
  52. html += ' onmouseout="UI.canvasFocus();">';
  53. html += ' <ul>';
  54. html += ' <li><input id="VNC_encrypt"';
  55. html += ' type="checkbox"> Encrypt</li>';
  56. html += ' <li><input id="VNC_true_color"';
  57. html += ' type="checkbox" checked> True Color</li>';
  58. html += ' <li><input id="VNC_cursor"';
  59. html += ' type="checkbox"> Local Cursor</li>';
  60. html += ' <li><input id="VNC_shared"';
  61. html += ' type="checkbox"> Shared Mode</li>';
  62. html += ' <li><input id="VNC_connectTimeout"';
  63. html += ' type="input"> Connect Timeout (s)</li>';
  64. html += ' <hr>';
  65. // Stylesheet selection dropdown
  66. html += ' <li><select id="VNC_stylesheet" name="vncStyle">';
  67. html += ' <option value="default">default</option>';
  68. sheet = WebUtil.selectStylesheet();
  69. sheets = WebUtil.getStylesheets();
  70. for (i = 0; i < sheets.length; i += 1) {
  71. html += '<option value="' + sheets[i].title + '">' + sheets[i].title + '</option>';
  72. }
  73. html += ' </select> Style</li>';
  74. // Logging selection dropdown
  75. html += ' <li><select id="VNC_logging" name="vncLogging">';
  76. llevels = ['error', 'warn', 'info', 'debug'];
  77. for (i = 0; i < llevels.length; i += 1) {
  78. html += '<option value="' + llevels[i] + '">' + llevels[i] + '</option>';
  79. }
  80. html += ' </select> Logging</li>';
  81. html += ' <hr>';
  82. html += ' <li><input type="button" id="VNC_apply" value="Apply"';
  83. html += ' onclick="UI.settingsApply()"></li>';
  84. html += ' </ul>';
  85. html += ' </span></div></td>';
  86. html += ' <td width=1%><div class="VNC_buttons_right">';
  87. html += ' <input type=button class="VNC_status_button" value="Send CtrlAltDel"';
  88. html += ' id="sendCtrlAltDelButton"';
  89. html += ' onclick="UI.sendCtrlAltDel();"></div></td>';
  90. html += ' </tr></table>';
  91. html += ' </div>';
  92. html += ' <canvas id="VNC_canvas" width="640px" height="20px">';
  93. html += ' Canvas not supported.';
  94. html += ' </canvas>';
  95. html += '</div>';
  96. html += '<br><br>';
  97. html += '<div id="VNC_clipboard">';
  98. html += ' VNC Clipboard:';
  99. html += ' <input id="VNC_clipboard_clear_button"';
  100. html += ' type="button" value="Clear"';
  101. html += ' onclick="UI.clipClear();">';
  102. html += ' <br>';
  103. html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5';
  104. html += ' onfocus="UI.canvasBlur();"';
  105. html += ' onblur="UI.canvasFocus();"';
  106. html += ' onchange="UI.clipSend();"></textarea>';
  107. html += '</div>';
  108. $(target).innerHTML = html;
  109. // Settings with immediate effects
  110. UI.initSetting('logging', 'warn');
  111. WebUtil.init_logging(UI.getSetting('logging'));
  112. UI.initSetting('stylesheet', 'default');
  113. WebUtil.selectStylesheet(null); // call twice to get around webkit bug
  114. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  115. /* Populate the controls if defaults are provided in the URL */
  116. UI.initSetting('host', '');
  117. UI.initSetting('port', '');
  118. UI.initSetting('password', '');
  119. UI.initSetting('encrypt', false);
  120. UI.initSetting('true_color', true);
  121. UI.initSetting('cursor', false);
  122. UI.initSetting('shared', true);
  123. UI.initSetting('connectTimeout', 2);
  124. UI.rfb = RFB({'target': 'VNC_canvas',
  125. 'updateState': UI.updateState,
  126. 'clipboardReceive': UI.clipReceive});
  127. // Unfocus clipboard when over the VNC area
  128. $('VNC_screen').onmousemove = function () {
  129. var canvas = UI.rfb.get_canvas();
  130. if ((! canvas) || (! canvas.get_focused())) {
  131. $('VNC_clipboard_text').blur();
  132. }
  133. };
  134. },
  135. // Read form control compatible setting from cookie
  136. getSetting: function(name) {
  137. var val, ctrl = $('VNC_' + name);
  138. val = WebUtil.readCookie(name);
  139. if (ctrl.type === 'checkbox') {
  140. if (val.toLowerCase() in {'0':1, 'no':1, 'false':1}) {
  141. val = false;
  142. } else {
  143. val = true;
  144. }
  145. }
  146. return val;
  147. },
  148. // Update cookie and form control setting. If value is not set, then
  149. // updates from control to current cookie setting.
  150. updateSetting: function(name, value) {
  151. var i, ctrl = $('VNC_' + name);
  152. // Save the cookie for this session
  153. if (typeof value !== 'undefined') {
  154. WebUtil.createCookie(name, value);
  155. }
  156. // Update the settings control
  157. value = UI.getSetting(name);
  158. if (ctrl.type === 'checkbox') {
  159. ctrl.checked = value;
  160. } else if (typeof ctrl.options !== 'undefined') {
  161. for (i = 0; i < ctrl.options.length; i += 1) {
  162. if (ctrl.options[i].value === value) {
  163. ctrl.selectedIndex = i;
  164. break;
  165. }
  166. }
  167. } else {
  168. ctrl.value = value;
  169. }
  170. },
  171. // Save control setting to cookie
  172. saveSetting: function(name) {
  173. var val, ctrl = $('VNC_' + name);
  174. if (ctrl.type === 'checkbox') {
  175. val = ctrl.checked;
  176. } else if (typeof ctrl.options !== 'undefined') {
  177. val = ctrl.options[ctrl.selectedIndex].value;
  178. } else {
  179. val = ctrl.value;
  180. }
  181. WebUtil.createCookie(name, val);
  182. //Util.Debug("Setting saved '" + name + "=" + val + "'");
  183. return val;
  184. },
  185. // Initial page load read/initialization of settings
  186. initSetting: function(name, defVal) {
  187. var val;
  188. // Check Query string followed by cookie
  189. val = WebUtil.getQueryVar(name);
  190. if (val === null) {
  191. val = WebUtil.readCookie(name, defVal);
  192. }
  193. UI.updateSetting(name, val);
  194. //Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
  195. return val;
  196. },
  197. // Toggle the settings menu:
  198. // On open, settings are refreshed from saved cookies.
  199. // On close, settings are applied
  200. clickSettingsMenu: function() {
  201. if (UI.settingsOpen) {
  202. UI.settingsApply();
  203. UI.closeSettingsMenu();
  204. } else {
  205. UI.updateSetting('encrypt');
  206. UI.updateSetting('true_color');
  207. if (UI.rfb.get_canvas().get_cursor_uri()) {
  208. UI.updateSetting('cursor');
  209. } else {
  210. UI.updateSetting('cursor', false);
  211. $('VNC_cursor').disabled = true;
  212. }
  213. UI.updateSetting('shared');
  214. UI.updateSetting('connectTimeout');
  215. UI.updateSetting('stylesheet');
  216. UI.updateSetting('logging');
  217. UI.openSettingsMenu();
  218. }
  219. },
  220. // Open menu
  221. openSettingsMenu: function() {
  222. $('VNC_settings_menu').style.display = "block";
  223. UI.settingsOpen = true;
  224. },
  225. // Close menu (without applying settings)
  226. closeSettingsMenu: function() {
  227. $('VNC_settings_menu').style.display = "none";
  228. UI.settingsOpen = false;
  229. },
  230. // Disable/enable controls depending on connection state
  231. settingsDisabled: function(disabled, rfb) {
  232. //Util.Debug(">> settingsDisabled");
  233. $('VNC_encrypt').disabled = disabled;
  234. $('VNC_true_color').disabled = disabled;
  235. if (rfb && rfb.get_canvas() && rfb.get_canvas().get_cursor_uri()) {
  236. $('VNC_cursor').disabled = disabled;
  237. } else {
  238. UI.updateSetting('cursor', false);
  239. $('VNC_cursor').disabled = true;
  240. }
  241. $('VNC_shared').disabled = disabled;
  242. $('VNC_connectTimeout').disabled = disabled;
  243. //Util.Debug("<< settingsDisabled");
  244. },
  245. // Save/apply settings when 'Apply' button is pressed
  246. settingsApply: function() {
  247. //Util.Debug(">> settingsApply");
  248. UI.saveSetting('encrypt');
  249. UI.saveSetting('true_color');
  250. if (UI.rfb.get_canvas().get_cursor_uri()) {
  251. UI.saveSetting('cursor');
  252. }
  253. UI.saveSetting('shared');
  254. UI.saveSetting('connectTimeout');
  255. UI.saveSetting('stylesheet');
  256. UI.saveSetting('logging');
  257. // Settings with immediate (non-connected related) effect
  258. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  259. WebUtil.init_logging(UI.getSetting('logging'));
  260. //Util.Debug("<< settingsApply");
  261. },
  262. setPassword: function() {
  263. UI.rfb.sendPassword($('VNC_password').value);
  264. return false;
  265. },
  266. sendCtrlAltDel: function() {
  267. UI.rfb.sendCtrlAltDel();
  268. },
  269. updateState: function(rfb, state, oldstate, msg) {
  270. var s, sb, c, cad, klass;
  271. s = $('VNC_status');
  272. sb = $('VNC_status_bar');
  273. c = $('VNC_connect_button');
  274. cad = $('sendCtrlAltDelButton');
  275. switch (state) {
  276. case 'failed':
  277. case 'fatal':
  278. c.disabled = true;
  279. cad.disabled = true;
  280. UI.settingsDisabled(true, rfb);
  281. klass = "VNC_status_error";
  282. break;
  283. case 'normal':
  284. c.value = "Disconnect";
  285. c.onclick = UI.disconnect;
  286. c.disabled = false;
  287. cad.disabled = false;
  288. UI.settingsDisabled(true, rfb);
  289. klass = "VNC_status_normal";
  290. break;
  291. case 'disconnected':
  292. case 'loaded':
  293. c.value = "Connect";
  294. c.onclick = UI.connect;
  295. c.disabled = false;
  296. cad.disabled = true;
  297. UI.settingsDisabled(false, rfb);
  298. klass = "VNC_status_normal";
  299. break;
  300. case 'password':
  301. c.value = "Send Password";
  302. c.onclick = UI.setPassword;
  303. c.disabled = false;
  304. cad.disabled = true;
  305. UI.settingsDisabled(true, rfb);
  306. klass = "VNC_status_warn";
  307. break;
  308. default:
  309. c.disabled = true;
  310. cad.disabled = true;
  311. UI.settingsDisabled(true, rfb);
  312. klass = "VNC_status_warn";
  313. break;
  314. }
  315. if (typeof(msg) !== 'undefined') {
  316. s.setAttribute("class", klass);
  317. sb.setAttribute("class", klass);
  318. s.innerHTML = msg;
  319. }
  320. },
  321. clipReceive: function(rfb, text) {
  322. Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
  323. $('VNC_clipboard_text').value = text;
  324. Util.Debug("<< UI.clipReceive");
  325. },
  326. connect: function() {
  327. var host, port, password;
  328. UI.closeSettingsMenu();
  329. host = $('VNC_host').value;
  330. port = $('VNC_port').value;
  331. password = $('VNC_password').value;
  332. if ((!host) || (!port)) {
  333. throw("Must set host and port");
  334. }
  335. UI.rfb.set_encrypt(UI.getSetting('encrypt'));
  336. UI.rfb.set_true_color(UI.getSetting('true_color'));
  337. UI.rfb.set_local_cursor(UI.getSetting('cursor'));
  338. UI.rfb.set_shared(UI.getSetting('shared'));
  339. UI.rfb.set_connectTimeout(UI.getSetting('connectTimeout'));
  340. UI.rfb.connect(host, port, password);
  341. },
  342. disconnect: function() {
  343. UI.closeSettingsMenu();
  344. UI.rfb.disconnect();
  345. },
  346. canvasBlur: function() {
  347. UI.rfb.get_canvas().set_focused(false);
  348. },
  349. canvasFocus: function() {
  350. UI.rfb.get_canvas().set_focused(true);
  351. },
  352. clipClear: function() {
  353. $('VNC_clipboard_text').value = "";
  354. UI.rfb.clipboardPasteFrom("");
  355. },
  356. clipSend: function() {
  357. var text = $('VNC_clipboard_text').value;
  358. Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
  359. UI.rfb.clipboardPasteFrom(text);
  360. Util.Debug("<< UI.clipSend");
  361. }
  362. };