You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
inav-configurator/js/gui.js

382 lines
11 KiB
JavaScript

8 years ago
/*global $*/
'use strict';
var TABS = {}; // filled by individual tab js file
var GUI_control = function () {
this.auto_connect = false;
this.connecting_to = false;
this.connected_to = false;
this.connect_lock = false;
this.active_tab;
this.tab_switch_in_progress = false;
this.operating_system;
this.interval_array = [];
this.timeout_array = [];
this.defaultAllowedTabsWhenDisconnected = [
'landing',
'firmware_flasher',
'help'
];
this.defaultAllowedTabsWhenConnected = [
'failsafe',
'transponder',
'adjustments',
'auxiliary',
'cli',
'configuration',
'gps',
'led_strip',
'logging',
'onboard_logging',
'modes',
'motors',
'pid_tuning',
'ports',
'receiver',
'sensors',
'servos',
8 years ago
'setup',
'osd',
'profiles'
];
this.allowedTabs = this.defaultAllowedTabsWhenDisconnected;
// check which operating system is user running
if (navigator.appVersion.indexOf("Win") != -1) this.operating_system = "Windows";
else if (navigator.appVersion.indexOf("Mac") != -1) this.operating_system = "MacOS";
else if (navigator.appVersion.indexOf("CrOS") != -1) this.operating_system = "ChromeOS";
else if (navigator.appVersion.indexOf("Linux") != -1) this.operating_system = "Linux";
else if (navigator.appVersion.indexOf("X11") != -1) this.operating_system = "UNIX";
11 years ago
else this.operating_system = "Unknown";
};
// Timer managing methods
// name = string
// code = function reference (code to be executed)
// interval = time interval in miliseconds
// first = true/false if code should be ran initially before next timer interval hits
GUI_control.prototype.interval_add = function (name, code, interval, first) {
var data = {'name': name, 'timer': null, 'code': code, 'interval': interval, 'fired': 0, 'paused': false};
if (first == true) {
code(); // execute code
data.fired++; // increment counter
}
data.timer = setInterval(function() {
code(); // execute code
data.fired++; // increment counter
}, interval);
this.interval_array.push(data); // push to primary interval array
11 years ago
return data;
};
// name = string
GUI_control.prototype.interval_remove = function (name) {
for (var i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name == name) {
clearInterval(this.interval_array[i].timer); // stop timer
this.interval_array.splice(i, 1); // remove element/object from array
return true;
}
}
return false;
};
// name = string
GUI_control.prototype.interval_pause = function (name) {
for (var i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name == name) {
clearInterval(this.interval_array[i].timer);
this.interval_array[i].paused = true;
return true;
}
}
11 years ago
return false;
};
// name = string
GUI_control.prototype.interval_resume = function (name) {
for (var i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name == name && this.interval_array[i].paused) {
var obj = this.interval_array[i];
obj.timer = setInterval(function() {
obj.code(); // execute code
obj.fired++; // increment counter
}, obj.interval);
obj.paused = false;
return true;
}
}
11 years ago
return false;
};
// input = array of timers thats meant to be kept, or nothing
// return = returns timers killed in last call
GUI_control.prototype.interval_kill_all = function (keep_array) {
var self = this;
var timers_killed = 0;
for (var i = (this.interval_array.length - 1); i >= 0; i--) { // reverse iteration
var keep = false;
if (keep_array) { // only run through the array if it exists
keep_array.forEach(function (name) {
if (self.interval_array[i].name == name) {
keep = true;
}
});
}
if (!keep) {
clearInterval(this.interval_array[i].timer); // stop timer
this.interval_array.splice(i, 1); // remove element/object from array
timers_killed++;
}
}
return timers_killed;
};
// name = string
// code = function reference (code to be executed)
// timeout = timeout in miliseconds
GUI_control.prototype.timeout_add = function (name, code, timeout) {
var self = this;
var data = {'name': name, 'timer': null, 'timeout': timeout};
11 years ago
// start timer with "cleaning" callback
11 years ago
data.timer = setTimeout(function() {
code(); // execute code
11 years ago
// remove object from array
var index = self.timeout_array.indexOf(data);
if (index > -1) self.timeout_array.splice(index, 1);
}, timeout);
11 years ago
this.timeout_array.push(data); // push to primary timeout array
return data;
};
// name = string
GUI_control.prototype.timeout_remove = function (name) {
for (var i = 0; i < this.timeout_array.length; i++) {
if (this.timeout_array[i].name == name) {
clearTimeout(this.timeout_array[i].timer); // stop timer
this.timeout_array.splice(i, 1); // remove element/object from array
return true;
}
}
return false;
};
// no input paremeters
// return = returns timers killed in last call
GUI_control.prototype.timeout_kill_all = function () {
var timers_killed = 0;
for (var i = 0; i < this.timeout_array.length; i++) {
clearTimeout(this.timeout_array[i].timer); // stop timer
timers_killed++;
}
this.timeout_array = []; // drop objects
return timers_killed;
};
// message = string
GUI_control.prototype.log = function (message) {
var command_log = $('div#log');
var d = new Date();
10 years ago
var year = d.getFullYear();
var month = ((d.getMonth() < 9) ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1));
var date = ((d.getDate() < 10) ? '0' + d.getDate() : d.getDate());
var time = ((d.getHours() < 10) ? '0' + d.getHours(): d.getHours())
10 years ago
+ ':' + ((d.getMinutes() < 10) ? '0' + d.getMinutes(): d.getMinutes())
+ ':' + ((d.getSeconds() < 10) ? '0' + d.getSeconds(): d.getSeconds());
var formattedDate = "{0}-{1}-{2} {3}".format(
year,
month,
date,
' @ ' + time
);
$('div.wrapper', command_log).append('<p>' + formattedDate + ' -- ' + message + '</p>');
command_log.scrollTop($('div.wrapper', command_log).height());
};
// Method is called every time a valid tab change event is received
// callback = code to run when cleanup is finished
// default switch doesn't require callback to be set
GUI_control.prototype.tab_switch_cleanup = function (callback) {
MSP.callbacks_cleanup(); // we don't care about any old data that might or might not arrive
GUI.interval_kill_all(); // all intervals (mostly data pulling) needs to be removed on tab switch
if (this.active_tab) {
TABS[this.active_tab].cleanup(callback);
} else {
callback();
}
};
8 years ago
GUI_control.prototype.switchery = function() {
$('.togglesmall').each(function(index, elem) {
var switchery = new Switchery(elem, {
size: 'small',
color: '#ffbb00',
secondaryColor: '#c4c4c4'
});
$(elem).on("change", function (evt) {
switchery.setPosition();
});
$(elem).removeClass('togglesmall');
});
$('.toggle').each(function(index, elem) {
var switchery = new Switchery(elem, {
color: '#ffbb00',
secondaryColor: '#c4c4c4'
});
$(elem).on("change", function (evt) {
switchery.setPosition();
});
$(elem).removeClass('toggle');
});
$('.togglemedium').each(function(index, elem) {
var switchery = new Switchery(elem, {
className: 'switcherymid',
color: '#ffbb00',
secondaryColor: '#c4c4c4'
});
$(elem).on("change", function (evt) {
switchery.setPosition();
});
$(elem).removeClass('togglemedium');
});
};
GUI_control.prototype.content_ready = function (callback) {
$('.togglesmall').each(function(index, elem) {
var switchery = new Switchery(elem, {
size: 'small',
color: '#37a8db',
secondaryColor: '#c4c4c4'
});
$(elem).on("change", function (evt) {
switchery.setPosition();
});
$(elem).removeClass('togglesmall');
});
$('.toggle').each(function(index, elem) {
var switchery = new Switchery(elem, {
color: '#37a8db',
secondaryColor: '#c4c4c4'
});
$(elem).on("change", function (evt) {
switchery.setPosition();
});
$(elem).removeClass('toggle');
});
$('.togglemedium').each(function(index, elem) {
var switchery = new Switchery(elem, {
className: 'switcherymid',
color: '#37a8db',
secondaryColor: '#c4c4c4'
});
$(elem).on("change", function (evt) {
switchery.setPosition();
});
$(elem).removeClass('togglemedium');
});
if (CONFIGURATOR.connectionValid) {
// Build link to in-use CF version documentation
var documentationButton = $('div#content #button-documentation');
9 years ago
documentationButton.html("Documentation for INAV");
documentationButton.attr("href","https://github.com/iNavFlight/inav/wiki");
}
// loading tooltip
jQuery(document).ready(function($) {
$('cf_tip').each(function() { // Grab all ".cf_tip" elements, and for each...
log(this); // ...print out "this", which now refers to each ".cf_tip" DOM element
});
$('.cf_tip').each(function() {
$(this).jBox('Tooltip', {
delayOpen: 100,
delayClose: 100,
position: {
x: 'right',
y: 'center'
},
outside: 'x'
});
});
});
if (callback) callback();
8 years ago
};
8 years ago
GUI_control.prototype.updateStatusBar = function() {
$('span.i2c-error').text(CONFIG.i2cError);
$('span.cycle-time').text(CONFIG.cycleTime);
$('span.cpu-load').text(chrome.i18n.getMessage('statusbar_cpu_load', [CONFIG.cpuload]));
};
GUI_control.prototype.updateProfileChange = function() {
$('#profilechange').val(CONFIG.profile);
};
8 years ago
GUI_control.prototype.fillSelect = function ($element, values, currentValue, unit) {
if (unit == null) {
unit = '';
}
$element.find("*").remove();
for (var i in values) {
if (values.hasOwnProperty(i)) {
$element.append('<option value="' + i + '">' + values[i] + '</option>');
}
}
/*
* If current Value is not on the list, add a new entry
*/
if (currentValue != null && $element.find('[value="' + currentValue + '"]').length == 0) {
$element.append('<option value="' + currentValue + '">' + currentValue + unit + '</option>');
}
};
// initialize object into GUI variable
10 years ago
var GUI = new GUI_control();