Sort downloads by completeness within category

This commit is contained in:
Sergei Morozov 2016-12-30 10:28:02 -08:00
parent 6ce17170fe
commit e101511141

View File

@ -308,48 +308,63 @@ function(
// actual downloads used by the view // actual downloads used by the view
scope.getDownloads = function() { scope.getDownloads = function() {
var downloads = []; var categories = [];
if (scope.filterActive) { if (scope.filterActive) {
if (!scope.filterSpeed) { if (!scope.filterSpeed) {
downloads = _.filter(scope.active, function (e) { categories.push(_.filter(scope.active, function (e) {
return !+e.uploadSpeed && !+e.downloadSpeed; return !+e.uploadSpeed && !+e.downloadSpeed;
}); }));
} }
else { else {
downloads = scope.active; categories.push(scope.active);
} }
} }
else if (scope.filterSpeed) { else if (scope.filterSpeed) {
downloads = _.filter(scope.active, function (e) { categories.push(_.filter(scope.active, function (e) {
return +e.uploadSpeed || +e.downloadSpeed; return +e.uploadSpeed || +e.downloadSpeed;
}); }));
} }
if (scope.filterWaiting) { if (scope.filterWaiting) {
downloads = downloads.concat(_.filter(scope.waiting, function (e) { categories.push(_.filter(scope.waiting, function (e) {
return e.status == "waiting"; return e.status == "waiting";
})); }));
} }
if (scope.filterPaused) { if (scope.filterPaused) {
downloads = downloads.concat(_.filter(scope.waiting, function (e) { categories.push(_.filter(scope.waiting, function (e) {
return e.status == "paused"; return e.status == "paused";
})); }));
} }
if (scope.filterError) { if (scope.filterError) {
downloads = downloads.concat(_.filter(scope.stopped, function (e) { categories.push(_.filter(scope.stopped, function (e) {
return e.status == "error"; return e.status == "error";
})); }));
} }
if (scope.filterComplete) { if (scope.filterComplete) {
downloads = downloads.concat(_.filter(scope.stopped, function (e) { categories.push(_.filter(scope.stopped, function (e) {
return e.status == "complete"; return e.status == "complete";
})); }));
} }
if (scope.filterRemoved) { if (scope.filterRemoved) {
downloads = downloads.concat(_.filter(scope.stopped, function (e) { categories.push(_.filter(scope.stopped, function (e) {
return e.status == "removed"; return e.status == "removed";
})); }));
} }
var downloads = categories.map(function (category) {
// sort downloads within category by completness, most completed first
return _.sortBy(category, function (e) {
return -(e.completedLength / e.totalLength);
});
}).reduce(function (downloads, category) {
return downloads.concat(category);
}, []);
downloads = scope.filterDownloads(downloads); downloads = scope.filterDownloads(downloads);
scope.totalDownloads = downloads.length; scope.totalDownloads = downloads.length;