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