var Units = {
units: {
size: [
- {short: '', long: 'byte', value: 1},
- {short: 'K', long: 'kilobyte', value: 1000},
- {short: 'M', long: 'megabyte', value: 1000},
- {short: 'G', long: 'gigabyte', value: 1000},
- {short: 'T', long: 'terabyte', value: 1000},
- {short: 'P', long: 'petabyte', value: 1000}
+ {short: '', long: 'B', value: 1},
+ {short: 'kb', long: 'KB', value: 1000},
+ {short: 'k', long: 'KiB', value: 1024},
+ {short: 'mb', long: 'MB', value: 1000},
+ {short: 'm', long: 'MiB', value: 1024},
+ {short: 'gb', long: 'GB', value: 1000},
+ {short: 'g', long: 'GiB', value: 1024},
+ {short: 'tb', long: 'TB', value: 1000},
+ {short: 't', long: 'TiB', value: 1024},
+ {short: 'pb', long: 'PB', value: 1000},
+ {short: 'p', long: 'PiB', value: 1024}
],
speed: [
{short: '', long: 'B/s', value: 1},
},
get_decimal_size: function(size) {
var dec_size;
- var size_unit = 'B';
var units = [];
for (var u in Units.units.size) {
- units.push(Units.units.size[u].short);
+ if ([1, 1000].indexOf(Units.units.size[u].value) !== -1) {
+ units.push(Units.units.size[u].long);
+ }
}
- units.shift(); // remove "byte" unit
if (size === null) {
size = 0;
}
- var size_pattern = new RegExp('^[\\d\\.]+(' + units.join('|') + ')?' + size_unit + '$');
-
+ var size_pattern = new RegExp('^[\\d\\.]+(' + units.join('|') + ')$');
if (size_pattern.test(size.toString())) {
// size is already formatted
dec_size = size;
} else {
+ units.shift(); // remove byte unit
size = parseInt(size, 10);
var unit;
- dec_size = size.toString() + ((size > 0 ) ? size_unit : '');
+ dec_size = size.toString();
while(size >= 1000) {
size /= 1000;
- unit = units.shift(units);
+ unit = units.shift();
}
if (unit) {
dec_size = (Math.floor(size * 10) / 10).toFixed(1);
- dec_size += unit + size_unit;
+ dec_size += unit;
+ } else if (size > 0) {
+ dec_size += 'B';
}
}
return dec_size;
class DirectiveSize extends DirectiveTemplate {
const SIZE_FORMAT = 'SizeFormat';
- const DEFAULT_SIZE_FORMAT = 'byte';
+ const DEFAULT_SIZE_FORMAT = '';
private $size_formats = array(
- array('format' => 'byte', 'value' => 1, 'label' => 'Bytes'),
- array('format' => 'kilobyte', 'value' => 1000, 'label' => 'Kilobytes'),
- array('format' => 'megabyte', 'value' => 1000, 'label' => 'Megabytes'),
- array('format' => 'gigabyte', 'value' => 1000, 'label' => 'Gigabytes'),
- array('format' => 'terabyte', 'value' => 1000, 'label' => 'Terabytes')
+ array('format' => '', 'value' => 1, 'label' => 'B'),
+ array('format' => 'kb', 'value' => 1000, 'label' => 'KB'),
+ array('format' => 'k', 'value' => 1024, 'label' => 'KiB'),
+ array('format' => 'mb', 'value' => 1000000, 'label' => 'MB'),
+ array('format' => 'm', 'value' => 1048576, 'label' => 'MiB'),
+ array('format' => 'gb', 'value' => 1000000000, 'label' => 'GB'),
+ array('format' => 'g', 'value' => 1073741824, 'label' => 'GiB'),
+ array('format' => 'tb', 'value' => 1000000000000, 'label' => 'TB'),
+ array('format' => 't', 'value' => 1099511627776, 'label' => 'TiB')
);
public function getValue() {
public function getSizeFormats() {
$size_formats = array();
for ($i = 0; $i < count($this->size_formats); $i++) {
- $format = array(
- 'label' => Prado::localize($this->size_formats[$i]['label']),
- 'format' => $this->size_formats[$i]['format']
- );
- array_push($size_formats, $format);
+ $size_formats[$this->size_formats[$i]['format']] = $this->size_formats[$i]['label'];
}
return $size_formats;
}
}
$formatted_value = $this->formatSize($directive_value, $size_format);
$this->Directive->Text = $formatted_value['value'];
- $this->SizeFormat->DataSource = $this->size_formats;
- $this->SizeFormat->SelectedValue = $formatted_value['format'];
+ $this->SizeFormat->DataSource = $this->getSizeFormats();
$this->SizeFormat->dataBind();
+ $this->SizeFormat->SelectedValue = $formatted_value['format'];
$this->Label->Text = $this->getLabel();
$validate = $this->getRequired();
$this->DirectiveValidator->setVisible($validate);
* then there will be returned value converted by using as close format as possible.
* Example:
* size_value: 121000
- * given format: byte
+ * given format: b
* returned value: 121
- * returned format: kilobyte
+ * returned format: kb
*/
private function formatSize($size_bytes, $format) {
$value = $size_bytes;
- for ($i = 0; $i < count($this->size_formats); $i++) {
- if ($this->size_formats[$i]['format'] != $format) {
- $remainder = $value % $this->size_formats[$i]['value'];
- if ($remainder === 0) {
- $value /= $this->size_formats[$i]['value'];
- $format = $this->size_formats[$i]['format'];
- continue;
+ if ($value > 0) {
+ for ($i = (count($this->size_formats) - 1); $i >= 0; $i--) {
+ if ($this->size_formats[$i]['format'] != $format) {
+ $remainder = $value % $this->size_formats[$i]['value'];
+ if ($remainder == 0) {
+ $value /= $this->size_formats[$i]['value'];
+ $format = $this->size_formats[$i]['format'];
+ break;
+ }
}
- break;
}
- break;
}
- $result = array('value' => $value, 'format' => $format);
- return $result;
+ return array('value' => $value, 'format' => $format);
}
private function getValueBytes($value, $size_format) {
for ($i = 0; $i < count($this->size_formats); $i++) {
- $value *= $this->size_formats[$i]['value'];
if ($this->size_formats[$i]['format'] === $size_format) {
+ $value *= $this->size_formats[$i]['value'];
break;
}
}