]>
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 | |
0b903af2 | 435 | // State change closes the password dialog |
6d6f0db0 SR |
436 | document.getElementById('noVNC_password_dlg') |
437 | .classList.remove('noVNC_open'); | |
6d6f0db0 | 438 | }, |
29475d77 | 439 | |
0e4808bf | 440 | showStatus(text, status_type, time) { |
2b5f94fa | 441 | const statusElem = document.getElementById('noVNC_status'); |
8a7ec6ea | 442 | |
6d6f0db0 | 443 | clearTimeout(UI.statusTimeout); |
29475d77 | 444 | |
6d6f0db0 SR |
445 | if (typeof status_type === 'undefined') { |
446 | status_type = 'normal'; | |
447 | } | |
4e471b5b | 448 | |
d623a029 SM |
449 | // Don't overwrite more severe visible statuses and never |
450 | // errors. Only shows the first error. | |
451 | let visible_status_type = 'none'; | |
452 | if (statusElem.classList.contains("noVNC_open")) { | |
453 | if (statusElem.classList.contains("noVNC_status_error")) { | |
454 | visible_status_type = 'error'; | |
455 | } else if (statusElem.classList.contains("noVNC_status_warn")) { | |
456 | visible_status_type = 'warn'; | |
457 | } else { | |
458 | visible_status_type = 'normal'; | |
459 | } | |
460 | } | |
461 | if (visible_status_type === 'error' || | |
462 | (visible_status_type === 'warn' && status_type === 'normal')) { | |
463 | return; | |
464 | } | |
6d6f0db0 SR |
465 | |
466 | switch (status_type) { | |
d623a029 SM |
467 | case 'error': |
468 | statusElem.classList.remove("noVNC_status_warn"); | |
469 | statusElem.classList.remove("noVNC_status_normal"); | |
470 | statusElem.classList.add("noVNC_status_error"); | |
471 | break; | |
6d6f0db0 SR |
472 | case 'warning': |
473 | case 'warn': | |
d623a029 SM |
474 | statusElem.classList.remove("noVNC_status_error"); |
475 | statusElem.classList.remove("noVNC_status_normal"); | |
6d6f0db0 SR |
476 | statusElem.classList.add("noVNC_status_warn"); |
477 | break; | |
6d6f0db0 SR |
478 | case 'normal': |
479 | case 'info': | |
480 | default: | |
d623a029 SM |
481 | statusElem.classList.remove("noVNC_status_error"); |
482 | statusElem.classList.remove("noVNC_status_warn"); | |
6d6f0db0 SR |
483 | statusElem.classList.add("noVNC_status_normal"); |
484 | break; | |
485 | } | |
8d7708c8 | 486 | |
6d6f0db0 SR |
487 | statusElem.textContent = text; |
488 | statusElem.classList.add("noVNC_open"); | |
8d7708c8 | 489 | |
6d6f0db0 SR |
490 | // If no time was specified, show the status for 1.5 seconds |
491 | if (typeof time === 'undefined') { | |
492 | time = 1500; | |
493 | } | |
8d7708c8 | 494 | |
6d6f0db0 SR |
495 | // Error messages do not timeout |
496 | if (status_type !== 'error') { | |
497 | UI.statusTimeout = window.setTimeout(UI.hideStatus, time); | |
498 | } | |
499 | }, | |
500 | ||
0e4808bf | 501 | hideStatus() { |
6d6f0db0 SR |
502 | clearTimeout(UI.statusTimeout); |
503 | document.getElementById('noVNC_status').classList.remove("noVNC_open"); | |
504 | }, | |
505 | ||
0e4808bf | 506 | activateControlbar(event) { |
6d6f0db0 SR |
507 | clearTimeout(UI.idleControlbarTimeout); |
508 | // We manipulate the anchor instead of the actual control | |
509 | // bar in order to avoid creating new a stacking group | |
510 | document.getElementById('noVNC_control_bar_anchor') | |
511 | .classList.remove("noVNC_idle"); | |
512 | UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000); | |
513 | }, | |
514 | ||
0e4808bf | 515 | idleControlbar() { |
6d6f0db0 SR |
516 | document.getElementById('noVNC_control_bar_anchor') |
517 | .classList.add("noVNC_idle"); | |
518 | }, | |
519 | ||
0e4808bf | 520 | keepControlbar() { |
6d6f0db0 SR |
521 | clearTimeout(UI.closeControlbarTimeout); |
522 | }, | |
523 | ||
0e4808bf | 524 | openControlbar() { |
6d6f0db0 SR |
525 | document.getElementById('noVNC_control_bar') |
526 | .classList.add("noVNC_open"); | |
527 | }, | |
528 | ||
0e4808bf | 529 | closeControlbar() { |
6d6f0db0 SR |
530 | UI.closeAllPanels(); |
531 | document.getElementById('noVNC_control_bar') | |
532 | .classList.remove("noVNC_open"); | |
533 | }, | |
534 | ||
0e4808bf | 535 | toggleControlbar() { |
6d6f0db0 SR |
536 | if (document.getElementById('noVNC_control_bar') |
537 | .classList.contains("noVNC_open")) { | |
538 | UI.closeControlbar(); | |
539 | } else { | |
540 | UI.openControlbar(); | |
541 | } | |
542 | }, | |
543 | ||
0e4808bf | 544 | toggleControlbarSide() { |
06309160 SM |
545 | // Temporarily disable animation, if bar is displayed, to avoid weird |
546 | // movement. The transitionend-event will not fire when display=none. | |
2b5f94fa JD |
547 | const bar = document.getElementById('noVNC_control_bar'); |
548 | const barDisplayStyle = window.getComputedStyle(bar).display; | |
06309160 SM |
549 | if (barDisplayStyle !== 'none') { |
550 | bar.style.transitionDuration = '0s'; | |
651c23ec | 551 | bar.addEventListener('transitionend', () => bar.style.transitionDuration = ''); |
06309160 | 552 | } |
6d6f0db0 | 553 | |
2b5f94fa | 554 | const anchor = document.getElementById('noVNC_control_bar_anchor'); |
6d6f0db0 SR |
555 | if (anchor.classList.contains("noVNC_right")) { |
556 | WebUtil.writeSetting('controlbar_pos', 'left'); | |
557 | anchor.classList.remove("noVNC_right"); | |
558 | } else { | |
559 | WebUtil.writeSetting('controlbar_pos', 'right'); | |
560 | anchor.classList.add("noVNC_right"); | |
561 | } | |
ca5c74ad | 562 | |
6d6f0db0 SR |
563 | // Consider this a movement of the handle |
564 | UI.controlbarDrag = true; | |
565 | }, | |
4e471b5b | 566 | |
0e4808bf | 567 | showControlbarHint(show) { |
2b5f94fa | 568 | const hint = document.getElementById('noVNC_control_bar_hint'); |
bbc1648c SM |
569 | if (show) { |
570 | hint.classList.add("noVNC_active"); | |
571 | } else { | |
572 | hint.classList.remove("noVNC_active"); | |
573 | } | |
574 | }, | |
575 | ||
0e4808bf | 576 | dragControlbarHandle(e) { |
6d6f0db0 | 577 | if (!UI.controlbarGrabbed) return; |
38323d4d | 578 | |
2b5f94fa | 579 | const ptr = getPointerEvent(e); |
8ee432f1 | 580 | |
2b5f94fa | 581 | const anchor = document.getElementById('noVNC_control_bar_anchor'); |
6d6f0db0 | 582 | if (ptr.clientX < (window.innerWidth * 0.1)) { |
cf348b78 | 583 | if (anchor.classList.contains("noVNC_right")) { |
6d6f0db0 | 584 | UI.toggleControlbarSide(); |
8ee432f1 | 585 | } |
6d6f0db0 SR |
586 | } else if (ptr.clientX > (window.innerWidth * 0.9)) { |
587 | if (!anchor.classList.contains("noVNC_right")) { | |
588 | UI.toggleControlbarSide(); | |
04b399e2 | 589 | } |
6d6f0db0 | 590 | } |
04b399e2 | 591 | |
6d6f0db0 SR |
592 | if (!UI.controlbarDrag) { |
593 | // The goal is to trigger on a certain physical width, the | |
594 | // devicePixelRatio brings us a bit closer but is not optimal. | |
2b5f94fa JD |
595 | const dragThreshold = 10 * (window.devicePixelRatio || 1); |
596 | const dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY); | |
04b399e2 | 597 | |
6d6f0db0 | 598 | if (dragDistance < dragThreshold) return; |
04b399e2 | 599 | |
6d6f0db0 SR |
600 | UI.controlbarDrag = true; |
601 | } | |
f75e4d3c | 602 | |
2b5f94fa | 603 | const eventY = ptr.clientY - UI.controlbarMouseDownOffsetY; |
04b399e2 | 604 | |
6d6f0db0 | 605 | UI.moveControlbarHandle(eventY); |
59cd99bc | 606 | |
6d6f0db0 SR |
607 | e.preventDefault(); |
608 | e.stopPropagation(); | |
609 | UI.keepControlbar(); | |
610 | UI.activateControlbar(); | |
611 | }, | |
04b399e2 | 612 | |
6d6f0db0 | 613 | // Move the handle but don't allow any position outside the bounds |
0e4808bf | 614 | moveControlbarHandle(viewportRelativeY) { |
2b5f94fa JD |
615 | const handle = document.getElementById("noVNC_control_bar_handle"); |
616 | const handleHeight = handle.getBoundingClientRect().height; | |
617 | const controlbarBounds = document.getElementById("noVNC_control_bar") | |
6d6f0db0 | 618 | .getBoundingClientRect(); |
2b5f94fa | 619 | const margin = 10; |
04b399e2 | 620 | |
6d6f0db0 SR |
621 | // These heights need to be non-zero for the below logic to work |
622 | if (handleHeight === 0 || controlbarBounds.height === 0) { | |
623 | return; | |
624 | } | |
04b399e2 | 625 | |
2b5f94fa | 626 | let newY = viewportRelativeY; |
04b399e2 | 627 | |
6d6f0db0 SR |
628 | // Check if the coordinates are outside the control bar |
629 | if (newY < controlbarBounds.top + margin) { | |
630 | // Force coordinates to be below the top of the control bar | |
631 | newY = controlbarBounds.top + margin; | |
04b399e2 | 632 | |
6d6f0db0 SR |
633 | } else if (newY > controlbarBounds.top + |
634 | controlbarBounds.height - handleHeight - margin) { | |
635 | // Force coordinates to be above the bottom of the control bar | |
636 | newY = controlbarBounds.top + | |
637 | controlbarBounds.height - handleHeight - margin; | |
638 | } | |
04b399e2 | 639 | |
6d6f0db0 SR |
640 | // Corner case: control bar too small for stable position |
641 | if (controlbarBounds.height < (handleHeight + margin * 2)) { | |
642 | newY = controlbarBounds.top + | |
643 | (controlbarBounds.height - handleHeight) / 2; | |
644 | } | |
04b399e2 | 645 | |
6d6f0db0 | 646 | // The transform needs coordinates that are relative to the parent |
2b5f94fa | 647 | const parentRelativeY = newY - controlbarBounds.top; |
6d6f0db0 SR |
648 | handle.style.transform = "translateY(" + parentRelativeY + "px)"; |
649 | }, | |
650 | ||
0e4808bf | 651 | updateControlbarHandle() { |
6d6f0db0 SR |
652 | // Since the control bar is fixed on the viewport and not the page, |
653 | // the move function expects coordinates relative the the viewport. | |
2b5f94fa JD |
654 | const handle = document.getElementById("noVNC_control_bar_handle"); |
655 | const handleBounds = handle.getBoundingClientRect(); | |
6d6f0db0 SR |
656 | UI.moveControlbarHandle(handleBounds.top); |
657 | }, | |
658 | ||
0e4808bf | 659 | controlbarHandleMouseUp(e) { |
6d6f0db0 SR |
660 | if ((e.type == "mouseup") && (e.button != 0)) return; |
661 | ||
662 | // mouseup and mousedown on the same place toggles the controlbar | |
663 | if (UI.controlbarGrabbed && !UI.controlbarDrag) { | |
664 | UI.toggleControlbar(); | |
04b399e2 SM |
665 | e.preventDefault(); |
666 | e.stopPropagation(); | |
de315d62 PO |
667 | UI.keepControlbar(); |
668 | UI.activateControlbar(); | |
6d6f0db0 SR |
669 | } |
670 | UI.controlbarGrabbed = false; | |
bbc1648c | 671 | UI.showControlbarHint(false); |
6d6f0db0 SR |
672 | }, |
673 | ||
0e4808bf | 674 | controlbarHandleMouseDown(e) { |
6d6f0db0 SR |
675 | if ((e.type == "mousedown") && (e.button != 0)) return; |
676 | ||
2b5f94fa | 677 | const ptr = getPointerEvent(e); |
6d6f0db0 | 678 | |
2b5f94fa JD |
679 | const handle = document.getElementById("noVNC_control_bar_handle"); |
680 | const bounds = handle.getBoundingClientRect(); | |
6d6f0db0 | 681 | |
333ad45c SM |
682 | // Touch events have implicit capture |
683 | if (e.type === "mousedown") { | |
684 | setCapture(handle); | |
685 | } | |
686 | ||
6d6f0db0 SR |
687 | UI.controlbarGrabbed = true; |
688 | UI.controlbarDrag = false; | |
689 | ||
bbc1648c SM |
690 | UI.showControlbarHint(true); |
691 | ||
6d6f0db0 SR |
692 | UI.controlbarMouseDownClientY = ptr.clientY; |
693 | UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top; | |
694 | e.preventDefault(); | |
695 | e.stopPropagation(); | |
696 | UI.keepControlbar(); | |
697 | UI.activateControlbar(); | |
698 | }, | |
699 | ||
0e4808bf | 700 | toggleExpander(e) { |
6d6f0db0 SR |
701 | if (this.classList.contains("noVNC_open")) { |
702 | this.classList.remove("noVNC_open"); | |
703 | } else { | |
704 | this.classList.add("noVNC_open"); | |
705 | } | |
706 | }, | |
575f6983 | 707 | |
95dd6001 | 708 | /* ------^------- |
709 | * /VISUAL | |
710 | * ============== | |
711 | * SETTINGS | |
712 | * ------v------*/ | |
713 | ||
6d6f0db0 | 714 | // Initial page load read/initialization of settings |
0e4808bf | 715 | initSetting(name, defVal) { |
6d6f0db0 | 716 | // Check Query string followed by cookie |
2b5f94fa | 717 | let val = WebUtil.getConfigVar(name); |
6d6f0db0 SR |
718 | if (val === null) { |
719 | val = WebUtil.readSetting(name, defVal); | |
720 | } | |
8ad8f15c AW |
721 | WebUtil.setSetting(name, val); |
722 | UI.updateSetting(name); | |
6d6f0db0 SR |
723 | return val; |
724 | }, | |
bbbf42bb | 725 | |
11715f20 | 726 | // Set the new value, update and disable form control setting |
0e4808bf | 727 | forceSetting(name, val) { |
11715f20 SM |
728 | WebUtil.setSetting(name, val); |
729 | UI.updateSetting(name); | |
730 | UI.disableSetting(name); | |
731 | }, | |
732 | ||
6d6f0db0 SR |
733 | // Update cookie and form control setting. If value is not set, then |
734 | // updates from control to current cookie setting. | |
0e4808bf | 735 | updateSetting(name) { |
bbbf42bb | 736 | |
6d6f0db0 | 737 | // Update the settings control |
2b5f94fa | 738 | let value = UI.getSetting(name); |
bbbf42bb | 739 | |
2b5f94fa | 740 | const ctrl = document.getElementById('noVNC_setting_' + name); |
6d6f0db0 SR |
741 | if (ctrl.type === 'checkbox') { |
742 | ctrl.checked = value; | |
bbbf42bb | 743 | |
6d6f0db0 | 744 | } else if (typeof ctrl.options !== 'undefined') { |
2b5f94fa | 745 | for (let i = 0; i < ctrl.options.length; i += 1) { |
6d6f0db0 SR |
746 | if (ctrl.options[i].value === value) { |
747 | ctrl.selectedIndex = i; | |
748 | break; | |
bbbf42bb | 749 | } |
bbbf42bb | 750 | } |
6d6f0db0 SR |
751 | } else { |
752 | /*Weird IE9 error leads to 'null' appearring | |
753 | in textboxes instead of ''.*/ | |
754 | if (value === null) { | |
755 | value = ""; | |
bbbf42bb | 756 | } |
6d6f0db0 SR |
757 | ctrl.value = value; |
758 | } | |
759 | }, | |
760 | ||
761 | // Save control setting to cookie | |
0e4808bf | 762 | saveSetting(name) { |
2b5f94fa JD |
763 | const ctrl = document.getElementById('noVNC_setting_' + name); |
764 | let val; | |
6d6f0db0 SR |
765 | if (ctrl.type === 'checkbox') { |
766 | val = ctrl.checked; | |
767 | } else if (typeof ctrl.options !== 'undefined') { | |
768 | val = ctrl.options[ctrl.selectedIndex].value; | |
769 | } else { | |
770 | val = ctrl.value; | |
771 | } | |
772 | WebUtil.writeSetting(name, val); | |
773 | //Log.Debug("Setting saved '" + name + "=" + val + "'"); | |
774 | return val; | |
775 | }, | |
776 | ||
777 | // Read form control compatible setting from cookie | |
0e4808bf | 778 | getSetting(name) { |
2b5f94fa JD |
779 | const ctrl = document.getElementById('noVNC_setting_' + name); |
780 | let val = WebUtil.readSetting(name); | |
6d6f0db0 SR |
781 | if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') { |
782 | if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) { | |
783 | val = false; | |
784 | } else { | |
785 | val = true; | |
bbbf42bb | 786 | } |
6d6f0db0 SR |
787 | } |
788 | return val; | |
789 | }, | |
790 | ||
791 | // These helpers compensate for the lack of parent-selectors and | |
792 | // previous-sibling-selectors in CSS which are needed when we want to | |
793 | // disable the labels that belong to disabled input elements. | |
0e4808bf | 794 | disableSetting(name) { |
2b5f94fa | 795 | const ctrl = document.getElementById('noVNC_setting_' + name); |
6d6f0db0 SR |
796 | ctrl.disabled = true; |
797 | ctrl.label.classList.add('noVNC_disabled'); | |
798 | }, | |
799 | ||
0e4808bf | 800 | enableSetting(name) { |
2b5f94fa | 801 | const ctrl = document.getElementById('noVNC_setting_' + name); |
6d6f0db0 SR |
802 | ctrl.disabled = false; |
803 | ctrl.label.classList.remove('noVNC_disabled'); | |
804 | }, | |
24584cca | 805 | |
ed8cbe4e PO |
806 | /* ------^------- |
807 | * /SETTINGS | |
808 | * ============== | |
809 | * PANELS | |
810 | * ------v------*/ | |
811 | ||
0e4808bf | 812 | closeAllPanels() { |
6d6f0db0 | 813 | UI.closeSettingsPanel(); |
cd523e8f | 814 | UI.closePowerPanel(); |
6d6f0db0 SR |
815 | UI.closeClipboardPanel(); |
816 | UI.closeExtraKeys(); | |
817 | }, | |
ed8cbe4e PO |
818 | |
819 | /* ------^------- | |
820 | * /PANELS | |
821 | * ============== | |
822 | * SETTINGS (panel) | |
823 | * ------v------*/ | |
824 | ||
0e4808bf | 825 | openSettingsPanel() { |
6d6f0db0 SR |
826 | UI.closeAllPanels(); |
827 | UI.openControlbar(); | |
828 | ||
829 | // Refresh UI elements from saved cookies | |
830 | UI.updateSetting('encrypt'); | |
a49ade5f | 831 | UI.updateSetting('view_clip'); |
6d6f0db0 SR |
832 | UI.updateSetting('resize'); |
833 | UI.updateSetting('shared'); | |
834 | UI.updateSetting('view_only'); | |
835 | UI.updateSetting('path'); | |
836 | UI.updateSetting('repeaterID'); | |
837 | UI.updateSetting('logging'); | |
838 | UI.updateSetting('reconnect'); | |
839 | UI.updateSetting('reconnect_delay'); | |
840 | ||
841 | document.getElementById('noVNC_settings') | |
842 | .classList.add("noVNC_open"); | |
843 | document.getElementById('noVNC_settings_button') | |
844 | .classList.add("noVNC_selected"); | |
845 | }, | |
846 | ||
0e4808bf | 847 | closeSettingsPanel() { |
6d6f0db0 SR |
848 | document.getElementById('noVNC_settings') |
849 | .classList.remove("noVNC_open"); | |
850 | document.getElementById('noVNC_settings_button') | |
851 | .classList.remove("noVNC_selected"); | |
852 | }, | |
853 | ||
0e4808bf | 854 | toggleSettingsPanel() { |
6d6f0db0 SR |
855 | if (document.getElementById('noVNC_settings') |
856 | .classList.contains("noVNC_open")) { | |
857 | UI.closeSettingsPanel(); | |
858 | } else { | |
859 | UI.openSettingsPanel(); | |
860 | } | |
861 | }, | |
bbbf42bb | 862 | |
95dd6001 | 863 | /* ------^------- |
864 | * /SETTINGS | |
865 | * ============== | |
cd523e8f | 866 | * POWER |
95dd6001 | 867 | * ------v------*/ |
868 | ||
0e4808bf | 869 | openPowerPanel() { |
6d6f0db0 SR |
870 | UI.closeAllPanels(); |
871 | UI.openControlbar(); | |
872 | ||
cd523e8f | 873 | document.getElementById('noVNC_power') |
6d6f0db0 | 874 | .classList.add("noVNC_open"); |
cd523e8f | 875 | document.getElementById('noVNC_power_button') |
6d6f0db0 SR |
876 | .classList.add("noVNC_selected"); |
877 | }, | |
878 | ||
0e4808bf | 879 | closePowerPanel() { |
cd523e8f | 880 | document.getElementById('noVNC_power') |
6d6f0db0 | 881 | .classList.remove("noVNC_open"); |
cd523e8f | 882 | document.getElementById('noVNC_power_button') |
6d6f0db0 SR |
883 | .classList.remove("noVNC_selected"); |
884 | }, | |
885 | ||
0e4808bf | 886 | togglePowerPanel() { |
cd523e8f | 887 | if (document.getElementById('noVNC_power') |
6d6f0db0 | 888 | .classList.contains("noVNC_open")) { |
cd523e8f | 889 | UI.closePowerPanel(); |
6d6f0db0 | 890 | } else { |
cd523e8f | 891 | UI.openPowerPanel(); |
6d6f0db0 SR |
892 | } |
893 | }, | |
ed8cbe4e | 894 | |
cd523e8f | 895 | // Disable/enable power button |
0e4808bf | 896 | updatePowerButton() { |
cd523e8f | 897 | if (UI.connected && |
747b4623 PO |
898 | UI.rfb.capabilities.power && |
899 | !UI.rfb.viewOnly) { | |
cd523e8f | 900 | document.getElementById('noVNC_power_button') |
6d6f0db0 SR |
901 | .classList.remove("noVNC_hidden"); |
902 | } else { | |
cd523e8f | 903 | document.getElementById('noVNC_power_button') |
6d6f0db0 | 904 | .classList.add("noVNC_hidden"); |
cd523e8f PO |
905 | // Close power panel if open |
906 | UI.closePowerPanel(); | |
6d6f0db0 SR |
907 | } |
908 | }, | |
bbbf42bb | 909 | |
95dd6001 | 910 | /* ------^------- |
cd523e8f | 911 | * /POWER |
95dd6001 | 912 | * ============== |
913 | * CLIPBOARD | |
914 | * ------v------*/ | |
f8b399d7 | 915 | |
0e4808bf | 916 | openClipboardPanel() { |
6d6f0db0 SR |
917 | UI.closeAllPanels(); |
918 | UI.openControlbar(); | |
919 | ||
920 | document.getElementById('noVNC_clipboard') | |
921 | .classList.add("noVNC_open"); | |
922 | document.getElementById('noVNC_clipboard_button') | |
923 | .classList.add("noVNC_selected"); | |
924 | }, | |
925 | ||
0e4808bf | 926 | closeClipboardPanel() { |
6d6f0db0 SR |
927 | document.getElementById('noVNC_clipboard') |
928 | .classList.remove("noVNC_open"); | |
929 | document.getElementById('noVNC_clipboard_button') | |
930 | .classList.remove("noVNC_selected"); | |
931 | }, | |
932 | ||
0e4808bf | 933 | toggleClipboardPanel() { |
6d6f0db0 SR |
934 | if (document.getElementById('noVNC_clipboard') |
935 | .classList.contains("noVNC_open")) { | |
936 | UI.closeClipboardPanel(); | |
937 | } else { | |
938 | UI.openClipboardPanel(); | |
939 | } | |
940 | }, | |
941 | ||
0e4808bf | 942 | clipboardReceive(e) { |
e89eef94 PO |
943 | Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0,40) + "..."); |
944 | document.getElementById('noVNC_clipboard_text').value = e.detail.text; | |
6d6f0db0 SR |
945 | Log.Debug("<< UI.clipboardReceive"); |
946 | }, | |
947 | ||
0e4808bf | 948 | clipboardClear() { |
6d6f0db0 SR |
949 | document.getElementById('noVNC_clipboard_text').value = ""; |
950 | UI.rfb.clipboardPasteFrom(""); | |
951 | }, | |
952 | ||
0e4808bf | 953 | clipboardSend() { |
2b5f94fa | 954 | const text = document.getElementById('noVNC_clipboard_text').value; |
6d6f0db0 SR |
955 | Log.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "..."); |
956 | UI.rfb.clipboardPasteFrom(text); | |
957 | Log.Debug("<< UI.clipboardSend"); | |
958 | }, | |
95dd6001 | 959 | |
960 | /* ------^------- | |
961 | * /CLIPBOARD | |
962 | * ============== | |
963 | * CONNECTION | |
964 | * ------v------*/ | |
965 | ||
0e4808bf | 966 | openConnectPanel() { |
6d6f0db0 SR |
967 | document.getElementById('noVNC_connect_dlg') |
968 | .classList.add("noVNC_open"); | |
969 | }, | |
b3c932c3 | 970 | |
0e4808bf | 971 | closeConnectPanel() { |
6d6f0db0 SR |
972 | document.getElementById('noVNC_connect_dlg') |
973 | .classList.remove("noVNC_open"); | |
974 | }, | |
b3c932c3 | 975 | |
0e4808bf | 976 | connect(event, password) { |
a4822c3a SM |
977 | |
978 | // Ignore when rfb already exists | |
979 | if (typeof UI.rfb !== 'undefined') { | |
980 | return; | |
981 | } | |
982 | ||
2b5f94fa JD |
983 | const host = UI.getSetting('host'); |
984 | const port = UI.getSetting('port'); | |
985 | const path = UI.getSetting('path'); | |
c55f05f6 | 986 | |
6d6f0db0 SR |
987 | if (typeof password === 'undefined') { |
988 | password = WebUtil.getConfigVar('password'); | |
2e735160 | 989 | UI.reconnect_password = password; |
6d6f0db0 | 990 | } |
044d54ed | 991 | |
6d6f0db0 SR |
992 | if (password === null) { |
993 | password = undefined; | |
994 | } | |
512d3605 | 995 | |
d623a029 SM |
996 | UI.hideStatus(); |
997 | ||
d593483e | 998 | if (!host) { |
8317524c SM |
999 | Log.Error("Can't connect when host is: " + host); |
1000 | UI.showStatus(_("Must set host"), 'error'); | |
6d6f0db0 SR |
1001 | return; |
1002 | } | |
53fc7392 | 1003 | |
6d6f0db0 SR |
1004 | UI.closeAllPanels(); |
1005 | UI.closeConnectPanel(); | |
4102b71c | 1006 | |
f3763a01 SM |
1007 | UI.updateVisualState('connecting'); |
1008 | ||
2b5f94fa | 1009 | let url; |
5b4e5d01 PO |
1010 | |
1011 | url = UI.getSetting('encrypt') ? 'wss' : 'ws'; | |
1012 | ||
1013 | url += '://' + host; | |
1014 | if(port) { | |
1015 | url += ':' + port; | |
1016 | } | |
1017 | url += '/' + path; | |
1018 | ||
9b84f516 | 1019 | UI.rfb = new RFB(document.getElementById('noVNC_container'), url, |
2f4516f2 PO |
1020 | { shared: UI.getSetting('shared'), |
1021 | repeaterID: UI.getSetting('repeaterID'), | |
1022 | credentials: { password: password } }); | |
ee5cae9f | 1023 | UI.rfb.addEventListener("connect", UI.connectFinished); |
e89eef94 PO |
1024 | UI.rfb.addEventListener("disconnect", UI.disconnectFinished); |
1025 | UI.rfb.addEventListener("credentialsrequired", UI.credentials); | |
d472f3f1 | 1026 | UI.rfb.addEventListener("securityfailure", UI.securityFailed); |
651c23ec | 1027 | UI.rfb.addEventListener("capabilities", UI.updatePowerButton); |
e89eef94 PO |
1028 | UI.rfb.addEventListener("clipboard", UI.clipboardReceive); |
1029 | UI.rfb.addEventListener("bell", UI.bell); | |
e89eef94 | 1030 | UI.rfb.addEventListener("desktopname", UI.updateDesktopName); |
9b84f516 PO |
1031 | UI.rfb.clipViewport = UI.getSetting('view_clip'); |
1032 | UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale'; | |
1033 | UI.rfb.resizeSession = UI.getSetting('resize') === 'remote'; | |
37c60935 | 1034 | |
f3763a01 | 1035 | UI.updateViewOnly(); // requires UI.rfb |
6d6f0db0 | 1036 | }, |
5299db1a | 1037 | |
0e4808bf | 1038 | disconnect() { |
6d6f0db0 SR |
1039 | UI.closeAllPanels(); |
1040 | UI.rfb.disconnect(); | |
8e0f0088 | 1041 | |
ee5cae9f SM |
1042 | UI.connected = false; |
1043 | ||
6d6f0db0 SR |
1044 | // Disable automatic reconnecting |
1045 | UI.inhibit_reconnect = true; | |
044d54ed | 1046 | |
ee5cae9f SM |
1047 | UI.updateVisualState('disconnecting'); |
1048 | ||
6d6f0db0 SR |
1049 | // Don't display the connection settings until we're actually disconnected |
1050 | }, | |
bbbf42bb | 1051 | |
0e4808bf | 1052 | reconnect() { |
6d6f0db0 | 1053 | UI.reconnect_callback = null; |
044d54ed | 1054 | |
6d6f0db0 SR |
1055 | // if reconnect has been disabled in the meantime, do nothing. |
1056 | if (UI.inhibit_reconnect) { | |
1057 | return; | |
1058 | } | |
044d54ed | 1059 | |
6d6f0db0 SR |
1060 | UI.connect(null, UI.reconnect_password); |
1061 | }, | |
044d54ed | 1062 | |
0e4808bf | 1063 | cancelReconnect() { |
68958038 SM |
1064 | if (UI.reconnect_callback !== null) { |
1065 | clearTimeout(UI.reconnect_callback); | |
1066 | UI.reconnect_callback = null; | |
1067 | } | |
1068 | ||
1069 | UI.updateVisualState('disconnected'); | |
1070 | ||
1071 | UI.openControlbar(); | |
1072 | UI.openConnectPanel(); | |
1073 | }, | |
1074 | ||
0e4808bf | 1075 | connectFinished(e) { |
ee5cae9f SM |
1076 | UI.connected = true; |
1077 | UI.inhibit_reconnect = false; | |
ee5cae9f SM |
1078 | |
1079 | let msg; | |
1080 | if (UI.getSetting('encrypt')) { | |
1081 | msg = _("Connected (encrypted) to ") + UI.desktopName; | |
1082 | } else { | |
1083 | msg = _("Connected (unencrypted) to ") + UI.desktopName; | |
1084 | } | |
1085 | UI.showStatus(msg); | |
1086 | UI.updateVisualState('connected'); | |
1087 | ||
1088 | // Do this last because it can only be used on rendered elements | |
9b84f516 | 1089 | UI.rfb.focus(); |
ee5cae9f SM |
1090 | }, |
1091 | ||
0e4808bf | 1092 | disconnectFinished(e) { |
2b5f94fa | 1093 | const wasConnected = UI.connected; |
7f1049c0 | 1094 | |
ee5cae9f SM |
1095 | // This variable is ideally set when disconnection starts, but |
1096 | // when the disconnection isn't clean or if it is initiated by | |
1097 | // the server, we need to do it here as well since | |
1098 | // UI.disconnect() won't be used in those cases. | |
1099 | UI.connected = false; | |
1100 | ||
d1aeb435 PO |
1101 | UI.rfb = undefined; |
1102 | ||
d472f3f1 | 1103 | if (!e.detail.clean) { |
ee5cae9f | 1104 | UI.updateVisualState('disconnected'); |
7f1049c0 SM |
1105 | if (wasConnected) { |
1106 | UI.showStatus(_("Something went wrong, connection is closed"), | |
1107 | 'error'); | |
1108 | } else { | |
1109 | UI.showStatus(_("Failed to connect to server"), 'error'); | |
1110 | } | |
6d6f0db0 | 1111 | } else if (UI.getSetting('reconnect', false) === true && !UI.inhibit_reconnect) { |
ee5cae9f | 1112 | UI.updateVisualState('reconnecting'); |
044d54ed | 1113 | |
2b5f94fa | 1114 | const delay = parseInt(UI.getSetting('reconnect_delay')); |
6d6f0db0 SR |
1115 | UI.reconnect_callback = setTimeout(UI.reconnect, delay); |
1116 | return; | |
ee5cae9f SM |
1117 | } else { |
1118 | UI.updateVisualState('disconnected'); | |
1119 | UI.showStatus(_("Disconnected"), 'normal'); | |
6d6f0db0 | 1120 | } |
044d54ed | 1121 | |
6d6f0db0 SR |
1122 | UI.openControlbar(); |
1123 | UI.openConnectPanel(); | |
1124 | }, | |
044d54ed | 1125 | |
0e4808bf | 1126 | securityFailed(e) { |
d472f3f1 SM |
1127 | let msg = ""; |
1128 | // On security failures we might get a string with a reason | |
1129 | // directly from the server. Note that we can't control if | |
1130 | // this string is translated or not. | |
1131 | if ('reason' in e.detail) { | |
1132 | msg = _("New connection has been rejected with reason: ") + | |
1133 | e.detail.reason; | |
1134 | } else { | |
1135 | msg = _("New connection has been rejected"); | |
6d6f0db0 | 1136 | } |
d472f3f1 | 1137 | UI.showStatus(msg, 'error'); |
6d6f0db0 | 1138 | }, |
3bb12056 | 1139 | |
7d714b15 SM |
1140 | /* ------^------- |
1141 | * /CONNECTION | |
1142 | * ============== | |
1143 | * PASSWORD | |
1144 | * ------v------*/ | |
1145 | ||
0e4808bf | 1146 | credentials(e) { |
430f00d6 | 1147 | // FIXME: handle more types |
6d6f0db0 SR |
1148 | document.getElementById('noVNC_password_dlg') |
1149 | .classList.add('noVNC_open'); | |
7d714b15 | 1150 | |
651c23ec JD |
1151 | setTimeout(() => document |
1152 | .getElementById('noVNC_password_input').focus(), 100); | |
7d714b15 | 1153 | |
8317524c SM |
1154 | Log.Warn("Server asked for a password"); |
1155 | UI.showStatus(_("Password is required"), "warning"); | |
6d6f0db0 SR |
1156 | }, |
1157 | ||
0e4808bf | 1158 | setPassword(e) { |
a80b5fda PO |
1159 | // Prevent actually submitting the form |
1160 | e.preventDefault(); | |
1161 | ||
2b5f94fa JD |
1162 | const inputElem = document.getElementById('noVNC_password_input'); |
1163 | const password = inputElem.value; | |
c23665dd SM |
1164 | // Clear the input after reading the password |
1165 | inputElem.value = ""; | |
430f00d6 | 1166 | UI.rfb.sendCredentials({ password: password }); |
6d6f0db0 SR |
1167 | UI.reconnect_password = password; |
1168 | document.getElementById('noVNC_password_dlg') | |
1169 | .classList.remove('noVNC_open'); | |
6d6f0db0 | 1170 | }, |
58ded70d | 1171 | |
95dd6001 | 1172 | /* ------^------- |
7d714b15 | 1173 | * /PASSWORD |
95dd6001 | 1174 | * ============== |
1175 | * FULLSCREEN | |
1176 | * ------v------*/ | |
1177 | ||
0e4808bf | 1178 | toggleFullscreen() { |
6d6f0db0 SR |
1179 | if (document.fullscreenElement || // alternative standard method |
1180 | document.mozFullScreenElement || // currently working methods | |
1181 | document.webkitFullscreenElement || | |
1182 | document.msFullscreenElement) { | |
1183 | if (document.exitFullscreen) { | |
1184 | document.exitFullscreen(); | |
1185 | } else if (document.mozCancelFullScreen) { | |
1186 | document.mozCancelFullScreen(); | |
1187 | } else if (document.webkitExitFullscreen) { | |
1188 | document.webkitExitFullscreen(); | |
1189 | } else if (document.msExitFullscreen) { | |
1190 | document.msExitFullscreen(); | |
1191 | } | |
1192 | } else { | |
1193 | if (document.documentElement.requestFullscreen) { | |
1194 | document.documentElement.requestFullscreen(); | |
1195 | } else if (document.documentElement.mozRequestFullScreen) { | |
1196 | document.documentElement.mozRequestFullScreen(); | |
1197 | } else if (document.documentElement.webkitRequestFullscreen) { | |
1198 | document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); | |
1199 | } else if (document.body.msRequestFullscreen) { | |
1200 | document.body.msRequestFullscreen(); | |
7d1dc09a | 1201 | } |
6d6f0db0 | 1202 | } |
6d6f0db0 SR |
1203 | UI.updateFullscreenButton(); |
1204 | }, | |
1205 | ||
0e4808bf | 1206 | updateFullscreenButton() { |
6d6f0db0 SR |
1207 | if (document.fullscreenElement || // alternative standard method |
1208 | document.mozFullScreenElement || // currently working methods | |
1209 | document.webkitFullscreenElement || | |
1210 | document.msFullscreenElement ) { | |
1211 | document.getElementById('noVNC_fullscreen_button') | |
1212 | .classList.add("noVNC_selected"); | |
1213 | } else { | |
1214 | document.getElementById('noVNC_fullscreen_button') | |
1215 | .classList.remove("noVNC_selected"); | |
1216 | } | |
1217 | }, | |
7d1dc09a | 1218 | |
95dd6001 | 1219 | /* ------^------- |
1220 | * /FULLSCREEN | |
1221 | * ============== | |
1222 | * RESIZE | |
1223 | * ------v------*/ | |
777cb7a0 | 1224 | |
6d6f0db0 | 1225 | // Apply remote resizing or local scaling |
0e4808bf | 1226 | applyResizeMode() { |
6d6f0db0 | 1227 | if (!UI.rfb) return; |
58ded70d | 1228 | |
9b84f516 PO |
1229 | UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale'; |
1230 | UI.rfb.resizeSession = UI.getSetting('resize') === 'remote'; | |
6d6f0db0 | 1231 | }, |
bbbf42bb | 1232 | |
95dd6001 | 1233 | /* ------^------- |
1234 | * /RESIZE | |
1235 | * ============== | |
a49ade5f | 1236 | * VIEW CLIPPING |
95dd6001 | 1237 | * ------v------*/ |
1238 | ||
a49ade5f | 1239 | // Update parameters that depend on the viewport clip setting |
0e4808bf | 1240 | updateViewClip() { |
6d6f0db0 SR |
1241 | if (!UI.rfb) return; |
1242 | ||
2b5f94fa JD |
1243 | const cur_clip = UI.rfb.clipViewport; |
1244 | let new_clip = UI.getSetting('view_clip'); | |
6d6f0db0 | 1245 | |
9b84f516 | 1246 | if (isTouchDevice) { |
6d6f0db0 SR |
1247 | // Touch devices usually have shit scrollbars |
1248 | new_clip = true; | |
1249 | } | |
5f38376a | 1250 | |
6d6f0db0 | 1251 | if (cur_clip !== new_clip) { |
0460e5fd | 1252 | UI.rfb.clipViewport = new_clip; |
6d6f0db0 | 1253 | } |
8e0f0088 | 1254 | |
6d6f0db0 SR |
1255 | // Changing the viewport may change the state of |
1256 | // the dragging button | |
1257 | UI.updateViewDrag(); | |
1258 | }, | |
1259 | ||
11715f20 | 1260 | // Handle special cases where viewport clipping is locked |
0e4808bf | 1261 | enableDisableViewClip() { |
2b5f94fa | 1262 | const resizeSetting = UI.getSetting('resize'); |
11715f20 SM |
1263 | if (isTouchDevice) { |
1264 | UI.forceSetting('view_clip', true); | |
1265 | } else if (resizeSetting === 'scale') { | |
a49ade5f | 1266 | UI.disableSetting('view_clip'); |
6d6f0db0 | 1267 | } else { |
a49ade5f | 1268 | UI.enableSetting('view_clip'); |
6d6f0db0 SR |
1269 | } |
1270 | }, | |
30bfff81 | 1271 | |
95dd6001 | 1272 | /* ------^------- |
a49ade5f | 1273 | * /VIEW CLIPPING |
95dd6001 | 1274 | * ============== |
1275 | * VIEWDRAG | |
1276 | * ------v------*/ | |
1277 | ||
0e4808bf | 1278 | toggleViewDrag() { |
6d6f0db0 | 1279 | if (!UI.rfb) return; |
bbbf42bb | 1280 | |
2bbd15cc | 1281 | UI.rfb.dragViewport = !UI.rfb.dragViewport; |
6d6f0db0 SR |
1282 | UI.updateViewDrag(); |
1283 | }, | |
f0d9ab96 | 1284 | |
0e4808bf | 1285 | updateViewDrag() { |
6d6f0db0 | 1286 | if (!UI.connected) return; |
6244e383 | 1287 | |
2b5f94fa | 1288 | const viewDragButton = document.getElementById('noVNC_view_drag_button'); |
29a0e6a8 | 1289 | |
9b84f516 PO |
1290 | if (!UI.rfb.clipViewport && UI.rfb.dragViewport) { |
1291 | // We are no longer clipping the viewport. Make sure | |
1292 | // viewport drag isn't active when it can't be used. | |
0460e5fd | 1293 | UI.rfb.dragViewport = false; |
6d6f0db0 SR |
1294 | } |
1295 | ||
0460e5fd | 1296 | if (UI.rfb.dragViewport) { |
6d6f0db0 SR |
1297 | viewDragButton.classList.add("noVNC_selected"); |
1298 | } else { | |
1299 | viewDragButton.classList.remove("noVNC_selected"); | |
1300 | } | |
1301 | ||
1302 | // Different behaviour for touch vs non-touch | |
1303 | // The button is disabled instead of hidden on touch devices | |
1304 | if (isTouchDevice) { | |
1305 | viewDragButton.classList.remove("noVNC_hidden"); | |
6244e383 | 1306 | |
9b84f516 | 1307 | if (UI.rfb.clipViewport) { |
6d6f0db0 | 1308 | viewDragButton.disabled = false; |
31ddaa1c | 1309 | } else { |
6d6f0db0 | 1310 | viewDragButton.disabled = true; |
6244e383 | 1311 | } |
6d6f0db0 SR |
1312 | } else { |
1313 | viewDragButton.disabled = false; | |
31ddaa1c | 1314 | |
9b84f516 | 1315 | if (UI.rfb.clipViewport) { |
6244e383 | 1316 | viewDragButton.classList.remove("noVNC_hidden"); |
6244e383 | 1317 | } else { |
6d6f0db0 | 1318 | viewDragButton.classList.add("noVNC_hidden"); |
f8b399d7 | 1319 | } |
6d6f0db0 SR |
1320 | } |
1321 | }, | |
f8b399d7 | 1322 | |
95dd6001 | 1323 | /* ------^------- |
1324 | * /VIEWDRAG | |
1325 | * ============== | |
1326 | * KEYBOARD | |
1327 | * ------v------*/ | |
fdf21468 | 1328 | |
0e4808bf | 1329 | showVirtualKeyboard() { |
6d6f0db0 | 1330 | if (!isTouchDevice) return; |
4b30f9ce | 1331 | |
2b5f94fa | 1332 | const input = document.getElementById('noVNC_keyboardinput'); |
4b30f9ce | 1333 | |
6d6f0db0 | 1334 | if (document.activeElement == input) return; |
4b30f9ce | 1335 | |
6d6f0db0 | 1336 | input.focus(); |
4b30f9ce | 1337 | |
6d6f0db0 | 1338 | try { |
2b5f94fa | 1339 | const l = input.value.length; |
6d6f0db0 SR |
1340 | // Move the caret to the end |
1341 | input.setSelectionRange(l, l); | |
8727f598 JD |
1342 | } catch (err) { |
1343 | // setSelectionRange is undefined in Google Chrome | |
1344 | } | |
6d6f0db0 | 1345 | }, |
4b30f9ce | 1346 | |
0e4808bf | 1347 | hideVirtualKeyboard() { |
6d6f0db0 | 1348 | if (!isTouchDevice) return; |
4b30f9ce | 1349 | |
2b5f94fa | 1350 | const input = document.getElementById('noVNC_keyboardinput'); |
4b30f9ce | 1351 | |
6d6f0db0 | 1352 | if (document.activeElement != input) return; |
4b30f9ce | 1353 | |
6d6f0db0 SR |
1354 | input.blur(); |
1355 | }, | |
4b30f9ce | 1356 | |
0e4808bf | 1357 | toggleVirtualKeyboard() { |
6d6f0db0 SR |
1358 | if (document.getElementById('noVNC_keyboard_button') |
1359 | .classList.contains("noVNC_selected")) { | |
1360 | UI.hideVirtualKeyboard(); | |
1361 | } else { | |
1362 | UI.showVirtualKeyboard(); | |
1363 | } | |
1364 | }, | |
bbbf42bb | 1365 | |
0e4808bf | 1366 | onfocusVirtualKeyboard(event) { |
6d6f0db0 SR |
1367 | document.getElementById('noVNC_keyboard_button') |
1368 | .classList.add("noVNC_selected"); | |
1d6ff4a3 | 1369 | if (UI.rfb) { |
b8dfb983 | 1370 | UI.rfb.focusOnClick = false; |
1d6ff4a3 | 1371 | } |
6d6f0db0 | 1372 | }, |
fdf21468 | 1373 | |
0e4808bf | 1374 | onblurVirtualKeyboard(event) { |
6d6f0db0 SR |
1375 | document.getElementById('noVNC_keyboard_button') |
1376 | .classList.remove("noVNC_selected"); | |
1d6ff4a3 | 1377 | if (UI.rfb) { |
b8dfb983 | 1378 | UI.rfb.focusOnClick = true; |
1d6ff4a3 | 1379 | } |
6d6f0db0 | 1380 | }, |
ffcadf95 | 1381 | |
0e4808bf | 1382 | keepVirtualKeyboard(event) { |
2b5f94fa | 1383 | const input = document.getElementById('noVNC_keyboardinput'); |
ffcadf95 | 1384 | |
6d6f0db0 SR |
1385 | // Only prevent focus change if the virtual keyboard is active |
1386 | if (document.activeElement != input) { | |
1387 | return; | |
1388 | } | |
ffcadf95 | 1389 | |
6d6f0db0 SR |
1390 | // Only allow focus to move to other elements that need |
1391 | // focus to function properly | |
1392 | if (event.target.form !== undefined) { | |
1393 | switch (event.target.type) { | |
1394 | case 'text': | |
1395 | case 'email': | |
1396 | case 'search': | |
1397 | case 'password': | |
1398 | case 'tel': | |
1399 | case 'url': | |
1400 | case 'textarea': | |
1401 | case 'select-one': | |
1402 | case 'select-multiple': | |
1403 | return; | |
ffcadf95 | 1404 | } |
6d6f0db0 | 1405 | } |
ffcadf95 | 1406 | |
1d6ff4a3 | 1407 | event.preventDefault(); |
6d6f0db0 | 1408 | }, |
bbbf42bb | 1409 | |
0e4808bf | 1410 | keyboardinputReset() { |
2b5f94fa | 1411 | const kbi = document.getElementById('noVNC_keyboardinput'); |
6d6f0db0 SR |
1412 | kbi.value = new Array(UI.defaultKeyboardinputLen).join("_"); |
1413 | UI.lastKeyboardinput = kbi.value; | |
1414 | }, | |
bbbf42bb | 1415 | |
0e4808bf | 1416 | keyEvent(keysym, code, down) { |
867daa98 PO |
1417 | if (!UI.rfb) return; |
1418 | ||
1419 | UI.rfb.sendKey(keysym, code, down); | |
1420 | }, | |
1421 | ||
6d6f0db0 SR |
1422 | // When normal keyboard events are left uncought, use the input events from |
1423 | // the keyboardinput element instead and generate the corresponding key events. | |
1424 | // This code is required since some browsers on Android are inconsistent in | |
1425 | // sending keyCodes in the normal keyboard events when using on screen keyboards. | |
0e4808bf | 1426 | keyInput(event) { |
3b8ec46f | 1427 | |
6d6f0db0 | 1428 | if (!UI.rfb) return; |
3b8ec46f | 1429 | |
2b5f94fa | 1430 | const newValue = event.target.value; |
1138bdd4 | 1431 | |
6d6f0db0 SR |
1432 | if (!UI.lastKeyboardinput) { |
1433 | UI.keyboardinputReset(); | |
1434 | } | |
2b5f94fa | 1435 | const oldValue = UI.lastKeyboardinput; |
bbbf42bb | 1436 | |
2b5f94fa | 1437 | let newLen; |
6d6f0db0 SR |
1438 | try { |
1439 | // Try to check caret position since whitespace at the end | |
1440 | // will not be considered by value.length in some browsers | |
1441 | newLen = Math.max(event.target.selectionStart, newValue.length); | |
1442 | } catch (err) { | |
1443 | // selectionStart is undefined in Google Chrome | |
1444 | newLen = newValue.length; | |
1445 | } | |
2b5f94fa | 1446 | const oldLen = oldValue.length; |
6d6f0db0 | 1447 | |
2b5f94fa JD |
1448 | let inputs = newLen - oldLen; |
1449 | let backspaces = inputs < 0 ? -inputs : 0; | |
8e0f0088 | 1450 | |
6d6f0db0 SR |
1451 | // Compare the old string with the new to account for |
1452 | // text-corrections or other input that modify existing text | |
2b5f94fa | 1453 | for (let i = 0; i < Math.min(oldLen, newLen); i++) { |
6d6f0db0 SR |
1454 | if (newValue.charAt(i) != oldValue.charAt(i)) { |
1455 | inputs = newLen - i; | |
1456 | backspaces = oldLen - i; | |
1457 | break; | |
bbbf42bb | 1458 | } |
6d6f0db0 | 1459 | } |
bbbf42bb | 1460 | |
6d6f0db0 | 1461 | // Send the key events |
2b5f94fa | 1462 | for (let i = 0; i < backspaces; i++) { |
94f5cf05 | 1463 | UI.rfb.sendKey(KeyTable.XK_BackSpace, "Backspace"); |
6d6f0db0 | 1464 | } |
2b5f94fa | 1465 | for (let i = newLen - inputs; i < newLen; i++) { |
524d67f2 | 1466 | UI.rfb.sendKey(keysyms.lookup(newValue.charCodeAt(i))); |
6d6f0db0 | 1467 | } |
bbbf42bb | 1468 | |
6d6f0db0 SR |
1469 | // Control the text content length in the keyboardinput element |
1470 | if (newLen > 2 * UI.defaultKeyboardinputLen) { | |
1471 | UI.keyboardinputReset(); | |
1472 | } else if (newLen < 1) { | |
1473 | // There always have to be some text in the keyboardinput | |
1474 | // element with which backspace can interact. | |
1475 | UI.keyboardinputReset(); | |
1476 | // This sometimes causes the keyboard to disappear for a second | |
1477 | // but it is required for the android keyboard to recognize that | |
1478 | // text has been added to the field | |
1479 | event.target.blur(); | |
1480 | // This has to be ran outside of the input handler in order to work | |
1481 | setTimeout(event.target.focus.bind(event.target), 0); | |
1482 | } else { | |
1483 | UI.lastKeyboardinput = newValue; | |
1484 | } | |
1485 | }, | |
bbbf42bb | 1486 | |
4b30f9ce SM |
1487 | /* ------^------- |
1488 | * /KEYBOARD | |
1489 | * ============== | |
1490 | * EXTRA KEYS | |
1491 | * ------v------*/ | |
1492 | ||
0e4808bf | 1493 | openExtraKeys() { |
6d6f0db0 SR |
1494 | UI.closeAllPanels(); |
1495 | UI.openControlbar(); | |
1496 | ||
1497 | document.getElementById('noVNC_modifiers') | |
1498 | .classList.add("noVNC_open"); | |
1499 | document.getElementById('noVNC_toggle_extra_keys_button') | |
1500 | .classList.add("noVNC_selected"); | |
1501 | }, | |
1502 | ||
0e4808bf | 1503 | closeExtraKeys() { |
6d6f0db0 SR |
1504 | document.getElementById('noVNC_modifiers') |
1505 | .classList.remove("noVNC_open"); | |
1506 | document.getElementById('noVNC_toggle_extra_keys_button') | |
1507 | .classList.remove("noVNC_selected"); | |
1508 | }, | |
1509 | ||
0e4808bf | 1510 | toggleExtraKeys() { |
6d6f0db0 SR |
1511 | if(document.getElementById('noVNC_modifiers') |
1512 | .classList.contains("noVNC_open")) { | |
1513 | UI.closeExtraKeys(); | |
1514 | } else { | |
1515 | UI.openExtraKeys(); | |
1516 | } | |
1517 | }, | |
1518 | ||
0e4808bf | 1519 | sendEsc() { |
94f5cf05 | 1520 | UI.rfb.sendKey(KeyTable.XK_Escape, "Escape"); |
6d6f0db0 SR |
1521 | }, |
1522 | ||
0e4808bf | 1523 | sendTab() { |
6d6f0db0 SR |
1524 | UI.rfb.sendKey(KeyTable.XK_Tab); |
1525 | }, | |
1526 | ||
0e4808bf | 1527 | toggleCtrl() { |
2b5f94fa | 1528 | const btn = document.getElementById('noVNC_toggle_ctrl_button'); |
6d6f0db0 | 1529 | if (btn.classList.contains("noVNC_selected")) { |
94f5cf05 | 1530 | UI.rfb.sendKey(KeyTable.XK_Control_L, "ControlLeft", false); |
6d6f0db0 SR |
1531 | btn.classList.remove("noVNC_selected"); |
1532 | } else { | |
94f5cf05 | 1533 | UI.rfb.sendKey(KeyTable.XK_Control_L, "ControlLeft", true); |
6d6f0db0 SR |
1534 | btn.classList.add("noVNC_selected"); |
1535 | } | |
1536 | }, | |
1537 | ||
0e4808bf | 1538 | toggleAlt() { |
2b5f94fa | 1539 | const btn = document.getElementById('noVNC_toggle_alt_button'); |
6d6f0db0 | 1540 | if (btn.classList.contains("noVNC_selected")) { |
94f5cf05 | 1541 | UI.rfb.sendKey(KeyTable.XK_Alt_L, "AltLeft", false); |
6d6f0db0 SR |
1542 | btn.classList.remove("noVNC_selected"); |
1543 | } else { | |
94f5cf05 | 1544 | UI.rfb.sendKey(KeyTable.XK_Alt_L, "AltLeft", true); |
6d6f0db0 SR |
1545 | btn.classList.add("noVNC_selected"); |
1546 | } | |
1547 | }, | |
bbbf42bb | 1548 | |
0e4808bf | 1549 | sendCtrlAltDel() { |
6d6f0db0 SR |
1550 | UI.rfb.sendCtrlAltDel(); |
1551 | }, | |
bbbf42bb | 1552 | |
95dd6001 | 1553 | /* ------^------- |
4b30f9ce | 1554 | * /EXTRA KEYS |
95dd6001 | 1555 | * ============== |
1556 | * MISC | |
1557 | * ------v------*/ | |
1558 | ||
0e4808bf | 1559 | setMouseButton(num) { |
2b5f94fa | 1560 | const view_only = UI.rfb.viewOnly; |
6d6f0db0 | 1561 | if (UI.rfb && !view_only) { |
747b4623 | 1562 | UI.rfb.touchButton = num; |
6d6f0db0 | 1563 | } |
95dd6001 | 1564 | |
2b5f94fa JD |
1565 | const blist = [0, 1,2,4]; |
1566 | for (let b = 0; b < blist.length; b++) { | |
1567 | const button = document.getElementById('noVNC_mouse_button' + | |
6d6f0db0 SR |
1568 | blist[b]); |
1569 | if (blist[b] === num && !view_only) { | |
1570 | button.classList.remove("noVNC_hidden"); | |
1571 | } else { | |
1572 | button.classList.add("noVNC_hidden"); | |
ceb847b0 | 1573 | } |
6d6f0db0 SR |
1574 | } |
1575 | }, | |
ef1e8bab | 1576 | |
0e4808bf | 1577 | updateViewOnly() { |
e6a8eb15 | 1578 | if (!UI.rfb) return; |
747b4623 | 1579 | UI.rfb.viewOnly = UI.getSetting('view_only'); |
f3763a01 SM |
1580 | |
1581 | // Hide input related buttons in view only mode | |
1582 | if (UI.rfb.viewOnly) { | |
1583 | document.getElementById('noVNC_keyboard_button') | |
1584 | .classList.add('noVNC_hidden'); | |
1585 | document.getElementById('noVNC_toggle_extra_keys_button') | |
1586 | .classList.add('noVNC_hidden'); | |
1587 | } else { | |
1588 | document.getElementById('noVNC_keyboard_button') | |
1589 | .classList.remove('noVNC_hidden'); | |
1590 | document.getElementById('noVNC_toggle_extra_keys_button') | |
1591 | .classList.remove('noVNC_hidden'); | |
1592 | } | |
61f93180 | 1593 | UI.setMouseButton(1); //has it's own logic for hiding/showing |
6d6f0db0 SR |
1594 | }, |
1595 | ||
0e4808bf | 1596 | updateLogging() { |
6d6f0db0 SR |
1597 | WebUtil.init_logging(UI.getSetting('logging')); |
1598 | }, | |
1599 | ||
0e4808bf | 1600 | updateDesktopName(e) { |
e89eef94 | 1601 | UI.desktopName = e.detail.name; |
6d6f0db0 | 1602 | // Display the desktop name in the document title |
e89eef94 | 1603 | document.title = e.detail.name + " - noVNC"; |
6d6f0db0 SR |
1604 | }, |
1605 | ||
0e4808bf | 1606 | bell(e) { |
6d6f0db0 | 1607 | if (WebUtil.getConfigVar('bell', 'on') === 'on') { |
2b5f94fa | 1608 | const promise = document.getElementById('noVNC_bell').play(); |
a342ed70 SM |
1609 | // The standards disagree on the return value here |
1610 | if (promise) { | |
651c23ec | 1611 | promise.catch((e) => { |
d4fc89d8 SM |
1612 | if (e.name === "NotAllowedError") { |
1613 | // Ignore when the browser doesn't let us play audio. | |
1614 | // It is common that the browsers require audio to be | |
1615 | // initiated from a user action. | |
1616 | } else { | |
1617 | Log.Error("Unable to play bell: " + e); | |
1618 | } | |
1619 | }); | |
a342ed70 | 1620 | } |
6d6f0db0 SR |
1621 | } |
1622 | }, | |
63bf2ba5 | 1623 | |
6d6f0db0 | 1624 | //Helper to add options to dropdown. |
0e4808bf | 1625 | addOption(selectbox, text, value) { |
2b5f94fa | 1626 | const optn = document.createElement("OPTION"); |
6d6f0db0 SR |
1627 | optn.text = text; |
1628 | optn.value = value; | |
1629 | selectbox.options.add(optn); | |
1630 | }, | |
bbbf42bb | 1631 | |
95dd6001 | 1632 | /* ------^------- |
1633 | * /MISC | |
1634 | * ============== | |
1635 | */ | |
6d6f0db0 SR |
1636 | }; |
1637 | ||
1638 | // Set up translations | |
2b5f94fa | 1639 | const LINGUAS = ["de", "el", "es", "nl", "pl", "sv", "tr", "zh_CN", "zh_TW"]; |
6d6f0db0 SR |
1640 | l10n.setup(LINGUAS); |
1641 | if (l10n.language !== "en" && l10n.dictionary === undefined) { | |
651c23ec | 1642 | WebUtil.fetchJSON('app/locale/' + l10n.language + '.json', (translations) => { |
6d6f0db0 SR |
1643 | l10n.dictionary = translations; |
1644 | ||
1645 | // wait for translations to load before loading the UI | |
1646 | UI.prime(); | |
651c23ec | 1647 | }, (err) => { |
7c332ad9 PO |
1648 | Log.Error("Failed to load translations: " + err); |
1649 | UI.prime(); | |
6d6f0db0 SR |
1650 | }); |
1651 | } else { | |
1652 | UI.prime(); | |
1653 | } | |
1654 | ||
3ae0bb09 | 1655 | export default UI; |