]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - src/Utils.js
Utils: refactor updateColumns from pve-manager
[proxmox-widget-toolkit.git] / src / Utils.js
index 236cc271c5714a86345c73130754fdef5f07b106..adff5f4886fe03f9a5510eac18319b91ed021946 100644 (file)
@@ -48,6 +48,7 @@ utilities: {
     noneText: gettext('none'),
     NoneText: gettext('None'),
     errorText: gettext('Error'),
+    warningsText: gettext('Warnings'),
     unknownText: gettext('Unknown'),
     defaultText: gettext('Default'),
     daysText: gettext('days'),
@@ -840,6 +841,66 @@ utilities: {
        return value;
     },
 
+    render_usage: function(val) {
+       return (val*100).toFixed(2) + '%';
+    },
+
+    render_cpu_usage: function(val, max) {
+       return Ext.String.format(gettext('{0}% of {1}') +
+           ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
+    },
+
+    render_size_usage: function(val, max) {
+       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)) + ')';
+    },
+
+    render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
+       if (!(record.data.uptime && Ext.isNumeric(value))) {
+           return '';
+       }
+
+       var maxcpu = record.data.maxcpu || 1;
+
+       if (!Ext.isNumeric(maxcpu) && maxcpu >= 1) {
+           return '';
+       }
+
+       var per = value * 100;
+
+       return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
+    },
+
+    render_size: function(value, metaData, record, rowIndex, colIndex, store) {
+       if (!Ext.isNumeric(value)) {
+           return '';
+       }
+
+       return Proxmox.Utils.format_size(value);
+    },
+
+    render_cpu_model: function(cpuinfo) {
+       return cpuinfo.cpus + " x " + cpuinfo.model + " (" +
+           cpuinfo.sockets.toString() + " " +
+           (cpuinfo.sockets > 1
+               ? gettext('Sockets')
+               : gettext('Socket')
+           ) + ")";
+    },
+
+    /* this is different for nodes */
+    render_node_cpu_usage: function(value, record) {
+       return Proxmox.Utils.render_cpu_usage(value, record.cpus);
+    },
+
+    render_node_size_usage: function(record) {
+       return Proxmox.Utils.render_size_usage(record.used, record.total);
+    },
+
     loadTextFromFile: function(file, callback, maxBytes) {
        let maxSize = maxBytes || 8192;
        if (file.size > maxSize) {
@@ -999,6 +1060,36 @@ utilities: {
        }
        return acme;
     },
+
+    updateColumns: function(container) {
+       let mode = Ext.state.Manager.get('summarycolumns') || 'auto';
+       let factor;
+       if (mode !== 'auto') {
+           factor = parseInt(mode, 10);
+           if (Number.isNaN(factor)) {
+               factor = 1;
+           }
+       } else {
+           factor = container.getSize().width < 1400 ? 1 : 2;
+       }
+
+       if (container.oldFactor === factor) {
+           return;
+       }
+
+       let items = container.query('>'); // direct childs
+       factor = Math.min(factor, items.length);
+       container.oldFactor = factor;
+
+       items.forEach((item) => {
+           item.columnWidth = 1 / factor;
+       });
+
+       // we have to update the layout twice, since the first layout change
+       // can trigger the scrollbar which reduces the amount of space left
+       container.updateLayout();
+       container.updateLayout();
+    },
 },
 
     singleton: true,