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