]> git.proxmox.com Git - mirror_novnc.git/blob - app/ui.js
Remove beforeunload handler
[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.setMouseButton();
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.setViewClip();
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.setViewClip();
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.setViewClip();
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.setMouseButton();
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.updateViewDrag(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.setViewClip();
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 var display;
960 if (UI.rfb) {
961 display = UI.rfb.get_display();
962 } else {
963 UI.forceSetting('clip', clip);
964 return;
965 }
966
967 var cur_clip = display.get_viewport();
968
969 if (typeof(clip) !== 'boolean') {
970 // Use current setting
971 clip = UI.getSetting('clip');
972 }
973
974 if (clip && !cur_clip) {
975 // Turn clipping on
976 UI.updateSetting('clip', true);
977 } else if (!clip && cur_clip) {
978 // Turn clipping off
979 UI.updateSetting('clip', false);
980 display.set_viewport(false);
981 // Disable max dimensions
982 display.set_maxWidth(0);
983 display.set_maxHeight(0);
984 display.viewportChangeSize();
985 }
986 if (UI.getSetting('clip')) {
987 // If clipping, update clipping settings
988 display.set_viewport(true);
989
990 var size = UI.screenSize();
991 if (size) {
992 display.set_maxWidth(size.w);
993 display.set_maxHeight(size.h);
994
995 // Hide potential scrollbars that can skew the position
996 document.getElementById('noVNC_screen').style.overflow = "hidden";
997
998 // The x position marks the left margin of the canvas,
999 // remove the margin from both sides to keep it centered
1000 var new_w = size.w - (2 * Util.getPosition(document.getElementById('noVNC_canvas')).x);
1001
1002 document.getElementById('noVNC_screen').style.overflow = "visible";
1003
1004 display.viewportChangeSize(new_w, size.h);
1005 }
1006 }
1007 },
1008
1009 // Handle special cases where clipping is forced on/off or locked
1010 enableDisableViewClip: function() {
1011 var resizeSetting = document.getElementById('noVNC_setting_resize');
1012 var connected = UI.rfb && UI.rfb_state === 'normal';
1013
1014 if (UI.isSafari) {
1015 // Safari auto-hides the scrollbars which makes them
1016 // impossible to use in most cases
1017 UI.setViewClip(true);
1018 document.getElementById('noVNC_setting_clip').disabled = true;
1019 } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') {
1020 // Disable clipping if we are scaling
1021 UI.setViewClip(false);
1022 document.getElementById('noVNC_setting_clip').disabled = true;
1023 } else if (document.msFullscreenElement) {
1024 // The browser is IE and we are in fullscreen mode.
1025 // - We need to force clipping while in fullscreen since
1026 // scrollbars doesn't work.
1027 UI.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1028 UI.rememberedClipSetting = UI.getSetting('clip');
1029 UI.setViewClip(true);
1030 document.getElementById('noVNC_setting_clip').disabled = true;
1031 } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
1032 // Restore view clip to what it was before fullscreen on IE
1033 UI.setViewClip(UI.rememberedClipSetting);
1034 document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
1035 } else {
1036 document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
1037 if (UI.isTouchDevice) {
1038 UI.setViewClip(true);
1039 }
1040 }
1041 },
1042
1043 /* ------^-------
1044 * /CLIPPING
1045 * ==============
1046 * VIEWDRAG
1047 * ------v------*/
1048
1049 // Update the viewport drag state
1050 updateViewDrag: function(drag) {
1051 if (!UI.rfb) return;
1052
1053 var viewDragButton = document.getElementById('noVNC_view_drag_button');
1054
1055 // Check if viewport drag is possible. It is only possible
1056 // if the remote display is clipping the client display.
1057 if (UI.rfb_state === 'normal' &&
1058 UI.rfb.get_display().get_viewport() &&
1059 UI.rfb.get_display().clippingDisplay()) {
1060
1061 viewDragButton.style.display = "inline";
1062 viewDragButton.disabled = false;
1063
1064 } else {
1065 // The size of the remote display is the same or smaller
1066 // than the client display. Make sure viewport drag isn't
1067 // active when it can't be used.
1068 if (UI.rfb.get_viewportDrag) {
1069 viewDragButton.className = "noVNC_status_button";
1070 UI.rfb.set_viewportDrag(false);
1071 }
1072
1073 // The button is disabled instead of hidden on touch devices
1074 if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
1075 viewDragButton.style.display = "inline";
1076 viewDragButton.disabled = true;
1077 } else {
1078 viewDragButton.style.display = "none";
1079 }
1080 return;
1081 }
1082
1083 if (typeof(drag) !== "undefined" &&
1084 typeof(drag) !== "object") {
1085 if (drag) {
1086 viewDragButton.className = "noVNC_status_button_selected";
1087 UI.rfb.set_viewportDrag(true);
1088 } else {
1089 viewDragButton.className = "noVNC_status_button";
1090 UI.rfb.set_viewportDrag(false);
1091 }
1092 }
1093 },
1094
1095 toggleViewDrag: function() {
1096 if (!UI.rfb) return;
1097
1098 var viewDragButton = document.getElementById('noVNC_view_drag_button');
1099 if (UI.rfb.get_viewportDrag()) {
1100 viewDragButton.className = "noVNC_status_button";
1101 UI.rfb.set_viewportDrag(false);
1102 } else {
1103 viewDragButton.className = "noVNC_status_button_selected";
1104 UI.rfb.set_viewportDrag(true);
1105 }
1106 },
1107
1108 /* ------^-------
1109 * /VIEWDRAG
1110 * ==============
1111 * KEYBOARD
1112 * ------v------*/
1113
1114 // On touch devices, show the OS keyboard
1115 showKeyboard: function() {
1116 var kbi = document.getElementById('noVNC_keyboardinput');
1117 var skb = document.getElementById('noVNC_keyboard_button');
1118 var l = kbi.value.length;
1119 if(UI.keyboardVisible === false) {
1120 kbi.focus();
1121 try { kbi.setSelectionRange(l, l); } // Move the caret to the end
1122 catch (err) {} // setSelectionRange is undefined in Google Chrome
1123 UI.keyboardVisible = true;
1124 skb.className = "noVNC_status_button_selected";
1125 } else if(UI.keyboardVisible === true) {
1126 kbi.blur();
1127 skb.className = "noVNC_status_button";
1128 UI.keyboardVisible = false;
1129 }
1130 },
1131
1132 hideKeyboard: function() {
1133 document.getElementById('noVNC_keyboard_button').className = "noVNC_status_button";
1134 //Weird bug in iOS if you change keyboardVisible
1135 //here it does not actually occur so next time
1136 //you click keyboard icon it doesnt work.
1137 UI.hideKeyboardTimeout = setTimeout(function() {
1138 UI.keyboardVisible = false;
1139 },100);
1140 },
1141
1142 keepKeyboard: function() {
1143 clearTimeout(UI.hideKeyboardTimeout);
1144 if(UI.keyboardVisible === true) {
1145 document.getElementById('noVNC_keyboardinput').focus();
1146 document.getElementById('noVNC_keyboard_button').className = "noVNC_status_button_selected";
1147 } else if(UI.keyboardVisible === false) {
1148 document.getElementById('noVNC_keyboardinput').blur();
1149 document.getElementById('noVNC_keyboard_button').className = "noVNC_status_button";
1150 }
1151 },
1152
1153 keyboardinputReset: function() {
1154 var kbi = document.getElementById('noVNC_keyboardinput');
1155 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1156 UI.lastKeyboardinput = kbi.value;
1157 },
1158
1159 // When normal keyboard events are left uncought, use the input events from
1160 // the keyboardinput element instead and generate the corresponding key events.
1161 // This code is required since some browsers on Android are inconsistent in
1162 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1163 keyInput: function(event) {
1164
1165 if (!UI.rfb) return;
1166
1167 var newValue = event.target.value;
1168
1169 if (!UI.lastKeyboardinput) {
1170 UI.keyboardinputReset();
1171 }
1172 var oldValue = UI.lastKeyboardinput;
1173
1174 var newLen;
1175 try {
1176 // Try to check caret position since whitespace at the end
1177 // will not be considered by value.length in some browsers
1178 newLen = Math.max(event.target.selectionStart, newValue.length);
1179 } catch (err) {
1180 // selectionStart is undefined in Google Chrome
1181 newLen = newValue.length;
1182 }
1183 var oldLen = oldValue.length;
1184
1185 var backspaces;
1186 var inputs = newLen - oldLen;
1187 if (inputs < 0) {
1188 backspaces = -inputs;
1189 } else {
1190 backspaces = 0;
1191 }
1192
1193 // Compare the old string with the new to account for
1194 // text-corrections or other input that modify existing text
1195 var i;
1196 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1197 if (newValue.charAt(i) != oldValue.charAt(i)) {
1198 inputs = newLen - i;
1199 backspaces = oldLen - i;
1200 break;
1201 }
1202 }
1203
1204 // Send the key events
1205 for (i = 0; i < backspaces; i++) {
1206 UI.rfb.sendKey(KeyTable.XK_BackSpace);
1207 }
1208 for (i = newLen - inputs; i < newLen; i++) {
1209 UI.rfb.sendKey(newValue.charCodeAt(i));
1210 }
1211
1212 // Control the text content length in the keyboardinput element
1213 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1214 UI.keyboardinputReset();
1215 } else if (newLen < 1) {
1216 // There always have to be some text in the keyboardinput
1217 // element with which backspace can interact.
1218 UI.keyboardinputReset();
1219 // This sometimes causes the keyboard to disappear for a second
1220 // but it is required for the android keyboard to recognize that
1221 // text has been added to the field
1222 event.target.blur();
1223 // This has to be ran outside of the input handler in order to work
1224 setTimeout(UI.keepKeyboard, 0);
1225 } else {
1226 UI.lastKeyboardinput = newValue;
1227 }
1228 },
1229
1230 toggleExtraKeys: function() {
1231 UI.keepKeyboard();
1232 if(UI.extraKeysVisible === false) {
1233 document.getElementById('noVNC_toggleCtrl_button').style.display = "inline";
1234 document.getElementById('noVNC_toggleAlt_button').style.display = "inline";
1235 document.getElementById('noVNC_sendTab_button').style.display = "inline";
1236 document.getElementById('noVNC_sendEsc_button').style.display = "inline";
1237 document.getElementById('noVNC_toggleExtraKeys_button').className = "noVNC_status_button_selected";
1238 UI.extraKeysVisible = true;
1239 } else if(UI.extraKeysVisible === true) {
1240 document.getElementById('noVNC_toggleCtrl_button').style.display = "";
1241 document.getElementById('noVNC_toggleAlt_button').style.display = "";
1242 document.getElementById('noVNC_sendTab_button').style.display = "";
1243 document.getElementById('noVNC_sendEsc_button').style.display = "";
1244 document.getElementById('noVNC_toggleExtraKeys_button').className = "noVNC_status_button";
1245 UI.extraKeysVisible = false;
1246 }
1247 },
1248
1249 sendEsc: function() {
1250 UI.keepKeyboard();
1251 UI.rfb.sendKey(KeyTable.XK_Escape);
1252 },
1253
1254 sendTab: function() {
1255 UI.keepKeyboard();
1256 UI.rfb.sendKey(KeyTable.XK_Tab);
1257 },
1258
1259 toggleCtrl: function() {
1260 UI.keepKeyboard();
1261 if(UI.ctrlOn === false) {
1262 UI.rfb.sendKey(KeyTable.XK_Control_L, true);
1263 document.getElementById('noVNC_toggleCtrl_button').className = "noVNC_status_button_selected";
1264 UI.ctrlOn = true;
1265 } else if(UI.ctrlOn === true) {
1266 UI.rfb.sendKey(KeyTable.XK_Control_L, false);
1267 document.getElementById('noVNC_toggleCtrl_button').className = "noVNC_status_button";
1268 UI.ctrlOn = false;
1269 }
1270 },
1271
1272 toggleAlt: function() {
1273 UI.keepKeyboard();
1274 if(UI.altOn === false) {
1275 UI.rfb.sendKey(KeyTable.XK_Alt_L, true);
1276 document.getElementById('noVNC_toggleAlt_button').className = "noVNC_status_button_selected";
1277 UI.altOn = true;
1278 } else if(UI.altOn === true) {
1279 UI.rfb.sendKey(KeyTable.XK_Alt_L, false);
1280 document.getElementById('noVNC_toggleAlt_button').className = "noVNC_status_button";
1281 UI.altOn = false;
1282 }
1283 },
1284
1285 sendCtrlAltDel: function() {
1286 UI.rfb.sendCtrlAltDel();
1287 },
1288
1289 /* ------^-------
1290 * /KEYBOARD
1291 * ==============
1292 * MISC
1293 * ------v------*/
1294
1295 setMouseButton: function(num) {
1296 if (typeof num === 'undefined') {
1297 // Disable mouse buttons
1298 num = -1;
1299 }
1300 if (UI.rfb) {
1301 UI.rfb.get_mouse().set_touchButton(num);
1302 }
1303
1304 var blist = [0, 1,2,4];
1305 for (var b = 0; b < blist.length; b++) {
1306 var button = document.getElementById('noVNC_mouse_button' + blist[b]);
1307 if (blist[b] === num) {
1308 button.style.display = "";
1309 } else {
1310 button.style.display = "none";
1311 }
1312 }
1313 },
1314
1315 displayBlur: function() {
1316 if (!UI.rfb) return;
1317
1318 UI.rfb.get_keyboard().set_focused(false);
1319 UI.rfb.get_mouse().set_focused(false);
1320 },
1321
1322 displayFocus: function() {
1323 if (!UI.rfb) return;
1324
1325 UI.rfb.get_keyboard().set_focused(true);
1326 UI.rfb.get_mouse().set_focused(true);
1327 },
1328
1329 // Display the desktop name in the document title
1330 updateDocumentTitle: function(rfb, name) {
1331 document.title = name + " - noVNC";
1332 },
1333
1334 //Helper to add options to dropdown.
1335 addOption: function(selectbox, text, value) {
1336 var optn = document.createElement("OPTION");
1337 optn.text = text;
1338 optn.value = value;
1339 selectbox.options.add(optn);
1340 },
1341
1342 setBarPosition: function() {
1343 document.getElementById('noVNC_control_bar').style.top = (window.pageYOffset) + 'px';
1344 document.getElementById('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
1345
1346 var vncwidth = document.getElementById('noVNC_container').style.offsetWidth;
1347 document.getElementById('noVNC_control_bar').style.width = vncwidth + 'px';
1348 }
1349
1350 /* ------^-------
1351 * /MISC
1352 * ==============
1353 */
1354 };
1355
1356 /* [module] UI.load(); */
1357 })();
1358
1359 /* [module] export default UI; */