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