]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - src/Utils.js
api-viewer: eslint fixes, code cleanups
[proxmox-widget-toolkit.git] / src / Utils.js
index b8aabda24702ce9506b14397fd73aea77881408d..bc602dea70e4b2a8b2c256c2dd2264f5d56670f3 100644 (file)
@@ -568,6 +568,7 @@ utilities: {
            Proxmox.Utils.unknownText;
     },
 
+    // NOTE: only add general, product agnostic, ones here! Else use override helper in product repos
     task_desc_table: {
        aptupdate: ['', gettext('Update package database')],
        diskinit: ['Disk', gettext('Initialize Disk with GPT')],
@@ -607,14 +608,22 @@ utilities: {
        return text;
     },
 
-    format_size: function(size) {
-       let units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
-       let num = 0;
-       while (size >= 1024 && num++ <= units.length) {
-           size = size / 1024;
+    format_size: function(size, useSI) {
+       let units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
+       let order = 0;
+       const baseValue = useSI ? 1000 : 1024;
+       while (size >= baseValue && order < units.length) {
+           size = size / baseValue;
+           order++;
        }
 
-       return size.toFixed(num > 0?2:0) + " " + units[num] + "B";
+       let unit = units[order], commaDigits = 2;
+       if (order === 0) {
+           commaDigits = 0;
+       } else if (!useSI) {
+           unit += 'i';
+       }
+       return `${size.toFixed(commaDigits)} ${unit}B`;
     },
 
     render_upid: function(value, metaData, record) {
@@ -738,6 +747,17 @@ utilities: {
        return 'error';
     },
 
+    format_task_status: function(status) {
+       let parsed = Proxmox.Utils.parse_task_status(status);
+       switch (parsed) {
+           case 'unknown': return Proxmox.Utils.unknownText;
+           case 'error': return Proxmox.Utils.errorText + ': ' + status;
+           case 'warning': return status.replace('WARNINGS', Proxmox.Utils.warningsText);
+           case 'ok': // fall-through
+           default: return status;
+       }
+    },
+
     render_duration: function(value) {
        if (value === undefined) {
            return '-';
@@ -843,7 +863,7 @@ utilities: {
        return value;
     },
 
-    render_usage: val => (val*100).toFixed(2) + '%',
+    render_usage: val => (val * 100).toFixed(2) + '%',
 
     render_cpu_usage: function(val, max) {
        return Ext.String.format(
@@ -853,13 +873,13 @@ utilities: {
        );
     },
 
-    render_size_usage: function(val, max) {
+    render_size_usage: function(val, max, useSI) {
        if (max === 0) {
            return gettext('N/A');
        }
-       return (val*100/max).toFixed(2) + '% (' +
-           Ext.String.format(gettext('{0} of {1}'),
-           Proxmox.Utils.render_size(val), Proxmox.Utils.render_size(max)) + ')';
+       let fmt = v => Proxmox.Utils.format_size(v, useSI);
+       let ratio = (val * 100 / max).toFixed(2);
+       return ratio + '% (' + Ext.String.format(gettext('{0} of {1}'), fmt(val), fmt(max)) + ')';
     },
 
     render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {