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