ui.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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, Display */
  11. var UI = {
  12. settingsOpen : false,
  13. connSettingsOpen : true,
  14. clipboardOpen: false,
  15. // Render default UI and initialize settings menu
  16. load: function() {
  17. var html = '', i, sheet, sheets, llevels;
  18. // Stylesheet selection dropdown
  19. sheet = WebUtil.selectStylesheet();
  20. sheets = WebUtil.getStylesheets();
  21. for (i = 0; i < sheets.length; i += 1) {
  22. UI.addOption($D('noVNC_stylesheet'),sheets[i].title, sheets[i].title);
  23. }
  24. // Logging selection dropdown
  25. llevels = ['error', 'warn', 'info', 'debug'];
  26. for (i = 0; i < llevels.length; i += 1) {
  27. UI.addOption($D('noVNC_logging'),llevels[i], llevels[i]);
  28. }
  29. // Settings with immediate effects
  30. UI.initSetting('logging', 'warn');
  31. WebUtil.init_logging(UI.getSetting('logging'));
  32. UI.initSetting('stylesheet', 'default');
  33. WebUtil.selectStylesheet(null);
  34. // call twice to get around webkit bug
  35. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  36. /* Populate the controls if defaults are provided in the URL */
  37. UI.initSetting('host', '');
  38. UI.initSetting('port', '');
  39. UI.initSetting('password', '');
  40. UI.initSetting('encrypt', false);
  41. UI.initSetting('true_color', true);
  42. UI.initSetting('cursor', false);
  43. UI.initSetting('shared', true);
  44. UI.initSetting('connectTimeout', 2);
  45. UI.rfb = RFB({'target': $D('noVNC_canvas'),
  46. 'onUpdateState': UI.updateState,
  47. 'onClipboard': UI.clipReceive});
  48. // Unfocus clipboard when over the VNC area
  49. //$D('VNC_screen').onmousemove = function () {
  50. // var keyboard = UI.rfb.get_keyboard();
  51. // if ((! keyboard) || (! keyboard.get_focused())) {
  52. // $D('VNC_clipboard_text').blur();
  53. // }
  54. // };
  55. // Show mouse selector buttons on touch screen devices
  56. if ('ontouchstart' in document.documentElement) {
  57. $D('noVNC_mobile_buttons').style.display = "inline";
  58. UI.setMouseButton();
  59. window.scrollTo(0, 1);
  60. }
  61. //iOS Safari does not support CSS position:fixed.
  62. //This detects iOS devices and enables javascript workaround.
  63. if ((navigator.userAgent.match(/iPhone/i)) ||
  64. (navigator.userAgent.match(/iPod/i)) ||
  65. (navigator.userAgent.match(/iPad/i))) {
  66. UI.setOnscroll();
  67. UI.setResize();
  68. }
  69. $D('noVNC_host').focus();
  70. },
  71. // Read form control compatible setting from cookie
  72. getSetting: function(name) {
  73. var val, ctrl = $D('noVNC_' + name);
  74. val = WebUtil.readCookie(name);
  75. if (ctrl.type === 'checkbox') {
  76. if (val.toLowerCase() in {'0':1, 'no':1, 'false':1}) {
  77. val = false;
  78. } else {
  79. val = true;
  80. }
  81. }
  82. return val;
  83. },
  84. // Update cookie and form control setting. If value is not set, then
  85. // updates from control to current cookie setting.
  86. updateSetting: function(name, value) {
  87. var i, ctrl = $D('noVNC_' + name);
  88. // Save the cookie for this session
  89. if (typeof value !== 'undefined') {
  90. WebUtil.createCookie(name, value);
  91. }
  92. // Update the settings control
  93. value = UI.getSetting(name);
  94. if (ctrl.type === 'checkbox') {
  95. ctrl.checked = value;
  96. } else if (typeof ctrl.options !== 'undefined') {
  97. for (i = 0; i < ctrl.options.length; i += 1) {
  98. if (ctrl.options[i].value === value) {
  99. ctrl.selectedIndex = i;
  100. break;
  101. }
  102. }
  103. } else {
  104. /*Weird IE9 error leads to 'null' appearring
  105. in textboxes instead of ''.*/
  106. if (value === null) {
  107. value = "";
  108. }
  109. ctrl.value = value;
  110. }
  111. },
  112. // Save control setting to cookie
  113. saveSetting: function(name) {
  114. var val, ctrl = $D('noVNC_' + name);
  115. if (ctrl.type === 'checkbox') {
  116. val = ctrl.checked;
  117. } else if (typeof ctrl.options !== 'undefined') {
  118. val = ctrl.options[ctrl.selectedIndex].value;
  119. } else {
  120. val = ctrl.value;
  121. }
  122. WebUtil.createCookie(name, val);
  123. //Util.Debug("Setting saved '" + name + "=" + val + "'");
  124. return val;
  125. },
  126. // Initial page load read/initialization of settings
  127. initSetting: function(name, defVal) {
  128. var val;
  129. // Check Query string followed by cookie
  130. val = WebUtil.getQueryVar(name);
  131. if (val === null) {
  132. val = WebUtil.readCookie(name, defVal);
  133. }
  134. UI.updateSetting(name, val);
  135. //Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
  136. return val;
  137. },
  138. // Toggle the settings menu:
  139. // On open, settings are refreshed from saved cookies.
  140. // On close, settings are applied
  141. clickSettingsMenu: function() {
  142. if (UI.settingsOpen) {
  143. UI.settingsApply();
  144. UI.closeSettingsMenu();
  145. } else {
  146. UI.updateSetting('encrypt');
  147. UI.updateSetting('true_color');
  148. if (UI.rfb.get_display().get_cursor_uri()) {
  149. UI.updateSetting('cursor');
  150. } else {
  151. UI.updateSetting('cursor', false);
  152. $D('noVNC_cursor').disabled = true;
  153. }
  154. UI.updateSetting('shared');
  155. UI.updateSetting('connectTimeout');
  156. UI.updateSetting('stylesheet');
  157. UI.updateSetting('logging');
  158. UI.openSettingsMenu();
  159. }
  160. },
  161. // Open menu
  162. openSettingsMenu: function() {
  163. if (UI.clipboardOpen == true) {
  164. UI.showClipboard();
  165. }
  166. //Close connection settings if open
  167. if (UI.connSettingsOpen == true) {
  168. UI.connectPanelbutton();
  169. }
  170. $D('noVNC_Settings').style.display = "block";
  171. UI.settingsOpen = true;
  172. },
  173. // Close menu (without applying settings)
  174. closeSettingsMenu: function() {
  175. $D('noVNC_Settings').style.display = "none";
  176. UI.settingsOpen = false;
  177. },
  178. // Disable/enable controls depending on connection state
  179. settingsDisabled: function(disabled, rfb) {
  180. //Util.Debug(">> settingsDisabled");
  181. $D('noVNC_encrypt').disabled = disabled;
  182. $D('noVNC_true_color').disabled = disabled;
  183. if (rfb && rfb.get_display() && rfb.get_display().get_cursor_uri()) {
  184. $D('noVNC_cursor').disabled = disabled;
  185. } else {
  186. UI.updateSetting('cursor', false);
  187. $D('noVNC_cursor').disabled = true;
  188. }
  189. $D('noVNC_shared').disabled = disabled;
  190. $D('noVNC_connectTimeout').disabled = disabled;
  191. //Util.Debug("<< settingsDisabled");
  192. },
  193. // Save/apply settings when 'Apply' button is pressed
  194. settingsApply: function() {
  195. //Util.Debug(">> settingsApply");
  196. UI.saveSetting('encrypt');
  197. UI.saveSetting('true_color');
  198. if (UI.rfb.get_display().get_cursor_uri()) {
  199. UI.saveSetting('cursor');
  200. }
  201. UI.saveSetting('shared');
  202. UI.saveSetting('connectTimeout');
  203. UI.saveSetting('stylesheet');
  204. UI.saveSetting('logging');
  205. // Settings with immediate (non-connected related) effect
  206. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  207. WebUtil.init_logging(UI.getSetting('logging'));
  208. //Util.Debug("<< settingsApply");
  209. },
  210. setPassword: function() {
  211. UI.rfb.sendPassword($D('noVNC_password').value);
  212. //Reset connect button.
  213. $D('noVNC_connect_button').value = "Connect";
  214. $D('noVNC_connect_button').onclick = UI.Connect;
  215. //Hide connection panel.
  216. UI.connectPanelbutton();
  217. return false;
  218. },
  219. sendCtrlAltDel: function() {
  220. UI.rfb.sendCtrlAltDel();
  221. },
  222. setMouseButton: function(num) {
  223. var b, blist = [1,2,4], button,
  224. mouse = UI.rfb.get_mouse();
  225. if (typeof num === 'undefined') {
  226. // Show the default
  227. num = mouse.get_touchButton();
  228. } else if (num === mouse.get_touchButton()) {
  229. // Set all buttons off (no clicks)
  230. mouse.set_touchButton(0);
  231. num = 0;
  232. } else {
  233. // Turn on one button
  234. mouse.set_touchButton(num);
  235. }
  236. for (b = 0; b < blist.length; b++) {
  237. button = $D('noVNC_mouse_button' + blist[b]);
  238. if (blist[b] === num) {
  239. button.style.backgroundColor = "black";
  240. button.style.color = "lightgray";
  241. } else {
  242. button.style.backgroundColor = "";
  243. button.style.color = "";
  244. }
  245. }
  246. },
  247. updateState: function(rfb, state, oldstate, msg) {
  248. var s, sb, c, cad, klass;
  249. s = $D('noVNC_status');
  250. sb = $D('noVNC_status_bar');
  251. c = $D('connectPanelbutton');
  252. cad = $D('sendCtrlAltDelButton');
  253. switch (state) {
  254. case 'failed':
  255. case 'fatal':
  256. c.disabled = true;
  257. cad.style.display = "none";
  258. UI.settingsDisabled(true, rfb);
  259. klass = "noVNC_status_error";
  260. break;
  261. case 'normal':
  262. c.value = "Disconnect";
  263. c.onclick = UI.disconnect;
  264. c.disabled = false;
  265. cad.style.display = "block";
  266. UI.settingsDisabled(true, rfb);
  267. klass = "noVNC_status_normal";
  268. break;
  269. case 'disconnected':
  270. $D('noVNC_defaultScreen').style.display = "block";
  271. c.value = "Connection";
  272. c.onclick = UI.connectPanelbutton;
  273. case 'loaded':
  274. c.value = "Connection";
  275. c.onclick = UI.connectPanelbutton;
  276. c.disabled = false;
  277. cad.style.display = "none";
  278. UI.settingsDisabled(false, rfb);
  279. klass = "noVNC_status_normal";
  280. break;
  281. case 'password':
  282. UI.connectPanelbutton();
  283. $D('noVNC_connect_button').value = "Send Password";
  284. $D('noVNC_connect_button').onclick = UI.setPassword;
  285. $D('noVNC_password').focus();
  286. c.disabled = false;
  287. cad.style.display = "none";
  288. UI.settingsDisabled(true, rfb);
  289. klass = "noVNC_status_warn";
  290. break;
  291. default:
  292. c.disabled = true;
  293. cad.style.display = "none";
  294. UI.settingsDisabled(true, rfb);
  295. klass = "noVNC_status_warn";
  296. break;
  297. }
  298. if (typeof(msg) !== 'undefined') {
  299. s.setAttribute("class", klass);
  300. sb.setAttribute("class", klass);
  301. s.innerHTML = msg;
  302. }
  303. },
  304. clipReceive: function(rfb, text) {
  305. Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
  306. $D('noVNC_clipboard_text').value = text;
  307. Util.Debug("<< UI.clipReceive");
  308. },
  309. connect: function() {
  310. var host, port, password;
  311. UI.closeSettingsMenu();
  312. UI.connectPanelbutton();
  313. host = $D('noVNC_host').value;
  314. port = $D('noVNC_port').value;
  315. password = $D('noVNC_password').value;
  316. if ((!host) || (!port)) {
  317. throw("Must set host and port");
  318. }
  319. UI.rfb.set_encrypt(UI.getSetting('encrypt'));
  320. UI.rfb.set_true_color(UI.getSetting('true_color'));
  321. UI.rfb.set_local_cursor(UI.getSetting('cursor'));
  322. UI.rfb.set_shared(UI.getSetting('shared'));
  323. UI.rfb.set_connectTimeout(UI.getSetting('connectTimeout'));
  324. UI.rfb.connect(host, port, password);
  325. //Close dialog.
  326. setTimeout("setBarPosition()",100);
  327. $D('noVNC_defaultScreen').style.display = "none";
  328. },
  329. disconnect: function() {
  330. UI.closeSettingsMenu();
  331. UI.rfb.disconnect();
  332. $D('noVNC_defaultScreen').style.display = "block";
  333. UI.connSettingsOpen = false;
  334. UI.connectPanelbutton();
  335. },
  336. displayBlur: function() {
  337. UI.rfb.get_keyboard().set_focused(false);
  338. UI.rfb.get_mouse().set_focused(false);
  339. },
  340. displayFocus: function() {
  341. UI.rfb.get_keyboard().set_focused(true);
  342. UI.rfb.get_mouse().set_focused(true);
  343. },
  344. clipClear: function() {
  345. $D('noVNC_clipboard_text').value = "";
  346. UI.rfb.clipboardPasteFrom("");
  347. },
  348. clipSend: function() {
  349. var text = $D('noVNC_clipboard_text').value;
  350. Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
  351. UI.rfb.clipboardPasteFrom(text);
  352. Util.Debug("<< UI.clipSend");
  353. },
  354. showClipboard: function() {
  355. //Close settings if open
  356. if (UI.settingsOpen == true) {
  357. UI.closeSettingsMenu();
  358. }
  359. //Close connection settings if open
  360. if (UI.connSettingsOpen == true) {
  361. UI.connectPanelbutton();
  362. }
  363. //Toggle Connection Panel
  364. if (UI.clipboardOpen == true) {
  365. $D('noVNC_clipboard').style.display = "none";
  366. UI.clipboardOpen = false;
  367. } else {
  368. $D('noVNC_clipboard').style.display = "block";
  369. UI.clipboardOpen = true;
  370. }
  371. },
  372. showKeyboard: function() {
  373. //Get Current Scroll Position
  374. var scrollx =
  375. (document.all)?document.body.scrollLeft:window.pageXOffset;
  376. var scrolly =
  377. (document.all)?document.body.scrollTop:window.pageYOffset;
  378. //Stop browser zooming on textbox.
  379. UI.zoomDisable();
  380. $D('keyboardinput').focus();
  381. scroll(scrollx,scrolly);
  382. //Renable user zoom.
  383. UI.zoomEnable();
  384. },
  385. zoomDisable: function() {
  386. //Change viewport meta data to disable zooming.
  387. UI.changeViewportMeta("user-scalable=0");
  388. },
  389. zoomEnable: function(){
  390. //Change viewport meta data to enable user zooming.
  391. UI.changeViewportMeta("user-scalable=1");
  392. },
  393. changeViewportMeta: function (newattributes) {
  394. // First, get the array of meta-tag elements
  395. var metatags = document.getElementsByTagName("meta");
  396. // Update only the Viewport meta tag
  397. for (var cnt = 0; cnt < metatags.length; cnt++)
  398. {
  399. var name = metatags[cnt].getAttribute("name");
  400. var content = metatags[cnt].getAttribute("content");
  401. // Update the Viewport meta tag
  402. if (metatags[cnt].getAttribute("name") == "viewport") {
  403. metatags[cnt].setAttribute("content", newattributes);
  404. }
  405. }
  406. },
  407. //iOS < Version 5 does not support position fixed. Javascript workaround:
  408. setOnscroll: function() {
  409. window.onscroll = function() {
  410. UI.setBarPosition();
  411. };
  412. },
  413. setResize: function () {
  414. window.onResize = function() {
  415. UI.setBarPosition();
  416. };
  417. },
  418. //Helper to add options to dropdown.
  419. addOption: function(selectbox,text,value )
  420. {
  421. var optn = document.createElement("OPTION");
  422. optn.text = text;
  423. optn.value = value;
  424. selectbox.options.add(optn);
  425. },
  426. setBarPosition: function() {
  427. $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
  428. $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
  429. $D('noVNC_mobile_buttons_right').style.right = 0 + 'px';
  430. var vncwidth = $('#noVNC_screen').width();
  431. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  432. },
  433. connectPanelbutton: function() {
  434. //Close connection settings if open
  435. if (UI.settingsOpen == true) {
  436. UI.closeSettingsMenu();
  437. }
  438. if (UI.clipboardOpen == true) {
  439. UI.showClipboard();
  440. }
  441. //Toggle Connection Panel
  442. if (UI.connSettingsOpen == true) {
  443. $D('noVNC_controls').style.display = "none";
  444. UI.connSettingsOpen = false;
  445. } else {
  446. $D('noVNC_controls').style.display = "block";
  447. UI.connSettingsOpen = true;
  448. $D('noVNC_host').focus();
  449. }
  450. }
  451. };