mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-03-19 12:00:01 +01:00
Adds optional binary prefixes.
This commit is contained in:
parent
3cd11139a2
commit
27c598d96e
@ -38,10 +38,13 @@ var H5AI_CONFIG = {
|
||||
so that it will be persistent.
|
||||
|
||||
Set parent folder labels to real folder names.
|
||||
Binary prefix set to true uses 1024B=1KiB when formatting
|
||||
file sizes (see http://en.wikipedia.org/wiki/Binary_prefix).
|
||||
*/
|
||||
"view": {
|
||||
"modes": ["details", "icons"],
|
||||
"setParentFolderLabels": true
|
||||
"setParentFolderLabels": true,
|
||||
"binaryPrefix": false
|
||||
},
|
||||
|
||||
|
||||
|
@ -2,13 +2,21 @@
|
||||
modulejs.define('core/format', ['_', 'moment'], function (_, moment) {
|
||||
|
||||
var reParseSize = /^\s*([\.\d]+)\s*([kmgt]?)b?\s*$/i,
|
||||
treshhold = 1000.0,
|
||||
kilo = 1000.0,
|
||||
sizeUnits = ['B', 'KB', 'MB', 'GB', 'TB'],
|
||||
decimalMetric = {
|
||||
t: 1000.0,
|
||||
k: 1000.0,
|
||||
u: ['B', 'KB', 'MB', 'GB', 'TB']
|
||||
},
|
||||
binaryMetric = {
|
||||
t: 1024.0,
|
||||
k: 1024.0,
|
||||
u: ['B', 'KiB', 'MiB', 'GiB', 'TiB']
|
||||
},
|
||||
|
||||
parseSize = function (str) {
|
||||
|
||||
var match = reParseSize.exec(str),
|
||||
kilo = decimalMetric.k,
|
||||
val, unit;
|
||||
|
||||
if (!match) {
|
||||
@ -29,20 +37,35 @@ modulejs.define('core/format', ['_', 'moment'], function (_, moment) {
|
||||
return val;
|
||||
},
|
||||
|
||||
formatSize = function (size) {
|
||||
defaultMetric = decimalMetric,
|
||||
|
||||
setDefaultMetric = function (metric) {
|
||||
|
||||
if (!metric) {
|
||||
defaultMetric = decimalMetric;
|
||||
} else if (metric === true) {
|
||||
defaultMetric = binaryMetric;
|
||||
} else {
|
||||
defaultMetric = metric;
|
||||
}
|
||||
},
|
||||
|
||||
formatSize = function (size, metric) {
|
||||
|
||||
metric = metric || defaultMetric;
|
||||
|
||||
if (!_.isNumber(size) || size < 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
var i = 0,
|
||||
maxI = sizeUnits.length - 1;
|
||||
maxI = metric.u.length - 1;
|
||||
|
||||
while (size >= treshhold && i < maxI) {
|
||||
size /= kilo;
|
||||
while (size >= metric.t && i < maxI) {
|
||||
size /= metric.k;
|
||||
i += 1;
|
||||
}
|
||||
return (i <= 1 ? Math.round(size) : size.toFixed(1)).toString() + ' ' + sizeUnits[i];
|
||||
return (i <= 1 ? Math.round(size) : size.toFixed(1)).toString() + ' ' + metric.u[i];
|
||||
},
|
||||
|
||||
defaultDateFormat = 'YYYY-MM-DD HH:mm',
|
||||
@ -72,6 +95,7 @@ modulejs.define('core/format', ['_', 'moment'], function (_, moment) {
|
||||
|
||||
return {
|
||||
parseSize: parseSize,
|
||||
setDefaultMetric: setDefaultMetric,
|
||||
formatSize: formatSize,
|
||||
setDefaultDateFormat: setDefaultDateFormat,
|
||||
parseDate: parseDate,
|
||||
|
@ -3,7 +3,8 @@ modulejs.define('view/extended', ['_', '$', 'core/settings', 'core/resource', 'c
|
||||
|
||||
var defaults = {
|
||||
modes: ['details', 'icons'],
|
||||
setParentFolderLabels: false
|
||||
setParentFolderLabels: false,
|
||||
binaryPrefix: false
|
||||
},
|
||||
|
||||
settings = _.extend({}, defaults, allsettings.view),
|
||||
@ -103,6 +104,8 @@ modulejs.define('view/extended', ['_', '$', 'core/settings', 'core/resource', 'c
|
||||
$ul = $(listTemplate),
|
||||
$emtpy = $(emptyTemplate);
|
||||
|
||||
format.setDefaultMetric(settings.binaryUnits);
|
||||
|
||||
if (entry.parent) {
|
||||
$ul.append(update(entry.parent));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user