From: Marcin Haba Date: Sun, 23 Jun 2019 10:52:41 +0000 (+0200) Subject: baculum: Fix showing unit for size and time period directive types X-Git-Tag: Release-9.6.0~205 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=774bd17a3ae702bbb2a98d182d3370fcf62e9545;p=thirdparty%2Fbacula.git baculum: Fix showing unit for size and time period directive types --- diff --git a/gui/baculum/protected/Web/JavaScript/misc.js b/gui/baculum/protected/Web/JavaScript/misc.js index e4722c4cf5..c7777b54a2 100644 --- a/gui/baculum/protected/Web/JavaScript/misc.js +++ b/gui/baculum/protected/Web/JavaScript/misc.js @@ -1,12 +1,17 @@ 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}, @@ -24,32 +29,34 @@ var Units = { }, 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; diff --git a/gui/baculum/protected/Web/Portlets/DirectiveSize.php b/gui/baculum/protected/Web/Portlets/DirectiveSize.php index 00edfa24d5..be1ced0383 100644 --- a/gui/baculum/protected/Web/Portlets/DirectiveSize.php +++ b/gui/baculum/protected/Web/Portlets/DirectiveSize.php @@ -28,14 +28,18 @@ Prado::using('Application.Web.Portlets.DirectiveTemplate'); 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() { @@ -61,11 +65,7 @@ class DirectiveSize extends DirectiveTemplate { 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; } @@ -83,9 +83,9 @@ class DirectiveSize extends DirectiveTemplate { } $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); @@ -98,32 +98,31 @@ class DirectiveSize extends DirectiveTemplate { * 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; } } diff --git a/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl b/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl index 02d3947b0e..d481c68fff 100644 --- a/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl +++ b/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl @@ -17,7 +17,7 @@ AutoPostBack="false" OnSelectedIndexChanged="saveValue" /> <%=$this->getRequired() ? ' ' : ''%> - + formatTimePeriod($directive_value, $time_format); $this->Directive->Text = $formatted_value['value']; $this->TimeFormat->DataSource = $this->getTimeFormats(); - $this->TimeFormat->SelectedValue = $formatted_value['format']; $this->TimeFormat->dataBind(); + $this->TimeFormat->SelectedValue = $formatted_value['format']; $this->Label->Text = $this->getLabel(); $validate = $this->getRequired(); $this->DirectiveValidator->setVisible($validate);