ui.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2019 The noVNC Authors
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. import * as Log from '../core/util/logging.js';
  9. import _, { l10n } from './localization.js';
  10. import { isTouchDevice, isSafari, hasScrollbarGutter, dragThreshold }
  11. from '../core/util/browser.js';
  12. import { setCapture, getPointerEvent } from '../core/util/events.js';
  13. import KeyTable from "../core/input/keysym.js";
  14. import keysyms from "../core/input/keysymdef.js";
  15. import Keyboard from "../core/input/keyboard.js";
  16. import RFB from "../core/rfb.js";
  17. import * as WebUtil from "./webutil.js";
  18. const PAGE_TITLE = "noVNC";
  19. const UI = {
  20. connected: false,
  21. desktopName: "",
  22. statusTimeout: null,
  23. hideKeyboardTimeout: null,
  24. idleControlbarTimeout: null,
  25. closeControlbarTimeout: null,
  26. controlbarGrabbed: false,
  27. controlbarDrag: false,
  28. controlbarMouseDownClientY: 0,
  29. controlbarMouseDownOffsetY: 0,
  30. lastKeyboardinput: null,
  31. defaultKeyboardinputLen: 100,
  32. inhibitReconnect: true,
  33. reconnectCallback: null,
  34. reconnectPassword: null,
  35. prime() {
  36. return WebUtil.initSettings().then(() => {
  37. if (document.readyState === "interactive" || document.readyState === "complete") {
  38. return UI.start();
  39. }
  40. return new Promise((resolve, reject) => {
  41. document.addEventListener('DOMContentLoaded', () => UI.start().then(resolve).catch(reject));
  42. });
  43. });
  44. },
  45. // Render default UI and initialize settings menu
  46. start() {
  47. UI.initSettings();
  48. // Translate the DOM
  49. l10n.translateDOM();
  50. fetch('./package.json')
  51. .then((response) => {
  52. if (!response.ok) {
  53. throw Error("" + response.status + " " + response.statusText);
  54. }
  55. return response.json();
  56. })
  57. .then((packageInfo) => {
  58. Array.from(document.getElementsByClassName('noVNC_version')).forEach(el => el.innerText = packageInfo.version);
  59. })
  60. .catch((err) => {
  61. Log.Error("Couldn't fetch package.json: " + err);
  62. Array.from(document.getElementsByClassName('noVNC_version_wrapper'))
  63. .concat(Array.from(document.getElementsByClassName('noVNC_version_separator')))
  64. .forEach(el => el.style.display = 'none');
  65. });
  66. // Adapt the interface for touch screen devices
  67. if (isTouchDevice) {
  68. document.documentElement.classList.add("noVNC_touch");
  69. // Remove the address bar
  70. setTimeout(() => window.scrollTo(0, 1), 100);
  71. }
  72. // Restore control bar position
  73. if (WebUtil.readSetting('controlbar_pos') === 'right') {
  74. UI.toggleControlbarSide();
  75. }
  76. UI.initFullscreen();
  77. // Setup event handlers
  78. UI.addControlbarHandlers();
  79. UI.addTouchSpecificHandlers();
  80. UI.addExtraKeysHandlers();
  81. UI.addMachineHandlers();
  82. UI.addConnectionControlHandlers();
  83. UI.addClipboardHandlers();
  84. UI.addSettingsHandlers();
  85. document.getElementById("noVNC_status")
  86. .addEventListener('click', UI.hideStatus);
  87. // Bootstrap fallback input handler
  88. UI.keyboardinputReset();
  89. UI.openControlbar();
  90. UI.updateVisualState('init');
  91. document.documentElement.classList.remove("noVNC_loading");
  92. let autoconnect = WebUtil.getConfigVar('autoconnect', false);
  93. if (autoconnect === 'true' || autoconnect == '1') {
  94. autoconnect = true;
  95. UI.connect();
  96. } else {
  97. autoconnect = false;
  98. // Show the connect panel on first load unless autoconnecting
  99. UI.openConnectPanel();
  100. }
  101. return Promise.resolve(UI.rfb);
  102. },
  103. initFullscreen() {
  104. // Only show the button if fullscreen is properly supported
  105. // * Safari doesn't support alphanumerical input while in fullscreen
  106. if (!isSafari() &&
  107. (document.documentElement.requestFullscreen ||
  108. document.documentElement.mozRequestFullScreen ||
  109. document.documentElement.webkitRequestFullscreen ||
  110. document.body.msRequestFullscreen)) {
  111. document.getElementById('noVNC_fullscreen_button')
  112. .classList.remove("noVNC_hidden");
  113. UI.addFullscreenHandlers();
  114. }
  115. },
  116. initSettings() {
  117. // Logging selection dropdown
  118. const llevels = ['error', 'warn', 'info', 'debug'];
  119. for (let i = 0; i < llevels.length; i += 1) {
  120. UI.addOption(document.getElementById('noVNC_setting_logging'), llevels[i], llevels[i]);
  121. }
  122. // Settings with immediate effects
  123. UI.initSetting('logging', 'warn');
  124. UI.updateLogging();
  125. // if port == 80 (or 443) then it won't be present and should be
  126. // set manually
  127. let port = window.location.port;
  128. if (!port) {
  129. if (window.location.protocol.substring(0, 5) == 'https') {
  130. port = 443;
  131. } else if (window.location.protocol.substring(0, 4) == 'http') {
  132. port = 80;
  133. }
  134. }
  135. /* Populate the controls if defaults are provided in the URL */
  136. UI.initSetting('host', window.location.hostname);
  137. UI.initSetting('port', port);
  138. UI.initSetting('encrypt', (window.location.protocol === "https:"));
  139. UI.initSetting('view_clip', false);
  140. UI.initSetting('resize', 'off');
  141. UI.initSetting('quality', 6);
  142. UI.initSetting('compression', 2);
  143. UI.initSetting('shared', true);
  144. UI.initSetting('view_only', false);
  145. UI.initSetting('show_dot', false);
  146. UI.initSetting('path', 'websockify');
  147. UI.initSetting('repeaterID', '');
  148. UI.initSetting('reconnect', false);
  149. UI.initSetting('reconnect_delay', 5000);
  150. UI.setupSettingLabels();
  151. },
  152. // Adds a link to the label elements on the corresponding input elements
  153. setupSettingLabels() {
  154. const labels = document.getElementsByTagName('LABEL');
  155. for (let i = 0; i < labels.length; i++) {
  156. const htmlFor = labels[i].htmlFor;
  157. if (htmlFor != '') {
  158. const elem = document.getElementById(htmlFor);
  159. if (elem) elem.label = labels[i];
  160. } else {
  161. // If 'for' isn't set, use the first input element child
  162. const children = labels[i].children;
  163. for (let j = 0; j < children.length; j++) {
  164. if (children[j].form !== undefined) {
  165. children[j].label = labels[i];
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. },
  172. /* ------^-------
  173. * /INIT
  174. * ==============
  175. * EVENT HANDLERS
  176. * ------v------*/
  177. addControlbarHandlers() {
  178. document.getElementById("noVNC_control_bar")
  179. .addEventListener('mousemove', UI.activateControlbar);
  180. document.getElementById("noVNC_control_bar")
  181. .addEventListener('mouseup', UI.activateControlbar);
  182. document.getElementById("noVNC_control_bar")
  183. .addEventListener('mousedown', UI.activateControlbar);
  184. document.getElementById("noVNC_control_bar")
  185. .addEventListener('keydown', UI.activateControlbar);
  186. document.getElementById("noVNC_control_bar")
  187. .addEventListener('mousedown', UI.keepControlbar);
  188. document.getElementById("noVNC_control_bar")
  189. .addEventListener('keydown', UI.keepControlbar);
  190. document.getElementById("noVNC_view_drag_button")
  191. .addEventListener('click', UI.toggleViewDrag);
  192. document.getElementById("noVNC_control_bar_handle")
  193. .addEventListener('mousedown', UI.controlbarHandleMouseDown);
  194. document.getElementById("noVNC_control_bar_handle")
  195. .addEventListener('mouseup', UI.controlbarHandleMouseUp);
  196. document.getElementById("noVNC_control_bar_handle")
  197. .addEventListener('mousemove', UI.dragControlbarHandle);
  198. // resize events aren't available for elements
  199. window.addEventListener('resize', UI.updateControlbarHandle);
  200. const exps = document.getElementsByClassName("noVNC_expander");
  201. for (let i = 0;i < exps.length;i++) {
  202. exps[i].addEventListener('click', UI.toggleExpander);
  203. }
  204. },
  205. addTouchSpecificHandlers() {
  206. document.getElementById("noVNC_keyboard_button")
  207. .addEventListener('click', UI.toggleVirtualKeyboard);
  208. UI.touchKeyboard = new Keyboard(document.getElementById('noVNC_keyboardinput'));
  209. UI.touchKeyboard.onkeyevent = UI.keyEvent;
  210. UI.touchKeyboard.grab();
  211. document.getElementById("noVNC_keyboardinput")
  212. .addEventListener('input', UI.keyInput);
  213. document.getElementById("noVNC_keyboardinput")
  214. .addEventListener('focus', UI.onfocusVirtualKeyboard);
  215. document.getElementById("noVNC_keyboardinput")
  216. .addEventListener('blur', UI.onblurVirtualKeyboard);
  217. document.getElementById("noVNC_keyboardinput")
  218. .addEventListener('submit', () => false);
  219. document.documentElement
  220. .addEventListener('mousedown', UI.keepVirtualKeyboard, true);
  221. document.getElementById("noVNC_control_bar")
  222. .addEventListener('touchstart', UI.activateControlbar);
  223. document.getElementById("noVNC_control_bar")
  224. .addEventListener('touchmove', UI.activateControlbar);
  225. document.getElementById("noVNC_control_bar")
  226. .addEventListener('touchend', UI.activateControlbar);
  227. document.getElementById("noVNC_control_bar")
  228. .addEventListener('input', UI.activateControlbar);
  229. document.getElementById("noVNC_control_bar")
  230. .addEventListener('touchstart', UI.keepControlbar);
  231. document.getElementById("noVNC_control_bar")
  232. .addEventListener('input', UI.keepControlbar);
  233. document.getElementById("noVNC_control_bar_handle")
  234. .addEventListener('touchstart', UI.controlbarHandleMouseDown);
  235. document.getElementById("noVNC_control_bar_handle")
  236. .addEventListener('touchend', UI.controlbarHandleMouseUp);
  237. document.getElementById("noVNC_control_bar_handle")
  238. .addEventListener('touchmove', UI.dragControlbarHandle);
  239. },
  240. addExtraKeysHandlers() {
  241. document.getElementById("noVNC_toggle_extra_keys_button")
  242. .addEventListener('click', UI.toggleExtraKeys);
  243. document.getElementById("noVNC_toggle_ctrl_button")
  244. .addEventListener('click', UI.toggleCtrl);
  245. document.getElementById("noVNC_toggle_windows_button")
  246. .addEventListener('click', UI.toggleWindows);
  247. document.getElementById("noVNC_toggle_alt_button")
  248. .addEventListener('click', UI.toggleAlt);
  249. document.getElementById("noVNC_send_tab_button")
  250. .addEventListener('click', UI.sendTab);
  251. document.getElementById("noVNC_send_esc_button")
  252. .addEventListener('click', UI.sendEsc);
  253. document.getElementById("noVNC_send_ctrl_alt_del_button")
  254. .addEventListener('click', UI.sendCtrlAltDel);
  255. },
  256. addMachineHandlers() {
  257. document.getElementById("noVNC_shutdown_button")
  258. .addEventListener('click', () => UI.rfb.machineShutdown());
  259. document.getElementById("noVNC_reboot_button")
  260. .addEventListener('click', () => UI.rfb.machineReboot());
  261. document.getElementById("noVNC_reset_button")
  262. .addEventListener('click', () => UI.rfb.machineReset());
  263. document.getElementById("noVNC_power_button")
  264. .addEventListener('click', UI.togglePowerPanel);
  265. },
  266. addConnectionControlHandlers() {
  267. document.getElementById("noVNC_disconnect_button")
  268. .addEventListener('click', UI.disconnect);
  269. document.getElementById("noVNC_connect_button")
  270. .addEventListener('click', UI.connect);
  271. document.getElementById("noVNC_cancel_reconnect_button")
  272. .addEventListener('click', UI.cancelReconnect);
  273. document.getElementById("noVNC_credentials_button")
  274. .addEventListener('click', UI.setCredentials);
  275. },
  276. addClipboardHandlers() {
  277. document.getElementById("noVNC_clipboard_button")
  278. .addEventListener('click', UI.toggleClipboardPanel);
  279. document.getElementById("noVNC_clipboard_text")
  280. .addEventListener('change', UI.clipboardSend);
  281. document.getElementById("noVNC_clipboard_clear_button")
  282. .addEventListener('click', UI.clipboardClear);
  283. },
  284. // Add a call to save settings when the element changes,
  285. // unless the optional parameter changeFunc is used instead.
  286. addSettingChangeHandler(name, changeFunc) {
  287. const settingElem = document.getElementById("noVNC_setting_" + name);
  288. if (changeFunc === undefined) {
  289. changeFunc = () => UI.saveSetting(name);
  290. }
  291. settingElem.addEventListener('change', changeFunc);
  292. },
  293. addSettingsHandlers() {
  294. document.getElementById("noVNC_settings_button")
  295. .addEventListener('click', UI.toggleSettingsPanel);
  296. UI.addSettingChangeHandler('encrypt');
  297. UI.addSettingChangeHandler('resize');
  298. UI.addSettingChangeHandler('resize', UI.applyResizeMode);
  299. UI.addSettingChangeHandler('resize', UI.updateViewClip);
  300. UI.addSettingChangeHandler('quality');
  301. UI.addSettingChangeHandler('quality', UI.updateQuality);
  302. UI.addSettingChangeHandler('compression');
  303. UI.addSettingChangeHandler('compression', UI.updateCompression);
  304. UI.addSettingChangeHandler('view_clip');
  305. UI.addSettingChangeHandler('view_clip', UI.updateViewClip);
  306. UI.addSettingChangeHandler('shared');
  307. UI.addSettingChangeHandler('view_only');
  308. UI.addSettingChangeHandler('view_only', UI.updateViewOnly);
  309. UI.addSettingChangeHandler('show_dot');
  310. UI.addSettingChangeHandler('show_dot', UI.updateShowDotCursor);
  311. UI.addSettingChangeHandler('host');
  312. UI.addSettingChangeHandler('port');
  313. UI.addSettingChangeHandler('path');
  314. UI.addSettingChangeHandler('repeaterID');
  315. UI.addSettingChangeHandler('logging');
  316. UI.addSettingChangeHandler('logging', UI.updateLogging);
  317. UI.addSettingChangeHandler('reconnect');
  318. UI.addSettingChangeHandler('reconnect_delay');
  319. },
  320. addFullscreenHandlers() {
  321. document.getElementById("noVNC_fullscreen_button")
  322. .addEventListener('click', UI.toggleFullscreen);
  323. window.addEventListener('fullscreenchange', UI.updateFullscreenButton);
  324. window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton);
  325. window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton);
  326. window.addEventListener('msfullscreenchange', UI.updateFullscreenButton);
  327. },
  328. /* ------^-------
  329. * /EVENT HANDLERS
  330. * ==============
  331. * VISUAL
  332. * ------v------*/
  333. // Disable/enable controls depending on connection state
  334. updateVisualState(state) {
  335. document.documentElement.classList.remove("noVNC_connecting");
  336. document.documentElement.classList.remove("noVNC_connected");
  337. document.documentElement.classList.remove("noVNC_disconnecting");
  338. document.documentElement.classList.remove("noVNC_reconnecting");
  339. const transitionElem = document.getElementById("noVNC_transition_text");
  340. switch (state) {
  341. case 'init':
  342. break;
  343. case 'connecting':
  344. transitionElem.textContent = _("Connecting...");
  345. document.documentElement.classList.add("noVNC_connecting");
  346. break;
  347. case 'connected':
  348. document.documentElement.classList.add("noVNC_connected");
  349. break;
  350. case 'disconnecting':
  351. transitionElem.textContent = _("Disconnecting...");
  352. document.documentElement.classList.add("noVNC_disconnecting");
  353. break;
  354. case 'disconnected':
  355. break;
  356. case 'reconnecting':
  357. transitionElem.textContent = _("Reconnecting...");
  358. document.documentElement.classList.add("noVNC_reconnecting");
  359. break;
  360. default:
  361. Log.Error("Invalid visual state: " + state);
  362. UI.showStatus(_("Internal error"), 'error');
  363. return;
  364. }
  365. if (UI.connected) {
  366. UI.updateViewClip();
  367. UI.disableSetting('encrypt');
  368. UI.disableSetting('shared');
  369. UI.disableSetting('host');
  370. UI.disableSetting('port');
  371. UI.disableSetting('path');
  372. UI.disableSetting('repeaterID');
  373. // Hide the controlbar after 2 seconds
  374. UI.closeControlbarTimeout = setTimeout(UI.closeControlbar, 2000);
  375. } else {
  376. UI.enableSetting('encrypt');
  377. UI.enableSetting('shared');
  378. UI.enableSetting('host');
  379. UI.enableSetting('port');
  380. UI.enableSetting('path');
  381. UI.enableSetting('repeaterID');
  382. UI.updatePowerButton();
  383. UI.keepControlbar();
  384. }
  385. // State change closes dialogs as they may not be relevant
  386. // anymore
  387. UI.closeAllPanels();
  388. document.getElementById('noVNC_credentials_dlg')
  389. .classList.remove('noVNC_open');
  390. },
  391. showStatus(text, statusType, time) {
  392. const statusElem = document.getElementById('noVNC_status');
  393. if (typeof statusType === 'undefined') {
  394. statusType = 'normal';
  395. }
  396. // Don't overwrite more severe visible statuses and never
  397. // errors. Only shows the first error.
  398. if (statusElem.classList.contains("noVNC_open")) {
  399. if (statusElem.classList.contains("noVNC_status_error")) {
  400. return;
  401. }
  402. if (statusElem.classList.contains("noVNC_status_warn") &&
  403. statusType === 'normal') {
  404. return;
  405. }
  406. }
  407. clearTimeout(UI.statusTimeout);
  408. switch (statusType) {
  409. case 'error':
  410. statusElem.classList.remove("noVNC_status_warn");
  411. statusElem.classList.remove("noVNC_status_normal");
  412. statusElem.classList.add("noVNC_status_error");
  413. break;
  414. case 'warning':
  415. case 'warn':
  416. statusElem.classList.remove("noVNC_status_error");
  417. statusElem.classList.remove("noVNC_status_normal");
  418. statusElem.classList.add("noVNC_status_warn");
  419. break;
  420. case 'normal':
  421. case 'info':
  422. default:
  423. statusElem.classList.remove("noVNC_status_error");
  424. statusElem.classList.remove("noVNC_status_warn");
  425. statusElem.classList.add("noVNC_status_normal");
  426. break;
  427. }
  428. statusElem.textContent = text;
  429. statusElem.classList.add("noVNC_open");
  430. // If no time was specified, show the status for 1.5 seconds
  431. if (typeof time === 'undefined') {
  432. time = 1500;
  433. }
  434. // Error messages do not timeout
  435. if (statusType !== 'error') {
  436. UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
  437. }
  438. },
  439. hideStatus() {
  440. clearTimeout(UI.statusTimeout);
  441. document.getElementById('noVNC_status').classList.remove("noVNC_open");
  442. },
  443. activateControlbar(event) {
  444. clearTimeout(UI.idleControlbarTimeout);
  445. // We manipulate the anchor instead of the actual control
  446. // bar in order to avoid creating new a stacking group
  447. document.getElementById('noVNC_control_bar_anchor')
  448. .classList.remove("noVNC_idle");
  449. UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
  450. },
  451. idleControlbar() {
  452. // Don't fade if a child of the control bar has focus
  453. if (document.getElementById('noVNC_control_bar')
  454. .contains(document.activeElement) && document.hasFocus()) {
  455. UI.activateControlbar();
  456. return;
  457. }
  458. document.getElementById('noVNC_control_bar_anchor')
  459. .classList.add("noVNC_idle");
  460. },
  461. keepControlbar() {
  462. clearTimeout(UI.closeControlbarTimeout);
  463. },
  464. openControlbar() {
  465. document.getElementById('noVNC_control_bar')
  466. .classList.add("noVNC_open");
  467. },
  468. closeControlbar() {
  469. UI.closeAllPanels();
  470. document.getElementById('noVNC_control_bar')
  471. .classList.remove("noVNC_open");
  472. UI.rfb.focus();
  473. },
  474. toggleControlbar() {
  475. if (document.getElementById('noVNC_control_bar')
  476. .classList.contains("noVNC_open")) {
  477. UI.closeControlbar();
  478. } else {
  479. UI.openControlbar();
  480. }
  481. },
  482. toggleControlbarSide() {
  483. // Temporarily disable animation, if bar is displayed, to avoid weird
  484. // movement. The transitionend-event will not fire when display=none.
  485. const bar = document.getElementById('noVNC_control_bar');
  486. const barDisplayStyle = window.getComputedStyle(bar).display;
  487. if (barDisplayStyle !== 'none') {
  488. bar.style.transitionDuration = '0s';
  489. bar.addEventListener('transitionend', () => bar.style.transitionDuration = '');
  490. }
  491. const anchor = document.getElementById('noVNC_control_bar_anchor');
  492. if (anchor.classList.contains("noVNC_right")) {
  493. WebUtil.writeSetting('controlbar_pos', 'left');
  494. anchor.classList.remove("noVNC_right");
  495. } else {
  496. WebUtil.writeSetting('controlbar_pos', 'right');
  497. anchor.classList.add("noVNC_right");
  498. }
  499. // Consider this a movement of the handle
  500. UI.controlbarDrag = true;
  501. },
  502. showControlbarHint(show) {
  503. const hint = document.getElementById('noVNC_control_bar_hint');
  504. if (show) {
  505. hint.classList.add("noVNC_active");
  506. } else {
  507. hint.classList.remove("noVNC_active");
  508. }
  509. },
  510. dragControlbarHandle(e) {
  511. if (!UI.controlbarGrabbed) return;
  512. const ptr = getPointerEvent(e);
  513. const anchor = document.getElementById('noVNC_control_bar_anchor');
  514. if (ptr.clientX < (window.innerWidth * 0.1)) {
  515. if (anchor.classList.contains("noVNC_right")) {
  516. UI.toggleControlbarSide();
  517. }
  518. } else if (ptr.clientX > (window.innerWidth * 0.9)) {
  519. if (!anchor.classList.contains("noVNC_right")) {
  520. UI.toggleControlbarSide();
  521. }
  522. }
  523. if (!UI.controlbarDrag) {
  524. const dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
  525. if (dragDistance < dragThreshold) return;
  526. UI.controlbarDrag = true;
  527. }
  528. const eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
  529. UI.moveControlbarHandle(eventY);
  530. e.preventDefault();
  531. e.stopPropagation();
  532. UI.keepControlbar();
  533. UI.activateControlbar();
  534. },
  535. // Move the handle but don't allow any position outside the bounds
  536. moveControlbarHandle(viewportRelativeY) {
  537. const handle = document.getElementById("noVNC_control_bar_handle");
  538. const handleHeight = handle.getBoundingClientRect().height;
  539. const controlbarBounds = document.getElementById("noVNC_control_bar")
  540. .getBoundingClientRect();
  541. const margin = 10;
  542. // These heights need to be non-zero for the below logic to work
  543. if (handleHeight === 0 || controlbarBounds.height === 0) {
  544. return;
  545. }
  546. let newY = viewportRelativeY;
  547. // Check if the coordinates are outside the control bar
  548. if (newY < controlbarBounds.top + margin) {
  549. // Force coordinates to be below the top of the control bar
  550. newY = controlbarBounds.top + margin;
  551. } else if (newY > controlbarBounds.top +
  552. controlbarBounds.height - handleHeight - margin) {
  553. // Force coordinates to be above the bottom of the control bar
  554. newY = controlbarBounds.top +
  555. controlbarBounds.height - handleHeight - margin;
  556. }
  557. // Corner case: control bar too small for stable position
  558. if (controlbarBounds.height < (handleHeight + margin * 2)) {
  559. newY = controlbarBounds.top +
  560. (controlbarBounds.height - handleHeight) / 2;
  561. }
  562. // The transform needs coordinates that are relative to the parent
  563. const parentRelativeY = newY - controlbarBounds.top;
  564. handle.style.transform = "translateY(" + parentRelativeY + "px)";
  565. },
  566. updateControlbarHandle() {
  567. // Since the control bar is fixed on the viewport and not the page,
  568. // the move function expects coordinates relative the the viewport.
  569. const handle = document.getElementById("noVNC_control_bar_handle");
  570. const handleBounds = handle.getBoundingClientRect();
  571. UI.moveControlbarHandle(handleBounds.top);
  572. },
  573. controlbarHandleMouseUp(e) {
  574. if ((e.type == "mouseup") && (e.button != 0)) return;
  575. // mouseup and mousedown on the same place toggles the controlbar
  576. if (UI.controlbarGrabbed && !UI.controlbarDrag) {
  577. UI.toggleControlbar();
  578. e.preventDefault();
  579. e.stopPropagation();
  580. UI.keepControlbar();
  581. UI.activateControlbar();
  582. }
  583. UI.controlbarGrabbed = false;
  584. UI.showControlbarHint(false);
  585. },
  586. controlbarHandleMouseDown(e) {
  587. if ((e.type == "mousedown") && (e.button != 0)) return;
  588. const ptr = getPointerEvent(e);
  589. const handle = document.getElementById("noVNC_control_bar_handle");
  590. const bounds = handle.getBoundingClientRect();
  591. // Touch events have implicit capture
  592. if (e.type === "mousedown") {
  593. setCapture(handle);
  594. }
  595. UI.controlbarGrabbed = true;
  596. UI.controlbarDrag = false;
  597. UI.showControlbarHint(true);
  598. UI.controlbarMouseDownClientY = ptr.clientY;
  599. UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
  600. e.preventDefault();
  601. e.stopPropagation();
  602. UI.keepControlbar();
  603. UI.activateControlbar();
  604. },
  605. toggleExpander(e) {
  606. if (this.classList.contains("noVNC_open")) {
  607. this.classList.remove("noVNC_open");
  608. } else {
  609. this.classList.add("noVNC_open");
  610. }
  611. },
  612. /* ------^-------
  613. * /VISUAL
  614. * ==============
  615. * SETTINGS
  616. * ------v------*/
  617. // Initial page load read/initialization of settings
  618. initSetting(name, defVal) {
  619. // Check Query string followed by cookie
  620. let val = WebUtil.getConfigVar(name);
  621. if (val === null) {
  622. val = WebUtil.readSetting(name, defVal);
  623. }
  624. WebUtil.setSetting(name, val);
  625. UI.updateSetting(name);
  626. return val;
  627. },
  628. // Set the new value, update and disable form control setting
  629. forceSetting(name, val) {
  630. WebUtil.setSetting(name, val);
  631. UI.updateSetting(name);
  632. UI.disableSetting(name);
  633. },
  634. // Update cookie and form control setting. If value is not set, then
  635. // updates from control to current cookie setting.
  636. updateSetting(name) {
  637. // Update the settings control
  638. let value = UI.getSetting(name);
  639. const ctrl = document.getElementById('noVNC_setting_' + name);
  640. if (ctrl.type === 'checkbox') {
  641. ctrl.checked = value;
  642. } else if (typeof ctrl.options !== 'undefined') {
  643. for (let i = 0; i < ctrl.options.length; i += 1) {
  644. if (ctrl.options[i].value === value) {
  645. ctrl.selectedIndex = i;
  646. break;
  647. }
  648. }
  649. } else {
  650. ctrl.value = value;
  651. }
  652. },
  653. // Save control setting to cookie
  654. saveSetting(name) {
  655. const ctrl = document.getElementById('noVNC_setting_' + name);
  656. let val;
  657. if (ctrl.type === 'checkbox') {
  658. val = ctrl.checked;
  659. } else if (typeof ctrl.options !== 'undefined') {
  660. val = ctrl.options[ctrl.selectedIndex].value;
  661. } else {
  662. val = ctrl.value;
  663. }
  664. WebUtil.writeSetting(name, val);
  665. //Log.Debug("Setting saved '" + name + "=" + val + "'");
  666. return val;
  667. },
  668. // Read form control compatible setting from cookie
  669. getSetting(name) {
  670. const ctrl = document.getElementById('noVNC_setting_' + name);
  671. let val = WebUtil.readSetting(name);
  672. if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
  673. if (val.toString().toLowerCase() in {'0': 1, 'no': 1, 'false': 1}) {
  674. val = false;
  675. } else {
  676. val = true;
  677. }
  678. }
  679. return val;
  680. },
  681. // These helpers compensate for the lack of parent-selectors and
  682. // previous-sibling-selectors in CSS which are needed when we want to
  683. // disable the labels that belong to disabled input elements.
  684. disableSetting(name) {
  685. const ctrl = document.getElementById('noVNC_setting_' + name);
  686. ctrl.disabled = true;
  687. ctrl.label.classList.add('noVNC_disabled');
  688. },
  689. enableSetting(name) {
  690. const ctrl = document.getElementById('noVNC_setting_' + name);
  691. ctrl.disabled = false;
  692. ctrl.label.classList.remove('noVNC_disabled');
  693. },
  694. /* ------^-------
  695. * /SETTINGS
  696. * ==============
  697. * PANELS
  698. * ------v------*/
  699. closeAllPanels() {
  700. UI.closeSettingsPanel();
  701. UI.closePowerPanel();
  702. UI.closeClipboardPanel();
  703. UI.closeExtraKeys();
  704. },
  705. /* ------^-------
  706. * /PANELS
  707. * ==============
  708. * SETTINGS (panel)
  709. * ------v------*/
  710. openSettingsPanel() {
  711. UI.closeAllPanels();
  712. UI.openControlbar();
  713. // Refresh UI elements from saved cookies
  714. UI.updateSetting('encrypt');
  715. UI.updateSetting('view_clip');
  716. UI.updateSetting('resize');
  717. UI.updateSetting('quality');
  718. UI.updateSetting('compression');
  719. UI.updateSetting('shared');
  720. UI.updateSetting('view_only');
  721. UI.updateSetting('path');
  722. UI.updateSetting('repeaterID');
  723. UI.updateSetting('logging');
  724. UI.updateSetting('reconnect');
  725. UI.updateSetting('reconnect_delay');
  726. document.getElementById('noVNC_settings')
  727. .classList.add("noVNC_open");
  728. document.getElementById('noVNC_settings_button')
  729. .classList.add("noVNC_selected");
  730. },
  731. closeSettingsPanel() {
  732. document.getElementById('noVNC_settings')
  733. .classList.remove("noVNC_open");
  734. document.getElementById('noVNC_settings_button')
  735. .classList.remove("noVNC_selected");
  736. },
  737. toggleSettingsPanel() {
  738. if (document.getElementById('noVNC_settings')
  739. .classList.contains("noVNC_open")) {
  740. UI.closeSettingsPanel();
  741. } else {
  742. UI.openSettingsPanel();
  743. }
  744. },
  745. /* ------^-------
  746. * /SETTINGS
  747. * ==============
  748. * POWER
  749. * ------v------*/
  750. openPowerPanel() {
  751. UI.closeAllPanels();
  752. UI.openControlbar();
  753. document.getElementById('noVNC_power')
  754. .classList.add("noVNC_open");
  755. document.getElementById('noVNC_power_button')
  756. .classList.add("noVNC_selected");
  757. },
  758. closePowerPanel() {
  759. document.getElementById('noVNC_power')
  760. .classList.remove("noVNC_open");
  761. document.getElementById('noVNC_power_button')
  762. .classList.remove("noVNC_selected");
  763. },
  764. togglePowerPanel() {
  765. if (document.getElementById('noVNC_power')
  766. .classList.contains("noVNC_open")) {
  767. UI.closePowerPanel();
  768. } else {
  769. UI.openPowerPanel();
  770. }
  771. },
  772. // Disable/enable power button
  773. updatePowerButton() {
  774. if (UI.connected &&
  775. UI.rfb.capabilities.power &&
  776. !UI.rfb.viewOnly) {
  777. document.getElementById('noVNC_power_button')
  778. .classList.remove("noVNC_hidden");
  779. } else {
  780. document.getElementById('noVNC_power_button')
  781. .classList.add("noVNC_hidden");
  782. // Close power panel if open
  783. UI.closePowerPanel();
  784. }
  785. },
  786. /* ------^-------
  787. * /POWER
  788. * ==============
  789. * CLIPBOARD
  790. * ------v------*/
  791. openClipboardPanel() {
  792. UI.closeAllPanels();
  793. UI.openControlbar();
  794. document.getElementById('noVNC_clipboard')
  795. .classList.add("noVNC_open");
  796. document.getElementById('noVNC_clipboard_button')
  797. .classList.add("noVNC_selected");
  798. },
  799. closeClipboardPanel() {
  800. document.getElementById('noVNC_clipboard')
  801. .classList.remove("noVNC_open");
  802. document.getElementById('noVNC_clipboard_button')
  803. .classList.remove("noVNC_selected");
  804. },
  805. toggleClipboardPanel() {
  806. if (document.getElementById('noVNC_clipboard')
  807. .classList.contains("noVNC_open")) {
  808. UI.closeClipboardPanel();
  809. } else {
  810. UI.openClipboardPanel();
  811. }
  812. },
  813. clipboardReceive(e) {
  814. Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0, 40) + "...");
  815. document.getElementById('noVNC_clipboard_text').value = e.detail.text;
  816. Log.Debug("<< UI.clipboardReceive");
  817. },
  818. clipboardClear() {
  819. document.getElementById('noVNC_clipboard_text').value = "";
  820. UI.rfb.clipboardPasteFrom("");
  821. },
  822. clipboardSend() {
  823. const text = document.getElementById('noVNC_clipboard_text').value;
  824. Log.Debug(">> UI.clipboardSend: " + text.substr(0, 40) + "...");
  825. UI.rfb.clipboardPasteFrom(text);
  826. Log.Debug("<< UI.clipboardSend");
  827. },
  828. /* ------^-------
  829. * /CLIPBOARD
  830. * ==============
  831. * CONNECTION
  832. * ------v------*/
  833. openConnectPanel() {
  834. document.getElementById('noVNC_connect_dlg')
  835. .classList.add("noVNC_open");
  836. },
  837. closeConnectPanel() {
  838. document.getElementById('noVNC_connect_dlg')
  839. .classList.remove("noVNC_open");
  840. },
  841. connect(event, password) {
  842. // Ignore when rfb already exists
  843. if (typeof UI.rfb !== 'undefined') {
  844. return;
  845. }
  846. const host = UI.getSetting('host');
  847. const port = UI.getSetting('port');
  848. const path = UI.getSetting('path');
  849. if (typeof password === 'undefined') {
  850. password = WebUtil.getConfigVar('password');
  851. UI.reconnectPassword = password;
  852. }
  853. if (password === null) {
  854. password = undefined;
  855. }
  856. UI.hideStatus();
  857. if (!host) {
  858. Log.Error("Can't connect when host is: " + host);
  859. UI.showStatus(_("Must set host"), 'error');
  860. return;
  861. }
  862. UI.closeConnectPanel();
  863. UI.updateVisualState('connecting');
  864. let url;
  865. url = UI.getSetting('encrypt') ? 'wss' : 'ws';
  866. url += '://' + host;
  867. if (port) {
  868. url += ':' + port;
  869. }
  870. url += '/' + path;
  871. UI.rfb = new RFB(document.getElementById('noVNC_container'), url,
  872. { shared: UI.getSetting('shared'),
  873. repeaterID: UI.getSetting('repeaterID'),
  874. credentials: { password: password } });
  875. UI.rfb.addEventListener("connect", UI.connectFinished);
  876. UI.rfb.addEventListener("disconnect", UI.disconnectFinished);
  877. UI.rfb.addEventListener("credentialsrequired", UI.credentials);
  878. UI.rfb.addEventListener("securityfailure", UI.securityFailed);
  879. UI.rfb.addEventListener("capabilities", UI.updatePowerButton);
  880. UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
  881. UI.rfb.addEventListener("bell", UI.bell);
  882. UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
  883. UI.rfb.clipViewport = UI.getSetting('view_clip');
  884. UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
  885. UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
  886. UI.rfb.qualityLevel = parseInt(UI.getSetting('quality'));
  887. UI.rfb.compressionLevel = parseInt(UI.getSetting('compression'));
  888. UI.rfb.showDotCursor = UI.getSetting('show_dot');
  889. UI.updateViewOnly(); // requires UI.rfb
  890. },
  891. disconnect() {
  892. UI.rfb.disconnect();
  893. UI.connected = false;
  894. // Disable automatic reconnecting
  895. UI.inhibitReconnect = true;
  896. UI.updateVisualState('disconnecting');
  897. // Don't display the connection settings until we're actually disconnected
  898. },
  899. reconnect() {
  900. UI.reconnectCallback = null;
  901. // if reconnect has been disabled in the meantime, do nothing.
  902. if (UI.inhibitReconnect) {
  903. return;
  904. }
  905. UI.connect(null, UI.reconnectPassword);
  906. },
  907. cancelReconnect() {
  908. if (UI.reconnectCallback !== null) {
  909. clearTimeout(UI.reconnectCallback);
  910. UI.reconnectCallback = null;
  911. }
  912. UI.updateVisualState('disconnected');
  913. UI.openControlbar();
  914. UI.openConnectPanel();
  915. },
  916. connectFinished(e) {
  917. UI.connected = true;
  918. UI.inhibitReconnect = false;
  919. let msg;
  920. if (UI.getSetting('encrypt')) {
  921. msg = _("Connected (encrypted) to ") + UI.desktopName;
  922. } else {
  923. msg = _("Connected (unencrypted) to ") + UI.desktopName;
  924. }
  925. UI.showStatus(msg);
  926. UI.updateVisualState('connected');
  927. // Do this last because it can only be used on rendered elements
  928. UI.rfb.focus();
  929. },
  930. disconnectFinished(e) {
  931. const wasConnected = UI.connected;
  932. // This variable is ideally set when disconnection starts, but
  933. // when the disconnection isn't clean or if it is initiated by
  934. // the server, we need to do it here as well since
  935. // UI.disconnect() won't be used in those cases.
  936. UI.connected = false;
  937. UI.rfb = undefined;
  938. if (!e.detail.clean) {
  939. UI.updateVisualState('disconnected');
  940. if (wasConnected) {
  941. UI.showStatus(_("Something went wrong, connection is closed"),
  942. 'error');
  943. } else {
  944. UI.showStatus(_("Failed to connect to server"), 'error');
  945. }
  946. } else if (UI.getSetting('reconnect', false) === true && !UI.inhibitReconnect) {
  947. UI.updateVisualState('reconnecting');
  948. const delay = parseInt(UI.getSetting('reconnect_delay'));
  949. UI.reconnectCallback = setTimeout(UI.reconnect, delay);
  950. return;
  951. } else {
  952. UI.updateVisualState('disconnected');
  953. UI.showStatus(_("Disconnected"), 'normal');
  954. }
  955. document.title = PAGE_TITLE;
  956. UI.openControlbar();
  957. UI.openConnectPanel();
  958. },
  959. securityFailed(e) {
  960. let msg = "";
  961. // On security failures we might get a string with a reason
  962. // directly from the server. Note that we can't control if
  963. // this string is translated or not.
  964. if ('reason' in e.detail) {
  965. msg = _("New connection has been rejected with reason: ") +
  966. e.detail.reason;
  967. } else {
  968. msg = _("New connection has been rejected");
  969. }
  970. UI.showStatus(msg, 'error');
  971. },
  972. /* ------^-------
  973. * /CONNECTION
  974. * ==============
  975. * PASSWORD
  976. * ------v------*/
  977. credentials(e) {
  978. // FIXME: handle more types
  979. document.getElementById("noVNC_username_block").classList.remove("noVNC_hidden");
  980. document.getElementById("noVNC_password_block").classList.remove("noVNC_hidden");
  981. let inputFocus = "none";
  982. if (e.detail.types.indexOf("username") === -1) {
  983. document.getElementById("noVNC_username_block").classList.add("noVNC_hidden");
  984. } else {
  985. inputFocus = inputFocus === "none" ? "noVNC_username_input" : inputFocus;
  986. }
  987. if (e.detail.types.indexOf("password") === -1) {
  988. document.getElementById("noVNC_password_block").classList.add("noVNC_hidden");
  989. } else {
  990. inputFocus = inputFocus === "none" ? "noVNC_password_input" : inputFocus;
  991. }
  992. document.getElementById('noVNC_credentials_dlg')
  993. .classList.add('noVNC_open');
  994. setTimeout(() => document
  995. .getElementById(inputFocus).focus(), 100);
  996. Log.Warn("Server asked for credentials");
  997. UI.showStatus(_("Credentials are required"), "warning");
  998. },
  999. setCredentials(e) {
  1000. // Prevent actually submitting the form
  1001. e.preventDefault();
  1002. let inputElemUsername = document.getElementById('noVNC_username_input');
  1003. const username = inputElemUsername.value;
  1004. let inputElemPassword = document.getElementById('noVNC_password_input');
  1005. const password = inputElemPassword.value;
  1006. // Clear the input after reading the password
  1007. inputElemPassword.value = "";
  1008. UI.rfb.sendCredentials({ username: username, password: password });
  1009. UI.reconnectPassword = password;
  1010. document.getElementById('noVNC_credentials_dlg')
  1011. .classList.remove('noVNC_open');
  1012. },
  1013. /* ------^-------
  1014. * /PASSWORD
  1015. * ==============
  1016. * FULLSCREEN
  1017. * ------v------*/
  1018. toggleFullscreen() {
  1019. if (document.fullscreenElement || // alternative standard method
  1020. document.mozFullScreenElement || // currently working methods
  1021. document.webkitFullscreenElement ||
  1022. document.msFullscreenElement) {
  1023. if (document.exitFullscreen) {
  1024. document.exitFullscreen();
  1025. } else if (document.mozCancelFullScreen) {
  1026. document.mozCancelFullScreen();
  1027. } else if (document.webkitExitFullscreen) {
  1028. document.webkitExitFullscreen();
  1029. } else if (document.msExitFullscreen) {
  1030. document.msExitFullscreen();
  1031. }
  1032. } else {
  1033. if (document.documentElement.requestFullscreen) {
  1034. document.documentElement.requestFullscreen();
  1035. } else if (document.documentElement.mozRequestFullScreen) {
  1036. document.documentElement.mozRequestFullScreen();
  1037. } else if (document.documentElement.webkitRequestFullscreen) {
  1038. document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  1039. } else if (document.body.msRequestFullscreen) {
  1040. document.body.msRequestFullscreen();
  1041. }
  1042. }
  1043. UI.updateFullscreenButton();
  1044. },
  1045. updateFullscreenButton() {
  1046. if (document.fullscreenElement || // alternative standard method
  1047. document.mozFullScreenElement || // currently working methods
  1048. document.webkitFullscreenElement ||
  1049. document.msFullscreenElement ) {
  1050. document.getElementById('noVNC_fullscreen_button')
  1051. .classList.add("noVNC_selected");
  1052. } else {
  1053. document.getElementById('noVNC_fullscreen_button')
  1054. .classList.remove("noVNC_selected");
  1055. }
  1056. },
  1057. /* ------^-------
  1058. * /FULLSCREEN
  1059. * ==============
  1060. * RESIZE
  1061. * ------v------*/
  1062. // Apply remote resizing or local scaling
  1063. applyResizeMode() {
  1064. if (!UI.rfb) return;
  1065. UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
  1066. UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
  1067. },
  1068. /* ------^-------
  1069. * /RESIZE
  1070. * ==============
  1071. * VIEW CLIPPING
  1072. * ------v------*/
  1073. // Update viewport clipping property for the connection. The normal
  1074. // case is to get the value from the setting. There are special cases
  1075. // for when the viewport is scaled or when a touch device is used.
  1076. updateViewClip() {
  1077. if (!UI.rfb) return;
  1078. const scaling = UI.getSetting('resize') === 'scale';
  1079. if (scaling) {
  1080. // Can't be clipping if viewport is scaled to fit
  1081. UI.forceSetting('view_clip', false);
  1082. UI.rfb.clipViewport = false;
  1083. } else if (!hasScrollbarGutter) {
  1084. // Some platforms have scrollbars that are difficult
  1085. // to use in our case, so we always use our own panning
  1086. UI.forceSetting('view_clip', true);
  1087. UI.rfb.clipViewport = true;
  1088. } else {
  1089. UI.enableSetting('view_clip');
  1090. UI.rfb.clipViewport = UI.getSetting('view_clip');
  1091. }
  1092. // Changing the viewport may change the state of
  1093. // the dragging button
  1094. UI.updateViewDrag();
  1095. },
  1096. /* ------^-------
  1097. * /VIEW CLIPPING
  1098. * ==============
  1099. * VIEWDRAG
  1100. * ------v------*/
  1101. toggleViewDrag() {
  1102. if (!UI.rfb) return;
  1103. UI.rfb.dragViewport = !UI.rfb.dragViewport;
  1104. UI.updateViewDrag();
  1105. },
  1106. updateViewDrag() {
  1107. if (!UI.connected) return;
  1108. const viewDragButton = document.getElementById('noVNC_view_drag_button');
  1109. if (!UI.rfb.clipViewport && UI.rfb.dragViewport) {
  1110. // We are no longer clipping the viewport. Make sure
  1111. // viewport drag isn't active when it can't be used.
  1112. UI.rfb.dragViewport = false;
  1113. }
  1114. if (UI.rfb.dragViewport) {
  1115. viewDragButton.classList.add("noVNC_selected");
  1116. } else {
  1117. viewDragButton.classList.remove("noVNC_selected");
  1118. }
  1119. if (UI.rfb.clipViewport) {
  1120. viewDragButton.classList.remove("noVNC_hidden");
  1121. } else {
  1122. viewDragButton.classList.add("noVNC_hidden");
  1123. }
  1124. },
  1125. /* ------^-------
  1126. * /VIEWDRAG
  1127. * ==============
  1128. * QUALITY
  1129. * ------v------*/
  1130. updateQuality() {
  1131. if (!UI.rfb) return;
  1132. UI.rfb.qualityLevel = parseInt(UI.getSetting('quality'));
  1133. },
  1134. /* ------^-------
  1135. * /QUALITY
  1136. * ==============
  1137. * COMPRESSION
  1138. * ------v------*/
  1139. updateCompression() {
  1140. if (!UI.rfb) return;
  1141. UI.rfb.compressionLevel = parseInt(UI.getSetting('compression'));
  1142. },
  1143. /* ------^-------
  1144. * /COMPRESSION
  1145. * ==============
  1146. * KEYBOARD
  1147. * ------v------*/
  1148. showVirtualKeyboard() {
  1149. if (!isTouchDevice) return;
  1150. const input = document.getElementById('noVNC_keyboardinput');
  1151. if (document.activeElement == input) return;
  1152. input.focus();
  1153. try {
  1154. const l = input.value.length;
  1155. // Move the caret to the end
  1156. input.setSelectionRange(l, l);
  1157. } catch (err) {
  1158. // setSelectionRange is undefined in Google Chrome
  1159. }
  1160. },
  1161. hideVirtualKeyboard() {
  1162. if (!isTouchDevice) return;
  1163. const input = document.getElementById('noVNC_keyboardinput');
  1164. if (document.activeElement != input) return;
  1165. input.blur();
  1166. },
  1167. toggleVirtualKeyboard() {
  1168. if (document.getElementById('noVNC_keyboard_button')
  1169. .classList.contains("noVNC_selected")) {
  1170. UI.hideVirtualKeyboard();
  1171. } else {
  1172. UI.showVirtualKeyboard();
  1173. }
  1174. },
  1175. onfocusVirtualKeyboard(event) {
  1176. document.getElementById('noVNC_keyboard_button')
  1177. .classList.add("noVNC_selected");
  1178. if (UI.rfb) {
  1179. UI.rfb.focusOnClick = false;
  1180. }
  1181. },
  1182. onblurVirtualKeyboard(event) {
  1183. document.getElementById('noVNC_keyboard_button')
  1184. .classList.remove("noVNC_selected");
  1185. if (UI.rfb) {
  1186. UI.rfb.focusOnClick = true;
  1187. }
  1188. },
  1189. keepVirtualKeyboard(event) {
  1190. const input = document.getElementById('noVNC_keyboardinput');
  1191. // Only prevent focus change if the virtual keyboard is active
  1192. if (document.activeElement != input) {
  1193. return;
  1194. }
  1195. // Only allow focus to move to other elements that need
  1196. // focus to function properly
  1197. if (event.target.form !== undefined) {
  1198. switch (event.target.type) {
  1199. case 'text':
  1200. case 'email':
  1201. case 'search':
  1202. case 'password':
  1203. case 'tel':
  1204. case 'url':
  1205. case 'textarea':
  1206. case 'select-one':
  1207. case 'select-multiple':
  1208. return;
  1209. }
  1210. }
  1211. event.preventDefault();
  1212. },
  1213. keyboardinputReset() {
  1214. const kbi = document.getElementById('noVNC_keyboardinput');
  1215. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  1216. UI.lastKeyboardinput = kbi.value;
  1217. },
  1218. keyEvent(keysym, code, down) {
  1219. if (!UI.rfb) return;
  1220. UI.rfb.sendKey(keysym, code, down);
  1221. },
  1222. // When normal keyboard events are left uncought, use the input events from
  1223. // the keyboardinput element instead and generate the corresponding key events.
  1224. // This code is required since some browsers on Android are inconsistent in
  1225. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  1226. keyInput(event) {
  1227. if (!UI.rfb) return;
  1228. const newValue = event.target.value;
  1229. if (!UI.lastKeyboardinput) {
  1230. UI.keyboardinputReset();
  1231. }
  1232. const oldValue = UI.lastKeyboardinput;
  1233. let newLen;
  1234. try {
  1235. // Try to check caret position since whitespace at the end
  1236. // will not be considered by value.length in some browsers
  1237. newLen = Math.max(event.target.selectionStart, newValue.length);
  1238. } catch (err) {
  1239. // selectionStart is undefined in Google Chrome
  1240. newLen = newValue.length;
  1241. }
  1242. const oldLen = oldValue.length;
  1243. let inputs = newLen - oldLen;
  1244. let backspaces = inputs < 0 ? -inputs : 0;
  1245. // Compare the old string with the new to account for
  1246. // text-corrections or other input that modify existing text
  1247. for (let i = 0; i < Math.min(oldLen, newLen); i++) {
  1248. if (newValue.charAt(i) != oldValue.charAt(i)) {
  1249. inputs = newLen - i;
  1250. backspaces = oldLen - i;
  1251. break;
  1252. }
  1253. }
  1254. // Send the key events
  1255. for (let i = 0; i < backspaces; i++) {
  1256. UI.rfb.sendKey(KeyTable.XK_BackSpace, "Backspace");
  1257. }
  1258. for (let i = newLen - inputs; i < newLen; i++) {
  1259. UI.rfb.sendKey(keysyms.lookup(newValue.charCodeAt(i)));
  1260. }
  1261. // Control the text content length in the keyboardinput element
  1262. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  1263. UI.keyboardinputReset();
  1264. } else if (newLen < 1) {
  1265. // There always have to be some text in the keyboardinput
  1266. // element with which backspace can interact.
  1267. UI.keyboardinputReset();
  1268. // This sometimes causes the keyboard to disappear for a second
  1269. // but it is required for the android keyboard to recognize that
  1270. // text has been added to the field
  1271. event.target.blur();
  1272. // This has to be ran outside of the input handler in order to work
  1273. setTimeout(event.target.focus.bind(event.target), 0);
  1274. } else {
  1275. UI.lastKeyboardinput = newValue;
  1276. }
  1277. },
  1278. /* ------^-------
  1279. * /KEYBOARD
  1280. * ==============
  1281. * EXTRA KEYS
  1282. * ------v------*/
  1283. openExtraKeys() {
  1284. UI.closeAllPanels();
  1285. UI.openControlbar();
  1286. document.getElementById('noVNC_modifiers')
  1287. .classList.add("noVNC_open");
  1288. document.getElementById('noVNC_toggle_extra_keys_button')
  1289. .classList.add("noVNC_selected");
  1290. },
  1291. closeExtraKeys() {
  1292. document.getElementById('noVNC_modifiers')
  1293. .classList.remove("noVNC_open");
  1294. document.getElementById('noVNC_toggle_extra_keys_button')
  1295. .classList.remove("noVNC_selected");
  1296. },
  1297. toggleExtraKeys() {
  1298. if (document.getElementById('noVNC_modifiers')
  1299. .classList.contains("noVNC_open")) {
  1300. UI.closeExtraKeys();
  1301. } else {
  1302. UI.openExtraKeys();
  1303. }
  1304. },
  1305. sendEsc() {
  1306. UI.sendKey(KeyTable.XK_Escape, "Escape");
  1307. },
  1308. sendTab() {
  1309. UI.sendKey(KeyTable.XK_Tab, "Tab");
  1310. },
  1311. toggleCtrl() {
  1312. const btn = document.getElementById('noVNC_toggle_ctrl_button');
  1313. if (btn.classList.contains("noVNC_selected")) {
  1314. UI.sendKey(KeyTable.XK_Control_L, "ControlLeft", false);
  1315. btn.classList.remove("noVNC_selected");
  1316. } else {
  1317. UI.sendKey(KeyTable.XK_Control_L, "ControlLeft", true);
  1318. btn.classList.add("noVNC_selected");
  1319. }
  1320. },
  1321. toggleWindows() {
  1322. const btn = document.getElementById('noVNC_toggle_windows_button');
  1323. if (btn.classList.contains("noVNC_selected")) {
  1324. UI.sendKey(KeyTable.XK_Super_L, "MetaLeft", false);
  1325. btn.classList.remove("noVNC_selected");
  1326. } else {
  1327. UI.sendKey(KeyTable.XK_Super_L, "MetaLeft", true);
  1328. btn.classList.add("noVNC_selected");
  1329. }
  1330. },
  1331. toggleAlt() {
  1332. const btn = document.getElementById('noVNC_toggle_alt_button');
  1333. if (btn.classList.contains("noVNC_selected")) {
  1334. UI.sendKey(KeyTable.XK_Alt_L, "AltLeft", false);
  1335. btn.classList.remove("noVNC_selected");
  1336. } else {
  1337. UI.sendKey(KeyTable.XK_Alt_L, "AltLeft", true);
  1338. btn.classList.add("noVNC_selected");
  1339. }
  1340. },
  1341. sendCtrlAltDel() {
  1342. UI.rfb.sendCtrlAltDel();
  1343. // See below
  1344. UI.rfb.focus();
  1345. UI.idleControlbar();
  1346. },
  1347. sendKey(keysym, code, down) {
  1348. UI.rfb.sendKey(keysym, code, down);
  1349. // Move focus to the screen in order to be able to use the
  1350. // keyboard right after these extra keys.
  1351. // The exception is when a virtual keyboard is used, because
  1352. // if we focus the screen the virtual keyboard would be closed.
  1353. // In this case we focus our special virtual keyboard input
  1354. // element instead.
  1355. if (document.getElementById('noVNC_keyboard_button')
  1356. .classList.contains("noVNC_selected")) {
  1357. document.getElementById('noVNC_keyboardinput').focus();
  1358. } else {
  1359. UI.rfb.focus();
  1360. }
  1361. // fade out the controlbar to highlight that
  1362. // the focus has been moved to the screen
  1363. UI.idleControlbar();
  1364. },
  1365. /* ------^-------
  1366. * /EXTRA KEYS
  1367. * ==============
  1368. * MISC
  1369. * ------v------*/
  1370. updateViewOnly() {
  1371. if (!UI.rfb) return;
  1372. UI.rfb.viewOnly = UI.getSetting('view_only');
  1373. // Hide input related buttons in view only mode
  1374. if (UI.rfb.viewOnly) {
  1375. document.getElementById('noVNC_keyboard_button')
  1376. .classList.add('noVNC_hidden');
  1377. document.getElementById('noVNC_toggle_extra_keys_button')
  1378. .classList.add('noVNC_hidden');
  1379. document.getElementById('noVNC_clipboard_button')
  1380. .classList.add('noVNC_hidden');
  1381. } else {
  1382. document.getElementById('noVNC_keyboard_button')
  1383. .classList.remove('noVNC_hidden');
  1384. document.getElementById('noVNC_toggle_extra_keys_button')
  1385. .classList.remove('noVNC_hidden');
  1386. document.getElementById('noVNC_clipboard_button')
  1387. .classList.remove('noVNC_hidden');
  1388. }
  1389. },
  1390. updateShowDotCursor() {
  1391. if (!UI.rfb) return;
  1392. UI.rfb.showDotCursor = UI.getSetting('show_dot');
  1393. },
  1394. updateLogging() {
  1395. WebUtil.initLogging(UI.getSetting('logging'));
  1396. },
  1397. updateDesktopName(e) {
  1398. UI.desktopName = e.detail.name;
  1399. // Display the desktop name in the document title
  1400. document.title = e.detail.name + " - " + PAGE_TITLE;
  1401. },
  1402. bell(e) {
  1403. if (WebUtil.getConfigVar('bell', 'on') === 'on') {
  1404. const promise = document.getElementById('noVNC_bell').play();
  1405. // The standards disagree on the return value here
  1406. if (promise) {
  1407. promise.catch((e) => {
  1408. if (e.name === "NotAllowedError") {
  1409. // Ignore when the browser doesn't let us play audio.
  1410. // It is common that the browsers require audio to be
  1411. // initiated from a user action.
  1412. } else {
  1413. Log.Error("Unable to play bell: " + e);
  1414. }
  1415. });
  1416. }
  1417. }
  1418. },
  1419. //Helper to add options to dropdown.
  1420. addOption(selectbox, text, value) {
  1421. const optn = document.createElement("OPTION");
  1422. optn.text = text;
  1423. optn.value = value;
  1424. selectbox.options.add(optn);
  1425. },
  1426. /* ------^-------
  1427. * /MISC
  1428. * ==============
  1429. */
  1430. };
  1431. // Set up translations
  1432. const LINGUAS = ["cs", "de", "el", "es", "fr", "ja", "ko", "nl", "pl", "pt_BR", "ru", "sv", "tr", "zh_CN", "zh_TW"];
  1433. l10n.setup(LINGUAS);
  1434. if (l10n.language === "en" || l10n.dictionary !== undefined) {
  1435. UI.prime();
  1436. } else {
  1437. fetch('app/locale/' + l10n.language + '.json')
  1438. .then((response) => {
  1439. if (!response.ok) {
  1440. throw Error("" + response.status + " " + response.statusText);
  1441. }
  1442. return response.json();
  1443. })
  1444. .then((translations) => { l10n.dictionary = translations; })
  1445. .catch(err => Log.Error("Failed to load translations: " + err))
  1446. .then(UI.prime);
  1447. }
  1448. export default UI;