fixed async issues, added string formatter proto

10.3.x-maintenance
Paul Rogalinski 2014-12-19 03:58:42 +01:00
parent 6051876f23
commit a8a6b8904c
2 changed files with 57 additions and 41 deletions

16
main.js
View File

@ -307,4 +307,20 @@ function bytesToSize(bytes) {
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
/**
* String formatting now supports currying (partial application).
* For a format string with N replacement indices, you can call .format
* with M <= N arguments. The result is going to be a format string
* with N-M replacement indices, properly counting from 0 .. N-M.
* The following Example should explane the usage of partial aplied format:
* "{0}:{1}:{2}".format("a","b","c") === "{0}:{1}:{2}".format("a","b").format("c")
* "{0}:{1}:{2}".format("a").format("b").format("c") === "{0}:{1}:{2}".format("a").format("b", "c")
**/
String.prototype.format = function () {
var args = arguments;
return this.replace(/\{(\d+)\}/g, function (t, i) {
return args[i] !== void 0 ? args[i] : "{"+(i-args.length)+"}";
});
};

View File

@ -29,53 +29,53 @@ TABS.firmware_flasher.initialize = function (callback) {
worker.postMessage(str);
}
// Fetch Releases
var processAssets = function(assets, release, releaseIndex) {
console.log('in callback');
console.log(release); // FIXME this is broken, release is undefined
for (var assetIndex = 0; assetIndex < assets.length; assetIndex++) {
var asset = assets[assetIndex];
var targetFromFilenameExpression = /.*_(.*)\.(.*)/;
var match = targetFromFilenameExpression.exec(asset.name);
var target = match[1];
var format = match[2];
if (format != 'hex') {
continue;
}
var summary = {
"name" : release.name,
"url" : asset.browser_download_url,
"target" : target,
"date" : release.published_at,
"message" : release.body
};
var element = $('<option value="' + releaseIndex + '_' + assetIndex + '">' + summary.name + ' ' + summary.target + ' ' + summary.date + '</option>').data('obj', summary);
releases_e.append(element);
}
};
var processReleases = function(releases) {
var processReleases = function (releases){
var releases_e = $('select[name="release"]').empty();
for (var releaseIndex = 0; releaseIndex < releases.length; releaseIndex++) {
console.log('processing assets release ' + releaseIndex);
console.log(releases[releaseIndex]);
for(var releaseIndex = 0; releaseIndex < releases.length; releaseIndex++){
$.get(releases[releaseIndex].assets_url).done(
(function (releases, releaseIndex, releases_e, assets){
var release = releases[releaseIndex];
for (var assetIndex = 0; assetIndex < assets.length; assetIndex++) {
$.get(releases[releaseIndex].assets_url).done(function (assets) {
processAssets(assets, releases[releaseIndex], releaseIndex);
});
var asset = assets[assetIndex];
var targetFromFilenameExpression = /.*_(.*)\.(.*)/;
var match = targetFromFilenameExpression.exec(asset.name);
var target = match[1];
var format = match[2];
if (format != 'hex') {
continue;
}
var summary = {
"name" : release.name,
"url" : asset.browser_download_url,
"target" : target,
"date" : release.published_at,
"message" : release.body
};
var date = new Date(summary.date);
var formattedDate = "{0}-{1}-{2} {3}:{4}".format(date.getFullYear(),date.getMonth(),date.getDay(),
date.getHours(),date.getMinutes());
releases_e.append(
$("<option value='{0}_{1}'>{2} {3} {4}</option>".
format(releaseIndex,assetIndex,summary.name,summary.target,formattedDate) )
.data('obj', summary));
}
}).bind(this, releases, releaseIndex, releases_e)
);
}
};
$.get('https://api.github.com/repos/cleanflight/cleanflight/releases', function (releases) {
$.get('https://api.github.com/repos/cleanflight/cleanflight/releases', function (releases){
processReleases(releases);
}).fail(function () {
}).fail(function (data){
if (data["responseJSON"]){
GUI.log("<b>GITHUB Query Failed: <code>{0}</code></b>".format(data["responseJSON"].message));
}
$('select[name="release"]').empty().append('<option value="0">Offline</option>');
});