ui.js 13 KB

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