]> git.proxmox.com Git - pve-manager.git/blobdiff - www/manager6/Utils.js
add new tree and grid classes for the 'unknown' status
[pve-manager.git] / www / manager6 / Utils.js
index a31beb92676e6d9829a079b1ff8204d3ebf5caa3..eaecc6f68a0db15647b08a363aef3e546f4fd88b 100644 (file)
@@ -29,46 +29,14 @@ Ext.Ajax.on('beforerequest', function(conn, options) {
     }
 });
 
-var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
-var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
-var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
-var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
+Ext.define('PVE.Utils', { utilities: {
 
-
-var IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
-var IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$");
-
-var IPV6_REGEXP = "(?:" +
-    "(?:(?:"                                                  + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
-    "(?:(?:"                                         +   "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
-    "(?:(?:(?:"                           + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
-    "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
-    "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
-    "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
-    "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" +                         ")" + IPV6_LS32 + ")|" +
-    "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" +                         ")" + IPV6_H16  + ")|" +
-    "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" +                         ")"             + ")"  +
-    ")";
-
-var IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
-var IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$");
-var IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
-
-var IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
-
-var DnsName_REGEXP = "(?:(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)\\.)*([A-Za-z0-9]([A-Za-z0-9\\-]*[A-Za-z0-9])?))";
-var DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
-
-var HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
-var HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
-var IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
-
-Ext.define('PVE.Utils', { statics: {
-
-    // this class only contains static functions
+    // this singleton contains miscellaneous utilities
 
     toolkit: undefined, // (extjs|touch), set inside Toolkit.js
 
+    bus_match: /^(ide|sata|virtio|scsi)\d+$/,
+
     log_severity_hash: {
        0: "panic",
        1: "alert",
@@ -90,33 +58,108 @@ Ext.define('PVE.Utils', { statics: {
     noSubKeyHtml: 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="http://www.proxmox.com/products/proxmox-ve/subscription-service-plans">www.proxmox.com</a> to get a list of available options.',
 
     kvm_ostypes: {
-       other: gettext('Other OS types'),
-       wxp: 'Microsoft Windows XP/2003',
-       w2k: 'Microsoft Windows 2000',
-       w2k8: 'Microsoft Windows Vista/2008',
-       win7: 'Microsoft Windows 7/2008r2',
-       win8: 'Microsoft Windows 8.x/10/2012/r2',
-       l24: 'Linux 2.4 Kernel',
-       l26: 'Linux 4.X/3.X/2.6 Kernel',
-       solaris: 'Solaris Kernel'
+       'Linux': [
+           { desc: '4.X/3.X/2.6 Kernel', val: 'l26' },
+           { desc: '2.4 Kernel', val: 'l24' }
+       ],
+       'Microsoft Windows': [
+           { desc: '10/2016', val: 'win10' },
+           { desc: '8.x/2012/2012r2', val: 'win8' },
+           { desc: '7/2008r2', val: 'win7' },
+           { desc: 'Vista/2008', val: 'w2k8' },
+           { desc: 'XP/2003', val: 'wxp' },
+           { desc: '2000', val: 'w2k' }
+       ],
+       'Solaris Kernel': [
+           { desc: '-', val: 'solaris'}
+       ],
+       'Other': [
+           { desc: '-', val: 'other'}
+       ]
+    },
+
+    get_health_icon: function(state, circle) {
+       if (circle === undefined) {
+           circle = false;
+       }
+
+       if (state === undefined) {
+           state = 'uknown';
+       }
+
+       var icon = 'faded fa-question';
+       switch(state) {
+           case 'good':
+               icon = 'good fa-check';
+               break;
+           case 'warning':
+               icon = 'warning fa-exclamation';
+               break;
+           case 'critical':
+               icon = 'critical fa-times';
+               break;
+           default: break;
+       }
+
+       if (circle) {
+           icon += '-circle';
+       }
+
+       return icon;
     },
 
-    render_kvm_ostype: function (value) {
-       if (!value) {
-           return gettext('Other OS types');
+    map_ceph_health: {
+       'HEALTH_OK':'good',
+       'HEALTH_WARN':'warning',
+       'HEALTH_ERR':'critical'
+    },
+
+    render_ceph_health: function(healthObj) {
+       var state = {
+           iconCls: PVE.Utils.get_health_icon(),
+           text: ''
+       };
+
+       if (!healthObj || !healthObj.status) {
+           return state;
        }
-       var text = PVE.Utils.kvm_ostypes[value];
-       if (text) {
-           return text;
+
+       var health = PVE.Utils.map_ceph_health[healthObj.status];
+
+       state.iconCls = PVE.Utils.get_health_icon(health, true);
+       state.text = healthObj.status;
+
+       return state;
+    },
+
+    get_kvm_osinfo: function(value) {
+       var info = { base: 'Other' }; // default
+       if (value) {
+           Ext.each(Object.keys(PVE.Utils.kvm_ostypes), function(k) {
+               Ext.each(PVE.Utils.kvm_ostypes[k], function(e) {
+                   if (e.val === value) {
+                       info = { desc: e.desc, base: k };
+                   }
+               });
+           });
+       }
+       return info;
+    },
+
+    render_kvm_ostype: function (value) {
+       var osinfo = PVE.Utils.get_kvm_osinfo(value);
+       if (osinfo.desc && osinfo.desc !== '-') {
+           return osinfo.base + ' ' + osinfo.desc;
+       } else {
+           return osinfo.base;
        }
-       return value;
     },
 
     render_hotplug_features: function (value) {
        var fa = [];
 
        if (!value || (value === '0')) {
-           return gettext('disabled');
+           return gettext('Disabled');
        }
 
        if (value === '1') {
@@ -129,7 +172,7 @@ Ext.define('PVE.Utils', { statics: {
            } else if (el === 'network') {
                fa.push(gettext('Network'));
            } else if (el === 'usb') {
-               fa.push(gettext('USB'));
+               fa.push('USB');
            } else if (el === 'memory') {
                fa.push(gettext('Memory'));
            } else if (el === 'cpu') {
@@ -230,7 +273,6 @@ Ext.define('PVE.Utils', { statics: {
     kvm_vga_drivers: {
        std: gettext('Standard VGA'),
        vmware: gettext('VMware compatible'),
-       cirrus: 'Cirrus Logic GD5446',
        qxl: 'SPICE',
        qxl2: 'SPICE dual monitor',
        qxl3: 'SPICE three monitors',
@@ -541,7 +583,7 @@ Ext.define('PVE.Utils', { statics: {
     },
 
     task_desc_table: {
-       diskinit: [ 'Disk', gettext('Initialize GPT') ],
+       diskinit: [ 'Disk', gettext('Initialize Disk with GPT') ],
        vncproxy: [ 'VM/CT', gettext('Console') ],
        spiceproxy: [ 'VM/CT', gettext('Console') + ' (Spice)' ],
        vncshell: [ '', gettext('Shell') ],
@@ -590,6 +632,8 @@ Ext.define('PVE.Utils', { statics: {
        cephdestroymon: ['Ceph Monitor', gettext('Destroy') ],
        cephcreateosd: ['Ceph OSD', gettext('Create') ],
        cephdestroyosd: ['Ceph OSD', gettext('Destroy') ],
+       cephcreatepool: ['Ceph Pool', gettext('Create') ],
+       cephdestroypool: ['Ceph Pool', gettext('Destroy') ],
        imgcopy: ['', gettext('Copy data') ],
        imgdel: ['', gettext('Erase data') ],
        download: ['', gettext('Download') ],
@@ -703,25 +747,27 @@ Ext.define('PVE.Utils', { statics: {
     format_duration_short: function(ut) {
 
        if (ut < 60) {
-           return ut.toString() + 's';
+           return ut.toFixed(1) + 's';
        }
 
        if (ut < 3600) {
            var mins = ut / 60;
-           return mins.toFixed(0) + 'm';
+           return mins.toFixed(1) + 'm';
        }
 
        if (ut < 86400) {
            var hours = ut / 3600;
-           return hours.toFixed(0) + 'h';
+           return hours.toFixed(1) + 'h';
        }
 
        var days = ut / 86400;
-       return days.toFixed(0) + 'd';
+       return days.toFixed(1) + 'd';
     },
 
     yesText: gettext('Yes'),
     noText: gettext('No'),
+    enabledText: gettext('Enabled'),
+    disabledText: gettext('Disabled'),
     noneText: gettext('none'),
     errorText: gettext('Error'),
     unknownText: gettext('Unknown'),
@@ -749,7 +795,10 @@ Ext.define('PVE.Utils', { statics: {
        return Ext.Date.format(date, "Y-m-d");
     },
 
-    format_storage_type: function(value) {
+    format_storage_type: function(value, md, record) {
+       if (value === 'rbd' && record) {
+           value = (record.get('monhost')?'rbd_ext':'pveceph');
+       }
        if (value === 'dir') {
            return PVE.Utils.directoryText;
        } else if (value === 'nfs') {
@@ -764,6 +813,10 @@ Ext.define('PVE.Utils', { statics: {
            return 'iSCSI';
        } else if (value === 'rbd') {
            return 'RBD';
+       } else if (value === 'rbd_ext') {
+           return 'RBD (external)';
+       } else if (value === 'pveceph') {
+           return 'RBD (PVE)';
        } else if (value === 'sheepdog') {
            return 'Sheepdog';
        } else if (value === 'zfs') {
@@ -794,12 +847,15 @@ Ext.define('PVE.Utils', { statics: {
        return !value ? PVE.Utils.yesText : PVE.Utils.noText;
     },
 
+    format_enabled_toggle: function(value) {
+       return value ? PVE.Utils.enabledText :PVE.Utils.disabledText;
+    },
+
     format_ha: function(value) {
-       var text = PVE.Utils.format_boolean(value.managed);
+       var text = PVE.Utils.noneText;
 
        if (value.managed) {
-           text += ', ' + PVE.Utils.stateText + ': ';
-           text += value.state || PVE.Utils.noneText;
+           text = value.state || PVE.Utils.noneText;
 
            text += ', ' +  PVE.Utils.groupText + ': ';
            text += value.group || PVE.Utils.noneText;
@@ -871,11 +927,30 @@ Ext.define('PVE.Utils', { statics: {
        return PVE.Utils.format_size(value);
     },
 
+    render_bandwidth: function(value) {
+       if (!Ext.isNumeric(value)) {
+           return '';
+       }
+
+       return PVE.Utils.format_size(value) + '/s';
+    },
+
     render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
        var servertime = new Date(value * 1000);
        return Ext.Date.format(servertime, 'Y-m-d H:i:s');
     },
 
+    render_timestamp_human_readable: function(value) {
+       return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
+    },
+
+    render_duration: function(value) {
+       if (value === undefined) {
+           return '-';
+       }
+       return PVE.Utils.format_duration_short(value);
+    },
+
     calculate_mem_usage: function(data) {
        if (!Ext.isNumeric(data.mem) ||
            data.maxmem === 0 ||
@@ -993,6 +1068,11 @@ Ext.define('PVE.Utils', { statics: {
            }
        }
 
+       // overwrite anything else
+       if (record.data.hastate === 'error') {
+           gridcls = '-offline';
+       }
+
        var fa = '<i class="fa fa-fw x-fa-grid' + gridcls  +  ' fa-' + icon  + '"></i> ';
        return fa + value;
     },
@@ -1035,6 +1115,9 @@ Ext.define('PVE.Utils', { statics: {
     },
 
     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}'),
            PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
@@ -1063,7 +1146,7 @@ Ext.define('PVE.Utils', { statics: {
     },
 
     windowHostname: function() {
-       return window.location.hostname.replace(IP6_bracket_match,
+       return window.location.hostname.replace(PVE.Utils.IP6_bracket_match,
             function(m, addr, offset, original) { return addr; });
     },
 
@@ -1244,6 +1327,23 @@ Ext.define('PVE.Utils', { statics: {
        }
     },
 
+    // test automation helper
+    call_menu_handler: function(menu, text) {
+
+       var list = menu.query('menuitem');
+
+       Ext.Array.each(list, function(item) {
+           if (item.text === text) {
+               if (item.handler) {
+                   item.handler();
+                   return 1;
+               } else {
+                   return undefined;
+               }
+           }
+       });
+    },
+
     createCmdMenu: function(v, record, item, index, event) {
        event.stopEvent();
        if (!(v instanceof Ext.tree.View)) {
@@ -1270,11 +1370,72 @@ Ext.define('PVE.Utils', { statics: {
            });
            */
            return;
+
+       } else if (record.data.type === 'node' ){
+           menu = Ext.create('PVE.node.CmdMenu', {
+               nodename: record.data.node
+           });
+
        } else {
            return;
        }
 
        menu.showAt(event.getXY());
+    }},
+
+    // helper for deleting field which are set to there default values
+    delete_if_default: function(values, fieldname, default_val, create) {
+       if (values[fieldname] === '' || values[fieldname] === default_val) {
+           if (!create) {
+               if (values['delete']) {
+                   values['delete'] += ',' + fieldname;
+               } else {
+                   values['delete'] = fieldname;
+               }
+           }
+
+           delete values[fieldname];
+       }
+    },
+
+    singleton: true,
+    constructor: function() {
+       var me = this;
+       Ext.apply(me, me.utilities);
+
+       var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
+       var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
+       var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
+       var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
+
+
+       me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
+       me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$");
+
+       var IPV6_REGEXP = "(?:" +
+           "(?:(?:"                                                  + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
+           "(?:(?:"                                         +   "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
+           "(?:(?:(?:"                           + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
+           "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
+           "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
+           "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
+           "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" +                         ")" + IPV6_LS32 + ")|" +
+           "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" +                         ")" + IPV6_H16  + ")|" +
+           "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" +                         ")"             + ")"  +
+           ")";
+
+       me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
+       me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$");
+       me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
+
+       me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
+
+       var DnsName_REGEXP = "(?:(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)\\.)*([A-Za-z0-9]([A-Za-z0-9\\-]*[A-Za-z0-9])?))";
+       me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
+
+       me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
+       me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
+       me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
     }
-}});
+});