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