webui-aria2/js/filters/bytes.js

41 lines
1.3 KiB
JavaScript
Raw Normal View History

angular
.module('webui.filters.bytes', [])
.filter('changeLength', function() {
2013-01-22 08:53:30 +01:00
return function(len, pref) {
len = parseFloat(len);
2013-03-16 09:13:06 +01:00
if (len <= (1<<10)) return len.toFixed(1) + " " + pref;
2013-01-22 08:53:30 +01:00
else if(len <= (1<<20)) return (len/(1<<10)).toFixed(1) + " K" + pref;
else if(len <= (1<<30)) return (len/(1<<20)).toFixed(1) + " M" + pref;
else return (len/(1<<30)).toFixed(1) + " G" + pref;
};
})
.filter('blength', ['$filter', function(filter) {
2013-01-22 08:53:30 +01:00
return function(len) {
return filter('changeLength')(len, 'B');
};
}])
.filter('bspeed', ['$filter', function(filter) {
2013-01-22 08:53:30 +01:00
return function(speed) {
return filter('changeLength')(speed, 'B/s');
};
}])
.filter('time', function() {
2013-07-29 17:20:08 +02:00
function pad(f) {
return ("0" + f).substr(-2);
}
2013-01-22 08:53:30 +01:00
return function(time) {
2013-07-29 17:20:08 +02:00
time = parseInt(time, 10);
if (!time || !isFinite(time)) return "∞";
var secs = time % 60;
if (time < 60) return secs + "s";
var mins = Math.floor((time % 3600) / 60)
if (time < 3600) return pad(mins) + ":" + pad(secs);
var hrs = Math.floor((time % 86400) / 3600);
if (time < 86400) return pad(hrs) + ":" + pad(mins) + ":" + pad(secs);
var days = Math.floor(time / 86400);
return days + "::" + pad(hrs) + ":" + pad(mins) + ":" + pad(secs);
2013-01-22 08:53:30 +01:00
};
});