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