]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - Utils.js
place space on correct side of colon
[proxmox-widget-toolkit.git] / Utils.js
index 7534ffb201d459e7658b4f1a5b5288aa38884e68..e7a589b5464a97389796d97ec0b2e2eba4a7b28c 100644 (file)
--- a/Utils.js
+++ b/Utils.js
@@ -1,9 +1,9 @@
 Ext.ns('Proxmox');
 Ext.ns('Proxmox.Setup');
 
-// TODO: implement gettext
-function gettext(buf) { return buf; }
-
+if (!Ext.isFunction(gettext)) {
+    function gettext(buf) { return buf; }
+}
 
 if (!Ext.isDefined(Proxmox.Setup.auth_cookie_name)) {
     throw "Proxmox library not initialized";
@@ -60,6 +60,39 @@ Ext.define('Proxmox.Utils', { utilities: {
     stateText: gettext('State'),
     groupText: gettext('Group'),
 
+    language_map: {
+       en: 'English',
+       fr: 'French',
+       de: 'German',
+       it: 'Italian',
+       es: 'Spanish'
+    },
+
+    render_language: function (value) {
+       if (!value) {
+           return Proxmox.Utils.defaultText + ' (English)';
+       }
+       var text = Proxmox.Utils.language_map[value];
+       if (text) {
+           return text + ' (' + value + ')';
+       }
+       return value;
+    },
+
+    language_array: function() {
+       var data = [['__default__', Proxmox.Utils.render_language('')]];
+       Ext.Object.each(Proxmox.Utils.language_map, function(key, value) {
+           data.push([key, Proxmox.Utils.render_language(value)]);
+       });
+
+       return data;
+    },
+
+    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;
@@ -131,6 +164,20 @@ Ext.define('Proxmox.Utils', { utilities: {
        return days.toFixed(0) + 'd';
     },
 
+    format_subscription_level: function(level) {
+       if (level === 'c') {
+           return 'Community';
+       } else if (level === 'b') {
+           return 'Basic';
+       } else if (level === 's') {
+           return 'Standard';
+       } else if (level === 'p') {
+           return 'Premium';
+       } else {
+           return Proxmox.Utils.noneText;
+       }
+    },
+
     compute_min_label_width: function(text, width) {
 
        if (width === undefined) { width = 100; }
@@ -167,13 +214,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) {
@@ -282,6 +335,37 @@ 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) {
@@ -334,6 +418,19 @@ Ext.define('Proxmox.Utils', { utilities: {
        return type + ' ' + id;
     },
 
+    format_size: function(size) {
+       /*jslint confusion: true */
+
+       var units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
+       var num = 0;
+
+       while (size >= 1024 && ((num++)+1) < units.length) {
+           size = size / 1024;
+       }
+
+       return size.toFixed((num > 0)?2:0) + " " + units[num] + "B";
+    },
+
     render_upid: function(value, metaData, record) {
        var type = record.data.type;
        var id = record.data.id;
@@ -381,7 +478,19 @@ Ext.define('Proxmox.Utils', { utilities: {
        return Ext.Date.format(servertime, 'Y-m-d H:i:s');
     },
 
-    },
+    openXtermJsViewer: function(vmtype, vmid, nodename, vmname) {
+       var url = Ext.urlEncode({
+           console: vmtype, // kvm, lxc, upgrade or shell
+           xtermjs: 1,
+           vmid: vmid,
+           vmname: vmname,
+           node: nodename
+       });
+       var nw = window.open("?" + url, '_blank', 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420');
+       nw.focus();
+    }
+
+},
 
     singleton: true,
     constructor: function() {