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