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