turning on strict mode for various js files

10.3.x-maintenance
cTn 2014-08-09 19:38:46 +02:00 committed by Dominic Clifton
parent 01769559cc
commit d9c315338d
12 changed files with 140 additions and 98 deletions

View File

@ -1,3 +1,5 @@
'use strict';
function configuration_backup() {
// request configuration data (one by one)

View File

@ -1,3 +1,5 @@
'use strict';
var firmware_version_accepted = 2.3;
var CONFIG = {

View File

@ -1,3 +1,5 @@
'use strict';
var tabs = {}; // filled by individual tab js file
var GUI_control = function() {

View File

@ -1,3 +1,5 @@
'use strict';
function localize() {
var localized = 0;

View File

@ -1,3 +1,5 @@
'use strict';
// MSP_codes needs to be re-integrated inside MSP object
var MSP_codes = {
MSP_IDENT: 100,
@ -55,7 +57,7 @@ var MSP_codes = {
var MSP = {
state: 0,
message_status: 1,
message_direction: 1,
code: 0,
message_length_expected: 0,
message_length_received: 0,
@ -66,7 +68,7 @@ var MSP = {
callbacks: [],
packet_error: 0,
callbacks_cleanup: function() {
callbacks_cleanup: function () {
for (var i = 0; i < this.callbacks.length; i++) {
clearInterval(this.callbacks[i].timer);
}
@ -74,7 +76,7 @@ var MSP = {
this.callbacks = [];
},
disconnect_cleanup: function() {
disconnect_cleanup: function () {
this.state = 0; // reset packet state for "clean" initial entry (this is only required if user hot-disconnects)
this.packet_error = 0; // reset CRC packet error counter for next session
@ -82,7 +84,7 @@ var MSP = {
}
};
MSP.read = function(readInfo) {
MSP.read = function (readInfo) {
var data = new Uint8Array(readInfo.data);
for (var i = 0; i < data.length; i++) {
@ -101,9 +103,9 @@ MSP.read = function(readInfo) {
break;
case 2: // direction (should be >)
if (data[i] == 62) { // >
message_status = 1;
} else { // unknown
message_status = 0;
this.message_direction = 1;
} else { // <
this.message_direction = 0;
}
this.state++;
@ -158,6 +160,7 @@ MSP.read = function(readInfo) {
};
MSP.process_data = function(code, message_buffer, message_length) {
'use strict';
var data = new DataView(message_buffer, 0); // DataView (allowing us to view arrayBuffer as struct/union)
switch (code) {

View File

@ -1,3 +1,5 @@
'use strict';
function port_handler() {
this.main_timeout_reference;
this.initial_ports = false;

View File

@ -1,3 +1,5 @@
'use strict';
var PortUsage = {
previous_received: 0,
previous_sent: 0,

View File

@ -1,18 +1,20 @@
'use strict';
function request_delay_balancer(refresh_period) {
this.balance_to = refresh_period;
this.request_t = 0;
this.finished_t = 0;
}
request_delay_balancer.prototype.requested = function() {
request_delay_balancer.prototype.requested = function () {
this.request_t = millitime();
};
request_delay_balancer.prototype.finished = function() {
request_delay_balancer.prototype.finished = function () {
this.finished_t = millitime();
};
request_delay_balancer.prototype.estimate = function() {
request_delay_balancer.prototype.estimate = function () {
var estimate = this.balance_to - (this.finished_t - this.request_t);
return (estimate > 0) ? estimate : 0;
};

View File

@ -1,3 +1,5 @@
'use strict';
var serial = {
connectionId: -1,
bitrate: 0,
@ -25,30 +27,30 @@ var serial = {
console.error(info);
googleAnalytics.sendException('Serial: ' + info.error, false);
function get_status() {
self.getInfo(crunch_status);
}
function crunch_status(info) {
if (!info.paused) {
console.log('SERIAL: Connection recovered from last onReceiveError');
googleAnalytics.sendException('Serial: onReceiveError - recovered', false);
} else {
console.log('SERIAL: Connection did not recover from last onReceiveError, disconnecting');
GUI.log('Unrecoverable <span style="color: red">failure</span> of serial connection, disconnecting...');
googleAnalytics.sendException('Serial: onReceiveError - unrecoverable', false);
if (GUI.connected_to || GUI.connecting_to) {
$('a.connect').click();
} else {
self.disconnect();
}
}
}
switch (info.error) {
case 'system_error': // we might be able to recover from this one
chrome.serial.setPaused(self.connectionId, false, get_status);
function get_status() {
self.getInfo(crunch_status);
}
function crunch_status(info) {
if (!info.paused) {
console.log('SERIAL: Connection recovered from last onReceiveError');
googleAnalytics.sendException('Serial: onReceiveError - recovered', false);
} else {
console.log('SERIAL: Connection did not recover from last onReceiveError, disconnecting');
GUI.log('Unrecoverable <span style="color: red">failure</span> of serial connection, disconnecting...');
googleAnalytics.sendException('Serial: onReceiveError - unrecoverable', false);
if (GUI.connected_to || GUI.connecting_to) {
$('a.connect').click();
} else {
self.disconnect();
}
}
}
break;
case 'timeout':
// TODO
@ -125,33 +127,33 @@ var serial = {
var self = this;
self.output_buffer.push({'data': data, 'callback': callback});
function sending() {
// store inside separate variables in case array gets destroyed
var data = self.output_buffer[0].data;
var callback = self.output_buffer[0].callback;
chrome.serial.send(self.connectionId, data, function(sendInfo) {
callback(sendInfo);
self.output_buffer.shift();
self.bytes_sent += sendInfo.bytesSent;
if (self.output_buffer.length) {
// keep the buffer withing reasonable limits
while (self.output_buffer.length > 500) {
self.output_buffer.pop();
}
sending();
} else {
self.transmitting = false;
}
});
};
if (!self.transmitting) {
self.transmitting = true;
function sending() {
// store inside separate variables in case array gets destroyed
var data = self.output_buffer[0].data;
var callback = self.output_buffer[0].callback;
chrome.serial.send(self.connectionId, data, function(sendInfo) {
callback(sendInfo);
self.output_buffer.shift();
self.bytes_sent += sendInfo.bytesSent;
if (self.output_buffer.length) {
// keep the buffer withing reasonable limits
while (self.output_buffer.length > 500) {
self.output_buffer.pop();
}
sending();
} else {
self.transmitting = false;
}
});
};
sending();
}
},

View File

@ -1,3 +1,5 @@
'use strict';
var configuration_received = false;
$(document).ready(function() {
@ -44,7 +46,7 @@ $(document).ready(function() {
$(this).text(chrome.i18n.getMessage('connect'));
$(this).removeClass('active');
sensor_status(sensors_detected = 0); // reset active sensor indicators
sensor_status(0); // reset active sensor indicators
$('#tabs > ul li').removeClass('active'); // de-select any selected tabs
// detach listeners and remove element data

View File

@ -1,10 +1,12 @@
'use strict';
var usbDevices = {
STM32DFU: {'vendorId': 1155, 'productId': 57105}
};
var usbPermissions = {permissions: [{'usbDevices': [usbDevices.STM32DFU]}]};
function check_usb_permissions(callback) {
chrome.permissions.contains(usbPermissions, function(result) {
chrome.permissions.contains(usbPermissions, function (result) {
if (result) {
GUI.optional_usb_permissions = true;
} else {
@ -15,8 +17,8 @@ function check_usb_permissions(callback) {
$('div.optional_permissions').show();
// UI hooks
document.getElementById("requestOptionalPermissions").addEventListener('click', function() {
chrome.permissions.request(usbPermissions, function(result) {
document.getElementById("requestOptionalPermissions").addEventListener('click', function () {
chrome.permissions.request(usbPermissions, function (result) {
if (result) {
GUI.log(chrome.i18n.getMessage('usb_permissions_granted'));
$('div.optional_permissions').hide();
@ -27,6 +29,8 @@ function check_usb_permissions(callback) {
});
}
if (callback) callback();
if (callback) {
callback();
}
});
}

93
main.js
View File

@ -1,6 +1,9 @@
'use strict';
// Get access to the background window object
// This object is used to pass variables between active page and background page
chrome.runtime.getBackgroundPage(function(result) {
var backgroundPage;
chrome.runtime.getBackgroundPage(function (result) {
backgroundPage = result;
backgroundPage.app_window = window;
});
@ -9,10 +12,11 @@ chrome.runtime.getBackgroundPage(function(result) {
var googleAnalyticsService = analytics.getService('ice_cream_app');
var googleAnalytics = googleAnalyticsService.getTracker(atob("VUEtNTI4MjA5MjAtMQ=="));
var googleAnalyticsConfig = false;
googleAnalyticsService.getConfig().addCallback(function(config) {
googleAnalyticsService.getConfig().addCallback(function (config) {
googleAnalyticsConfig = config;
});
$(document).ready(function() {
$(document).ready(function () {
googleAnalytics.sendAppView('Application Started');
// translate to user-selected language
@ -20,7 +24,7 @@ $(document).ready(function() {
// alternative - window.navigator.appVersion.match(/Chrome\/([0-9.]*)/)[1];
GUI.log('Running - OS: <strong>' + GUI.operating_system + '</strong>, ' +
'Chrome: <strong>' + window.navigator.appVersion.replace(/.*Chrome\/([0-9.]*).*/,"$1") + '</strong>, ' +
'Chrome: <strong>' + window.navigator.appVersion.replace(/.*Chrome\/([0-9.]*).*/, "$1") + '</strong>, ' +
'Configurator: <strong>' + chrome.runtime.getManifest().version + '</strong>');
// notification messages for various operating systems
@ -40,11 +44,10 @@ $(document).ready(function() {
// Tabs
var ui_tabs = $('#tabs > ul');
$('a', ui_tabs).click(function() {
$('a', ui_tabs).click(function () {
if ($(this).parent().hasClass('active') == false && !GUI.tab_switch_in_progress) { // only initialize when the tab isn't already active
var self = this;
var index = $(self).parent().index();
var tab = $(self).parent().prop('class');
var self = this,
tab = $(self).parent().prop('class');
// if there is no active connection, return
if (!configuration_received && tab != 'tab_logging') {
@ -54,7 +57,7 @@ $(document).ready(function() {
GUI.tab_switch_in_progress = true;
GUI.tab_switch_cleanup(function() {
GUI.tab_switch_cleanup(function () {
// disable previously active tab highlight
$('li', ui_tabs).removeClass('active');
@ -68,6 +71,10 @@ $(document).ready(function() {
// display loading screen
$('#cache .data-loading').clone().appendTo(content);
function content_ready() {
GUI.tab_switch_in_progress = false;
}
switch (tab) {
case 'tab_initial_setup':
tabs.initial_setup.initialize(content_ready);
@ -100,10 +107,6 @@ $(document).ready(function() {
tabs.logging.initialize(content_ready);
break;
}
function content_ready() {
GUI.tab_switch_in_progress = false;
}
});
}
});
@ -111,27 +114,27 @@ $(document).ready(function() {
tabs.default.initialize();
// options
$('a#options').click(function() {
$('a#options').click(function () {
var el = $(this);
if (!el.hasClass('active')) {
el.addClass('active');
el.after('<div id="options-window"></div>');
$('div#options-window').load('./tabs/options.html', function() {
$('div#options-window').load('./tabs/options.html', function () {
googleAnalytics.sendAppView('Options');
// translate to user-selected language
localize();
// if notifications are enabled, or wasn't set, check the notifications checkbox
chrome.storage.local.get('update_notify', function(result) {
if (typeof result.update_notify === 'undefined' || result.update_notify) {
chrome.storage.local.get('update_notify', function (result) {
if (result.update_notify === 'undefined' || result.update_notify) {
$('div.notifications input').prop('checked', true);
}
});
$('div.notifications input').change(function() {
$('div.notifications input').change(function () {
var check = $(this).is(':checked');
chrome.storage.local.set({'update_notify': check});
@ -142,7 +145,7 @@ $(document).ready(function() {
$('div.statistics input').prop('checked', true);
}
$('div.statistics input').change(function() {
$('div.statistics input').change(function () {
var result = $(this).is(':checked');
googleAnalyticsConfig.setTrackingPermitted(result);
});
@ -151,7 +154,7 @@ $(document).ready(function() {
if (e.type == 'click' && !$.contains($('div#options-window')[0], e.target) || e.type == 'keyup' && e.keyCode == 27) {
$(document).unbind('click keyup', close_and_cleanup);
$('div#options-window').slideUp(function() {
$('div#options-window').slideUp(function () {
el.removeClass('active');
$(this).empty().remove();
});
@ -166,16 +169,16 @@ $(document).ready(function() {
});
// listen to all input change events and adjust the value within limits if necessary
$("#content").on('focus', 'input[type="number"]', function() {
var element = $(this);
var val = element.val();
$("#content").on('focus', 'input[type="number"]', function () {
var element = $(this),
val = element.val();
if (!isNaN(val)) {
element.data('previousValue', parseFloat(val));
}
});
$("#content").on('keydown', 'input[type="number"]', function(e) {
$("#content").on('keydown', 'input[type="number"]', function (e) {
// whitelist all that we need for numeric control
var whitelist = [
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, // numpad and standard number keypad
@ -185,24 +188,31 @@ $(document).ready(function() {
37, 38, 39, 40, 13 // arrows and enter
];
if (whitelist.indexOf(e.keyCode) == -1) e.preventDefault();
if (whitelist.indexOf(e.keyCode) == -1) {
e.preventDefault();
}
});
$("#content").on('change', 'input[type="number"]', function() {
var element = $(this);
var min = parseFloat(element.prop('min'));
var max = parseFloat(element.prop('max'));
var step = parseFloat(element.prop('step'));
var val = parseFloat(element.val());
$("#content").on('change', 'input[type="number"]', function () {
var element = $(this),
min = parseFloat(element.prop('min')),
max = parseFloat(element.prop('max')),
step = parseFloat(element.prop('step')),
val = parseFloat(element.val()),
decimal_places;
// only adjust minimal end if bound is set
if (element.prop('min')) {
if (val < min) element.val(min);
if (val < min) {
element.val(min);
}
}
// only adjust maximal end if bound is set
if (element.prop('max')) {
if (val > max) element.val(max);
if (val > max) {
element.val(max);
}
}
// if entered value is illegal use previous value instead
@ -219,7 +229,7 @@ $(document).ready(function() {
// if step is set and is float and value is int, convert to float, keep decimal places in float according to step *experimental*
if (!isNaN(step) && step % 1 !== 0) {
var decimal_places = String(step).split('.')[1].length;
decimal_places = String(step).split('.')[1].length;
if (val % 1 === 0) {
element.val(val.toFixed(decimal_places));
@ -243,10 +253,17 @@ function millitime() {
}
function bytesToSize(bytes) {
if (bytes < 1024) return bytes + ' Bytes';
else if (bytes < 1048576) return(bytes / 1024).toFixed(3) + ' KB';
else if (bytes < 1073741824) return(bytes / 1048576).toFixed(3) + ' MB';
else return (bytes / 1073741824).toFixed(3) + ' GB';
if (bytes < 1024) {
bytes = bytes + ' Bytes';
} else if (bytes < 1048576) {
bytes = (bytes / 1024).toFixed(3) + ' KB';
} else if (bytes < 1073741824) {
bytes = (bytes / 1048576).toFixed(3) + ' MB';
} else {
bytes = (bytes / 1073741824).toFixed(3) + ' GB';
}
return bytes;
}
/*