]>
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 | ||
14 | /* [module] | |
15 | * import Util from "../core/util"; | |
bd5340c7 | 16 | * import KeyTable from "../core/input/keysym"; |
ae510306 SR |
17 | * import RFB from "../core/rfb"; |
18 | * import Display from "../core/display"; | |
19 | * import WebUtil from "./webutil"; | |
20 | */ | |
bbbf42bb SR |
21 | |
22 | var UI; | |
23 | ||
24 | (function () { | |
25 | "use strict"; | |
26 | ||
ae510306 | 27 | /* [begin skip-as-module] */ |
bbbf42bb | 28 | // Load supporting scripts |
72bdd06e | 29 | WebUtil.load_scripts( |
bd5340c7 SR |
30 | {'core': ["base64.js", "websock.js", "des.js", "input/keysymdef.js", |
31 | "input/xtscancodes.js", "input/util.js", "input/devices.js", | |
32 | "display.js", "inflator.js", "rfb.js", "input/keysym.js"]}); | |
ae510306 | 33 | |
bbbf42bb | 34 | window.onscriptsload = function () { UI.load(); }; |
ae510306 | 35 | /* [end skip-as-module] */ |
bbbf42bb | 36 | |
bd6874e0 | 37 | UI = { |
bbbf42bb | 38 | |
045d9224 | 39 | rfb_state: 'loaded', |
529c64e1 | 40 | |
045d9224 | 41 | resizeTimeout: null, |
ca5c74ad | 42 | statusTimeout: null, |
529c64e1 | 43 | hideKeyboardTimeout: null, |
44 | ||
bbbf42bb | 45 | keyboardVisible: false, |
529c64e1 | 46 | |
bbbf42bb | 47 | isTouchDevice: false, |
f620259b | 48 | isSafari: false, |
a6357e82 | 49 | rememberedClipSetting: null, |
529c64e1 | 50 | lastKeyboardinput: null, |
51 | defaultKeyboardinputLen: 100, | |
52 | ||
bbbf42bb SR |
53 | // Setup rfb object, load settings from browser storage, then call |
54 | // UI.init to setup the UI/menus | |
0bd2cbac | 55 | load: function(callback) { |
bbbf42bb SR |
56 | WebUtil.initSettings(UI.start, callback); |
57 | }, | |
58 | ||
59 | // Render default UI and initialize settings menu | |
60 | start: function(callback) { | |
0f6af1e3 | 61 | |
62 | // Setup global variables first | |
bbbf42bb | 63 | UI.isTouchDevice = 'ontouchstart' in document.documentElement; |
0f6af1e3 | 64 | UI.isSafari = (navigator.userAgent.indexOf('Safari') !== -1 && |
65 | navigator.userAgent.indexOf('Chrome') === -1); | |
66 | ||
67 | UI.initSettings(); | |
68 | ||
6244e383 | 69 | // Adapt the interface for touch screen devices |
0f6af1e3 | 70 | if (UI.isTouchDevice) { |
6244e383 | 71 | document.documentElement.classList.add("noVNC_touch"); |
0f6af1e3 | 72 | // Remove the address bar |
73 | setTimeout(function() { window.scrollTo(0, 1); }, 100); | |
74 | UI.forceSetting('clip', true); | |
75 | } else { | |
76 | UI.initSetting('clip', false); | |
77 | } | |
78 | ||
79 | // Setup and initialize event handlers | |
80 | UI.setupWindowEvents(); | |
81 | UI.setupFullscreen(); | |
82 | UI.addControlbarHandlers(); | |
83 | UI.addTouchSpecificHandlers(); | |
ebbec43a | 84 | UI.addExtraKeysHandlers(); |
0f6af1e3 | 85 | UI.addXvpHandlers(); |
86 | UI.addConnectionControlHandlers(); | |
87 | UI.addClipboardHandlers(); | |
88 | UI.addSettingsHandlers(); | |
bbbf42bb | 89 | |
f9fff037 | 90 | // Show the connect panel on first load unless autoconnecting |
ed8cbe4e PO |
91 | if (!autoconnect) { |
92 | UI.openConnectPanel(); | |
93 | } | |
0f6af1e3 | 94 | |
f0d9ab96 | 95 | UI.updateViewClip(); |
0f6af1e3 | 96 | |
97 | UI.updateVisualState(); | |
98 | ||
99 | document.getElementById('noVNC_setting_host').focus(); | |
100 | ||
101 | var autoconnect = WebUtil.getConfigVar('autoconnect', false); | |
102 | if (autoconnect === 'true' || autoconnect == '1') { | |
103 | autoconnect = true; | |
104 | UI.connect(); | |
105 | } else { | |
106 | autoconnect = false; | |
107 | } | |
108 | ||
109 | if (typeof callback === "function") { | |
110 | callback(UI.rfb); | |
111 | } | |
112 | }, | |
113 | ||
114 | initSettings: function() { | |
bbbf42bb SR |
115 | // Stylesheet selection dropdown |
116 | var sheet = WebUtil.selectStylesheet(); | |
117 | var sheets = WebUtil.getStylesheets(); | |
118 | var i; | |
119 | for (i = 0; i < sheets.length; i += 1) { | |
ae510306 | 120 | UI.addOption(document.getElementById('noVNC_setting_stylesheet'),sheets[i].title, sheets[i].title); |
bbbf42bb SR |
121 | } |
122 | ||
123 | // Logging selection dropdown | |
124 | var llevels = ['error', 'warn', 'info', 'debug']; | |
125 | for (i = 0; i < llevels.length; i += 1) { | |
ae510306 | 126 | UI.addOption(document.getElementById('noVNC_setting_logging'),llevels[i], llevels[i]); |
bbbf42bb SR |
127 | } |
128 | ||
129 | // Settings with immediate effects | |
130 | UI.initSetting('logging', 'warn'); | |
131 | WebUtil.init_logging(UI.getSetting('logging')); | |
132 | ||
133 | UI.initSetting('stylesheet', 'default'); | |
134 | WebUtil.selectStylesheet(null); | |
135 | // call twice to get around webkit bug | |
136 | WebUtil.selectStylesheet(UI.getSetting('stylesheet')); | |
137 | ||
138 | // if port == 80 (or 443) then it won't be present and should be | |
139 | // set manually | |
140 | var port = window.location.port; | |
141 | if (!port) { | |
142 | if (window.location.protocol.substring(0,5) == 'https') { | |
143 | port = 443; | |
144 | } | |
145 | else if (window.location.protocol.substring(0,4) == 'http') { | |
146 | port = 80; | |
147 | } | |
148 | } | |
149 | ||
150 | /* Populate the controls if defaults are provided in the URL */ | |
151 | UI.initSetting('host', window.location.hostname); | |
152 | UI.initSetting('port', port); | |
153 | UI.initSetting('password', ''); | |
154 | UI.initSetting('encrypt', (window.location.protocol === "https:")); | |
155 | UI.initSetting('true_color', true); | |
156 | UI.initSetting('cursor', !UI.isTouchDevice); | |
8b46c0de | 157 | UI.initSetting('resize', 'off'); |
bbbf42bb SR |
158 | UI.initSetting('shared', true); |
159 | UI.initSetting('view_only', false); | |
160 | UI.initSetting('path', 'websockify'); | |
161 | UI.initSetting('repeaterID', ''); | |
c55f05f6 | 162 | UI.initSetting('token', ''); |
0f6af1e3 | 163 | }, |
bbbf42bb | 164 | |
0f6af1e3 | 165 | setupWindowEvents: function() { |
166 | window.addEventListener( 'resize', function () { | |
777cb7a0 | 167 | UI.applyResizeMode(); |
f0d9ab96 | 168 | UI.updateViewClip(); |
31ddaa1c | 169 | UI.updateViewDrag(); |
f8b399d7 | 170 | } ); |
ca5c74ad | 171 | |
172 | document.getElementById("noVNC_status") | |
173 | .addEventListener('click', UI.hideStatus); | |
bbbf42bb SR |
174 | }, |
175 | ||
0f6af1e3 | 176 | setupFullscreen: function() { |
177 | // Only show the button if fullscreen is properly supported | |
178 | // * Safari doesn't support alphanumerical input while in fullscreen | |
179 | if (!UI.isSafari && | |
180 | (document.documentElement.requestFullscreen || | |
181 | document.documentElement.mozRequestFullScreen || | |
182 | document.documentElement.webkitRequestFullscreen || | |
183 | document.body.msRequestFullscreen)) { | |
e40978c7 PO |
184 | document.getElementById('noVNC_fullscreen_button') |
185 | .classList.remove("noVNC_hidden"); | |
0f6af1e3 | 186 | UI.addFullscreenHandlers(); |
d9fc1c7b | 187 | } |
e543525f SR |
188 | }, |
189 | ||
0f6af1e3 | 190 | addControlbarHandlers: function() { |
d7f79071 | 191 | document.getElementById("noVNC_view_drag_button") |
192 | .addEventListener('click', UI.toggleViewDrag); | |
a49d9298 | 193 | document.getElementById("noVNC_send_ctrl_alt_del_button") |
0f6af1e3 | 194 | .addEventListener('click', UI.sendCtrlAltDel); |
0f6af1e3 | 195 | }, |
196 | ||
197 | addTouchSpecificHandlers: function() { | |
d7f79071 | 198 | document.getElementById("noVNC_mouse_button0") |
199 | .addEventListener('click', function () { UI.setMouseButton(1); }); | |
200 | document.getElementById("noVNC_mouse_button1") | |
201 | .addEventListener('click', function () { UI.setMouseButton(2); }); | |
202 | document.getElementById("noVNC_mouse_button2") | |
203 | .addEventListener('click', function () { UI.setMouseButton(4); }); | |
204 | document.getElementById("noVNC_mouse_button4") | |
205 | .addEventListener('click', function () { UI.setMouseButton(0); }); | |
206 | document.getElementById("noVNC_keyboard_button") | |
207 | .addEventListener('click', UI.showKeyboard); | |
208 | ||
209 | document.getElementById("noVNC_keyboardinput") | |
210 | .addEventListener('input', UI.keyInput); | |
211 | document.getElementById("noVNC_keyboardinput") | |
212 | .addEventListener('blur', UI.hideKeyboard); | |
213 | document.getElementById("noVNC_keyboardinput") | |
214 | .addEventListener('submit', function () { return false; }); | |
215 | ||
0f6af1e3 | 216 | window.addEventListener('load', UI.keyboardinputReset); |
ebbec43a | 217 | }, |
0f6af1e3 | 218 | |
ebbec43a | 219 | addExtraKeysHandlers: function() { |
a49d9298 | 220 | document.getElementById("noVNC_toggle_extra_keys_button") |
d7f79071 | 221 | .addEventListener('click', UI.toggleExtraKeys); |
a49d9298 | 222 | document.getElementById("noVNC_toggle_ctrl_button") |
d7f79071 | 223 | .addEventListener('click', UI.toggleCtrl); |
a49d9298 | 224 | document.getElementById("noVNC_toggle_alt_button") |
d7f79071 | 225 | .addEventListener('click', UI.toggleAlt); |
a49d9298 | 226 | document.getElementById("noVNC_send_tab_button") |
d7f79071 | 227 | .addEventListener('click', UI.sendTab); |
a49d9298 | 228 | document.getElementById("noVNC_send_esc_button") |
d7f79071 | 229 | .addEventListener('click', UI.sendEsc); |
0f6af1e3 | 230 | }, |
d7f79071 | 231 | |
0f6af1e3 | 232 | addXvpHandlers: function() { |
a49d9298 | 233 | document.getElementById("noVNC_xvp_shutdown_button") |
d7f79071 | 234 | .addEventListener('click', function() { UI.rfb.xvpShutdown(); }); |
a49d9298 | 235 | document.getElementById("noVNC_xvp_reboot_button") |
d7f79071 | 236 | .addEventListener('click', function() { UI.rfb.xvpReboot(); }); |
a49d9298 | 237 | document.getElementById("noVNC_xvp_reset_button") |
d7f79071 | 238 | .addEventListener('click', function() { UI.rfb.xvpReset(); }); |
a49d9298 | 239 | document.getElementById("noVNC_xvp_button") |
d7f79071 | 240 | .addEventListener('click', UI.toggleXvpPanel); |
0f6af1e3 | 241 | }, |
242 | ||
243 | addConnectionControlHandlers: function() { | |
a49d9298 | 244 | document.getElementById("noVNC_connect_controls_button") |
d7f79071 | 245 | .addEventListener('click', UI.toggleConnectPanel); |
246 | document.getElementById("noVNC_disconnect_button") | |
247 | .addEventListener('click', UI.disconnect); | |
0f6af1e3 | 248 | document.getElementById("noVNC_connect_button") |
249 | .addEventListener('click', UI.connect); | |
8a7ec6ea SM |
250 | |
251 | document.getElementById("noVNC_password_button") | |
252 | .addEventListener('click', UI.setPassword); | |
0f6af1e3 | 253 | }, |
d7f79071 | 254 | |
0f6af1e3 | 255 | addClipboardHandlers: function() { |
256 | document.getElementById("noVNC_clipboard_button") | |
257 | .addEventListener('click', UI.toggleClipboardPanel); | |
d7f79071 | 258 | document.getElementById("noVNC_clipboard_text") |
259 | .addEventListener('focus', UI.displayBlur); | |
260 | document.getElementById("noVNC_clipboard_text") | |
261 | .addEventListener('blur', UI.displayFocus); | |
262 | document.getElementById("noVNC_clipboard_text") | |
263 | .addEventListener('change', UI.clipboardSend); | |
264 | document.getElementById("noVNC_clipboard_clear_button") | |
265 | .addEventListener('click', UI.clipboardClear); | |
0f6af1e3 | 266 | }, |
d7f79071 | 267 | |
0f6af1e3 | 268 | addSettingsHandlers: function() { |
269 | document.getElementById("noVNC_settings_button") | |
270 | .addEventListener('click', UI.toggleSettingsPanel); | |
d7f79071 | 271 | document.getElementById("noVNC_settings_apply") |
272 | .addEventListener('click', UI.settingsApply); | |
273 | ||
d7f79071 | 274 | document.getElementById("noVNC_setting_resize") |
275 | .addEventListener('change', UI.enableDisableViewClip); | |
bbbf42bb SR |
276 | }, |
277 | ||
0f6af1e3 | 278 | addFullscreenHandlers: function() { |
279 | document.getElementById("noVNC_fullscreen_button") | |
280 | .addEventListener('click', UI.toggleFullscreen); | |
281 | ||
282 | window.addEventListener('fullscreenchange', UI.updateFullscreenButton); | |
283 | window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton); | |
284 | window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton); | |
285 | window.addEventListener('msfullscreenchange', UI.updateFullscreenButton); | |
286 | }, | |
287 | ||
288 | initRFB: function() { | |
289 | try { | |
290 | UI.rfb = new RFB({'target': document.getElementById('noVNC_canvas'), | |
291 | 'onUpdateState': UI.updateState, | |
292 | 'onXvpInit': UI.updateXvpButton, | |
293 | 'onClipboard': UI.clipboardReceive, | |
294 | 'onFBUComplete': UI.initialResize, | |
295 | 'onFBResize': UI.updateViewDrag, | |
296 | 'onDesktopName': UI.updateDocumentTitle}); | |
297 | return true; | |
298 | } catch (exc) { | |
299 | UI.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc); | |
300 | return false; | |
301 | } | |
302 | }, | |
303 | ||
95dd6001 | 304 | /* ------^------- |
305 | * /INIT | |
306 | * ============== | |
307 | * VISUAL | |
308 | * ------v------*/ | |
58ded70d | 309 | |
29475d77 | 310 | updateState: function(rfb, state, oldstate, msg) { |
311 | UI.rfb_state = state; | |
fdedbafb | 312 | |
29475d77 | 313 | if (typeof(msg) !== 'undefined') { |
8d7708c8 SM |
314 | switch (state) { |
315 | case 'failed': | |
316 | case 'fatal': | |
317 | // zero means no timeout | |
318 | UI.showStatus(msg, 'error', 0); | |
319 | break; | |
320 | case 'normal': | |
321 | /* falls through */ | |
322 | case 'disconnected': | |
323 | case 'loaded': | |
324 | UI.showStatus(msg, 'normal'); | |
325 | break; | |
326 | case 'password': | |
8a7ec6ea SM |
327 | document.getElementById('noVNC_password_dlg') |
328 | .classList.add('noVNC_open'); | |
329 | setTimeout(function () { | |
330 | document.getElementById(('noVNC_password_input').focus()); | |
331 | }, 100); | |
8d7708c8 SM |
332 | |
333 | UI.showStatus(msg, 'warn'); | |
334 | break; | |
335 | default: | |
336 | UI.showStatus(msg, 'warn'); | |
337 | break; | |
338 | } | |
fdedbafb | 339 | } |
29475d77 | 340 | |
341 | UI.updateVisualState(); | |
fdedbafb | 342 | }, |
343 | ||
29475d77 | 344 | // Disable/enable controls depending on connection state |
345 | updateVisualState: function() { | |
346 | var connected = UI.rfb && UI.rfb_state === 'normal'; | |
fdedbafb | 347 | |
29475d77 | 348 | //Util.Debug(">> updateVisualState"); |
ae510306 SR |
349 | document.getElementById('noVNC_setting_encrypt').disabled = connected; |
350 | document.getElementById('noVNC_setting_true_color').disabled = connected; | |
29475d77 | 351 | if (Util.browserSupportsCursorURIs()) { |
ae510306 | 352 | document.getElementById('noVNC_setting_cursor').disabled = connected; |
29475d77 | 353 | } else { |
354 | UI.updateSetting('cursor', !UI.isTouchDevice); | |
ae510306 | 355 | document.getElementById('noVNC_setting_cursor').disabled = true; |
29475d77 | 356 | } |
fdedbafb | 357 | |
29475d77 | 358 | UI.enableDisableViewClip(); |
ae510306 SR |
359 | document.getElementById('noVNC_setting_resize').disabled = connected; |
360 | document.getElementById('noVNC_setting_shared').disabled = connected; | |
361 | document.getElementById('noVNC_setting_view_only').disabled = connected; | |
362 | document.getElementById('noVNC_setting_path').disabled = connected; | |
363 | document.getElementById('noVNC_setting_repeaterID').disabled = connected; | |
fdedbafb | 364 | |
29475d77 | 365 | if (connected) { |
6244e383 | 366 | document.documentElement.classList.add("noVNC_connected"); |
f0d9ab96 | 367 | UI.updateViewClip(); |
29475d77 | 368 | UI.setMouseButton(1); |
29475d77 | 369 | } else { |
6244e383 | 370 | document.documentElement.classList.remove("noVNC_connected"); |
9e45354e | 371 | UI.updateXvpButton(0); |
29475d77 | 372 | } |
fdedbafb | 373 | |
29475d77 | 374 | // State change disables viewport dragging. |
375 | // It is enabled (toggled) by direct click on the button | |
f0d9ab96 | 376 | UI.setViewDrag(false); |
29475d77 | 377 | |
8a7ec6ea SM |
378 | // State change also closes the password dialog |
379 | document.getElementById('noVNC_password_dlg') | |
380 | .classList.remove('noVNC_open'); | |
381 | ||
29475d77 | 382 | switch (UI.rfb_state) { |
383 | case 'fatal': | |
384 | case 'failed': | |
385 | case 'disconnected': | |
ed8cbe4e | 386 | UI.openConnectPanel(); |
29475d77 | 387 | break; |
388 | case 'loaded': | |
29475d77 | 389 | break; |
390 | default: | |
29475d77 | 391 | break; |
392 | } | |
393 | ||
394 | //Util.Debug("<< updateVisualState"); | |
395 | }, | |
396 | ||
8d7708c8 | 397 | showStatus: function(text, status_type, time) { |
ca5c74ad | 398 | var statusElem = document.getElementById('noVNC_status'); |
4e471b5b | 399 | |
ca5c74ad | 400 | clearTimeout(UI.statusTimeout); |
4e471b5b | 401 | |
8d7708c8 SM |
402 | if (typeof status_type === 'undefined') { |
403 | status_type = 'normal'; | |
404 | } | |
405 | ||
406 | statusElem.classList.remove("noVNC_status_normal", | |
407 | "noVNC_status_warn", | |
408 | "noVNC_status_error"); | |
409 | ||
410 | switch (status_type) { | |
411 | case 'warning': | |
412 | case 'warn': | |
413 | statusElem.classList.add("noVNC_status_warn"); | |
414 | break; | |
415 | case 'error': | |
416 | statusElem.classList.add("noVNC_status_error"); | |
417 | break; | |
418 | case 'normal': | |
419 | case 'info': | |
420 | default: | |
421 | statusElem.classList.add("noVNC_status_normal"); | |
422 | break; | |
423 | } | |
424 | ||
ca5c74ad | 425 | statusElem.innerHTML = text; |
426 | statusElem.classList.add("noVNC_open"); | |
427 | ||
428 | // If no time was specified, show the status for 1.5 seconds | |
429 | if (typeof time === 'undefined') { | |
430 | time = 1500; | |
fdedbafb | 431 | } |
4e471b5b | 432 | |
ca5c74ad | 433 | // A specified time of zero means no timeout |
434 | if (time != 0) { | |
435 | UI.statusTimeout = window.setTimeout(UI.hideStatus, time); | |
436 | } | |
fdedbafb | 437 | }, |
438 | ||
ca5c74ad | 439 | hideStatus: function() { |
440 | clearTimeout(UI.statusTimeout); | |
441 | document.getElementById('noVNC_status').classList.remove("noVNC_open"); | |
4e471b5b | 442 | }, |
443 | ||
95dd6001 | 444 | /* ------^------- |
445 | * /VISUAL | |
446 | * ============== | |
447 | * SETTINGS | |
448 | * ------v------*/ | |
449 | ||
45c70c9e | 450 | // Initial page load read/initialization of settings |
451 | initSetting: function(name, defVal) { | |
452 | // Check Query string followed by cookie | |
453 | var val = WebUtil.getConfigVar(name); | |
454 | if (val === null) { | |
455 | val = WebUtil.readSetting(name, defVal); | |
bbbf42bb | 456 | } |
45c70c9e | 457 | UI.updateSetting(name, val); |
bbbf42bb SR |
458 | return val; |
459 | }, | |
460 | ||
461 | // Update cookie and form control setting. If value is not set, then | |
462 | // updates from control to current cookie setting. | |
463 | updateSetting: function(name, value) { | |
464 | ||
465 | // Save the cookie for this session | |
466 | if (typeof value !== 'undefined') { | |
467 | WebUtil.writeSetting(name, value); | |
468 | } | |
469 | ||
470 | // Update the settings control | |
471 | value = UI.getSetting(name); | |
472 | ||
ae510306 | 473 | var ctrl = document.getElementById('noVNC_setting_' + name); |
bbbf42bb SR |
474 | if (ctrl.type === 'checkbox') { |
475 | ctrl.checked = value; | |
476 | ||
477 | } else if (typeof ctrl.options !== 'undefined') { | |
478 | for (var i = 0; i < ctrl.options.length; i += 1) { | |
479 | if (ctrl.options[i].value === value) { | |
480 | ctrl.selectedIndex = i; | |
481 | break; | |
482 | } | |
483 | } | |
484 | } else { | |
485 | /*Weird IE9 error leads to 'null' appearring | |
486 | in textboxes instead of ''.*/ | |
487 | if (value === null) { | |
488 | value = ""; | |
489 | } | |
490 | ctrl.value = value; | |
491 | } | |
492 | }, | |
493 | ||
494 | // Save control setting to cookie | |
495 | saveSetting: function(name) { | |
ae510306 | 496 | var val, ctrl = document.getElementById('noVNC_setting_' + name); |
bbbf42bb SR |
497 | if (ctrl.type === 'checkbox') { |
498 | val = ctrl.checked; | |
499 | } else if (typeof ctrl.options !== 'undefined') { | |
500 | val = ctrl.options[ctrl.selectedIndex].value; | |
501 | } else { | |
502 | val = ctrl.value; | |
503 | } | |
504 | WebUtil.writeSetting(name, val); | |
505 | //Util.Debug("Setting saved '" + name + "=" + val + "'"); | |
506 | return val; | |
507 | }, | |
508 | ||
bbbf42bb SR |
509 | // Force a setting to be a certain value |
510 | forceSetting: function(name, val) { | |
511 | UI.updateSetting(name, val); | |
512 | return val; | |
513 | }, | |
514 | ||
45c70c9e | 515 | // Read form control compatible setting from cookie |
516 | getSetting: function(name) { | |
ae510306 | 517 | var ctrl = document.getElementById('noVNC_setting_' + name); |
45c70c9e | 518 | var val = WebUtil.readSetting(name); |
519 | if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') { | |
520 | if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) { | |
521 | val = false; | |
4f19e5c6 | 522 | } else { |
45c70c9e | 523 | val = true; |
4f19e5c6 | 524 | } |
bbbf42bb | 525 | } |
bbbf42bb | 526 | return val; |
bbbf42bb SR |
527 | }, |
528 | ||
95dd6001 | 529 | // Save/apply settings when 'Apply' button is pressed |
530 | settingsApply: function() { | |
531 | //Util.Debug(">> settingsApply"); | |
532 | UI.saveSetting('encrypt'); | |
533 | UI.saveSetting('true_color'); | |
534 | if (Util.browserSupportsCursorURIs()) { | |
535 | UI.saveSetting('cursor'); | |
bbbf42bb | 536 | } |
bbbf42bb | 537 | |
95dd6001 | 538 | UI.saveSetting('resize'); |
bbbf42bb | 539 | |
95dd6001 | 540 | if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') { |
541 | UI.forceSetting('clip', false); | |
7d1dc09a | 542 | } |
7d1dc09a | 543 | |
95dd6001 | 544 | UI.saveSetting('clip'); |
545 | UI.saveSetting('shared'); | |
546 | UI.saveSetting('view_only'); | |
547 | UI.saveSetting('path'); | |
548 | UI.saveSetting('repeaterID'); | |
549 | UI.saveSetting('stylesheet'); | |
550 | UI.saveSetting('logging'); | |
551 | ||
552 | // Settings with immediate (non-connected related) effect | |
553 | WebUtil.selectStylesheet(UI.getSetting('stylesheet')); | |
554 | WebUtil.init_logging(UI.getSetting('logging')); | |
f0d9ab96 | 555 | UI.updateViewClip(); |
95dd6001 | 556 | UI.updateViewDrag(); |
557 | //Util.Debug("<< settingsApply"); | |
7d1dc09a | 558 | }, |
559 | ||
ed8cbe4e PO |
560 | /* ------^------- |
561 | * /SETTINGS | |
562 | * ============== | |
563 | * PANELS | |
564 | * ------v------*/ | |
565 | ||
566 | closeAllPanels: function() { | |
567 | UI.closeSettingsPanel(); | |
568 | UI.closeXvpPanel(); | |
569 | UI.closeClipboardPanel(); | |
570 | UI.closeConnectPanel(); | |
fb7c3b3b | 571 | UI.closeExtraKeys(); |
ed8cbe4e PO |
572 | }, |
573 | ||
574 | /* ------^------- | |
575 | * /PANELS | |
576 | * ============== | |
577 | * SETTINGS (panel) | |
578 | * ------v------*/ | |
579 | ||
580 | openSettingsPanel: function() { | |
581 | UI.closeAllPanels(); | |
582 | ||
583 | UI.updateSetting('encrypt'); | |
584 | UI.updateSetting('true_color'); | |
585 | if (Util.browserSupportsCursorURIs()) { | |
586 | UI.updateSetting('cursor'); | |
587 | } else { | |
588 | UI.updateSetting('cursor', !UI.isTouchDevice); | |
589 | document.getElementById('noVNC_setting_cursor').disabled = true; | |
bbbf42bb | 590 | } |
ed8cbe4e PO |
591 | UI.updateSetting('clip'); |
592 | UI.updateSetting('resize'); | |
593 | UI.updateSetting('shared'); | |
594 | UI.updateSetting('view_only'); | |
595 | UI.updateSetting('path'); | |
596 | UI.updateSetting('repeaterID'); | |
597 | UI.updateSetting('stylesheet'); | |
598 | UI.updateSetting('logging'); | |
599 | ||
e40978c7 PO |
600 | document.getElementById('noVNC_settings') |
601 | .classList.add("noVNC_open"); | |
602 | document.getElementById('noVNC_settings_button') | |
d9e86214 | 603 | .classList.add("noVNC_selected"); |
45c70c9e | 604 | }, |
bbbf42bb | 605 | |
ed8cbe4e | 606 | closeSettingsPanel: function() { |
e40978c7 PO |
607 | document.getElementById('noVNC_settings') |
608 | .classList.remove("noVNC_open"); | |
609 | document.getElementById('noVNC_settings_button') | |
d9e86214 | 610 | .classList.remove("noVNC_selected"); |
bbbf42bb SR |
611 | }, |
612 | ||
613 | // Toggle the settings menu: | |
614 | // On open, settings are refreshed from saved cookies. | |
615 | // On close, settings are applied | |
616 | toggleSettingsPanel: function() { | |
ed8cbe4e PO |
617 | if (document.getElementById('noVNC_settings') |
618 | .classList.contains("noVNC_open")) { | |
bbbf42bb | 619 | UI.settingsApply(); |
ed8cbe4e | 620 | UI.closeSettingsPanel(); |
bbbf42bb | 621 | } else { |
ed8cbe4e | 622 | UI.openSettingsPanel(); |
bbbf42bb SR |
623 | } |
624 | }, | |
625 | ||
95dd6001 | 626 | /* ------^------- |
627 | * /SETTINGS | |
628 | * ============== | |
629 | * XVP | |
630 | * ------v------*/ | |
631 | ||
ed8cbe4e PO |
632 | openXvpPanel: function() { |
633 | UI.closeAllPanels(); | |
634 | ||
635 | document.getElementById('noVNC_xvp') | |
636 | .classList.add("noVNC_open"); | |
637 | document.getElementById('noVNC_xvp_button') | |
638 | .classList.add("noVNC_selected"); | |
639 | }, | |
640 | ||
641 | closeXvpPanel: function() { | |
642 | document.getElementById('noVNC_xvp') | |
643 | .classList.remove("noVNC_open"); | |
644 | document.getElementById('noVNC_xvp_button') | |
645 | .classList.remove("noVNC_selected"); | |
646 | }, | |
647 | ||
bbbf42bb | 648 | toggleXvpPanel: function() { |
ed8cbe4e PO |
649 | if (document.getElementById('noVNC_xvp') |
650 | .classList.contains("noVNC_open")) { | |
651 | UI.closeXvpPanel(); | |
bbbf42bb | 652 | } else { |
ed8cbe4e | 653 | UI.openXvpPanel(); |
bbbf42bb | 654 | } |
bbbf42bb SR |
655 | }, |
656 | ||
657 | // Disable/enable XVP button | |
9e45354e | 658 | updateXvpButton: function(ver) { |
bbbf42bb | 659 | if (ver >= 1) { |
a49d9298 | 660 | document.getElementById('noVNC_xvp_button') |
e40978c7 | 661 | .classList.remove("noVNC_hidden"); |
bbbf42bb | 662 | } else { |
a49d9298 | 663 | document.getElementById('noVNC_xvp_button') |
e40978c7 | 664 | .classList.add("noVNC_hidden"); |
bbbf42bb | 665 | // Close XVP panel if open |
ed8cbe4e | 666 | UI.closeXvpPanel(); |
bbbf42bb SR |
667 | } |
668 | }, | |
669 | ||
95dd6001 | 670 | /* ------^------- |
671 | * /XVP | |
672 | * ============== | |
673 | * CLIPBOARD | |
674 | * ------v------*/ | |
f8b399d7 | 675 | |
ed8cbe4e PO |
676 | openClipboardPanel: function() { |
677 | UI.closeAllPanels(); | |
678 | ||
679 | document.getElementById('noVNC_clipboard') | |
680 | .classList.add("noVNC_open"); | |
681 | document.getElementById('noVNC_clipboard_button') | |
682 | .classList.add("noVNC_selected"); | |
683 | }, | |
684 | ||
685 | closeClipboardPanel: function() { | |
686 | document.getElementById('noVNC_clipboard') | |
687 | .classList.remove("noVNC_open"); | |
688 | document.getElementById('noVNC_clipboard_button') | |
689 | .classList.remove("noVNC_selected"); | |
690 | }, | |
691 | ||
bbbf42bb | 692 | toggleClipboardPanel: function() { |
ed8cbe4e PO |
693 | if (document.getElementById('noVNC_clipboard') |
694 | .classList.contains("noVNC_open")) { | |
695 | UI.closeClipboardPanel(); | |
bbbf42bb | 696 | } else { |
ed8cbe4e | 697 | UI.openClipboardPanel(); |
bbbf42bb | 698 | } |
bbbf42bb SR |
699 | }, |
700 | ||
4d26f58e | 701 | clipboardReceive: function(rfb, text) { |
702 | Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "..."); | |
ae510306 | 703 | document.getElementById('noVNC_clipboard_text').value = text; |
4d26f58e | 704 | Util.Debug("<< UI.clipboardReceive"); |
95dd6001 | 705 | }, |
706 | ||
4d26f58e | 707 | clipboardClear: function() { |
ae510306 | 708 | document.getElementById('noVNC_clipboard_text').value = ""; |
95dd6001 | 709 | UI.rfb.clipboardPasteFrom(""); |
710 | }, | |
711 | ||
4d26f58e | 712 | clipboardSend: function() { |
ae510306 | 713 | var text = document.getElementById('noVNC_clipboard_text').value; |
4d26f58e | 714 | Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "..."); |
95dd6001 | 715 | UI.rfb.clipboardPasteFrom(text); |
4d26f58e | 716 | Util.Debug("<< UI.clipboardSend"); |
95dd6001 | 717 | }, |
718 | ||
719 | /* ------^------- | |
720 | * /CLIPBOARD | |
721 | * ============== | |
722 | * CONNECTION | |
723 | * ------v------*/ | |
724 | ||
ed8cbe4e PO |
725 | openConnectPanel: function() { |
726 | UI.closeAllPanels(); | |
ab81ddf5 | 727 | |
ed8cbe4e PO |
728 | document.getElementById('noVNC_connect_controls') |
729 | .classList.add("noVNC_open"); | |
730 | document.getElementById('noVNC_connect_controls_button') | |
731 | .classList.add("noVNC_selected"); | |
732 | ||
733 | document.getElementById('noVNC_setting_host').focus(); | |
734 | }, | |
735 | ||
736 | closeConnectPanel: function() { | |
737 | document.getElementById('noVNC_connect_controls') | |
738 | .classList.remove("noVNC_open"); | |
739 | document.getElementById('noVNC_connect_controls_button') | |
740 | .classList.remove("noVNC_selected"); | |
741 | ||
742 | UI.saveSetting('host'); | |
743 | UI.saveSetting('port'); | |
744 | UI.saveSetting('token'); | |
745 | //UI.saveSetting('password'); | |
746 | }, | |
747 | ||
748 | toggleConnectPanel: function() { | |
749 | if (document.getElementById('noVNC_connect_controls') | |
750 | .classList.contains("noVNC_open")) { | |
751 | UI.closeConnectPanel(); | |
ab81ddf5 | 752 | } else { |
ed8cbe4e | 753 | UI.openConnectPanel(); |
ab81ddf5 | 754 | } |
bbbf42bb SR |
755 | }, |
756 | ||
757 | connect: function() { | |
ed8cbe4e | 758 | UI.closeAllPanels(); |
f00b1e37 | 759 | |
ae510306 SR |
760 | var host = document.getElementById('noVNC_setting_host').value; |
761 | var port = document.getElementById('noVNC_setting_port').value; | |
762 | var password = document.getElementById('noVNC_setting_password').value; | |
763 | var token = document.getElementById('noVNC_setting_token').value; | |
764 | var path = document.getElementById('noVNC_setting_path').value; | |
c55f05f6 MXPN |
765 | |
766 | //if token is in path then ignore the new token variable | |
767 | if (token) { | |
768 | path = WebUtil.injectParamIfMissing(path, "token", token); | |
769 | } | |
770 | ||
bbbf42bb SR |
771 | if ((!host) || (!port)) { |
772 | throw new Error("Must set host and port"); | |
773 | } | |
53fc7392 | 774 | |
d9fc1c7b | 775 | if (!UI.initRFB()) return; |
58ded70d | 776 | |
bbbf42bb SR |
777 | UI.rfb.set_encrypt(UI.getSetting('encrypt')); |
778 | UI.rfb.set_true_color(UI.getSetting('true_color')); | |
779 | UI.rfb.set_local_cursor(UI.getSetting('cursor')); | |
780 | UI.rfb.set_shared(UI.getSetting('shared')); | |
781 | UI.rfb.set_view_only(UI.getSetting('view_only')); | |
782 | UI.rfb.set_repeaterID(UI.getSetting('repeaterID')); | |
53fc7392 | 783 | |
bbbf42bb | 784 | UI.rfb.connect(host, port, password, path); |
bbbf42bb | 785 | }, |
5299db1a | 786 | |
bbbf42bb | 787 | disconnect: function() { |
ed8cbe4e | 788 | UI.closeAllPanels(); |
bbbf42bb | 789 | UI.rfb.disconnect(); |
8e0f0088 | 790 | |
f8b399d7 | 791 | // Restore the callback used for initial resize |
ab81ddf5 | 792 | UI.rfb.set_onFBUComplete(UI.initialResize); |
f8b399d7 | 793 | |
e543525f | 794 | // Don't display the connection settings until we're actually disconnected |
bbbf42bb SR |
795 | }, |
796 | ||
95dd6001 | 797 | setPassword: function() { |
8a7ec6ea SM |
798 | UI.rfb.sendPassword(document.getElementById('noVNC_password_input').value); |
799 | document.getElementById('noVNC_password_dlg') | |
800 | .classList.remove('noVNC_open'); | |
95dd6001 | 801 | return false; |
802 | }, | |
58ded70d | 803 | |
95dd6001 | 804 | /* ------^------- |
805 | * /CONNECTION | |
806 | * ============== | |
807 | * FULLSCREEN | |
808 | * ------v------*/ | |
809 | ||
7d1dc09a | 810 | toggleFullscreen: function() { |
811 | if (document.fullscreenElement || // alternative standard method | |
812 | document.mozFullScreenElement || // currently working methods | |
813 | document.webkitFullscreenElement || | |
a6357e82 | 814 | document.msFullscreenElement) { |
7d1dc09a | 815 | if (document.exitFullscreen) { |
816 | document.exitFullscreen(); | |
817 | } else if (document.mozCancelFullScreen) { | |
818 | document.mozCancelFullScreen(); | |
819 | } else if (document.webkitExitFullscreen) { | |
820 | document.webkitExitFullscreen(); | |
821 | } else if (document.msExitFullscreen) { | |
822 | document.msExitFullscreen(); | |
823 | } | |
824 | } else { | |
825 | if (document.documentElement.requestFullscreen) { | |
826 | document.documentElement.requestFullscreen(); | |
827 | } else if (document.documentElement.mozRequestFullScreen) { | |
828 | document.documentElement.mozRequestFullScreen(); | |
829 | } else if (document.documentElement.webkitRequestFullscreen) { | |
830 | document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); | |
831 | } else if (document.body.msRequestFullscreen) { | |
832 | document.body.msRequestFullscreen(); | |
833 | } | |
834 | } | |
a6357e82 | 835 | UI.enableDisableViewClip(); |
7d1dc09a | 836 | UI.updateFullscreenButton(); |
bbbf42bb SR |
837 | }, |
838 | ||
7d1dc09a | 839 | updateFullscreenButton: function() { |
840 | if (document.fullscreenElement || // alternative standard method | |
841 | document.mozFullScreenElement || // currently working methods | |
842 | document.webkitFullscreenElement || | |
843 | document.msFullscreenElement ) { | |
a49d9298 | 844 | document.getElementById('noVNC_fullscreen_button') |
d9e86214 | 845 | .classList.add("noVNC_selected"); |
7d1dc09a | 846 | } else { |
a49d9298 | 847 | document.getElementById('noVNC_fullscreen_button') |
d9e86214 | 848 | .classList.remove("noVNC_selected"); |
7d1dc09a | 849 | } |
850 | }, | |
851 | ||
95dd6001 | 852 | /* ------^------- |
853 | * /FULLSCREEN | |
854 | * ============== | |
855 | * RESIZE | |
856 | * ------v------*/ | |
777cb7a0 | 857 | |
858 | // Apply remote resizing or local scaling | |
0bd2cbac | 859 | applyResizeMode: function() { |
58ded70d SR |
860 | if (!UI.rfb) return; |
861 | ||
777cb7a0 | 862 | var screen = UI.screenSize(); |
863 | ||
864 | if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) { | |
865 | ||
866 | var display = UI.rfb.get_display(); | |
867 | var resizeMode = UI.getSetting('resize'); | |
868 | ||
869 | if (resizeMode === 'remote') { | |
870 | ||
871 | // Request changing the resolution of the remote display to | |
872 | // the size of the local browser viewport. | |
873 | ||
874 | // In order to not send multiple requests before the browser-resize | |
875 | // is finished we wait 0.5 seconds before sending the request. | |
876 | clearTimeout(UI.resizeTimeout); | |
877 | UI.resizeTimeout = setTimeout(function(){ | |
878 | ||
879 | // Limit the viewport to the size of the browser window | |
880 | display.set_maxWidth(screen.w); | |
881 | display.set_maxHeight(screen.h); | |
882 | ||
5fd3f88e | 883 | Util.Debug('Attempting requestDesktopSize(' + |
777cb7a0 | 884 | screen.w + ', ' + screen.h + ')'); |
885 | ||
886 | // Request a remote size covering the viewport | |
5fd3f88e | 887 | UI.rfb.requestDesktopSize(screen.w, screen.h); |
777cb7a0 | 888 | }, 500); |
889 | ||
890 | } else if (resizeMode === 'scale' || resizeMode === 'downscale') { | |
891 | var downscaleOnly = resizeMode === 'downscale'; | |
892 | var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly); | |
893 | UI.rfb.get_mouse().set_scale(scaleRatio); | |
894 | Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale()); | |
895 | } | |
896 | } | |
bbbf42bb SR |
897 | }, |
898 | ||
777cb7a0 | 899 | // The screen is always the same size as the available viewport |
900 | // in the browser window minus the height of the control bar | |
0bd2cbac | 901 | screenSize: function() { |
ae510306 | 902 | var screen = document.getElementById('noVNC_screen'); |
777cb7a0 | 903 | |
904 | // Hide the scrollbars until the size is calculated | |
905 | screen.style.overflow = "hidden"; | |
906 | ||
907 | var pos = Util.getPosition(screen); | |
908 | var w = pos.width; | |
909 | var h = pos.height; | |
910 | ||
911 | screen.style.overflow = "visible"; | |
912 | ||
913 | if (isNaN(w) || isNaN(h)) { | |
914 | return false; | |
915 | } else { | |
916 | return {w: w, h: h}; | |
917 | } | |
bbbf42bb SR |
918 | }, |
919 | ||
777cb7a0 | 920 | // Normally we only apply the current resize mode after a window resize |
921 | // event. This means that when a new connection is opened, there is no | |
922 | // resize mode active. | |
923 | // We have to wait until the first FBU because this is where the client | |
924 | // will find the supported encodings of the server. Some calls later in | |
925 | // the chain is dependant on knowing the server-capabilities. | |
926 | initialResize: function(rfb, fbu) { | |
927 | UI.applyResizeMode(); | |
928 | // After doing this once, we remove the callback. | |
929 | UI.rfb.set_onFBUComplete(function() { }); | |
bbbf42bb SR |
930 | }, |
931 | ||
95dd6001 | 932 | /* ------^------- |
933 | * /RESIZE | |
934 | * ============== | |
935 | * CLIPPING | |
936 | * ------v------*/ | |
937 | ||
30bfff81 | 938 | // Set and configure viewport clipping |
bbbf42bb | 939 | setViewClip: function(clip) { |
f0d9ab96 | 940 | UI.updateSetting('clip', clip); |
941 | UI.updateViewClip(); | |
942 | }, | |
943 | ||
944 | // Update parameters that depend on the clip setting | |
945 | updateViewClip: function() { | |
bbbf42bb | 946 | var display; |
f0d9ab96 | 947 | if (!UI.rfb) { |
bbbf42bb SR |
948 | return; |
949 | } | |
8e0f0088 | 950 | |
f0d9ab96 | 951 | var display = UI.rfb.get_display(); |
bbbf42bb | 952 | var cur_clip = display.get_viewport(); |
f0d9ab96 | 953 | var new_clip = UI.getSetting('clip'); |
bbbf42bb | 954 | |
f0d9ab96 | 955 | if (cur_clip !== new_clip) { |
956 | display.set_viewport(new_clip); | |
bbbf42bb | 957 | } |
8e0f0088 | 958 | |
f0d9ab96 | 959 | var size = UI.screenSize(); |
fdedbafb | 960 | |
f0d9ab96 | 961 | if (new_clip && size) { |
962 | // When clipping is enabled, the screen is limited to | |
963 | // the size of the browser window. | |
964 | display.set_maxWidth(size.w); | |
965 | display.set_maxHeight(size.h); | |
fdedbafb | 966 | |
f0d9ab96 | 967 | var screen = document.getElementById('noVNC_screen'); |
968 | var canvas = document.getElementById('noVNC_canvas'); | |
fdedbafb | 969 | |
f0d9ab96 | 970 | // Hide potential scrollbars that can skew the position |
971 | screen.style.overflow = "hidden"; | |
fdedbafb | 972 | |
f0d9ab96 | 973 | // The x position marks the left margin of the canvas, |
974 | // remove the margin from both sides to keep it centered. | |
975 | var new_w = size.w - (2 * Util.getPosition(canvas).x); | |
fdedbafb | 976 | |
f0d9ab96 | 977 | screen.style.overflow = "visible"; |
978 | ||
979 | display.viewportChangeSize(new_w, size.h); | |
980 | } else { | |
981 | // Disable max dimensions | |
982 | display.set_maxWidth(0); | |
983 | display.set_maxHeight(0); | |
984 | display.viewportChangeSize(); | |
bbbf42bb SR |
985 | } |
986 | }, | |
987 | ||
30bfff81 | 988 | // Handle special cases where clipping is forced on/off or locked |
0bd2cbac | 989 | enableDisableViewClip: function() { |
ae510306 | 990 | var resizeSetting = document.getElementById('noVNC_setting_resize'); |
a6357e82 | 991 | var connected = UI.rfb && UI.rfb_state === 'normal'; |
992 | ||
f620259b | 993 | if (UI.isSafari) { |
994 | // Safari auto-hides the scrollbars which makes them | |
995 | // impossible to use in most cases | |
996 | UI.setViewClip(true); | |
ae510306 | 997 | document.getElementById('noVNC_setting_clip').disabled = true; |
682fd02b | 998 | } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') { |
a6357e82 | 999 | // Disable clipping if we are scaling |
1000 | UI.setViewClip(false); | |
ae510306 | 1001 | document.getElementById('noVNC_setting_clip').disabled = true; |
a6357e82 | 1002 | } else if (document.msFullscreenElement) { |
1003 | // The browser is IE and we are in fullscreen mode. | |
1004 | // - We need to force clipping while in fullscreen since | |
1005 | // scrollbars doesn't work. | |
ca5c74ad | 1006 | UI.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen"); |
a6357e82 | 1007 | UI.rememberedClipSetting = UI.getSetting('clip'); |
1008 | UI.setViewClip(true); | |
ae510306 | 1009 | document.getElementById('noVNC_setting_clip').disabled = true; |
a6357e82 | 1010 | } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) { |
1011 | // Restore view clip to what it was before fullscreen on IE | |
1012 | UI.setViewClip(UI.rememberedClipSetting); | |
ae510306 | 1013 | document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice; |
30bfff81 | 1014 | } else { |
ae510306 | 1015 | document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice; |
30bfff81 | 1016 | if (UI.isTouchDevice) { |
a6357e82 | 1017 | UI.setViewClip(true); |
30bfff81 | 1018 | } |
1019 | } | |
1020 | }, | |
1021 | ||
95dd6001 | 1022 | /* ------^------- |
1023 | * /CLIPPING | |
1024 | * ============== | |
1025 | * VIEWDRAG | |
1026 | * ------v------*/ | |
1027 | ||
f0d9ab96 | 1028 | toggleViewDrag: function() { |
58ded70d | 1029 | if (!UI.rfb) return; |
bbbf42bb | 1030 | |
f0d9ab96 | 1031 | var drag = UI.rfb.get_viewportDrag(); |
1032 | UI.setViewDrag(!drag); | |
1033 | }, | |
1034 | ||
1035 | // Set the view drag mode which moves the viewport on mouse drags | |
1036 | setViewDrag: function(drag) { | |
1037 | if (!UI.rfb) return; | |
1038 | ||
1039 | UI.rfb.set_viewportDrag(drag); | |
1040 | ||
1041 | UI.updateViewDrag(); | |
1042 | }, | |
1043 | ||
1044 | updateViewDrag: function() { | |
1045 | var clipping = false; | |
bbbf42bb | 1046 | |
6244e383 PO |
1047 | if (UI.rfb_state !== 'normal') return; |
1048 | ||
e00698fe | 1049 | // Check if viewport drag is possible. It is only possible |
1050 | // if the remote display is clipping the client display. | |
6244e383 | 1051 | if (UI.rfb.get_display().get_viewport() && |
fdedbafb | 1052 | UI.rfb.get_display().clippingDisplay()) { |
f0d9ab96 | 1053 | clipping = true; |
1054 | } | |
31ddaa1c | 1055 | |
f0d9ab96 | 1056 | var viewDragButton = document.getElementById('noVNC_view_drag_button'); |
29a0e6a8 | 1057 | |
6244e383 PO |
1058 | if (!clipping && |
1059 | UI.rfb.get_viewportDrag()) { | |
1060 | // The size of the remote display is the same or smaller | |
1061 | // than the client display. Make sure viewport drag isn't | |
1062 | // active when it can't be used. | |
1063 | UI.rfb.set_viewportDrag(false); | |
1064 | } | |
1065 | ||
1066 | if (UI.rfb.get_viewportDrag()) { | |
1067 | viewDragButton.classList.add("noVNC_selected"); | |
31ddaa1c | 1068 | } else { |
6244e383 PO |
1069 | viewDragButton.classList.remove("noVNC_selected"); |
1070 | } | |
31ddaa1c | 1071 | |
6244e383 PO |
1072 | // Different behaviour for touch vs non-touch |
1073 | // The button is disabled instead of hidden on touch devices | |
1074 | if (UI.isTouchDevice) { | |
1075 | viewDragButton.classList.remove("noVNC_hidden"); | |
1076 | ||
1077 | if (clipping) { | |
1078 | viewDragButton.disabled = false; | |
31ddaa1c | 1079 | } else { |
6244e383 | 1080 | viewDragButton.disabled = true; |
31ddaa1c | 1081 | } |
6244e383 PO |
1082 | } else { |
1083 | viewDragButton.disabled = false; | |
29a0e6a8 | 1084 | |
6244e383 | 1085 | if (clipping) { |
e40978c7 | 1086 | viewDragButton.classList.remove("noVNC_hidden"); |
f0d9ab96 | 1087 | } else { |
6244e383 | 1088 | viewDragButton.classList.add("noVNC_hidden"); |
f0d9ab96 | 1089 | } |
f8b399d7 | 1090 | } |
1091 | }, | |
1092 | ||
95dd6001 | 1093 | /* ------^------- |
1094 | * /VIEWDRAG | |
1095 | * ============== | |
1096 | * KEYBOARD | |
1097 | * ------v------*/ | |
fdf21468 | 1098 | |
bbbf42bb SR |
1099 | // On touch devices, show the OS keyboard |
1100 | showKeyboard: function() { | |
ae510306 SR |
1101 | var kbi = document.getElementById('noVNC_keyboardinput'); |
1102 | var skb = document.getElementById('noVNC_keyboard_button'); | |
bbbf42bb SR |
1103 | var l = kbi.value.length; |
1104 | if(UI.keyboardVisible === false) { | |
1105 | kbi.focus(); | |
1106 | try { kbi.setSelectionRange(l, l); } // Move the caret to the end | |
1107 | catch (err) {} // setSelectionRange is undefined in Google Chrome | |
1108 | UI.keyboardVisible = true; | |
d9e86214 | 1109 | skb.classList.add("noVNC_selected"); |
bbbf42bb SR |
1110 | } else if(UI.keyboardVisible === true) { |
1111 | kbi.blur(); | |
d9e86214 | 1112 | skb.classList.remove("noVNC_selected"); |
bbbf42bb SR |
1113 | UI.keyboardVisible = false; |
1114 | } | |
1115 | }, | |
1116 | ||
fdf21468 | 1117 | hideKeyboard: function() { |
a49d9298 | 1118 | document.getElementById('noVNC_keyboard_button') |
d9e86214 | 1119 | .classList.remove("noVNC_selected"); |
fdf21468 | 1120 | //Weird bug in iOS if you change keyboardVisible |
1121 | //here it does not actually occur so next time | |
1122 | //you click keyboard icon it doesnt work. | |
1123 | UI.hideKeyboardTimeout = setTimeout(function() { | |
1124 | UI.keyboardVisible = false; | |
1125 | },100); | |
1126 | }, | |
1127 | ||
bbbf42bb SR |
1128 | keepKeyboard: function() { |
1129 | clearTimeout(UI.hideKeyboardTimeout); | |
1130 | if(UI.keyboardVisible === true) { | |
ae510306 | 1131 | document.getElementById('noVNC_keyboardinput').focus(); |
a49d9298 | 1132 | document.getElementById('noVNC_keyboard_button') |
d9e86214 | 1133 | .classList.add("noVNC_selected"); |
bbbf42bb | 1134 | } else if(UI.keyboardVisible === false) { |
ae510306 | 1135 | document.getElementById('noVNC_keyboardinput').blur(); |
a49d9298 | 1136 | document.getElementById('noVNC_keyboard_button') |
d9e86214 | 1137 | .classList.remove("noVNC_selected"); |
bbbf42bb SR |
1138 | } |
1139 | }, | |
1140 | ||
1141 | keyboardinputReset: function() { | |
ae510306 | 1142 | var kbi = document.getElementById('noVNC_keyboardinput'); |
bbbf42bb SR |
1143 | kbi.value = new Array(UI.defaultKeyboardinputLen).join("_"); |
1144 | UI.lastKeyboardinput = kbi.value; | |
1145 | }, | |
1146 | ||
1147 | // When normal keyboard events are left uncought, use the input events from | |
1148 | // the keyboardinput element instead and generate the corresponding key events. | |
1149 | // This code is required since some browsers on Android are inconsistent in | |
1150 | // sending keyCodes in the normal keyboard events when using on screen keyboards. | |
1151 | keyInput: function(event) { | |
3b8ec46f | 1152 | |
58ded70d | 1153 | if (!UI.rfb) return; |
3b8ec46f | 1154 | |
bbbf42bb | 1155 | var newValue = event.target.value; |
1138bdd4 | 1156 | |
1157 | if (!UI.lastKeyboardinput) { | |
1158 | UI.keyboardinputReset(); | |
1159 | } | |
cb3e4deb | 1160 | var oldValue = UI.lastKeyboardinput; |
bbbf42bb SR |
1161 | |
1162 | var newLen; | |
1163 | try { | |
1164 | // Try to check caret position since whitespace at the end | |
1165 | // will not be considered by value.length in some browsers | |
1166 | newLen = Math.max(event.target.selectionStart, newValue.length); | |
1167 | } catch (err) { | |
1168 | // selectionStart is undefined in Google Chrome | |
1169 | newLen = newValue.length; | |
1170 | } | |
1171 | var oldLen = oldValue.length; | |
1172 | ||
1173 | var backspaces; | |
1174 | var inputs = newLen - oldLen; | |
1175 | if (inputs < 0) { | |
1176 | backspaces = -inputs; | |
1177 | } else { | |
1178 | backspaces = 0; | |
1179 | } | |
8e0f0088 | 1180 | |
bbbf42bb SR |
1181 | // Compare the old string with the new to account for |
1182 | // text-corrections or other input that modify existing text | |
1183 | var i; | |
1184 | for (i = 0; i < Math.min(oldLen, newLen); i++) { | |
1185 | if (newValue.charAt(i) != oldValue.charAt(i)) { | |
1186 | inputs = newLen - i; | |
1187 | backspaces = oldLen - i; | |
1188 | break; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | // Send the key events | |
1193 | for (i = 0; i < backspaces; i++) { | |
ae510306 | 1194 | UI.rfb.sendKey(KeyTable.XK_BackSpace); |
bbbf42bb SR |
1195 | } |
1196 | for (i = newLen - inputs; i < newLen; i++) { | |
1197 | UI.rfb.sendKey(newValue.charCodeAt(i)); | |
1198 | } | |
1199 | ||
1200 | // Control the text content length in the keyboardinput element | |
1201 | if (newLen > 2 * UI.defaultKeyboardinputLen) { | |
1202 | UI.keyboardinputReset(); | |
1203 | } else if (newLen < 1) { | |
1204 | // There always have to be some text in the keyboardinput | |
1205 | // element with which backspace can interact. | |
1206 | UI.keyboardinputReset(); | |
1207 | // This sometimes causes the keyboard to disappear for a second | |
1208 | // but it is required for the android keyboard to recognize that | |
1209 | // text has been added to the field | |
1210 | event.target.blur(); | |
1211 | // This has to be ran outside of the input handler in order to work | |
67685d07 | 1212 | setTimeout(UI.keepKeyboard, 0); |
bbbf42bb SR |
1213 | } else { |
1214 | UI.lastKeyboardinput = newValue; | |
1215 | } | |
1216 | }, | |
1217 | ||
ed8cbe4e | 1218 | openExtraKeys: function() { |
fb7c3b3b PO |
1219 | UI.closeAllPanels(); |
1220 | ||
ed8cbe4e PO |
1221 | document.getElementById('noVNC_modifiers') |
1222 | .classList.add("noVNC_open"); | |
1223 | document.getElementById('noVNC_toggle_extra_keys_button') | |
1224 | .classList.add("noVNC_selected"); | |
1225 | }, | |
1226 | ||
1227 | closeExtraKeys: function() { | |
1228 | document.getElementById('noVNC_modifiers') | |
1229 | .classList.remove("noVNC_open"); | |
1230 | document.getElementById('noVNC_toggle_extra_keys_button') | |
1231 | .classList.remove("noVNC_selected"); | |
1232 | }, | |
1233 | ||
cd611a53 | 1234 | toggleExtraKeys: function() { |
bbbf42bb | 1235 | UI.keepKeyboard(); |
ed8cbe4e PO |
1236 | if(document.getElementById('noVNC_modifiers') |
1237 | .classList.contains("noVNC_open")) { | |
1238 | UI.closeExtraKeys(); | |
1239 | } else { | |
1240 | UI.openExtraKeys(); | |
bbbf42bb SR |
1241 | } |
1242 | }, | |
1243 | ||
fdf21468 | 1244 | sendEsc: function() { |
1245 | UI.keepKeyboard(); | |
ae510306 | 1246 | UI.rfb.sendKey(KeyTable.XK_Escape); |
fdf21468 | 1247 | }, |
1248 | ||
1249 | sendTab: function() { | |
1250 | UI.keepKeyboard(); | |
ae510306 | 1251 | UI.rfb.sendKey(KeyTable.XK_Tab); |
fdf21468 | 1252 | }, |
1253 | ||
bbbf42bb SR |
1254 | toggleCtrl: function() { |
1255 | UI.keepKeyboard(); | |
b0c6d3c6 | 1256 | var btn = document.getElementById('noVNC_toggle_ctrl_button'); |
1257 | if (btn.classList.contains("noVNC_selected")) { | |
ae510306 | 1258 | UI.rfb.sendKey(KeyTable.XK_Control_L, false); |
b0c6d3c6 | 1259 | btn.classList.remove("noVNC_selected"); |
1260 | } else { | |
1261 | UI.rfb.sendKey(KeyTable.XK_Control_L, true); | |
1262 | btn.classList.add("noVNC_selected"); | |
bbbf42bb SR |
1263 | } |
1264 | }, | |
1265 | ||
1266 | toggleAlt: function() { | |
1267 | UI.keepKeyboard(); | |
b0c6d3c6 | 1268 | var btn = document.getElementById('noVNC_toggle_alt_button'); |
1269 | if (btn.classList.contains("noVNC_selected")) { | |
ae510306 | 1270 | UI.rfb.sendKey(KeyTable.XK_Alt_L, false); |
b0c6d3c6 | 1271 | btn.classList.remove("noVNC_selected"); |
1272 | } else { | |
1273 | UI.rfb.sendKey(KeyTable.XK_Alt_L, true); | |
1274 | btn.classList.add("noVNC_selected"); | |
bbbf42bb SR |
1275 | } |
1276 | }, | |
1277 | ||
fdf21468 | 1278 | sendCtrlAltDel: function() { |
1279 | UI.rfb.sendCtrlAltDel(); | |
bbbf42bb SR |
1280 | }, |
1281 | ||
95dd6001 | 1282 | /* ------^------- |
1283 | * /KEYBOARD | |
1284 | * ============== | |
1285 | * MISC | |
1286 | * ------v------*/ | |
1287 | ||
1288 | setMouseButton: function(num) { | |
95dd6001 | 1289 | if (UI.rfb) { |
1290 | UI.rfb.get_mouse().set_touchButton(num); | |
1291 | } | |
1292 | ||
1293 | var blist = [0, 1,2,4]; | |
1294 | for (var b = 0; b < blist.length; b++) { | |
ae510306 | 1295 | var button = document.getElementById('noVNC_mouse_button' + blist[b]); |
95dd6001 | 1296 | if (blist[b] === num) { |
e40978c7 | 1297 | button.classList.remove("noVNC_hidden"); |
95dd6001 | 1298 | } else { |
e40978c7 | 1299 | button.classList.add("noVNC_hidden"); |
95dd6001 | 1300 | } |
1301 | } | |
1302 | }, | |
1303 | ||
1304 | displayBlur: function() { | |
1305 | if (!UI.rfb) return; | |
1306 | ||
1307 | UI.rfb.get_keyboard().set_focused(false); | |
1308 | UI.rfb.get_mouse().set_focused(false); | |
1309 | }, | |
1310 | ||
1311 | displayFocus: function() { | |
1312 | if (!UI.rfb) return; | |
1313 | ||
1314 | UI.rfb.get_keyboard().set_focused(true); | |
1315 | UI.rfb.get_mouse().set_focused(true); | |
bbbf42bb SR |
1316 | }, |
1317 | ||
95dd6001 | 1318 | // Display the desktop name in the document title |
1319 | updateDocumentTitle: function(rfb, name) { | |
1320 | document.title = name + " - noVNC"; | |
bbbf42bb SR |
1321 | }, |
1322 | ||
bbbf42bb SR |
1323 | //Helper to add options to dropdown. |
1324 | addOption: function(selectbox, text, value) { | |
1325 | var optn = document.createElement("OPTION"); | |
1326 | optn.text = text; | |
1327 | optn.value = value; | |
1328 | selectbox.options.add(optn); | |
1329 | }, | |
1330 | ||
95dd6001 | 1331 | /* ------^------- |
1332 | * /MISC | |
1333 | * ============== | |
1334 | */ | |
bbbf42bb | 1335 | }; |
ae510306 SR |
1336 | |
1337 | /* [module] UI.load(); */ | |
bbbf42bb | 1338 | })(); |
ae510306 SR |
1339 | |
1340 | /* [module] export default UI; */ |