Merge pull request #374 from morozov/sort-by-completeness

Sort downloads by completeness within category
This commit is contained in:
hamza zia 2017-08-24 17:57:20 +05:00 committed by GitHub
commit d234359a46

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;