]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - Utils.js
new checked_command helper, add APT.js
[proxmox-widget-toolkit.git] / Utils.js
index 7447f05b99d4c6acbea309dae61f03005a328294..fd23eb175b83bb6e6dd04e4818466b96192a89bf 100644 (file)
--- a/Utils.js
+++ b/Utils.js
@@ -41,6 +41,111 @@ Ext.define('Proxmox.Utils', { utilities: {
 
     // this singleton contains miscellaneous utilities
 
+    yesText: gettext('Yes'),
+    noText: gettext('No'),
+    enabledText: gettext('Enabled'),
+    disabledText: gettext('Disabled'),
+    noneText: gettext('none'),
+    errorText: gettext('Error'),
+    unknownText: gettext('Unknown'),
+    defaultText: gettext('Default'),
+    daysText: gettext('days'),
+    dayText: gettext('day'),
+    runningText: gettext('running'),
+    stoppedText: gettext('stopped'),
+    neverText: gettext('never'),
+    totalText: gettext('Total'),
+    usedText: gettext('Used'),
+    directoryText: gettext('Directory'),
+    stateText: gettext('State'),
+    groupText: gettext('Group'),
+
+    getNoSubKeyHtml: function(url) {
+       // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans
+       return Ext.String.format('You do not have a valid subscription for this server. Please visit <a target="_blank" href="{0}">www.proxmox.com</a> to get a list of available options.', url || 'http://www.proxmox.com');
+    },
+
+    format_boolean_with_default: function(value) {
+       if (Ext.isDefined(value) && value !== '__default__') {
+           return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText;
+       }
+       return Proxmox.Utils.defaultText;
+    },
+
+    format_boolean: function(value) {
+       return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText;
+    },
+
+    format_neg_boolean: function(value) {
+       return !value ? Proxmox.Utils.yesText : Proxmox.Utils.noText;
+    },
+
+    format_enabled_toggle: function(value) {
+       return value ? Proxmox.Utils.enabledText : Proxmox.Utils.disabledText;
+    },
+
+    format_expire: function(date) {
+       if (!date) {
+           return Proxmox.Utils.neverText;
+       }
+       return Ext.Date.format(date, "Y-m-d");
+    },
+
+    format_duration_long: function(ut) {
+
+       var days = Math.floor(ut / 86400);
+       ut -= days*86400;
+       var hours = Math.floor(ut / 3600);
+       ut -= hours*3600;
+       var mins = Math.floor(ut / 60);
+       ut -= mins*60;
+
+       var hours_str = '00' + hours.toString();
+       hours_str = hours_str.substr(hours_str.length - 2);
+       var mins_str = "00" + mins.toString();
+       mins_str = mins_str.substr(mins_str.length - 2);
+       var ut_str = "00" + ut.toString();
+       ut_str = ut_str.substr(ut_str.length - 2);
+
+       if (days) {
+           var ds = days > 1 ? Proxmox.Utils.daysText : Proxmox.Utils.dayText;
+           return days.toString() + ' ' + ds + ' ' +
+               hours_str + ':' + mins_str + ':' + ut_str;
+       } else {
+           return hours_str + ':' + mins_str + ':' + ut_str;
+       }
+    },
+
+    format_duration_short: function(ut) {
+
+       if (ut < 60) {
+           return ut.toString() + 's';
+       }
+
+       if (ut < 3600) {
+           var mins = ut / 60;
+           return mins.toFixed(0) + 'm';
+       }
+
+       if (ut < 86400) {
+           var hours = ut / 3600;
+           return hours.toFixed(0) + 'h';
+       }
+
+       var days = ut / 86400;
+       return days.toFixed(0) + 'd';
+    },
+
+    compute_min_label_width: function(text, width) {
+
+       if (width === undefined) { width = 100; }
+
+       var tm = new Ext.util.TextMetrics();
+       var min = tm.getWidth(text + ':');
+
+       return min < width ? width : min;
+    },
+
     authOK: function() {
        return (Proxmox.UserName !== '') && Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name);
     },
@@ -67,13 +172,19 @@ Ext.define('Proxmox.Utils', { utilities: {
        }
     },
 
-    monStoreErrors: function(me, store) {
-       me.mon(store, 'beforeload', function(s, operation, eOpts) {
-           if (!me.loadCount) {
-               me.loadCount = 0; // make sure it is numeric
-               Proxmox.Utils.setErrorMask(me, true);
-           }
-       });
+    monStoreErrors: function(me, store, clearMaskBeforeLoad) {
+       if (clearMaskBeforeLoad) {
+           me.mon(store, 'beforeload', function(s, operation, eOpts) {
+               Proxmox.Utils.setErrorMask(me, false);
+           })
+       } else {
+           me.mon(store, 'beforeload', function(s, operation, eOpts) {
+               if (!me.loadCount) {
+                   me.loadCount = 0; // make sure it is numeric
+                   Proxmox.Utils.setErrorMask(me, true);
+               }
+           });
+       }
 
        // only works with 'proxmox' proxy
        me.mon(store.proxy, 'afterload', function(proxy, request, success) {
@@ -182,8 +293,138 @@ Ext.define('Proxmox.Utils', { utilities: {
        Ext.Ajax.request(newopts);
     },
 
+    checked_command: function(orig_cmd) {
+       Proxmox.Utils.API2Request({
+           url: '/nodes/localhost/subscription',
+           method: 'GET',
+           //waitMsgTarget: me,
+           failure: function(response, opts) {
+               Ext.Msg.alert(gettext('Error'), response.htmlStatus);
+           },
+           success: function(response, opts) {
+               var data = response.result.data;
+
+               if (data.status !== 'Active') {
+                   Ext.Msg.show({
+                       title: gettext('No valid subscription'),
+                       icon: Ext.Msg.WARNING,
+                       msg: Proxmox.Utils.getNoSubKeyHtml(data.url),
+                       buttons: Ext.Msg.OK,
+                       callback: function(btn) {
+                           if (btn !== 'ok') {
+                               return;
+                           }
+                           orig_cmd();
+                       }
+                   });
+               } else {
+                   orig_cmd();
+               }
+           }
+       });
+    },
+
+    assemble_field_data: function(values, data) {
+        if (Ext.isObject(data)) {
+           Ext.Object.each(data, function(name, val) {
+               if (values.hasOwnProperty(name)) {
+                    var bucket = values[name];
+                    if (!Ext.isArray(bucket)) {
+                        bucket = values[name] = [bucket];
+                    }
+                    if (Ext.isArray(val)) {
+                        values[name] = bucket.concat(val);
+                    } else {
+                        bucket.push(val);
+                    }
+                } else {
+                   values[name] = val;
+                }
+            });
+       }
+    },
+
+    dialog_title: function(subject, create, isAdd) {
+       if (create) {
+           if (isAdd) {
+               return gettext('Add') + ': ' + subject;
+           } else {
+               return gettext('Create') + ': ' + subject;
+           }
+       } else {
+           return gettext('Edit') + ': ' + subject;
+       }
+    },
+
+    network_iface_types: {
+       eth: gettext("Network Device"),
+       bridge: 'Linux Bridge',
+       bond: 'Linux Bond',
+       OVSBridge: 'OVS Bridge',
+       OVSBond: 'OVS Bond',
+       OVSPort: 'OVS Port',
+       OVSIntPort: 'OVS IntPort'
+    },
+
+    render_network_iface_type: function(value) {
+       return Proxmox.Utils.network_iface_types[value] ||
+           Proxmox.Utils.unknownText;
+    },
+
+    // you can override this to provide nicer task descriptions
+    format_task_description: function(type, id) {
+       return type + ' ' + id;
+    },
+
+    render_upid: function(value, metaData, record) {
+       var type = record.data.type;
+       var id = record.data.id;
+
+       return Proxmox.Utils.format_task_description(type, id);
     },
-                         
+
+    render_uptime: function(value) {
+
+       var uptime = value;
+
+       if (uptime === undefined) {
+           return '';
+       }
+
+       if (uptime <= 0) {
+           return '-';
+       }
+
+       return Proxmox.Utils.format_duration_long(uptime);
+    },
+
+    parse_task_upid: function(upid) {
+       var task = {};
+
+       var res = upid.match(/^UPID:(\S+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8,9}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
+       if (!res) {
+           throw "unable to parse upid '" + upid + "'";
+       }
+       task.node = res[1];
+       task.pid = parseInt(res[2], 16);
+       task.pstart = parseInt(res[3], 16);
+       task.starttime = parseInt(res[4], 16);
+       task.type = res[5];
+       task.id = res[6];
+       task.user = res[7];
+
+       task.desc = Proxmox.Utils.format_task_description(task.type, task.id);
+
+       return task;
+    },
+
+    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');
+    },
+
+    },
+
     singleton: true,
     constructor: function() {
        var me = this;