]> git.proxmox.com Git - pve-manager.git/blobdiff - www/manager6/Utils.js
add right-click menu for nodes
[pve-manager.git] / www / manager6 / Utils.js
index 4ec7d5bd7992b36539167904a52c65664427950a..d1ada771f02bc37ca56edfe27de190f43ca4ece8 100644 (file)
@@ -29,43 +29,9 @@ 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
 
@@ -97,12 +63,67 @@ Ext.define('PVE.Utils', { statics: {
        w2k: 'Microsoft Windows 2000',
        w2k8: 'Microsoft Windows Vista/2008',
        win7: 'Microsoft Windows 7/2008r2',
-       win8: 'Microsoft Windows 8.x/10/2012/r2',
+       win8: 'Microsoft Windows 8.x/2012/2012r2',
+       win10: 'Microsoft Windows 10/2016',
        l24: 'Linux 2.4 Kernel',
        l26: 'Linux 4.X/3.X/2.6 Kernel',
        solaris: 'Solaris Kernel'
     },
 
+    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;
+    },
+
+    map_ceph_health: {
+       'HEALTH_OK':'good',
+       'HEALTH_WARN':'warning',
+       'HEALTH_ERR':'critical'
+    },
+
+    render_ceph_health: function(record) {
+       var state = {
+           iconCls: PVE.Utils.get_health_icon(),
+           text: ''
+       };
+
+       if (!record || !record.data) {
+           return state;
+       }
+
+       var health = PVE.Utils.map_ceph_health[record.data.health.overall_status];
+
+       state.iconCls = PVE.Utils.get_health_icon(health, true);
+       state.text = record.data.health.overall_status;
+
+       return state;
+    },
+
     render_kvm_ostype: function (value) {
        if (!value) {
            return gettext('Other OS types');
@@ -118,7 +139,7 @@ Ext.define('PVE.Utils', { statics: {
        var fa = [];
 
        if (!value || (value === '0')) {
-           return gettext('disabled');
+           return gettext('Disabled');
        }
 
        if (value === '1') {
@@ -131,7 +152,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') {
@@ -543,7 +564,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') ],
@@ -724,6 +745,8 @@ Ext.define('PVE.Utils', { statics: {
 
     yesText: gettext('Yes'),
     noText: gettext('No'),
+    enabledText: gettext('Enabled'),
+    disabledText: gettext('Disabled'),
     noneText: gettext('none'),
     errorText: gettext('Error'),
     unknownText: gettext('Unknown'),
@@ -796,6 +819,10 @@ 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);
 
@@ -873,11 +900,23 @@ 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');
+    },
+
     calculate_mem_usage: function(data) {
        if (!Ext.isNumeric(data.mem) ||
            data.maxmem === 0 ||
@@ -995,6 +1034,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;
     },
@@ -1037,6 +1081,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)) + ')';
@@ -1065,7 +1112,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; });
     },
 
@@ -1246,6 +1293,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)) {
@@ -1272,77 +1336,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];
+       }
     },
 
-    DocsPages: {
-       'pve-admin-guide.html':'Proxmox VE Administration Guide',
-       'chapter-sysadmin.html':'Host System Administration',
-       'chapter-pvecm.html':'Cluster Manager',
-       'chapter-pmxcfs.html':'Proxmox Cluster File System (pmxcfs)',
-       'chapter-pvesm.html':'Proxmox VE Storage',
-       'chapter-qm.html': 'Qemu/KVM Virtual Machines',
-       'chapter-pve-firewall.html': 'Proxmox VE Firewall',
-       'chapter-pveum.html': 'User Management',
-       'chapter-pct.html': 'Proxmox Container Toolkit',
-       'chapter-ha-manager.html': 'High Availability',
-       'chapter-vzdump.html': 'Backup and Restore',
-       'chapter-pve-faq.html': 'Frequently Asked Questions',
-       'chapter-pve-bibliography.html': 'Bibliography',
-       'qm.1.html': 'Qemu/KVM Virtual Machine Manager',
-       'qmrestore.1.html': 'Restore QemuServer vzdump Backups',
-       'pct.1.html': 'Tool to manage Linux Containers (LXC) on Proxmox VE',
-       'pveam.1.html': 'Proxmox VE Appliance Manager',
-       'pveceph.1.html': 'Manage CEPH Services on Proxmox VE Nodes',
-       'pvecm.1.html': 'Proxmox VE Cluster Manager',
-       'pveum.1.html': 'Proxmox VE User Manager',
-       'pvesm.1.html': 'Proxmox VE Storage Manager',
-       'pvesubscription.1.html': 'Proxmox VE Subscription Manager',
-       'vzdump.1.html': 'Backup Utility for VMs and Containers',
-       'ha-manager.1.html': 'Proxmox VE HA Manager',
-       'index.html':'',
-       'datacenter.cfg.5.html':'Proxmox VE Datacenter Configuration'
-    },
-
-    DocsSubTitles: {
-       '_vm_container_configuration':'VM/Container configuration',
-       '_ip_aliases':'IP Aliases',
-       '_ip_sets':'IP Sets'
-    },
-
-    mapDocsUrlToTitle: function(url) {
-       var title, subtitle;
-
-       // if there is a subtitle
-       if (url.indexOf('#') !== -1) {
-           title = PVE.Utils.DocsPages[url.split('#')[0]] || '';
-           subtitle = PVE.Utils.DocsSubTitles[url.split('#')[1]];
-
-           // if we do not find the subtitle,
-           // capitalize the beginning of every word
-           // and replace '_' with ' '
-           // e.g.:
-           // '_my_text' -> 'My Text'
-           if (!subtitle) {
-               subtitle = url.split('#')[1].replace(/_(\w)/gi, function(match,p1){
-                   return ' ' + p1.toUpperCase();
-               }).slice(1);
-           }
+    singleton: true,
+    constructor: function() {
+       var me = this;
+       Ext.apply(me, me.utilities);
 
-           if (title !== '') {
-               title += ' - ';
-           }
+       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 + ")";
 
-           title += subtitle;
-       } else {
-           title = PVE.Utils.DocsPages[url] || '';
-       }
 
-       return title;
+       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+)?$");
     }
-}});
+});