]> git.proxmox.com Git - mirror_novnc.git/blame - app/ui.js
Add hover hints to buttons
[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")
234 .addEventListener('click', UI.showKeyboard);
235
236 document.getElementById("noVNC_keyboardinput")
237 .addEventListener('input', UI.keyInput);
238 document.getElementById("noVNC_keyboardinput")
239 .addEventListener('blur', UI.hideKeyboard);
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) {
594 if ((e.type == "mouseup") && (e.button != 0))
595 return;
596
597 // mouseup and mousedown on the same place toggles the controlbar
598 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
599 UI.toggleControlbar();
600 e.preventDefault();
601 e.stopPropagation();
602 }
603 UI.controlbarGrabbed = false;
604 },
605
606 controlbarHandleMouseDown: function(e) {
607 if ((e.type == "mousedown") && (e.button != 0))
608 return;
609
610 var ptr = Util.getPointerEvent(e);
611
612 var handle = document.getElementById("noVNC_control_bar_handle");
613 var bounds = handle.getBoundingClientRect();
614
615 WebUtil.setCapture(handle);
616 UI.controlbarGrabbed = true;
617 UI.controlbarDrag = false;
618
619 UI.controlbarMouseDownClientY = ptr.clientY;
620 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
621 e.preventDefault();
622 e.stopPropagation();
623 },
624
95dd6001 625/* ------^-------
626 * /VISUAL
627 * ==============
628 * SETTINGS
629 * ------v------*/
630
45c70c9e 631 // Initial page load read/initialization of settings
632 initSetting: function(name, defVal) {
633 // Check Query string followed by cookie
634 var val = WebUtil.getConfigVar(name);
635 if (val === null) {
636 val = WebUtil.readSetting(name, defVal);
bbbf42bb 637 }
45c70c9e 638 UI.updateSetting(name, val);
bbbf42bb
SR
639 return val;
640 },
641
642 // Update cookie and form control setting. If value is not set, then
643 // updates from control to current cookie setting.
644 updateSetting: function(name, value) {
645
646 // Save the cookie for this session
647 if (typeof value !== 'undefined') {
648 WebUtil.writeSetting(name, value);
649 }
650
651 // Update the settings control
652 value = UI.getSetting(name);
653
ae510306 654 var ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
655 if (ctrl.type === 'checkbox') {
656 ctrl.checked = value;
657
658 } else if (typeof ctrl.options !== 'undefined') {
659 for (var i = 0; i < ctrl.options.length; i += 1) {
660 if (ctrl.options[i].value === value) {
661 ctrl.selectedIndex = i;
662 break;
663 }
664 }
665 } else {
666 /*Weird IE9 error leads to 'null' appearring
667 in textboxes instead of ''.*/
668 if (value === null) {
669 value = "";
670 }
671 ctrl.value = value;
672 }
673 },
674
675 // Save control setting to cookie
676 saveSetting: function(name) {
ae510306 677 var val, ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
678 if (ctrl.type === 'checkbox') {
679 val = ctrl.checked;
680 } else if (typeof ctrl.options !== 'undefined') {
681 val = ctrl.options[ctrl.selectedIndex].value;
682 } else {
683 val = ctrl.value;
684 }
685 WebUtil.writeSetting(name, val);
686 //Util.Debug("Setting saved '" + name + "=" + val + "'");
687 return val;
688 },
689
bbbf42bb
SR
690 // Force a setting to be a certain value
691 forceSetting: function(name, val) {
692 UI.updateSetting(name, val);
693 return val;
694 },
695
45c70c9e 696 // Read form control compatible setting from cookie
697 getSetting: function(name) {
ae510306 698 var ctrl = document.getElementById('noVNC_setting_' + name);
45c70c9e 699 var val = WebUtil.readSetting(name);
700 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
701 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
702 val = false;
4f19e5c6 703 } else {
45c70c9e 704 val = true;
4f19e5c6 705 }
bbbf42bb 706 }
bbbf42bb 707 return val;
bbbf42bb
SR
708 },
709
95dd6001 710 // Save/apply settings when 'Apply' button is pressed
711 settingsApply: function() {
712 //Util.Debug(">> settingsApply");
713 UI.saveSetting('encrypt');
714 UI.saveSetting('true_color');
715 if (Util.browserSupportsCursorURIs()) {
716 UI.saveSetting('cursor');
bbbf42bb 717 }
bbbf42bb 718
95dd6001 719 UI.saveSetting('resize');
bbbf42bb 720
95dd6001 721 if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
722 UI.forceSetting('clip', false);
7d1dc09a 723 }
7d1dc09a 724
95dd6001 725 UI.saveSetting('clip');
726 UI.saveSetting('shared');
727 UI.saveSetting('view_only');
728 UI.saveSetting('path');
729 UI.saveSetting('repeaterID');
730 UI.saveSetting('stylesheet');
731 UI.saveSetting('logging');
732
733 // Settings with immediate (non-connected related) effect
734 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
735 WebUtil.init_logging(UI.getSetting('logging'));
f0d9ab96 736 UI.updateViewClip();
95dd6001 737 UI.updateViewDrag();
738 //Util.Debug("<< settingsApply");
7d1dc09a 739 },
740
ed8cbe4e
PO
741/* ------^-------
742 * /SETTINGS
743 * ==============
744 * PANELS
745 * ------v------*/
746
747 closeAllPanels: function() {
748 UI.closeSettingsPanel();
749 UI.closeXvpPanel();
750 UI.closeClipboardPanel();
751 UI.closeConnectPanel();
fb7c3b3b 752 UI.closeExtraKeys();
ed8cbe4e
PO
753 },
754
755/* ------^-------
756 * /PANELS
757 * ==============
758 * SETTINGS (panel)
759 * ------v------*/
760
761 openSettingsPanel: function() {
762 UI.closeAllPanels();
38323d4d 763 UI.openControlbar();
ed8cbe4e
PO
764
765 UI.updateSetting('encrypt');
766 UI.updateSetting('true_color');
767 if (Util.browserSupportsCursorURIs()) {
768 UI.updateSetting('cursor');
769 } else {
770 UI.updateSetting('cursor', !UI.isTouchDevice);
771 document.getElementById('noVNC_setting_cursor').disabled = true;
bbbf42bb 772 }
ed8cbe4e
PO
773 UI.updateSetting('clip');
774 UI.updateSetting('resize');
775 UI.updateSetting('shared');
776 UI.updateSetting('view_only');
777 UI.updateSetting('path');
778 UI.updateSetting('repeaterID');
779 UI.updateSetting('stylesheet');
780 UI.updateSetting('logging');
781
e40978c7
PO
782 document.getElementById('noVNC_settings')
783 .classList.add("noVNC_open");
784 document.getElementById('noVNC_settings_button')
d9e86214 785 .classList.add("noVNC_selected");
45c70c9e 786 },
bbbf42bb 787
ed8cbe4e 788 closeSettingsPanel: function() {
e40978c7
PO
789 document.getElementById('noVNC_settings')
790 .classList.remove("noVNC_open");
791 document.getElementById('noVNC_settings_button')
d9e86214 792 .classList.remove("noVNC_selected");
bbbf42bb
SR
793 },
794
795 // Toggle the settings menu:
796 // On open, settings are refreshed from saved cookies.
797 // On close, settings are applied
798 toggleSettingsPanel: function() {
ed8cbe4e
PO
799 if (document.getElementById('noVNC_settings')
800 .classList.contains("noVNC_open")) {
bbbf42bb 801 UI.settingsApply();
ed8cbe4e 802 UI.closeSettingsPanel();
bbbf42bb 803 } else {
ed8cbe4e 804 UI.openSettingsPanel();
bbbf42bb
SR
805 }
806 },
807
95dd6001 808/* ------^-------
809 * /SETTINGS
810 * ==============
811 * XVP
812 * ------v------*/
813
ed8cbe4e
PO
814 openXvpPanel: function() {
815 UI.closeAllPanels();
38323d4d 816 UI.openControlbar();
ed8cbe4e
PO
817
818 document.getElementById('noVNC_xvp')
819 .classList.add("noVNC_open");
820 document.getElementById('noVNC_xvp_button')
821 .classList.add("noVNC_selected");
822 },
823
824 closeXvpPanel: function() {
825 document.getElementById('noVNC_xvp')
826 .classList.remove("noVNC_open");
827 document.getElementById('noVNC_xvp_button')
828 .classList.remove("noVNC_selected");
829 },
830
bbbf42bb 831 toggleXvpPanel: function() {
ed8cbe4e
PO
832 if (document.getElementById('noVNC_xvp')
833 .classList.contains("noVNC_open")) {
834 UI.closeXvpPanel();
bbbf42bb 835 } else {
ed8cbe4e 836 UI.openXvpPanel();
bbbf42bb 837 }
bbbf42bb
SR
838 },
839
840 // Disable/enable XVP button
9e45354e 841 updateXvpButton: function(ver) {
bbbf42bb 842 if (ver >= 1) {
a49d9298 843 document.getElementById('noVNC_xvp_button')
e40978c7 844 .classList.remove("noVNC_hidden");
bbbf42bb 845 } else {
a49d9298 846 document.getElementById('noVNC_xvp_button')
e40978c7 847 .classList.add("noVNC_hidden");
bbbf42bb 848 // Close XVP panel if open
ed8cbe4e 849 UI.closeXvpPanel();
bbbf42bb
SR
850 }
851 },
852
95dd6001 853/* ------^-------
854 * /XVP
855 * ==============
856 * CLIPBOARD
857 * ------v------*/
f8b399d7 858
ed8cbe4e
PO
859 openClipboardPanel: function() {
860 UI.closeAllPanels();
38323d4d 861 UI.openControlbar();
ed8cbe4e
PO
862
863 document.getElementById('noVNC_clipboard')
864 .classList.add("noVNC_open");
865 document.getElementById('noVNC_clipboard_button')
866 .classList.add("noVNC_selected");
867 },
868
869 closeClipboardPanel: function() {
870 document.getElementById('noVNC_clipboard')
871 .classList.remove("noVNC_open");
872 document.getElementById('noVNC_clipboard_button')
873 .classList.remove("noVNC_selected");
874 },
875
bbbf42bb 876 toggleClipboardPanel: function() {
ed8cbe4e
PO
877 if (document.getElementById('noVNC_clipboard')
878 .classList.contains("noVNC_open")) {
879 UI.closeClipboardPanel();
bbbf42bb 880 } else {
ed8cbe4e 881 UI.openClipboardPanel();
bbbf42bb 882 }
bbbf42bb
SR
883 },
884
4d26f58e 885 clipboardReceive: function(rfb, text) {
886 Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "...");
ae510306 887 document.getElementById('noVNC_clipboard_text').value = text;
4d26f58e 888 Util.Debug("<< UI.clipboardReceive");
95dd6001 889 },
890
4d26f58e 891 clipboardClear: function() {
ae510306 892 document.getElementById('noVNC_clipboard_text').value = "";
95dd6001 893 UI.rfb.clipboardPasteFrom("");
894 },
895
4d26f58e 896 clipboardSend: function() {
ae510306 897 var text = document.getElementById('noVNC_clipboard_text').value;
4d26f58e 898 Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "...");
95dd6001 899 UI.rfb.clipboardPasteFrom(text);
4d26f58e 900 Util.Debug("<< UI.clipboardSend");
95dd6001 901 },
902
903/* ------^-------
904 * /CLIPBOARD
905 * ==============
906 * CONNECTION
907 * ------v------*/
908
ed8cbe4e
PO
909 openConnectPanel: function() {
910 UI.closeAllPanels();
38323d4d 911 UI.openControlbar();
ab81ddf5 912
ed8cbe4e
PO
913 document.getElementById('noVNC_connect_controls')
914 .classList.add("noVNC_open");
915 document.getElementById('noVNC_connect_controls_button')
916 .classList.add("noVNC_selected");
917
918 document.getElementById('noVNC_setting_host').focus();
919 },
920
921 closeConnectPanel: function() {
922 document.getElementById('noVNC_connect_controls')
923 .classList.remove("noVNC_open");
924 document.getElementById('noVNC_connect_controls_button')
925 .classList.remove("noVNC_selected");
926
927 UI.saveSetting('host');
928 UI.saveSetting('port');
929 UI.saveSetting('token');
930 //UI.saveSetting('password');
931 },
932
933 toggleConnectPanel: function() {
934 if (document.getElementById('noVNC_connect_controls')
935 .classList.contains("noVNC_open")) {
936 UI.closeConnectPanel();
ab81ddf5 937 } else {
ed8cbe4e 938 UI.openConnectPanel();
ab81ddf5 939 }
bbbf42bb
SR
940 },
941
942 connect: function() {
ed8cbe4e 943 UI.closeAllPanels();
f00b1e37 944
ae510306
SR
945 var host = document.getElementById('noVNC_setting_host').value;
946 var port = document.getElementById('noVNC_setting_port').value;
947 var password = document.getElementById('noVNC_setting_password').value;
948 var token = document.getElementById('noVNC_setting_token').value;
949 var path = document.getElementById('noVNC_setting_path').value;
c55f05f6
MXPN
950
951 //if token is in path then ignore the new token variable
952 if (token) {
953 path = WebUtil.injectParamIfMissing(path, "token", token);
954 }
955
bbbf42bb
SR
956 if ((!host) || (!port)) {
957 throw new Error("Must set host and port");
958 }
53fc7392 959
d9fc1c7b 960 if (!UI.initRFB()) return;
58ded70d 961
bbbf42bb
SR
962 UI.rfb.set_encrypt(UI.getSetting('encrypt'));
963 UI.rfb.set_true_color(UI.getSetting('true_color'));
964 UI.rfb.set_local_cursor(UI.getSetting('cursor'));
965 UI.rfb.set_shared(UI.getSetting('shared'));
966 UI.rfb.set_view_only(UI.getSetting('view_only'));
967 UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
53fc7392 968
bbbf42bb 969 UI.rfb.connect(host, port, password, path);
bbbf42bb 970 },
5299db1a 971
bbbf42bb 972 disconnect: function() {
ed8cbe4e 973 UI.closeAllPanels();
bbbf42bb 974 UI.rfb.disconnect();
8e0f0088 975
f8b399d7 976 // Restore the callback used for initial resize
ab81ddf5 977 UI.rfb.set_onFBUComplete(UI.initialResize);
f8b399d7 978
e543525f 979 // Don't display the connection settings until we're actually disconnected
bbbf42bb
SR
980 },
981
95dd6001 982 setPassword: function() {
8a7ec6ea
SM
983 UI.rfb.sendPassword(document.getElementById('noVNC_password_input').value);
984 document.getElementById('noVNC_password_dlg')
985 .classList.remove('noVNC_open');
95dd6001 986 return false;
987 },
58ded70d 988
95dd6001 989/* ------^-------
990 * /CONNECTION
991 * ==============
992 * FULLSCREEN
993 * ------v------*/
994
7d1dc09a 995 toggleFullscreen: function() {
996 if (document.fullscreenElement || // alternative standard method
997 document.mozFullScreenElement || // currently working methods
998 document.webkitFullscreenElement ||
a6357e82 999 document.msFullscreenElement) {
7d1dc09a 1000 if (document.exitFullscreen) {
1001 document.exitFullscreen();
1002 } else if (document.mozCancelFullScreen) {
1003 document.mozCancelFullScreen();
1004 } else if (document.webkitExitFullscreen) {
1005 document.webkitExitFullscreen();
1006 } else if (document.msExitFullscreen) {
1007 document.msExitFullscreen();
1008 }
1009 } else {
1010 if (document.documentElement.requestFullscreen) {
1011 document.documentElement.requestFullscreen();
1012 } else if (document.documentElement.mozRequestFullScreen) {
1013 document.documentElement.mozRequestFullScreen();
1014 } else if (document.documentElement.webkitRequestFullscreen) {
1015 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1016 } else if (document.body.msRequestFullscreen) {
1017 document.body.msRequestFullscreen();
1018 }
1019 }
a6357e82 1020 UI.enableDisableViewClip();
7d1dc09a 1021 UI.updateFullscreenButton();
bbbf42bb
SR
1022 },
1023
7d1dc09a 1024 updateFullscreenButton: function() {
1025 if (document.fullscreenElement || // alternative standard method
1026 document.mozFullScreenElement || // currently working methods
1027 document.webkitFullscreenElement ||
1028 document.msFullscreenElement ) {
a49d9298 1029 document.getElementById('noVNC_fullscreen_button')
d9e86214 1030 .classList.add("noVNC_selected");
7d1dc09a 1031 } else {
a49d9298 1032 document.getElementById('noVNC_fullscreen_button')
d9e86214 1033 .classList.remove("noVNC_selected");
7d1dc09a 1034 }
1035 },
1036
95dd6001 1037/* ------^-------
1038 * /FULLSCREEN
1039 * ==============
1040 * RESIZE
1041 * ------v------*/
777cb7a0 1042
1043 // Apply remote resizing or local scaling
0bd2cbac 1044 applyResizeMode: function() {
58ded70d
SR
1045 if (!UI.rfb) return;
1046
777cb7a0 1047 var screen = UI.screenSize();
1048
1049 if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
1050
1051 var display = UI.rfb.get_display();
1052 var resizeMode = UI.getSetting('resize');
1053
1054 if (resizeMode === 'remote') {
1055
1056 // Request changing the resolution of the remote display to
1057 // the size of the local browser viewport.
1058
1059 // In order to not send multiple requests before the browser-resize
1060 // is finished we wait 0.5 seconds before sending the request.
1061 clearTimeout(UI.resizeTimeout);
1062 UI.resizeTimeout = setTimeout(function(){
1063
1064 // Limit the viewport to the size of the browser window
1065 display.set_maxWidth(screen.w);
1066 display.set_maxHeight(screen.h);
1067
5fd3f88e 1068 Util.Debug('Attempting requestDesktopSize(' +
777cb7a0 1069 screen.w + ', ' + screen.h + ')');
1070
1071 // Request a remote size covering the viewport
5fd3f88e 1072 UI.rfb.requestDesktopSize(screen.w, screen.h);
777cb7a0 1073 }, 500);
1074
1075 } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
1076 var downscaleOnly = resizeMode === 'downscale';
1077 var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
1078 UI.rfb.get_mouse().set_scale(scaleRatio);
1079 Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
1080 }
1081 }
bbbf42bb
SR
1082 },
1083
777cb7a0 1084 // The screen is always the same size as the available viewport
1085 // in the browser window minus the height of the control bar
0bd2cbac 1086 screenSize: function() {
ae510306 1087 var screen = document.getElementById('noVNC_screen');
777cb7a0 1088
1089 // Hide the scrollbars until the size is calculated
1090 screen.style.overflow = "hidden";
1091
1092 var pos = Util.getPosition(screen);
1093 var w = pos.width;
1094 var h = pos.height;
1095
1096 screen.style.overflow = "visible";
1097
1098 if (isNaN(w) || isNaN(h)) {
1099 return false;
1100 } else {
1101 return {w: w, h: h};
1102 }
bbbf42bb
SR
1103 },
1104
777cb7a0 1105 // Normally we only apply the current resize mode after a window resize
1106 // event. This means that when a new connection is opened, there is no
1107 // resize mode active.
1108 // We have to wait until the first FBU because this is where the client
1109 // will find the supported encodings of the server. Some calls later in
1110 // the chain is dependant on knowing the server-capabilities.
1111 initialResize: function(rfb, fbu) {
1112 UI.applyResizeMode();
1113 // After doing this once, we remove the callback.
1114 UI.rfb.set_onFBUComplete(function() { });
bbbf42bb
SR
1115 },
1116
95dd6001 1117/* ------^-------
1118 * /RESIZE
1119 * ==============
1120 * CLIPPING
1121 * ------v------*/
1122
30bfff81 1123 // Set and configure viewport clipping
bbbf42bb 1124 setViewClip: function(clip) {
f0d9ab96 1125 UI.updateSetting('clip', clip);
1126 UI.updateViewClip();
1127 },
1128
1129 // Update parameters that depend on the clip setting
1130 updateViewClip: function() {
bbbf42bb 1131 var display;
f0d9ab96 1132 if (!UI.rfb) {
bbbf42bb
SR
1133 return;
1134 }
8e0f0088 1135
f0d9ab96 1136 var display = UI.rfb.get_display();
bbbf42bb 1137 var cur_clip = display.get_viewport();
f0d9ab96 1138 var new_clip = UI.getSetting('clip');
bbbf42bb 1139
f0d9ab96 1140 if (cur_clip !== new_clip) {
1141 display.set_viewport(new_clip);
bbbf42bb 1142 }
8e0f0088 1143
f0d9ab96 1144 var size = UI.screenSize();
fdedbafb 1145
f0d9ab96 1146 if (new_clip && size) {
1147 // When clipping is enabled, the screen is limited to
1148 // the size of the browser window.
1149 display.set_maxWidth(size.w);
1150 display.set_maxHeight(size.h);
fdedbafb 1151
f0d9ab96 1152 var screen = document.getElementById('noVNC_screen');
1153 var canvas = document.getElementById('noVNC_canvas');
fdedbafb 1154
f0d9ab96 1155 // Hide potential scrollbars that can skew the position
1156 screen.style.overflow = "hidden";
fdedbafb 1157
f0d9ab96 1158 // The x position marks the left margin of the canvas,
1159 // remove the margin from both sides to keep it centered.
1160 var new_w = size.w - (2 * Util.getPosition(canvas).x);
fdedbafb 1161
f0d9ab96 1162 screen.style.overflow = "visible";
1163
1164 display.viewportChangeSize(new_w, size.h);
1165 } else {
1166 // Disable max dimensions
1167 display.set_maxWidth(0);
1168 display.set_maxHeight(0);
1169 display.viewportChangeSize();
bbbf42bb
SR
1170 }
1171 },
1172
30bfff81 1173 // Handle special cases where clipping is forced on/off or locked
0bd2cbac 1174 enableDisableViewClip: function() {
ae510306 1175 var resizeSetting = document.getElementById('noVNC_setting_resize');
a6357e82 1176 var connected = UI.rfb && UI.rfb_state === 'normal';
1177
f620259b 1178 if (UI.isSafari) {
1179 // Safari auto-hides the scrollbars which makes them
1180 // impossible to use in most cases
1181 UI.setViewClip(true);
ae510306 1182 document.getElementById('noVNC_setting_clip').disabled = true;
682fd02b 1183 } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') {
a6357e82 1184 // Disable clipping if we are scaling
1185 UI.setViewClip(false);
ae510306 1186 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1187 } else if (document.msFullscreenElement) {
1188 // The browser is IE and we are in fullscreen mode.
1189 // - We need to force clipping while in fullscreen since
1190 // scrollbars doesn't work.
ca5c74ad 1191 UI.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
a6357e82 1192 UI.rememberedClipSetting = UI.getSetting('clip');
1193 UI.setViewClip(true);
ae510306 1194 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1195 } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
1196 // Restore view clip to what it was before fullscreen on IE
1197 UI.setViewClip(UI.rememberedClipSetting);
ae510306 1198 document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
30bfff81 1199 } else {
ae510306 1200 document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
30bfff81 1201 if (UI.isTouchDevice) {
a6357e82 1202 UI.setViewClip(true);
30bfff81 1203 }
1204 }
1205 },
1206
95dd6001 1207/* ------^-------
1208 * /CLIPPING
1209 * ==============
1210 * VIEWDRAG
1211 * ------v------*/
1212
f0d9ab96 1213 toggleViewDrag: function() {
58ded70d 1214 if (!UI.rfb) return;
bbbf42bb 1215
f0d9ab96 1216 var drag = UI.rfb.get_viewportDrag();
1217 UI.setViewDrag(!drag);
1218 },
1219
1220 // Set the view drag mode which moves the viewport on mouse drags
1221 setViewDrag: function(drag) {
1222 if (!UI.rfb) return;
1223
1224 UI.rfb.set_viewportDrag(drag);
1225
1226 UI.updateViewDrag();
1227 },
1228
1229 updateViewDrag: function() {
1230 var clipping = false;
bbbf42bb 1231
6244e383
PO
1232 if (UI.rfb_state !== 'normal') return;
1233
e00698fe 1234 // Check if viewport drag is possible. It is only possible
1235 // if the remote display is clipping the client display.
6244e383 1236 if (UI.rfb.get_display().get_viewport() &&
fdedbafb 1237 UI.rfb.get_display().clippingDisplay()) {
f0d9ab96 1238 clipping = true;
1239 }
31ddaa1c 1240
f0d9ab96 1241 var viewDragButton = document.getElementById('noVNC_view_drag_button');
29a0e6a8 1242
6244e383
PO
1243 if (!clipping &&
1244 UI.rfb.get_viewportDrag()) {
1245 // The size of the remote display is the same or smaller
1246 // than the client display. Make sure viewport drag isn't
1247 // active when it can't be used.
1248 UI.rfb.set_viewportDrag(false);
1249 }
1250
1251 if (UI.rfb.get_viewportDrag()) {
1252 viewDragButton.classList.add("noVNC_selected");
31ddaa1c 1253 } else {
6244e383
PO
1254 viewDragButton.classList.remove("noVNC_selected");
1255 }
31ddaa1c 1256
6244e383
PO
1257 // Different behaviour for touch vs non-touch
1258 // The button is disabled instead of hidden on touch devices
1259 if (UI.isTouchDevice) {
1260 viewDragButton.classList.remove("noVNC_hidden");
1261
1262 if (clipping) {
1263 viewDragButton.disabled = false;
31ddaa1c 1264 } else {
6244e383 1265 viewDragButton.disabled = true;
31ddaa1c 1266 }
6244e383
PO
1267 } else {
1268 viewDragButton.disabled = false;
29a0e6a8 1269
6244e383 1270 if (clipping) {
e40978c7 1271 viewDragButton.classList.remove("noVNC_hidden");
f0d9ab96 1272 } else {
6244e383 1273 viewDragButton.classList.add("noVNC_hidden");
f0d9ab96 1274 }
f8b399d7 1275 }
1276 },
1277
95dd6001 1278/* ------^-------
1279 * /VIEWDRAG
1280 * ==============
1281 * KEYBOARD
1282 * ------v------*/
fdf21468 1283
bbbf42bb
SR
1284 // On touch devices, show the OS keyboard
1285 showKeyboard: function() {
ae510306
SR
1286 var kbi = document.getElementById('noVNC_keyboardinput');
1287 var skb = document.getElementById('noVNC_keyboard_button');
bbbf42bb
SR
1288 var l = kbi.value.length;
1289 if(UI.keyboardVisible === false) {
1290 kbi.focus();
1291 try { kbi.setSelectionRange(l, l); } // Move the caret to the end
1292 catch (err) {} // setSelectionRange is undefined in Google Chrome
1293 UI.keyboardVisible = true;
d9e86214 1294 skb.classList.add("noVNC_selected");
bbbf42bb
SR
1295 } else if(UI.keyboardVisible === true) {
1296 kbi.blur();
d9e86214 1297 skb.classList.remove("noVNC_selected");
bbbf42bb
SR
1298 UI.keyboardVisible = false;
1299 }
1300 },
1301
fdf21468 1302 hideKeyboard: function() {
a49d9298 1303 document.getElementById('noVNC_keyboard_button')
d9e86214 1304 .classList.remove("noVNC_selected");
fdf21468 1305 //Weird bug in iOS if you change keyboardVisible
1306 //here it does not actually occur so next time
1307 //you click keyboard icon it doesnt work.
1308 UI.hideKeyboardTimeout = setTimeout(function() {
1309 UI.keyboardVisible = false;
1310 },100);
1311 },
1312
bbbf42bb
SR
1313 keepKeyboard: function() {
1314 clearTimeout(UI.hideKeyboardTimeout);
1315 if(UI.keyboardVisible === true) {
ae510306 1316 document.getElementById('noVNC_keyboardinput').focus();
a49d9298 1317 document.getElementById('noVNC_keyboard_button')
d9e86214 1318 .classList.add("noVNC_selected");
bbbf42bb 1319 } else if(UI.keyboardVisible === false) {
ae510306 1320 document.getElementById('noVNC_keyboardinput').blur();
a49d9298 1321 document.getElementById('noVNC_keyboard_button')
d9e86214 1322 .classList.remove("noVNC_selected");
bbbf42bb
SR
1323 }
1324 },
1325
1326 keyboardinputReset: function() {
ae510306 1327 var kbi = document.getElementById('noVNC_keyboardinput');
bbbf42bb
SR
1328 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1329 UI.lastKeyboardinput = kbi.value;
1330 },
1331
1332 // When normal keyboard events are left uncought, use the input events from
1333 // the keyboardinput element instead and generate the corresponding key events.
1334 // This code is required since some browsers on Android are inconsistent in
1335 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1336 keyInput: function(event) {
3b8ec46f 1337
58ded70d 1338 if (!UI.rfb) return;
3b8ec46f 1339
bbbf42bb 1340 var newValue = event.target.value;
1138bdd4 1341
1342 if (!UI.lastKeyboardinput) {
1343 UI.keyboardinputReset();
1344 }
cb3e4deb 1345 var oldValue = UI.lastKeyboardinput;
bbbf42bb
SR
1346
1347 var newLen;
1348 try {
1349 // Try to check caret position since whitespace at the end
1350 // will not be considered by value.length in some browsers
1351 newLen = Math.max(event.target.selectionStart, newValue.length);
1352 } catch (err) {
1353 // selectionStart is undefined in Google Chrome
1354 newLen = newValue.length;
1355 }
1356 var oldLen = oldValue.length;
1357
1358 var backspaces;
1359 var inputs = newLen - oldLen;
1360 if (inputs < 0) {
1361 backspaces = -inputs;
1362 } else {
1363 backspaces = 0;
1364 }
8e0f0088 1365
bbbf42bb
SR
1366 // Compare the old string with the new to account for
1367 // text-corrections or other input that modify existing text
1368 var i;
1369 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1370 if (newValue.charAt(i) != oldValue.charAt(i)) {
1371 inputs = newLen - i;
1372 backspaces = oldLen - i;
1373 break;
1374 }
1375 }
1376
1377 // Send the key events
1378 for (i = 0; i < backspaces; i++) {
ae510306 1379 UI.rfb.sendKey(KeyTable.XK_BackSpace);
bbbf42bb
SR
1380 }
1381 for (i = newLen - inputs; i < newLen; i++) {
1382 UI.rfb.sendKey(newValue.charCodeAt(i));
1383 }
1384
1385 // Control the text content length in the keyboardinput element
1386 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1387 UI.keyboardinputReset();
1388 } else if (newLen < 1) {
1389 // There always have to be some text in the keyboardinput
1390 // element with which backspace can interact.
1391 UI.keyboardinputReset();
1392 // This sometimes causes the keyboard to disappear for a second
1393 // but it is required for the android keyboard to recognize that
1394 // text has been added to the field
1395 event.target.blur();
1396 // This has to be ran outside of the input handler in order to work
67685d07 1397 setTimeout(UI.keepKeyboard, 0);
bbbf42bb
SR
1398 } else {
1399 UI.lastKeyboardinput = newValue;
1400 }
1401 },
1402
ed8cbe4e 1403 openExtraKeys: function() {
fb7c3b3b 1404 UI.closeAllPanels();
38323d4d 1405 UI.openControlbar();
fb7c3b3b 1406
ed8cbe4e
PO
1407 document.getElementById('noVNC_modifiers')
1408 .classList.add("noVNC_open");
1409 document.getElementById('noVNC_toggle_extra_keys_button')
1410 .classList.add("noVNC_selected");
1411 },
1412
1413 closeExtraKeys: function() {
1414 document.getElementById('noVNC_modifiers')
1415 .classList.remove("noVNC_open");
1416 document.getElementById('noVNC_toggle_extra_keys_button')
1417 .classList.remove("noVNC_selected");
1418 },
1419
cd611a53 1420 toggleExtraKeys: function() {
bbbf42bb 1421 UI.keepKeyboard();
ed8cbe4e
PO
1422 if(document.getElementById('noVNC_modifiers')
1423 .classList.contains("noVNC_open")) {
1424 UI.closeExtraKeys();
1425 } else {
1426 UI.openExtraKeys();
bbbf42bb
SR
1427 }
1428 },
1429
fdf21468 1430 sendEsc: function() {
1431 UI.keepKeyboard();
ae510306 1432 UI.rfb.sendKey(KeyTable.XK_Escape);
fdf21468 1433 },
1434
1435 sendTab: function() {
1436 UI.keepKeyboard();
ae510306 1437 UI.rfb.sendKey(KeyTable.XK_Tab);
fdf21468 1438 },
1439
bbbf42bb
SR
1440 toggleCtrl: function() {
1441 UI.keepKeyboard();
b0c6d3c6 1442 var btn = document.getElementById('noVNC_toggle_ctrl_button');
1443 if (btn.classList.contains("noVNC_selected")) {
ae510306 1444 UI.rfb.sendKey(KeyTable.XK_Control_L, false);
b0c6d3c6 1445 btn.classList.remove("noVNC_selected");
1446 } else {
1447 UI.rfb.sendKey(KeyTable.XK_Control_L, true);
1448 btn.classList.add("noVNC_selected");
bbbf42bb
SR
1449 }
1450 },
1451
1452 toggleAlt: function() {
1453 UI.keepKeyboard();
b0c6d3c6 1454 var btn = document.getElementById('noVNC_toggle_alt_button');
1455 if (btn.classList.contains("noVNC_selected")) {
ae510306 1456 UI.rfb.sendKey(KeyTable.XK_Alt_L, false);
b0c6d3c6 1457 btn.classList.remove("noVNC_selected");
1458 } else {
1459 UI.rfb.sendKey(KeyTable.XK_Alt_L, true);
1460 btn.classList.add("noVNC_selected");
bbbf42bb
SR
1461 }
1462 },
1463
fdf21468 1464 sendCtrlAltDel: function() {
ca25d2ae 1465 UI.keepKeyboard();
fdf21468 1466 UI.rfb.sendCtrlAltDel();
bbbf42bb
SR
1467 },
1468
95dd6001 1469/* ------^-------
1470 * /KEYBOARD
1471 * ==============
1472 * MISC
1473 * ------v------*/
1474
1475 setMouseButton: function(num) {
95dd6001 1476 if (UI.rfb) {
1477 UI.rfb.get_mouse().set_touchButton(num);
1478 }
1479
1480 var blist = [0, 1,2,4];
1481 for (var b = 0; b < blist.length; b++) {
ae510306 1482 var button = document.getElementById('noVNC_mouse_button' + blist[b]);
95dd6001 1483 if (blist[b] === num) {
e40978c7 1484 button.classList.remove("noVNC_hidden");
95dd6001 1485 } else {
e40978c7 1486 button.classList.add("noVNC_hidden");
95dd6001 1487 }
1488 }
1489 },
1490
1491 displayBlur: function() {
1492 if (!UI.rfb) return;
1493
1494 UI.rfb.get_keyboard().set_focused(false);
1495 UI.rfb.get_mouse().set_focused(false);
1496 },
1497
1498 displayFocus: function() {
1499 if (!UI.rfb) return;
1500
1501 UI.rfb.get_keyboard().set_focused(true);
1502 UI.rfb.get_mouse().set_focused(true);
bbbf42bb
SR
1503 },
1504
95dd6001 1505 // Display the desktop name in the document title
1506 updateDocumentTitle: function(rfb, name) {
1507 document.title = name + " - noVNC";
bbbf42bb
SR
1508 },
1509
bbbf42bb
SR
1510 //Helper to add options to dropdown.
1511 addOption: function(selectbox, text, value) {
1512 var optn = document.createElement("OPTION");
1513 optn.text = text;
1514 optn.value = value;
1515 selectbox.options.add(optn);
1516 },
1517
95dd6001 1518/* ------^-------
1519 * /MISC
1520 * ==============
1521 */
bbbf42bb 1522 };
ae510306
SR
1523
1524 /* [module] UI.load(); */
bbbf42bb 1525})();
ae510306
SR
1526
1527/* [module] export default UI; */