ui.js 16 KB

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