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