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