2014-08-09 17:38:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-12 13:51:31 +00:00
|
|
|
var TABS = {}; // filled by individual tab js file
|
2014-07-10 16:13:38 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
var GUI_control = function () {
|
2013-12-29 10:15:04 +00:00
|
|
|
this.auto_connect = false;
|
2013-11-20 11:26:00 +00:00
|
|
|
this.connecting_to = false;
|
|
|
|
this.connected_to = false;
|
2014-10-19 13:55:56 +00:00
|
|
|
this.connect_lock = false;
|
2013-11-09 04:57:05 +00:00
|
|
|
this.active_tab;
|
2014-07-09 14:10:02 +00:00
|
|
|
this.tab_switch_in_progress = false;
|
2013-11-15 16:13:36 +00:00
|
|
|
this.operating_system;
|
2014-06-08 17:32:24 +00:00
|
|
|
this.optional_usb_permissions = false; // controlled by usb permissions code
|
2013-11-09 04:57:05 +00:00
|
|
|
this.interval_array = [];
|
|
|
|
this.timeout_array = [];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-15 16:13:36 +00:00
|
|
|
// 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";
|
2014-04-11 19:22:05 +00:00
|
|
|
else this.operating_system = "Unknown";
|
2013-11-09 04:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.interval_add = function (name, code, interval, first) {
|
2014-08-14 14:41:49 +00:00
|
|
|
var data = {'name': name, 'timer': null, 'code': code, 'interval': interval, 'fired': 0, 'paused': false};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
if (first == true) {
|
|
|
|
code(); // execute code
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
data.fired++; // increment counter
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
data.timer = setInterval(function() {
|
|
|
|
code(); // execute code
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
data.fired++; // increment counter
|
|
|
|
}, interval);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
this.interval_array.push(data); // push to primary interval array
|
2014-03-09 02:14:59 +00:00
|
|
|
|
|
|
|
return data;
|
2013-11-09 04:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// name = string
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.interval_remove = function (name) {
|
2013-11-09 04:57:05 +00:00
|
|
|
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
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
this.interval_array.splice(i, 1); // remove element/object from array
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-12-05 10:03:17 +00:00
|
|
|
// name = string
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.interval_pause = function (name) {
|
2013-12-05 10:03:17 +00:00
|
|
|
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;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:03:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:04:15 +00:00
|
|
|
return false;
|
2013-12-05 10:03:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// name = string
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.interval_resume = function (name) {
|
2013-12-05 10:03:17 +00:00
|
|
|
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];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:03:17 +00:00
|
|
|
obj.timer = setInterval(function() {
|
|
|
|
obj.code(); // execute code
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:03:17 +00:00
|
|
|
obj.fired++; // increment counter
|
|
|
|
}, obj.interval);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:03:17 +00:00
|
|
|
obj.paused = false;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:03:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 10:04:15 +00:00
|
|
|
return false;
|
2013-12-05 10:03:17 +00:00
|
|
|
};
|
|
|
|
|
2014-02-22 10:13:37 +00:00
|
|
|
// input = array of timers thats meant to be kept, or nothing
|
2013-11-09 04:57:05 +00:00
|
|
|
// return = returns timers killed in last call
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.interval_kill_all = function (keep_array) {
|
2014-02-22 10:19:18 +00:00
|
|
|
var self = this;
|
2013-11-09 04:57:05 +00:00
|
|
|
var timers_killed = 0;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
for (var i = (this.interval_array.length - 1); i >= 0; i--) { // reverse iteration
|
|
|
|
var keep = false;
|
2014-02-22 10:13:37 +00:00
|
|
|
if (keep_array) { // only run through the array if it exists
|
2014-08-10 04:01:44 +00:00
|
|
|
keep_array.forEach(function (name) {
|
2014-02-22 10:13:37 +00:00
|
|
|
if (self.interval_array[i].name == name) {
|
|
|
|
keep = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
if (!keep) {
|
|
|
|
clearInterval(this.interval_array[i].timer); // stop timer
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
this.interval_array.splice(i, 1); // remove element/object from array
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
timers_killed++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
return timers_killed;
|
|
|
|
};
|
|
|
|
|
|
|
|
// name = string
|
|
|
|
// code = function reference (code to be executed)
|
|
|
|
// timeout = timeout in miliseconds
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.timeout_add = function (name, code, timeout) {
|
2013-11-09 04:57:05 +00:00
|
|
|
var self = this;
|
2014-08-14 14:41:49 +00:00
|
|
|
var data = {'name': name, 'timer': null, 'timeout': timeout};
|
2014-03-09 02:14:59 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
// start timer with "cleaning" callback
|
2014-03-09 02:14:59 +00:00
|
|
|
data.timer = setTimeout(function() {
|
2013-11-09 04:57:05 +00:00
|
|
|
code(); // execute code
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-09 02:11:11 +00:00
|
|
|
// remove object from array
|
|
|
|
var index = self.timeout_array.indexOf(data);
|
|
|
|
if (index > -1) self.timeout_array.splice(index, 1);
|
2013-11-09 04:57:05 +00:00
|
|
|
}, timeout);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-09 02:14:59 +00:00
|
|
|
this.timeout_array.push(data); // push to primary timeout array
|
|
|
|
|
|
|
|
return data;
|
2013-11-09 04:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// name = string
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.timeout_remove = function (name) {
|
2013-11-09 04:57:05 +00:00
|
|
|
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
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
this.timeout_array.splice(i, 1); // remove element/object from array
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// no input paremeters
|
|
|
|
// return = returns timers killed in last call
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.timeout_kill_all = function () {
|
2013-11-09 04:57:05 +00:00
|
|
|
var timers_killed = 0;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
for (var i = 0; i < this.timeout_array.length; i++) {
|
|
|
|
clearTimeout(this.timeout_array[i].timer); // stop timer
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
timers_killed++;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
this.timeout_array = []; // drop objects
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
return timers_killed;
|
|
|
|
};
|
|
|
|
|
2014-02-03 05:19:06 +00:00
|
|
|
// message = string
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.log = function (message) {
|
2014-02-03 05:19:06 +00:00
|
|
|
var command_log = $('div#log');
|
|
|
|
var d = new Date();
|
2014-03-08 05:25:15 +00:00
|
|
|
var time = ((d.getHours() < 10) ? '0' + d.getHours(): d.getHours())
|
|
|
|
+ ':' + ((d.getMinutes() < 10) ? '0' + d.getMinutes(): d.getMinutes())
|
2014-02-03 05:19:06 +00:00
|
|
|
+ ':' + ((d.getSeconds() < 10) ? '0' + d.getSeconds(): d.getSeconds());
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-03 05:19:06 +00:00
|
|
|
$('div.wrapper', command_log).append('<p>' + time + ' -- ' + message + '</p>');
|
2014-03-08 05:25:15 +00:00
|
|
|
command_log.scrollTop($('div.wrapper', command_log).height());
|
2014-02-03 05:19:06 +00:00
|
|
|
};
|
|
|
|
|
2013-11-09 04:57:05 +00:00
|
|
|
// 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
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI_control.prototype.tab_switch_cleanup = function (callback) {
|
2014-02-28 14:44:07 +00:00
|
|
|
MSP.callbacks_cleanup(); // we don't care about any old data that might or might not arrive
|
2014-07-10 12:48:33 +00:00
|
|
|
GUI.interval_kill_all(); // all intervals (mostly data pulling) needs to be removed on tab switch
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-10-01 10:58:09 +00:00
|
|
|
TABS[this.active_tab].cleanup(callback);
|
2013-11-09 04:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// initialize object into GUI variable
|
|
|
|
var GUI = new GUI_control();
|