]> git.proxmox.com Git - mirror_novnc.git/blob - app/ui.js
Move writeSetting from updateSetting to initSetting
[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 import * as Log from '../core/util/logging.js';
12 import _, { l10n } from './localization.js';
13 import { isTouchDevice } from '../core/util/browser.js';
14 import { setCapture, getPointerEvent } from '../core/util/events.js';
15 import KeyTable from "../core/input/keysym.js";
16 import keysyms from "../core/input/keysymdef.js";
17 import Keyboard from "../core/input/keyboard.js";
18 import RFB from "../core/rfb.js";
19 import Display from "../core/display.js";
20 import * as WebUtil from "./webutil.js";
21
22 var UI = {
23
24 connected: false,
25 desktopName: "",
26
27 statusTimeout: null,
28 hideKeyboardTimeout: null,
29 idleControlbarTimeout: null,
30 closeControlbarTimeout: null,
31
32 controlbarGrabbed: false,
33 controlbarDrag: false,
34 controlbarMouseDownClientY: 0,
35 controlbarMouseDownOffsetY: 0,
36
37 isSafari: false,
38 lastKeyboardinput: null,
39 defaultKeyboardinputLen: 100,
40
41 inhibit_reconnect: true,
42 reconnect_callback: null,
43 reconnect_password: null,
44
45 prime: function(callback) {
46 if (document.readyState === "interactive" || document.readyState === "complete") {
47 UI.load(callback);
48 } else {
49 document.addEventListener('DOMContentLoaded', UI.load.bind(UI, callback));
50 }
51 },
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.isSafari = (navigator.userAgent.indexOf('Safari') !== -1 &&
64 navigator.userAgent.indexOf('Chrome') === -1);
65
66 UI.initSettings();
67
68 // Translate the DOM
69 l10n.translateDOM();
70
71 // Adapt the interface for touch screen devices
72 if (isTouchDevice) {
73 document.documentElement.classList.add("noVNC_touch");
74 // Remove the address bar
75 setTimeout(function() { window.scrollTo(0, 1); }, 100);
76 }
77
78 // Restore control bar position
79 if (WebUtil.readSetting('controlbar_pos') === 'right') {
80 UI.toggleControlbarSide();
81 }
82
83 UI.initFullscreen();
84
85 // Setup event handlers
86 UI.addControlbarHandlers();
87 UI.addTouchSpecificHandlers();
88 UI.addExtraKeysHandlers();
89 UI.addMachineHandlers();
90 UI.addConnectionControlHandlers();
91 UI.addClipboardHandlers();
92 UI.addSettingsHandlers();
93 document.getElementById("noVNC_status")
94 .addEventListener('click', UI.hideStatus);
95
96 // Bootstrap fallback input handler
97 UI.keyboardinputReset();
98
99 UI.openControlbar();
100
101 UI.updateVisualState('init');
102
103 document.documentElement.classList.remove("noVNC_loading");
104
105 var autoconnect = WebUtil.getConfigVar('autoconnect', false);
106 if (autoconnect === 'true' || autoconnect == '1') {
107 autoconnect = true;
108 UI.connect();
109 } else {
110 autoconnect = false;
111 // Show the connect panel on first load unless autoconnecting
112 UI.openConnectPanel();
113 }
114
115 if (typeof callback === "function") {
116 callback(UI.rfb);
117 }
118 },
119
120 initFullscreen: function() {
121 // Only show the button if fullscreen is properly supported
122 // * Safari doesn't support alphanumerical input while in fullscreen
123 if (!UI.isSafari &&
124 (document.documentElement.requestFullscreen ||
125 document.documentElement.mozRequestFullScreen ||
126 document.documentElement.webkitRequestFullscreen ||
127 document.body.msRequestFullscreen)) {
128 document.getElementById('noVNC_fullscreen_button')
129 .classList.remove("noVNC_hidden");
130 UI.addFullscreenHandlers();
131 }
132 },
133
134 initSettings: function() {
135 var i;
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 UI.updateLogging();
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('encrypt', (window.location.protocol === "https:"));
163 UI.initSetting('view_clip', false);
164 UI.initSetting('resize', 'off');
165 UI.initSetting('shared', true);
166 UI.initSetting('view_only', false);
167 UI.initSetting('path', 'websockify');
168 UI.initSetting('repeaterID', '');
169 UI.initSetting('reconnect', false);
170 UI.initSetting('reconnect_delay', 5000);
171
172 UI.setupSettingLabels();
173 },
174 // Adds a link to the label elements on the corresponding input elements
175 setupSettingLabels: function() {
176 var labels = document.getElementsByTagName('LABEL');
177 for (var i = 0; i < labels.length; i++) {
178 var htmlFor = labels[i].htmlFor;
179 if (htmlFor != '') {
180 var elem = document.getElementById(htmlFor);
181 if (elem) elem.label = labels[i];
182 } else {
183 // If 'for' isn't set, use the first input element child
184 var children = labels[i].children;
185 for (var j = 0; j < children.length; j++) {
186 if (children[j].form !== undefined) {
187 children[j].label = labels[i];
188 break;
189 }
190 }
191 }
192 }
193 },
194
195 /* ------^-------
196 * /INIT
197 * ==============
198 * EVENT HANDLERS
199 * ------v------*/
200
201 addControlbarHandlers: function() {
202 document.getElementById("noVNC_control_bar")
203 .addEventListener('mousemove', UI.activateControlbar);
204 document.getElementById("noVNC_control_bar")
205 .addEventListener('mouseup', UI.activateControlbar);
206 document.getElementById("noVNC_control_bar")
207 .addEventListener('mousedown', UI.activateControlbar);
208 document.getElementById("noVNC_control_bar")
209 .addEventListener('keydown', UI.activateControlbar);
210
211 document.getElementById("noVNC_control_bar")
212 .addEventListener('mousedown', UI.keepControlbar);
213 document.getElementById("noVNC_control_bar")
214 .addEventListener('keydown', UI.keepControlbar);
215
216 document.getElementById("noVNC_view_drag_button")
217 .addEventListener('click', UI.toggleViewDrag);
218
219 document.getElementById("noVNC_control_bar_handle")
220 .addEventListener('mousedown', UI.controlbarHandleMouseDown);
221 document.getElementById("noVNC_control_bar_handle")
222 .addEventListener('mouseup', UI.controlbarHandleMouseUp);
223 document.getElementById("noVNC_control_bar_handle")
224 .addEventListener('mousemove', UI.dragControlbarHandle);
225 // resize events aren't available for elements
226 window.addEventListener('resize', UI.updateControlbarHandle);
227
228 var exps = document.getElementsByClassName("noVNC_expander");
229 for (var i = 0;i < exps.length;i++) {
230 exps[i].addEventListener('click', UI.toggleExpander);
231 }
232 },
233
234 addTouchSpecificHandlers: function() {
235 document.getElementById("noVNC_mouse_button0")
236 .addEventListener('click', function () { UI.setMouseButton(1); });
237 document.getElementById("noVNC_mouse_button1")
238 .addEventListener('click', function () { UI.setMouseButton(2); });
239 document.getElementById("noVNC_mouse_button2")
240 .addEventListener('click', function () { UI.setMouseButton(4); });
241 document.getElementById("noVNC_mouse_button4")
242 .addEventListener('click', function () { UI.setMouseButton(0); });
243 document.getElementById("noVNC_keyboard_button")
244 .addEventListener('click', UI.toggleVirtualKeyboard);
245
246 UI.touchKeyboard = new Keyboard(document.getElementById('noVNC_keyboardinput'));
247 UI.touchKeyboard.onkeyevent = UI.keyEvent;
248 UI.touchKeyboard.grab();
249 document.getElementById("noVNC_keyboardinput")
250 .addEventListener('input', UI.keyInput);
251 document.getElementById("noVNC_keyboardinput")
252 .addEventListener('focus', UI.onfocusVirtualKeyboard);
253 document.getElementById("noVNC_keyboardinput")
254 .addEventListener('blur', UI.onblurVirtualKeyboard);
255 document.getElementById("noVNC_keyboardinput")
256 .addEventListener('submit', function () { return false; });
257
258 document.documentElement
259 .addEventListener('mousedown', UI.keepVirtualKeyboard, true);
260
261 document.getElementById("noVNC_control_bar")
262 .addEventListener('touchstart', UI.activateControlbar);
263 document.getElementById("noVNC_control_bar")
264 .addEventListener('touchmove', UI.activateControlbar);
265 document.getElementById("noVNC_control_bar")
266 .addEventListener('touchend', UI.activateControlbar);
267 document.getElementById("noVNC_control_bar")
268 .addEventListener('input', UI.activateControlbar);
269
270 document.getElementById("noVNC_control_bar")
271 .addEventListener('touchstart', UI.keepControlbar);
272 document.getElementById("noVNC_control_bar")
273 .addEventListener('input', UI.keepControlbar);
274
275 document.getElementById("noVNC_control_bar_handle")
276 .addEventListener('touchstart', UI.controlbarHandleMouseDown);
277 document.getElementById("noVNC_control_bar_handle")
278 .addEventListener('touchend', UI.controlbarHandleMouseUp);
279 document.getElementById("noVNC_control_bar_handle")
280 .addEventListener('touchmove', UI.dragControlbarHandle);
281 },
282
283 addExtraKeysHandlers: function() {
284 document.getElementById("noVNC_toggle_extra_keys_button")
285 .addEventListener('click', UI.toggleExtraKeys);
286 document.getElementById("noVNC_toggle_ctrl_button")
287 .addEventListener('click', UI.toggleCtrl);
288 document.getElementById("noVNC_toggle_alt_button")
289 .addEventListener('click', UI.toggleAlt);
290 document.getElementById("noVNC_send_tab_button")
291 .addEventListener('click', UI.sendTab);
292 document.getElementById("noVNC_send_esc_button")
293 .addEventListener('click', UI.sendEsc);
294 document.getElementById("noVNC_send_ctrl_alt_del_button")
295 .addEventListener('click', UI.sendCtrlAltDel);
296 },
297
298 addMachineHandlers: function() {
299 document.getElementById("noVNC_shutdown_button")
300 .addEventListener('click', function() { UI.rfb.machineShutdown(); });
301 document.getElementById("noVNC_reboot_button")
302 .addEventListener('click', function() { UI.rfb.machineReboot(); });
303 document.getElementById("noVNC_reset_button")
304 .addEventListener('click', function() { UI.rfb.machineReset(); });
305 document.getElementById("noVNC_power_button")
306 .addEventListener('click', UI.togglePowerPanel);
307 },
308
309 addConnectionControlHandlers: function() {
310 document.getElementById("noVNC_disconnect_button")
311 .addEventListener('click', UI.disconnect);
312 document.getElementById("noVNC_connect_button")
313 .addEventListener('click', UI.connect);
314 document.getElementById("noVNC_cancel_reconnect_button")
315 .addEventListener('click', UI.cancelReconnect);
316
317 document.getElementById("noVNC_password_button")
318 .addEventListener('click', UI.setPassword);
319 },
320
321 addClipboardHandlers: function() {
322 document.getElementById("noVNC_clipboard_button")
323 .addEventListener('click', UI.toggleClipboardPanel);
324 document.getElementById("noVNC_clipboard_text")
325 .addEventListener('change', UI.clipboardSend);
326 document.getElementById("noVNC_clipboard_clear_button")
327 .addEventListener('click', UI.clipboardClear);
328 },
329
330 // Add a call to save settings when the element changes,
331 // unless the optional parameter changeFunc is used instead.
332 addSettingChangeHandler: function(name, changeFunc) {
333 var settingElem = document.getElementById("noVNC_setting_" + name);
334 if (changeFunc === undefined) {
335 changeFunc = function () { UI.saveSetting(name); };
336 }
337 settingElem.addEventListener('change', changeFunc);
338 },
339
340 addSettingsHandlers: function() {
341 document.getElementById("noVNC_settings_button")
342 .addEventListener('click', UI.toggleSettingsPanel);
343
344 UI.addSettingChangeHandler('encrypt');
345 UI.addSettingChangeHandler('resize');
346 UI.addSettingChangeHandler('resize', UI.enableDisableViewClip);
347 UI.addSettingChangeHandler('resize', UI.applyResizeMode);
348 UI.addSettingChangeHandler('view_clip');
349 UI.addSettingChangeHandler('view_clip', UI.updateViewClip);
350 UI.addSettingChangeHandler('shared');
351 UI.addSettingChangeHandler('view_only');
352 UI.addSettingChangeHandler('view_only', UI.updateViewOnly);
353 UI.addSettingChangeHandler('host');
354 UI.addSettingChangeHandler('port');
355 UI.addSettingChangeHandler('path');
356 UI.addSettingChangeHandler('repeaterID');
357 UI.addSettingChangeHandler('logging');
358 UI.addSettingChangeHandler('logging', UI.updateLogging);
359 UI.addSettingChangeHandler('reconnect');
360 UI.addSettingChangeHandler('reconnect_delay');
361 },
362
363 addFullscreenHandlers: function() {
364 document.getElementById("noVNC_fullscreen_button")
365 .addEventListener('click', UI.toggleFullscreen);
366
367 window.addEventListener('fullscreenchange', UI.updateFullscreenButton);
368 window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton);
369 window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton);
370 window.addEventListener('msfullscreenchange', UI.updateFullscreenButton);
371 },
372
373 /* ------^-------
374 * /EVENT HANDLERS
375 * ==============
376 * VISUAL
377 * ------v------*/
378
379 // Disable/enable controls depending on connection state
380 updateVisualState: function(state) {
381
382 document.documentElement.classList.remove("noVNC_connecting");
383 document.documentElement.classList.remove("noVNC_connected");
384 document.documentElement.classList.remove("noVNC_disconnecting");
385 document.documentElement.classList.remove("noVNC_reconnecting");
386
387 let transition_elem = document.getElementById("noVNC_transition_text");
388 switch (state) {
389 case 'init':
390 break;
391 case 'connecting':
392 transition_elem.textContent = _("Connecting...");
393 document.documentElement.classList.add("noVNC_connecting");
394 break;
395 case 'connected':
396 document.documentElement.classList.add("noVNC_connected");
397 break;
398 case 'disconnecting':
399 transition_elem.textContent = _("Disconnecting...");
400 document.documentElement.classList.add("noVNC_disconnecting");
401 break;
402 case 'disconnected':
403 break;
404 case 'reconnecting':
405 transition_elem.textContent = _("Reconnecting...");
406 document.documentElement.classList.add("noVNC_reconnecting");
407 break;
408 default:
409 Log.Error("Invalid visual state: " + state);
410 UI.showStatus(_("Internal error"), 'error');
411 return;
412 }
413
414 UI.enableDisableViewClip();
415
416 if (UI.connected) {
417 UI.disableSetting('encrypt');
418 UI.disableSetting('shared');
419 UI.disableSetting('host');
420 UI.disableSetting('port');
421 UI.disableSetting('path');
422 UI.disableSetting('repeaterID');
423 UI.setMouseButton(1);
424
425 // Hide the controlbar after 2 seconds
426 UI.closeControlbarTimeout = setTimeout(UI.closeControlbar, 2000);
427 } else {
428 UI.enableSetting('encrypt');
429 UI.enableSetting('shared');
430 UI.enableSetting('host');
431 UI.enableSetting('port');
432 UI.enableSetting('path');
433 UI.enableSetting('repeaterID');
434 UI.updatePowerButton();
435 UI.keepControlbar();
436 }
437
438 // State change disables viewport dragging.
439 // It is enabled (toggled) by direct click on the button
440 UI.setViewDrag(false);
441
442 // State change also closes the password dialog
443 document.getElementById('noVNC_password_dlg')
444 .classList.remove('noVNC_open');
445 },
446
447 showStatus: function(text, status_type, time) {
448 var statusElem = document.getElementById('noVNC_status');
449
450 clearTimeout(UI.statusTimeout);
451
452 if (typeof status_type === 'undefined') {
453 status_type = 'normal';
454 }
455
456 // Don't overwrite more severe visible statuses and never
457 // errors. Only shows the first error.
458 let visible_status_type = 'none';
459 if (statusElem.classList.contains("noVNC_open")) {
460 if (statusElem.classList.contains("noVNC_status_error")) {
461 visible_status_type = 'error';
462 } else if (statusElem.classList.contains("noVNC_status_warn")) {
463 visible_status_type = 'warn';
464 } else {
465 visible_status_type = 'normal';
466 }
467 }
468 if (visible_status_type === 'error' ||
469 (visible_status_type === 'warn' && status_type === 'normal')) {
470 return;
471 }
472
473 switch (status_type) {
474 case 'error':
475 statusElem.classList.remove("noVNC_status_warn");
476 statusElem.classList.remove("noVNC_status_normal");
477 statusElem.classList.add("noVNC_status_error");
478 break;
479 case 'warning':
480 case 'warn':
481 statusElem.classList.remove("noVNC_status_error");
482 statusElem.classList.remove("noVNC_status_normal");
483 statusElem.classList.add("noVNC_status_warn");
484 break;
485 case 'normal':
486 case 'info':
487 default:
488 statusElem.classList.remove("noVNC_status_error");
489 statusElem.classList.remove("noVNC_status_warn");
490 statusElem.classList.add("noVNC_status_normal");
491 break;
492 }
493
494 statusElem.textContent = text;
495 statusElem.classList.add("noVNC_open");
496
497 // If no time was specified, show the status for 1.5 seconds
498 if (typeof time === 'undefined') {
499 time = 1500;
500 }
501
502 // Error messages do not timeout
503 if (status_type !== 'error') {
504 UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
505 }
506 },
507
508 hideStatus: function() {
509 clearTimeout(UI.statusTimeout);
510 document.getElementById('noVNC_status').classList.remove("noVNC_open");
511 },
512
513 activateControlbar: function(event) {
514 clearTimeout(UI.idleControlbarTimeout);
515 // We manipulate the anchor instead of the actual control
516 // bar in order to avoid creating new a stacking group
517 document.getElementById('noVNC_control_bar_anchor')
518 .classList.remove("noVNC_idle");
519 UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
520 },
521
522 idleControlbar: function() {
523 document.getElementById('noVNC_control_bar_anchor')
524 .classList.add("noVNC_idle");
525 },
526
527 keepControlbar: function() {
528 clearTimeout(UI.closeControlbarTimeout);
529 },
530
531 openControlbar: function() {
532 document.getElementById('noVNC_control_bar')
533 .classList.add("noVNC_open");
534 },
535
536 closeControlbar: function() {
537 UI.closeAllPanels();
538 document.getElementById('noVNC_control_bar')
539 .classList.remove("noVNC_open");
540 },
541
542 toggleControlbar: function() {
543 if (document.getElementById('noVNC_control_bar')
544 .classList.contains("noVNC_open")) {
545 UI.closeControlbar();
546 } else {
547 UI.openControlbar();
548 }
549 },
550
551 toggleControlbarSide: function () {
552 // Temporarily disable animation to avoid weird movement
553 var bar = document.getElementById('noVNC_control_bar');
554 bar.style.transitionDuration = '0s';
555 bar.addEventListener('transitionend', function () { this.style.transitionDuration = ""; });
556
557 var anchor = document.getElementById('noVNC_control_bar_anchor');
558 if (anchor.classList.contains("noVNC_right")) {
559 WebUtil.writeSetting('controlbar_pos', 'left');
560 anchor.classList.remove("noVNC_right");
561 } else {
562 WebUtil.writeSetting('controlbar_pos', 'right');
563 anchor.classList.add("noVNC_right");
564 }
565
566 // Consider this a movement of the handle
567 UI.controlbarDrag = true;
568 },
569
570 showControlbarHint: function (show) {
571 var hint = document.getElementById('noVNC_control_bar_hint');
572 if (show) {
573 hint.classList.add("noVNC_active");
574 } else {
575 hint.classList.remove("noVNC_active");
576 }
577 },
578
579 dragControlbarHandle: function (e) {
580 if (!UI.controlbarGrabbed) return;
581
582 var ptr = getPointerEvent(e);
583
584 var anchor = document.getElementById('noVNC_control_bar_anchor');
585 if (ptr.clientX < (window.innerWidth * 0.1)) {
586 if (anchor.classList.contains("noVNC_right")) {
587 UI.toggleControlbarSide();
588 }
589 } else if (ptr.clientX > (window.innerWidth * 0.9)) {
590 if (!anchor.classList.contains("noVNC_right")) {
591 UI.toggleControlbarSide();
592 }
593 }
594
595 if (!UI.controlbarDrag) {
596 // The goal is to trigger on a certain physical width, the
597 // devicePixelRatio brings us a bit closer but is not optimal.
598 var dragThreshold = 10 * (window.devicePixelRatio || 1);
599 var dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
600
601 if (dragDistance < dragThreshold) return;
602
603 UI.controlbarDrag = true;
604 }
605
606 var eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
607
608 UI.moveControlbarHandle(eventY);
609
610 e.preventDefault();
611 e.stopPropagation();
612 UI.keepControlbar();
613 UI.activateControlbar();
614 },
615
616 // Move the handle but don't allow any position outside the bounds
617 moveControlbarHandle: function (viewportRelativeY) {
618 var handle = document.getElementById("noVNC_control_bar_handle");
619 var handleHeight = handle.getBoundingClientRect().height;
620 var controlbarBounds = document.getElementById("noVNC_control_bar")
621 .getBoundingClientRect();
622 var margin = 10;
623
624 // These heights need to be non-zero for the below logic to work
625 if (handleHeight === 0 || controlbarBounds.height === 0) {
626 return;
627 }
628
629 var newY = viewportRelativeY;
630
631 // Check if the coordinates are outside the control bar
632 if (newY < controlbarBounds.top + margin) {
633 // Force coordinates to be below the top of the control bar
634 newY = controlbarBounds.top + margin;
635
636 } else if (newY > controlbarBounds.top +
637 controlbarBounds.height - handleHeight - margin) {
638 // Force coordinates to be above the bottom of the control bar
639 newY = controlbarBounds.top +
640 controlbarBounds.height - handleHeight - margin;
641 }
642
643 // Corner case: control bar too small for stable position
644 if (controlbarBounds.height < (handleHeight + margin * 2)) {
645 newY = controlbarBounds.top +
646 (controlbarBounds.height - handleHeight) / 2;
647 }
648
649 // The transform needs coordinates that are relative to the parent
650 var parentRelativeY = newY - controlbarBounds.top;
651 handle.style.transform = "translateY(" + parentRelativeY + "px)";
652 },
653
654 updateControlbarHandle: function () {
655 // Since the control bar is fixed on the viewport and not the page,
656 // the move function expects coordinates relative the the viewport.
657 var handle = document.getElementById("noVNC_control_bar_handle");
658 var handleBounds = handle.getBoundingClientRect();
659 UI.moveControlbarHandle(handleBounds.top);
660 },
661
662 controlbarHandleMouseUp: function(e) {
663 if ((e.type == "mouseup") && (e.button != 0)) return;
664
665 // mouseup and mousedown on the same place toggles the controlbar
666 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
667 UI.toggleControlbar();
668 e.preventDefault();
669 e.stopPropagation();
670 UI.keepControlbar();
671 UI.activateControlbar();
672 }
673 UI.controlbarGrabbed = false;
674 UI.showControlbarHint(false);
675 },
676
677 controlbarHandleMouseDown: function(e) {
678 if ((e.type == "mousedown") && (e.button != 0)) return;
679
680 var ptr = getPointerEvent(e);
681
682 var handle = document.getElementById("noVNC_control_bar_handle");
683 var bounds = handle.getBoundingClientRect();
684
685 // Touch events have implicit capture
686 if (e.type === "mousedown") {
687 setCapture(handle);
688 }
689
690 UI.controlbarGrabbed = true;
691 UI.controlbarDrag = false;
692
693 UI.showControlbarHint(true);
694
695 UI.controlbarMouseDownClientY = ptr.clientY;
696 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
697 e.preventDefault();
698 e.stopPropagation();
699 UI.keepControlbar();
700 UI.activateControlbar();
701 },
702
703 toggleExpander: function(e) {
704 if (this.classList.contains("noVNC_open")) {
705 this.classList.remove("noVNC_open");
706 } else {
707 this.classList.add("noVNC_open");
708 }
709 },
710
711 /* ------^-------
712 * /VISUAL
713 * ==============
714 * SETTINGS
715 * ------v------*/
716
717 // Initial page load read/initialization of settings
718 initSetting: function(name, defVal) {
719 // Check Query string followed by cookie
720 var val = WebUtil.getConfigVar(name);
721 if (val === null) {
722 val = WebUtil.readSetting(name, defVal);
723 }
724 WebUtil.setSetting(name, val);
725 UI.updateSetting(name);
726 return val;
727 },
728
729 // Update cookie and form control setting. If value is not set, then
730 // updates from control to current cookie setting.
731 updateSetting: function(name) {
732
733 // Update the settings control
734 var value = UI.getSetting(name);
735
736 var ctrl = document.getElementById('noVNC_setting_' + name);
737 if (ctrl.type === 'checkbox') {
738 ctrl.checked = value;
739
740 } else if (typeof ctrl.options !== 'undefined') {
741 for (var i = 0; i < ctrl.options.length; i += 1) {
742 if (ctrl.options[i].value === value) {
743 ctrl.selectedIndex = i;
744 break;
745 }
746 }
747 } else {
748 /*Weird IE9 error leads to 'null' appearring
749 in textboxes instead of ''.*/
750 if (value === null) {
751 value = "";
752 }
753 ctrl.value = value;
754 }
755 },
756
757 // Save control setting to cookie
758 saveSetting: function(name) {
759 var val, ctrl = document.getElementById('noVNC_setting_' + name);
760 if (ctrl.type === 'checkbox') {
761 val = ctrl.checked;
762 } else if (typeof ctrl.options !== 'undefined') {
763 val = ctrl.options[ctrl.selectedIndex].value;
764 } else {
765 val = ctrl.value;
766 }
767 WebUtil.writeSetting(name, val);
768 //Log.Debug("Setting saved '" + name + "=" + val + "'");
769 return val;
770 },
771
772 // Read form control compatible setting from cookie
773 getSetting: function(name) {
774 var ctrl = document.getElementById('noVNC_setting_' + name);
775 var val = WebUtil.readSetting(name);
776 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
777 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
778 val = false;
779 } else {
780 val = true;
781 }
782 }
783 return val;
784 },
785
786 // These helpers compensate for the lack of parent-selectors and
787 // previous-sibling-selectors in CSS which are needed when we want to
788 // disable the labels that belong to disabled input elements.
789 disableSetting: function(name) {
790 var ctrl = document.getElementById('noVNC_setting_' + name);
791 ctrl.disabled = true;
792 ctrl.label.classList.add('noVNC_disabled');
793 },
794
795 enableSetting: function(name) {
796 var ctrl = document.getElementById('noVNC_setting_' + name);
797 ctrl.disabled = false;
798 ctrl.label.classList.remove('noVNC_disabled');
799 },
800
801 /* ------^-------
802 * /SETTINGS
803 * ==============
804 * PANELS
805 * ------v------*/
806
807 closeAllPanels: function() {
808 UI.closeSettingsPanel();
809 UI.closePowerPanel();
810 UI.closeClipboardPanel();
811 UI.closeExtraKeys();
812 },
813
814 /* ------^-------
815 * /PANELS
816 * ==============
817 * SETTINGS (panel)
818 * ------v------*/
819
820 openSettingsPanel: function() {
821 UI.closeAllPanels();
822 UI.openControlbar();
823
824 // Refresh UI elements from saved cookies
825 UI.updateSetting('encrypt');
826 UI.updateSetting('view_clip');
827 UI.updateSetting('resize');
828 UI.updateSetting('shared');
829 UI.updateSetting('view_only');
830 UI.updateSetting('path');
831 UI.updateSetting('repeaterID');
832 UI.updateSetting('logging');
833 UI.updateSetting('reconnect');
834 UI.updateSetting('reconnect_delay');
835
836 document.getElementById('noVNC_settings')
837 .classList.add("noVNC_open");
838 document.getElementById('noVNC_settings_button')
839 .classList.add("noVNC_selected");
840 },
841
842 closeSettingsPanel: function() {
843 document.getElementById('noVNC_settings')
844 .classList.remove("noVNC_open");
845 document.getElementById('noVNC_settings_button')
846 .classList.remove("noVNC_selected");
847 },
848
849 toggleSettingsPanel: function() {
850 if (document.getElementById('noVNC_settings')
851 .classList.contains("noVNC_open")) {
852 UI.closeSettingsPanel();
853 } else {
854 UI.openSettingsPanel();
855 }
856 },
857
858 /* ------^-------
859 * /SETTINGS
860 * ==============
861 * POWER
862 * ------v------*/
863
864 openPowerPanel: function() {
865 UI.closeAllPanels();
866 UI.openControlbar();
867
868 document.getElementById('noVNC_power')
869 .classList.add("noVNC_open");
870 document.getElementById('noVNC_power_button')
871 .classList.add("noVNC_selected");
872 },
873
874 closePowerPanel: function() {
875 document.getElementById('noVNC_power')
876 .classList.remove("noVNC_open");
877 document.getElementById('noVNC_power_button')
878 .classList.remove("noVNC_selected");
879 },
880
881 togglePowerPanel: function() {
882 if (document.getElementById('noVNC_power')
883 .classList.contains("noVNC_open")) {
884 UI.closePowerPanel();
885 } else {
886 UI.openPowerPanel();
887 }
888 },
889
890 // Disable/enable power button
891 updatePowerButton: function() {
892 if (UI.connected &&
893 UI.rfb.capabilities.power &&
894 !UI.rfb.viewOnly) {
895 document.getElementById('noVNC_power_button')
896 .classList.remove("noVNC_hidden");
897 } else {
898 document.getElementById('noVNC_power_button')
899 .classList.add("noVNC_hidden");
900 // Close power panel if open
901 UI.closePowerPanel();
902 }
903 },
904
905 /* ------^-------
906 * /POWER
907 * ==============
908 * CLIPBOARD
909 * ------v------*/
910
911 openClipboardPanel: function() {
912 UI.closeAllPanels();
913 UI.openControlbar();
914
915 document.getElementById('noVNC_clipboard')
916 .classList.add("noVNC_open");
917 document.getElementById('noVNC_clipboard_button')
918 .classList.add("noVNC_selected");
919 },
920
921 closeClipboardPanel: function() {
922 document.getElementById('noVNC_clipboard')
923 .classList.remove("noVNC_open");
924 document.getElementById('noVNC_clipboard_button')
925 .classList.remove("noVNC_selected");
926 },
927
928 toggleClipboardPanel: function() {
929 if (document.getElementById('noVNC_clipboard')
930 .classList.contains("noVNC_open")) {
931 UI.closeClipboardPanel();
932 } else {
933 UI.openClipboardPanel();
934 }
935 },
936
937 clipboardReceive: function(e) {
938 Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0,40) + "...");
939 document.getElementById('noVNC_clipboard_text').value = e.detail.text;
940 Log.Debug("<< UI.clipboardReceive");
941 },
942
943 clipboardClear: function() {
944 document.getElementById('noVNC_clipboard_text').value = "";
945 UI.rfb.clipboardPasteFrom("");
946 },
947
948 clipboardSend: function() {
949 var text = document.getElementById('noVNC_clipboard_text').value;
950 Log.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "...");
951 UI.rfb.clipboardPasteFrom(text);
952 Log.Debug("<< UI.clipboardSend");
953 },
954
955 /* ------^-------
956 * /CLIPBOARD
957 * ==============
958 * CONNECTION
959 * ------v------*/
960
961 openConnectPanel: function() {
962 document.getElementById('noVNC_connect_dlg')
963 .classList.add("noVNC_open");
964 },
965
966 closeConnectPanel: function() {
967 document.getElementById('noVNC_connect_dlg')
968 .classList.remove("noVNC_open");
969 },
970
971 connect: function(event, password) {
972
973 // Ignore when rfb already exists
974 if (typeof UI.rfb !== 'undefined') {
975 return;
976 }
977
978 var host = UI.getSetting('host');
979 var port = UI.getSetting('port');
980 var path = UI.getSetting('path');
981
982 if (typeof password === 'undefined') {
983 password = WebUtil.getConfigVar('password');
984 UI.reconnect_password = password;
985 }
986
987 if (password === null) {
988 password = undefined;
989 }
990
991 UI.hideStatus();
992
993 if (!host) {
994 Log.Error("Can't connect when host is: " + host);
995 UI.showStatus(_("Must set host"), 'error');
996 return;
997 }
998
999 UI.closeAllPanels();
1000 UI.closeConnectPanel();
1001
1002 UI.updateVisualState('connecting');
1003
1004 var url;
1005
1006 url = UI.getSetting('encrypt') ? 'wss' : 'ws';
1007
1008 url += '://' + host;
1009 if(port) {
1010 url += ':' + port;
1011 }
1012 url += '/' + path;
1013
1014 UI.rfb = new RFB(document.getElementById('noVNC_container'), url,
1015 { shared: UI.getSetting('shared'),
1016 repeaterID: UI.getSetting('repeaterID'),
1017 credentials: { password: password } });
1018 UI.rfb.addEventListener("connect", UI.connectFinished);
1019 UI.rfb.addEventListener("disconnect", UI.disconnectFinished);
1020 UI.rfb.addEventListener("credentialsrequired", UI.credentials);
1021 UI.rfb.addEventListener("securityfailure", UI.securityFailed);
1022 UI.rfb.addEventListener("capabilities", function () { UI.updatePowerButton(); });
1023 UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
1024 UI.rfb.addEventListener("bell", UI.bell);
1025 UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
1026 UI.rfb.clipViewport = UI.getSetting('view_clip');
1027 UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
1028 UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
1029
1030 UI.updateViewOnly(); // requires UI.rfb
1031 },
1032
1033 disconnect: function() {
1034 UI.closeAllPanels();
1035 UI.rfb.disconnect();
1036
1037 UI.connected = false;
1038
1039 // Disable automatic reconnecting
1040 UI.inhibit_reconnect = true;
1041
1042 UI.updateVisualState('disconnecting');
1043
1044 // Don't display the connection settings until we're actually disconnected
1045 },
1046
1047 reconnect: function() {
1048 UI.reconnect_callback = null;
1049
1050 // if reconnect has been disabled in the meantime, do nothing.
1051 if (UI.inhibit_reconnect) {
1052 return;
1053 }
1054
1055 UI.connect(null, UI.reconnect_password);
1056 },
1057
1058 cancelReconnect: function() {
1059 if (UI.reconnect_callback !== null) {
1060 clearTimeout(UI.reconnect_callback);
1061 UI.reconnect_callback = null;
1062 }
1063
1064 UI.updateVisualState('disconnected');
1065
1066 UI.openControlbar();
1067 UI.openConnectPanel();
1068 },
1069
1070 connectFinished: function (e) {
1071 UI.connected = true;
1072 UI.inhibit_reconnect = false;
1073
1074 let msg;
1075 if (UI.getSetting('encrypt')) {
1076 msg = _("Connected (encrypted) to ") + UI.desktopName;
1077 } else {
1078 msg = _("Connected (unencrypted) to ") + UI.desktopName;
1079 }
1080 UI.showStatus(msg);
1081 UI.updateVisualState('connected');
1082
1083 // Do this last because it can only be used on rendered elements
1084 UI.rfb.focus();
1085 },
1086
1087 disconnectFinished: function (e) {
1088 let wasConnected = UI.connected;
1089
1090 // This variable is ideally set when disconnection starts, but
1091 // when the disconnection isn't clean or if it is initiated by
1092 // the server, we need to do it here as well since
1093 // UI.disconnect() won't be used in those cases.
1094 UI.connected = false;
1095
1096 UI.rfb = undefined;
1097
1098 if (!e.detail.clean) {
1099 UI.updateVisualState('disconnected');
1100 if (wasConnected) {
1101 UI.showStatus(_("Something went wrong, connection is closed"),
1102 'error');
1103 } else {
1104 UI.showStatus(_("Failed to connect to server"), 'error');
1105 }
1106 } else if (UI.getSetting('reconnect', false) === true && !UI.inhibit_reconnect) {
1107 UI.updateVisualState('reconnecting');
1108
1109 var delay = parseInt(UI.getSetting('reconnect_delay'));
1110 UI.reconnect_callback = setTimeout(UI.reconnect, delay);
1111 return;
1112 } else {
1113 UI.updateVisualState('disconnected');
1114 UI.showStatus(_("Disconnected"), 'normal');
1115 }
1116
1117 UI.openControlbar();
1118 UI.openConnectPanel();
1119 },
1120
1121 securityFailed: function (e) {
1122 let msg = "";
1123 // On security failures we might get a string with a reason
1124 // directly from the server. Note that we can't control if
1125 // this string is translated or not.
1126 if ('reason' in e.detail) {
1127 msg = _("New connection has been rejected with reason: ") +
1128 e.detail.reason;
1129 } else {
1130 msg = _("New connection has been rejected");
1131 }
1132 UI.showStatus(msg, 'error');
1133 },
1134
1135 /* ------^-------
1136 * /CONNECTION
1137 * ==============
1138 * PASSWORD
1139 * ------v------*/
1140
1141 credentials: function(e) {
1142 // FIXME: handle more types
1143 document.getElementById('noVNC_password_dlg')
1144 .classList.add('noVNC_open');
1145
1146 setTimeout(function () {
1147 document.getElementById('noVNC_password_input').focus();
1148 }, 100);
1149
1150 Log.Warn("Server asked for a password");
1151 UI.showStatus(_("Password is required"), "warning");
1152 },
1153
1154 setPassword: function(e) {
1155 // Prevent actually submitting the form
1156 e.preventDefault();
1157
1158 var inputElem = document.getElementById('noVNC_password_input');
1159 var password = inputElem.value;
1160 // Clear the input after reading the password
1161 inputElem.value = "";
1162 UI.rfb.sendCredentials({ password: password });
1163 UI.reconnect_password = password;
1164 document.getElementById('noVNC_password_dlg')
1165 .classList.remove('noVNC_open');
1166 },
1167
1168 /* ------^-------
1169 * /PASSWORD
1170 * ==============
1171 * FULLSCREEN
1172 * ------v------*/
1173
1174 toggleFullscreen: function() {
1175 if (document.fullscreenElement || // alternative standard method
1176 document.mozFullScreenElement || // currently working methods
1177 document.webkitFullscreenElement ||
1178 document.msFullscreenElement) {
1179 if (document.exitFullscreen) {
1180 document.exitFullscreen();
1181 } else if (document.mozCancelFullScreen) {
1182 document.mozCancelFullScreen();
1183 } else if (document.webkitExitFullscreen) {
1184 document.webkitExitFullscreen();
1185 } else if (document.msExitFullscreen) {
1186 document.msExitFullscreen();
1187 }
1188 } else {
1189 if (document.documentElement.requestFullscreen) {
1190 document.documentElement.requestFullscreen();
1191 } else if (document.documentElement.mozRequestFullScreen) {
1192 document.documentElement.mozRequestFullScreen();
1193 } else if (document.documentElement.webkitRequestFullscreen) {
1194 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1195 } else if (document.body.msRequestFullscreen) {
1196 document.body.msRequestFullscreen();
1197 }
1198 }
1199 UI.enableDisableViewClip();
1200 UI.updateFullscreenButton();
1201 },
1202
1203 updateFullscreenButton: function() {
1204 if (document.fullscreenElement || // alternative standard method
1205 document.mozFullScreenElement || // currently working methods
1206 document.webkitFullscreenElement ||
1207 document.msFullscreenElement ) {
1208 document.getElementById('noVNC_fullscreen_button')
1209 .classList.add("noVNC_selected");
1210 } else {
1211 document.getElementById('noVNC_fullscreen_button')
1212 .classList.remove("noVNC_selected");
1213 }
1214 },
1215
1216 /* ------^-------
1217 * /FULLSCREEN
1218 * ==============
1219 * RESIZE
1220 * ------v------*/
1221
1222 // Apply remote resizing or local scaling
1223 applyResizeMode: function() {
1224 if (!UI.rfb) return;
1225
1226 UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
1227 UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
1228 },
1229
1230 /* ------^-------
1231 * /RESIZE
1232 * ==============
1233 * VIEW CLIPPING
1234 * ------v------*/
1235
1236 // Update parameters that depend on the viewport clip setting
1237 updateViewClip: function() {
1238 if (!UI.rfb) return;
1239
1240 var cur_clip = UI.rfb.clipViewport;
1241 var new_clip = UI.getSetting('view_clip');
1242
1243 if (isTouchDevice) {
1244 // Touch devices usually have shit scrollbars
1245 new_clip = true;
1246 }
1247
1248 if (cur_clip !== new_clip) {
1249 UI.rfb.clipViewport = new_clip;
1250 }
1251
1252 // Changing the viewport may change the state of
1253 // the dragging button
1254 UI.updateViewDrag();
1255 },
1256
1257 // Handle special cases where viewport clipping is forced on/off or locked
1258 enableDisableViewClip: function() {
1259 var resizeSetting = UI.getSetting('resize');
1260 // Disable clipping if we are scaling, connected or on touch
1261 if (resizeSetting === 'scale' ||
1262 isTouchDevice) {
1263 UI.disableSetting('view_clip');
1264 } else {
1265 UI.enableSetting('view_clip');
1266 }
1267 },
1268
1269 /* ------^-------
1270 * /VIEW CLIPPING
1271 * ==============
1272 * VIEWDRAG
1273 * ------v------*/
1274
1275 toggleViewDrag: function() {
1276 if (!UI.rfb) return;
1277
1278 var drag = UI.rfb.dragViewport;
1279 UI.setViewDrag(!drag);
1280 },
1281
1282 // Set the view drag mode which moves the viewport on mouse drags
1283 setViewDrag: function(drag) {
1284 if (!UI.rfb) return;
1285
1286 UI.rfb.dragViewport = drag;
1287
1288 UI.updateViewDrag();
1289 },
1290
1291 updateViewDrag: function() {
1292 if (!UI.connected) return;
1293
1294 var viewDragButton = document.getElementById('noVNC_view_drag_button');
1295
1296 if (!UI.rfb.clipViewport && UI.rfb.dragViewport) {
1297 // We are no longer clipping the viewport. Make sure
1298 // viewport drag isn't active when it can't be used.
1299 UI.rfb.dragViewport = false;
1300 }
1301
1302 if (UI.rfb.dragViewport) {
1303 viewDragButton.classList.add("noVNC_selected");
1304 } else {
1305 viewDragButton.classList.remove("noVNC_selected");
1306 }
1307
1308 // Different behaviour for touch vs non-touch
1309 // The button is disabled instead of hidden on touch devices
1310 if (isTouchDevice) {
1311 viewDragButton.classList.remove("noVNC_hidden");
1312
1313 if (UI.rfb.clipViewport) {
1314 viewDragButton.disabled = false;
1315 } else {
1316 viewDragButton.disabled = true;
1317 }
1318 } else {
1319 viewDragButton.disabled = false;
1320
1321 if (UI.rfb.clipViewport) {
1322 viewDragButton.classList.remove("noVNC_hidden");
1323 } else {
1324 viewDragButton.classList.add("noVNC_hidden");
1325 }
1326 }
1327 },
1328
1329 /* ------^-------
1330 * /VIEWDRAG
1331 * ==============
1332 * KEYBOARD
1333 * ------v------*/
1334
1335 showVirtualKeyboard: function() {
1336 if (!isTouchDevice) return;
1337
1338 var input = document.getElementById('noVNC_keyboardinput');
1339
1340 if (document.activeElement == input) return;
1341
1342 input.focus();
1343
1344 try {
1345 var l = input.value.length;
1346 // Move the caret to the end
1347 input.setSelectionRange(l, l);
1348 } catch (err) {} // setSelectionRange is undefined in Google Chrome
1349 },
1350
1351 hideVirtualKeyboard: function() {
1352 if (!isTouchDevice) return;
1353
1354 var input = document.getElementById('noVNC_keyboardinput');
1355
1356 if (document.activeElement != input) return;
1357
1358 input.blur();
1359 },
1360
1361 toggleVirtualKeyboard: function () {
1362 if (document.getElementById('noVNC_keyboard_button')
1363 .classList.contains("noVNC_selected")) {
1364 UI.hideVirtualKeyboard();
1365 } else {
1366 UI.showVirtualKeyboard();
1367 }
1368 },
1369
1370 onfocusVirtualKeyboard: function(event) {
1371 document.getElementById('noVNC_keyboard_button')
1372 .classList.add("noVNC_selected");
1373 if (UI.rfb) {
1374 UI.rfb.focusOnClick = false;
1375 }
1376 },
1377
1378 onblurVirtualKeyboard: function(event) {
1379 document.getElementById('noVNC_keyboard_button')
1380 .classList.remove("noVNC_selected");
1381 if (UI.rfb) {
1382 UI.rfb.focusOnClick = true;
1383 }
1384 },
1385
1386 keepVirtualKeyboard: function(event) {
1387 var input = document.getElementById('noVNC_keyboardinput');
1388
1389 // Only prevent focus change if the virtual keyboard is active
1390 if (document.activeElement != input) {
1391 return;
1392 }
1393
1394 // Only allow focus to move to other elements that need
1395 // focus to function properly
1396 if (event.target.form !== undefined) {
1397 switch (event.target.type) {
1398 case 'text':
1399 case 'email':
1400 case 'search':
1401 case 'password':
1402 case 'tel':
1403 case 'url':
1404 case 'textarea':
1405 case 'select-one':
1406 case 'select-multiple':
1407 return;
1408 }
1409 }
1410
1411 event.preventDefault();
1412 },
1413
1414 keyboardinputReset: function() {
1415 var kbi = document.getElementById('noVNC_keyboardinput');
1416 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1417 UI.lastKeyboardinput = kbi.value;
1418 },
1419
1420 keyEvent: function (keysym, code, down) {
1421 if (!UI.rfb) return;
1422
1423 UI.rfb.sendKey(keysym, code, down);
1424 },
1425
1426 // When normal keyboard events are left uncought, use the input events from
1427 // the keyboardinput element instead and generate the corresponding key events.
1428 // This code is required since some browsers on Android are inconsistent in
1429 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1430 keyInput: function(event) {
1431
1432 if (!UI.rfb) return;
1433
1434 var newValue = event.target.value;
1435
1436 if (!UI.lastKeyboardinput) {
1437 UI.keyboardinputReset();
1438 }
1439 var oldValue = UI.lastKeyboardinput;
1440
1441 var newLen;
1442 try {
1443 // Try to check caret position since whitespace at the end
1444 // will not be considered by value.length in some browsers
1445 newLen = Math.max(event.target.selectionStart, newValue.length);
1446 } catch (err) {
1447 // selectionStart is undefined in Google Chrome
1448 newLen = newValue.length;
1449 }
1450 var oldLen = oldValue.length;
1451
1452 var backspaces;
1453 var inputs = newLen - oldLen;
1454 if (inputs < 0) {
1455 backspaces = -inputs;
1456 } else {
1457 backspaces = 0;
1458 }
1459
1460 // Compare the old string with the new to account for
1461 // text-corrections or other input that modify existing text
1462 var i;
1463 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1464 if (newValue.charAt(i) != oldValue.charAt(i)) {
1465 inputs = newLen - i;
1466 backspaces = oldLen - i;
1467 break;
1468 }
1469 }
1470
1471 // Send the key events
1472 for (i = 0; i < backspaces; i++) {
1473 UI.rfb.sendKey(KeyTable.XK_BackSpace, "Backspace");
1474 }
1475 for (i = newLen - inputs; i < newLen; i++) {
1476 UI.rfb.sendKey(keysyms.lookup(newValue.charCodeAt(i)));
1477 }
1478
1479 // Control the text content length in the keyboardinput element
1480 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1481 UI.keyboardinputReset();
1482 } else if (newLen < 1) {
1483 // There always have to be some text in the keyboardinput
1484 // element with which backspace can interact.
1485 UI.keyboardinputReset();
1486 // This sometimes causes the keyboard to disappear for a second
1487 // but it is required for the android keyboard to recognize that
1488 // text has been added to the field
1489 event.target.blur();
1490 // This has to be ran outside of the input handler in order to work
1491 setTimeout(event.target.focus.bind(event.target), 0);
1492 } else {
1493 UI.lastKeyboardinput = newValue;
1494 }
1495 },
1496
1497 /* ------^-------
1498 * /KEYBOARD
1499 * ==============
1500 * EXTRA KEYS
1501 * ------v------*/
1502
1503 openExtraKeys: function() {
1504 UI.closeAllPanels();
1505 UI.openControlbar();
1506
1507 document.getElementById('noVNC_modifiers')
1508 .classList.add("noVNC_open");
1509 document.getElementById('noVNC_toggle_extra_keys_button')
1510 .classList.add("noVNC_selected");
1511 },
1512
1513 closeExtraKeys: function() {
1514 document.getElementById('noVNC_modifiers')
1515 .classList.remove("noVNC_open");
1516 document.getElementById('noVNC_toggle_extra_keys_button')
1517 .classList.remove("noVNC_selected");
1518 },
1519
1520 toggleExtraKeys: function() {
1521 if(document.getElementById('noVNC_modifiers')
1522 .classList.contains("noVNC_open")) {
1523 UI.closeExtraKeys();
1524 } else {
1525 UI.openExtraKeys();
1526 }
1527 },
1528
1529 sendEsc: function() {
1530 UI.rfb.sendKey(KeyTable.XK_Escape, "Escape");
1531 },
1532
1533 sendTab: function() {
1534 UI.rfb.sendKey(KeyTable.XK_Tab);
1535 },
1536
1537 toggleCtrl: function() {
1538 var btn = document.getElementById('noVNC_toggle_ctrl_button');
1539 if (btn.classList.contains("noVNC_selected")) {
1540 UI.rfb.sendKey(KeyTable.XK_Control_L, "ControlLeft", false);
1541 btn.classList.remove("noVNC_selected");
1542 } else {
1543 UI.rfb.sendKey(KeyTable.XK_Control_L, "ControlLeft", true);
1544 btn.classList.add("noVNC_selected");
1545 }
1546 },
1547
1548 toggleAlt: function() {
1549 var btn = document.getElementById('noVNC_toggle_alt_button');
1550 if (btn.classList.contains("noVNC_selected")) {
1551 UI.rfb.sendKey(KeyTable.XK_Alt_L, "AltLeft", false);
1552 btn.classList.remove("noVNC_selected");
1553 } else {
1554 UI.rfb.sendKey(KeyTable.XK_Alt_L, "AltLeft", true);
1555 btn.classList.add("noVNC_selected");
1556 }
1557 },
1558
1559 sendCtrlAltDel: function() {
1560 UI.rfb.sendCtrlAltDel();
1561 },
1562
1563 /* ------^-------
1564 * /EXTRA KEYS
1565 * ==============
1566 * MISC
1567 * ------v------*/
1568
1569 setMouseButton: function(num) {
1570 var view_only = UI.rfb.viewOnly;
1571 if (UI.rfb && !view_only) {
1572 UI.rfb.touchButton = num;
1573 }
1574
1575 var blist = [0, 1,2,4];
1576 for (var b = 0; b < blist.length; b++) {
1577 var button = document.getElementById('noVNC_mouse_button' +
1578 blist[b]);
1579 if (blist[b] === num && !view_only) {
1580 button.classList.remove("noVNC_hidden");
1581 } else {
1582 button.classList.add("noVNC_hidden");
1583 }
1584 }
1585 },
1586
1587 updateViewOnly: function() {
1588 if (!UI.rfb) return;
1589 UI.rfb.viewOnly = UI.getSetting('view_only');
1590
1591 // Hide input related buttons in view only mode
1592 if (UI.rfb.viewOnly) {
1593 document.getElementById('noVNC_keyboard_button')
1594 .classList.add('noVNC_hidden');
1595 document.getElementById('noVNC_toggle_extra_keys_button')
1596 .classList.add('noVNC_hidden');
1597 } else {
1598 document.getElementById('noVNC_keyboard_button')
1599 .classList.remove('noVNC_hidden');
1600 document.getElementById('noVNC_toggle_extra_keys_button')
1601 .classList.remove('noVNC_hidden');
1602 }
1603 UI.setMouseButton(1); //has it's own logic for hiding/showing
1604 },
1605
1606 updateLogging: function() {
1607 WebUtil.init_logging(UI.getSetting('logging'));
1608 },
1609
1610 updateDesktopName: function(e) {
1611 UI.desktopName = e.detail.name;
1612 // Display the desktop name in the document title
1613 document.title = e.detail.name + " - noVNC";
1614 },
1615
1616 bell: function(e) {
1617 if (WebUtil.getConfigVar('bell', 'on') === 'on') {
1618 var promise = document.getElementById('noVNC_bell').play();
1619 // The standards disagree on the return value here
1620 if (promise) {
1621 promise.catch(function(e) {
1622 if (e.name === "NotAllowedError") {
1623 // Ignore when the browser doesn't let us play audio.
1624 // It is common that the browsers require audio to be
1625 // initiated from a user action.
1626 } else {
1627 Log.Error("Unable to play bell: " + e);
1628 }
1629 });
1630 }
1631 }
1632 },
1633
1634 //Helper to add options to dropdown.
1635 addOption: function(selectbox, text, value) {
1636 var optn = document.createElement("OPTION");
1637 optn.text = text;
1638 optn.value = value;
1639 selectbox.options.add(optn);
1640 },
1641
1642 /* ------^-------
1643 * /MISC
1644 * ==============
1645 */
1646 };
1647
1648 // Set up translations
1649 var LINGUAS = ["de", "el", "es", "nl", "pl", "sv", "tr", "zh"];
1650 l10n.setup(LINGUAS);
1651 if (l10n.language !== "en" && l10n.dictionary === undefined) {
1652 WebUtil.fetchJSON('app/locale/' + l10n.language + '.json', function (translations) {
1653 l10n.dictionary = translations;
1654
1655 // wait for translations to load before loading the UI
1656 UI.prime();
1657 }, function (err) {
1658 Log.Error("Failed to load translations: " + err);
1659 UI.prime();
1660 });
1661 } else {
1662 UI.prime();
1663 }
1664
1665 export default UI;