From 4c277d42040418d17dcc45e2fbed060ea5326eb5 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 4 Dec 2016 23:21:06 +0800 Subject: [PATCH] aria2 settings support human readable size --- src/scripts/directives/setting.js | 32 +++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/scripts/directives/setting.js b/src/scripts/directives/setting.js index 12bfe0b..5083379 100644 --- a/src/scripts/directives/setting.js +++ b/src/scripts/directives/setting.js @@ -52,6 +52,34 @@ }, options.errorTooltipDelay); }; + var getHumanReadableSize = function (size) { + if (!size || parseInt(size).toString() != size) { + return size; + } + + var sizeUnits = ['', 'K', 'M', 'G']; + var unitIndex = 0; + + for (var i = 0; i < sizeUnits.length; i++) { + if ((size < 1024) || (size % 1024 != 0)) { + break; + } + + size = size / 1024; + unitIndex++; + } + + return size + sizeUnits[unitIndex]; + }; + + var getHumanReadableValue = function (value) { + if (scope.option.suffix === 'Bytes') { + return getHumanReadableSize(value); + } + + return value; + }; + scope.optionStatus = (function () { var value = 'ready'; @@ -205,7 +233,7 @@ scope.$watch(function () { return ngModel.$viewValue; }, function (value) { - scope.optionValue = value; + scope.optionValue = getHumanReadableValue(value); }); } @@ -227,7 +255,7 @@ } } - scope.placeholder = displayValue; + scope.placeholder = getHumanReadableValue(displayValue); }); } };