Cleanup: UI code
File: ui.js Tests Added: False Changes: - Fix JSHint errors - add some curly braces to improve clarity - move variable declarations to relevant locations instead of at the top of methods
This commit is contained in:
parent
b1dee94788
commit
bbbf42bb5a
126
include/ui.js
126
include/ui.js
|
@ -7,10 +7,14 @@
|
|||
* See README.md for usage and integration instructions.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
/* jslint white: false, browser: true */
|
||||
/* global window, $D, Util, WebUtil, RFB, Display */
|
||||
|
||||
var UI;
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
// Load supporting scripts
|
||||
window.onscriptsload = function () { UI.load(); };
|
||||
window.onload = function () { UI.keyboardinputReset(); };
|
||||
|
@ -42,19 +46,18 @@ load: function (callback) {
|
|||
|
||||
// Render default UI and initialize settings menu
|
||||
start: function(callback) {
|
||||
var html = '', i, sheet, sheets, llevels, port, autoconnect;
|
||||
|
||||
UI.isTouchDevice = 'ontouchstart' in document.documentElement;
|
||||
|
||||
// Stylesheet selection dropdown
|
||||
sheet = WebUtil.selectStylesheet();
|
||||
sheets = WebUtil.getStylesheets();
|
||||
var sheet = WebUtil.selectStylesheet();
|
||||
var sheets = WebUtil.getStylesheets();
|
||||
var i;
|
||||
for (i = 0; i < sheets.length; i += 1) {
|
||||
UI.addOption($D('noVNC_stylesheet'),sheets[i].title, sheets[i].title);
|
||||
}
|
||||
|
||||
// Logging selection dropdown
|
||||
llevels = ['error', 'warn', 'info', 'debug'];
|
||||
var llevels = ['error', 'warn', 'info', 'debug'];
|
||||
for (i = 0; i < llevels.length; i += 1) {
|
||||
UI.addOption($D('noVNC_logging'),llevels[i], llevels[i]);
|
||||
}
|
||||
|
@ -70,7 +73,7 @@ start: function(callback) {
|
|||
|
||||
// if port == 80 (or 443) then it won't be present and should be
|
||||
// set manually
|
||||
port = window.location.port;
|
||||
var port = window.location.port;
|
||||
if (!port) {
|
||||
if (window.location.protocol.substring(0,5) == 'https') {
|
||||
port = 443;
|
||||
|
@ -92,13 +95,13 @@ start: function(callback) {
|
|||
UI.initSetting('path', 'websockify');
|
||||
UI.initSetting('repeaterID', '');
|
||||
|
||||
UI.rfb = RFB({'target': $D('noVNC_canvas'),
|
||||
UI.rfb = new RFB({'target': $D('noVNC_canvas'),
|
||||
'onUpdateState': UI.updateState,
|
||||
'onXvpInit': UI.updateXvpVisualState,
|
||||
'onClipboard': UI.clipReceive,
|
||||
'onDesktopName': UI.updateDocumentTitle});
|
||||
|
||||
autoconnect = WebUtil.getQueryVar('autoconnect', false);
|
||||
var autoconnect = WebUtil.getQueryVar('autoconnect', false);
|
||||
if (autoconnect === 'true' || autoconnect == '1') {
|
||||
autoconnect = true;
|
||||
UI.connect();
|
||||
|
@ -108,14 +111,6 @@ start: function(callback) {
|
|||
|
||||
UI.updateVisualState();
|
||||
|
||||
// Unfocus clipboard when over the VNC area
|
||||
//$D('VNC_screen').onmousemove = function () {
|
||||
// var keyboard = UI.rfb.get_keyboard();
|
||||
// if ((! keyboard) || (! keyboard.get_focused())) {
|
||||
// $D('VNC_clipboard_text').blur();
|
||||
// }
|
||||
// };
|
||||
|
||||
// Show mouse selector buttons on touch screen devices
|
||||
if (UI.isTouchDevice) {
|
||||
// Show mobile buttons
|
||||
|
@ -214,8 +209,8 @@ addMouseHandlers: function() {
|
|||
|
||||
// Read form control compatible setting from cookie
|
||||
getSetting: function(name) {
|
||||
var val, ctrl = $D('noVNC_' + name);
|
||||
val = WebUtil.readSetting(name);
|
||||
var ctrl = $D('noVNC_' + name);
|
||||
var val = WebUtil.readSetting(name);
|
||||
if (val !== null && ctrl.type === 'checkbox') {
|
||||
if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
|
||||
val = false;
|
||||
|
@ -230,7 +225,6 @@ getSetting: function(name) {
|
|||
// updates from control to current cookie setting.
|
||||
updateSetting: function(name, value) {
|
||||
|
||||
var i, ctrl = $D('noVNC_' + name);
|
||||
// Save the cookie for this session
|
||||
if (typeof value !== 'undefined') {
|
||||
WebUtil.writeSetting(name, value);
|
||||
|
@ -239,11 +233,12 @@ updateSetting: function(name, value) {
|
|||
// Update the settings control
|
||||
value = UI.getSetting(name);
|
||||
|
||||
var ctrl = $D('noVNC_' + name);
|
||||
if (ctrl.type === 'checkbox') {
|
||||
ctrl.checked = value;
|
||||
|
||||
} else if (typeof ctrl.options !== 'undefined') {
|
||||
for (i = 0; i < ctrl.options.length; i += 1) {
|
||||
for (var i = 0; i < ctrl.options.length; i += 1) {
|
||||
if (ctrl.options[i].value === value) {
|
||||
ctrl.selectedIndex = i;
|
||||
break;
|
||||
|
@ -276,15 +271,12 @@ saveSetting: function(name) {
|
|||
|
||||
// Initial page load read/initialization of settings
|
||||
initSetting: function(name, defVal) {
|
||||
var val;
|
||||
|
||||
// Check Query string followed by cookie
|
||||
val = WebUtil.getQueryVar(name);
|
||||
var val = WebUtil.getQueryVar(name);
|
||||
if (val === null) {
|
||||
val = WebUtil.readSetting(name, defVal);
|
||||
}
|
||||
UI.updateSetting(name, val);
|
||||
//Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
|
||||
return val;
|
||||
},
|
||||
|
||||
|
@ -530,8 +522,6 @@ xvpReset: function() {
|
|||
},
|
||||
|
||||
setMouseButton: function(num) {
|
||||
var b, blist = [0, 1,2,4], button;
|
||||
|
||||
if (typeof num === 'undefined') {
|
||||
// Disable mouse buttons
|
||||
num = -1;
|
||||
|
@ -540,25 +530,20 @@ setMouseButton: function(num) {
|
|||
UI.rfb.get_mouse().set_touchButton(num);
|
||||
}
|
||||
|
||||
for (b = 0; b < blist.length; b++) {
|
||||
button = $D('noVNC_mouse_button' + blist[b]);
|
||||
var blist = [0, 1,2,4];
|
||||
for (var b = 0; b < blist.length; b++) {
|
||||
var button = $D('noVNC_mouse_button' + blist[b]);
|
||||
if (blist[b] === num) {
|
||||
button.style.display = "";
|
||||
} else {
|
||||
button.style.display = "none";
|
||||
/*
|
||||
button.style.backgroundColor = "black";
|
||||
button.style.color = "lightgray";
|
||||
button.style.backgroundColor = "";
|
||||
button.style.color = "";
|
||||
*/
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
updateState: function(rfb, state, oldstate, msg) {
|
||||
var s, sb, c, d, cad, vd, klass;
|
||||
UI.rfb_state = state;
|
||||
var klass;
|
||||
switch (state) {
|
||||
case 'failed':
|
||||
case 'fatal':
|
||||
|
@ -569,7 +554,7 @@ updateState: function(rfb, state, oldstate, msg) {
|
|||
break;
|
||||
case 'disconnected':
|
||||
$D('noVNC_logo').style.display = "block";
|
||||
// Fall through
|
||||
/* falls through */
|
||||
case 'loaded':
|
||||
klass = "noVNC_status_normal";
|
||||
break;
|
||||
|
@ -664,32 +649,27 @@ updateXvpVisualState: function(ver) {
|
|||
}
|
||||
},
|
||||
|
||||
|
||||
// Display the desktop name in the document title
|
||||
updateDocumentTitle: function(rfb, name) {
|
||||
document.title = name + " - noVNC";
|
||||
},
|
||||
|
||||
|
||||
clipReceive: function(rfb, text) {
|
||||
Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
|
||||
$D('noVNC_clipboard_text').value = text;
|
||||
Util.Debug("<< UI.clipReceive");
|
||||
},
|
||||
|
||||
|
||||
connect: function() {
|
||||
var host, port, password, path;
|
||||
|
||||
UI.closeSettingsMenu();
|
||||
UI.toggleConnectPanel();
|
||||
|
||||
host = $D('noVNC_host').value;
|
||||
port = $D('noVNC_port').value;
|
||||
password = $D('noVNC_password').value;
|
||||
path = $D('noVNC_path').value;
|
||||
var host = $D('noVNC_host').value;
|
||||
var port = $D('noVNC_port').value;
|
||||
var password = $D('noVNC_password').value;
|
||||
var path = $D('noVNC_path').value;
|
||||
if ((!host) || (!port)) {
|
||||
throw("Must set host and port");
|
||||
throw new Error("Must set host and port");
|
||||
}
|
||||
|
||||
UI.rfb.set_encrypt(UI.getSetting('encrypt'));
|
||||
|
@ -737,18 +717,16 @@ clipSend: function() {
|
|||
Util.Debug("<< UI.clipSend");
|
||||
},
|
||||
|
||||
|
||||
// Enable/disable and configure viewport clipping
|
||||
setViewClip: function(clip) {
|
||||
var display, cur_clip, pos, new_w, new_h;
|
||||
|
||||
var display;
|
||||
if (UI.rfb) {
|
||||
display = UI.rfb.get_display();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
cur_clip = display.get_viewport();
|
||||
var cur_clip = display.get_viewport();
|
||||
|
||||
if (typeof(clip) !== 'boolean') {
|
||||
// Use current setting
|
||||
|
@ -768,9 +746,9 @@ setViewClip: function(clip) {
|
|||
if (UI.getSetting('clip')) {
|
||||
// If clipping, update clipping settings
|
||||
$D('noVNC_canvas').style.position = 'absolute';
|
||||
pos = Util.getPosition($D('noVNC_canvas'));
|
||||
new_w = window.innerWidth - pos.x;
|
||||
new_h = window.innerHeight - pos.y;
|
||||
var pos = Util.getPosition($D('noVNC_canvas'));
|
||||
var new_w = window.innerWidth - pos.x;
|
||||
var new_h = window.innerHeight - pos.y;
|
||||
display.set_viewport(true);
|
||||
display.viewportChange(0, 0, new_w, new_h);
|
||||
}
|
||||
|
@ -804,10 +782,9 @@ setViewDrag: function(drag) {
|
|||
|
||||
// On touch devices, show the OS keyboard
|
||||
showKeyboard: function() {
|
||||
var kbi, skb, l;
|
||||
kbi = $D('keyboardinput');
|
||||
skb = $D('showKeyboard');
|
||||
l = kbi.value.length;
|
||||
var kbi = $D('keyboardinput');
|
||||
var skb = $D('showKeyboard');
|
||||
var l = kbi.value.length;
|
||||
if(UI.keyboardVisible === false) {
|
||||
kbi.focus();
|
||||
try { kbi.setSelectionRange(l, l); } // Move the caret to the end
|
||||
|
@ -834,7 +811,7 @@ keepKeyboard: function() {
|
|||
|
||||
keyboardinputReset: function() {
|
||||
var kbi = $D('keyboardinput');
|
||||
kbi.value = Array(UI.defaultKeyboardinputLen).join("_");
|
||||
kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
|
||||
UI.lastKeyboardinput = kbi.value;
|
||||
},
|
||||
|
||||
|
@ -843,10 +820,10 @@ keyboardinputReset: function() {
|
|||
// This code is required since some browsers on Android are inconsistent in
|
||||
// sending keyCodes in the normal keyboard events when using on screen keyboards.
|
||||
keyInput: function(event) {
|
||||
var newValue, oldValue, newLen, oldLen;
|
||||
newValue = event.target.value;
|
||||
oldValue = UI.lastKeyboardinput;
|
||||
var newValue = event.target.value;
|
||||
var oldValue = UI.lastKeyboardinput;
|
||||
|
||||
var newLen;
|
||||
try {
|
||||
// Try to check caret position since whitespace at the end
|
||||
// will not be considered by value.length in some browsers
|
||||
|
@ -855,18 +832,20 @@ keyInput: function(event) {
|
|||
// selectionStart is undefined in Google Chrome
|
||||
newLen = newValue.length;
|
||||
}
|
||||
oldLen = oldValue.length;
|
||||
var oldLen = oldValue.length;
|
||||
|
||||
var backspaces;
|
||||
var inputs = newLen - oldLen;
|
||||
if (inputs < 0)
|
||||
if (inputs < 0) {
|
||||
backspaces = -inputs;
|
||||
else
|
||||
} else {
|
||||
backspaces = 0;
|
||||
}
|
||||
|
||||
// Compare the old string with the new to account for
|
||||
// text-corrections or other input that modify existing text
|
||||
for (var i = 0; i < Math.min(oldLen, newLen); i++) {
|
||||
var i;
|
||||
for (i = 0; i < Math.min(oldLen, newLen); i++) {
|
||||
if (newValue.charAt(i) != oldValue.charAt(i)) {
|
||||
inputs = newLen - i;
|
||||
backspaces = oldLen - i;
|
||||
|
@ -875,10 +854,12 @@ keyInput: function(event) {
|
|||
}
|
||||
|
||||
// Send the key events
|
||||
for (var i = 0; i < backspaces; i++)
|
||||
for (i = 0; i < backspaces; i++) {
|
||||
UI.rfb.sendKey(XK_BackSpace);
|
||||
for (var i = newLen - inputs; i < newLen; i++)
|
||||
}
|
||||
for (i = newLen - inputs; i < newLen; i++) {
|
||||
UI.rfb.sendKey(newValue.charCodeAt(i));
|
||||
}
|
||||
|
||||
// Control the text content length in the keyboardinput element
|
||||
if (newLen > 2 * UI.defaultKeyboardinputLen) {
|
||||
|
@ -893,7 +874,6 @@ keyInput: function(event) {
|
|||
event.target.blur();
|
||||
// This has to be ran outside of the input handler in order to work
|
||||
setTimeout(function() { UI.keepKeyboard(); }, 0);
|
||||
|
||||
} else {
|
||||
UI.lastKeyboardinput = newValue;
|
||||
}
|
||||
|
@ -980,8 +960,7 @@ setResize: function () {
|
|||
},
|
||||
|
||||
//Helper to add options to dropdown.
|
||||
addOption: function(selectbox,text,value )
|
||||
{
|
||||
addOption: function(selectbox, text, value) {
|
||||
var optn = document.createElement("OPTION");
|
||||
optn.text = text;
|
||||
optn.value = value;
|
||||
|
@ -997,7 +976,4 @@ setBarPosition: function() {
|
|||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
})();
|
||||
|
|
Loading…
Reference in New Issue