]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Fix showing unit for size and time period directive types
authorMarcin Haba <marcin.haba@bacula.pl>
Sun, 23 Jun 2019 10:52:41 +0000 (12:52 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Sat, 14 Dec 2019 14:54:13 +0000 (15:54 +0100)
gui/baculum/protected/Web/JavaScript/misc.js
gui/baculum/protected/Web/Portlets/DirectiveSize.php
gui/baculum/protected/Web/Portlets/DirectiveSize.tpl
gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.php

index e4722c4cf56363bce8c4babfa1d5c6bb66bfd702..c7777b54a29e760440152bfb47ce8a8077538109 100644 (file)
@@ -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;
index 00edfa24d55c3c0d1f922fb52432bcfba83a1600..be1ced03830003db239cf8c285d376053e357bc6 100644 (file)
@@ -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;
                        }
                }
index 02d3947b0ec753886a1e212546ed73a14db26dc1..d481c68fffcdf44d7db0a4efa2d3ea4933a1d5ab 100644 (file)
@@ -17,7 +17,7 @@
                        AutoPostBack="false"
                        OnSelectedIndexChanged="saveValue"
                /> <%=$this->getRequired() ? '&nbsp;<i class="fa fa-asterisk w3-text-red" style="line-height: 40px"></i>' : ''%>
-               <i class="fa fa-undo reset_btn" onclick="var fsize = Units.format_size(parseInt('<%=$this->getDefaultValue()%>', 10), 'byte'); document.getElementById('<%=$this->Directive->ClientID%>').value = fsize.value; document.getElementById('<%=$this->SizeFormat->ClientID%>').value = fsize.format;" alt="<%[ Reset to default value ]%>" title="<%[ Reset to default value ]%>"></i>
+               <i class="fa fa-undo reset_btn" onclick="var fsize = Units.format_size(parseInt('<%=$this->getDefaultValue()%>', 10), 'B'); document.getElementById('<%=$this->Directive->ClientID%>').value = fsize.value; document.getElementById('<%=$this->SizeFormat->ClientID%>').value = fsize.format;" alt="<%[ Reset to default value ]%>" title="<%[ Reset to default value ]%>"></i>
                <i class="fa fa-trash-alt remove_btn" onclick="document.getElementById('<%=$this->Directive->ClientID%>').value = '';" alt="<%[ Remove directive ]%>" title="<%[ Remove directive ]%>"></i>
                <com:TRequiredFieldValidator
                        ID="DirectiveValidator"
index ba70247777455f68dc0a9b7c9f1dcb809f9b2cc9..b0b95aa1bb5848e03abc5ed57ffbee45b87369cd 100644 (file)
@@ -83,8 +83,8 @@ class DirectiveTimePeriod extends DirectiveTemplate {
                $formatted_value = $this->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);