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