]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - src/Utils.js
utils: clear cookies with secure flag set
[proxmox-widget-toolkit.git] / src / Utils.js
index b8ffd851a1bb20f5eef8a55b938c367e06aa7990..52cc626f1438b6bf1023e5b109cc5b5ecebe07c1 100644 (file)
@@ -90,7 +90,7 @@ utilities: {
     },
 
     render_language: function(value) {
-       if (!value) {
+       if (!value || value === '__default__') {
            return Proxmox.Utils.defaultText + ' (English)';
        }
        let text = Proxmox.Utils.language_map[value];
@@ -155,7 +155,7 @@ utilities: {
     // somewhat like a human would tell durations, omit zero values and do not
     // give seconds precision if we talk days already
     format_duration_human: function(ut) {
-       let seconds = 0, minutes = 0, hours = 0, days = 0;
+       let seconds = 0, minutes = 0, hours = 0, days = 0, years = 0;
 
        if (ut <= 0.1) {
            return '<0.1s';
@@ -171,7 +171,11 @@ utilities: {
                hours = remaining % 24;
                remaining = Math.trunc(remaining / 24);
                if (remaining > 0) {
-                   days = remaining;
+                   days = remaining % 365;
+                   remaining = Math.trunc(remaining / 365); // yea, just lets ignore leap years...
+                   if (remaining > 0) {
+                       years = remaining;
+                   }
                }
            }
        }
@@ -182,11 +186,14 @@ utilities: {
            return t > 0;
        };
 
+       let addMinutes = !add(years, 'y');
        let addSeconds = !add(days, 'd');
        add(hours, 'h');
-       add(minutes, 'm');
-       if (addSeconds) {
-           add(seconds, 's');
+       if (addMinutes) {
+           add(minutes, 'm');
+           if (addSeconds) {
+               add(seconds, 's');
+           }
        }
        return res.join(' ');
     },
@@ -299,7 +306,7 @@ utilities: {
        if (Proxmox.LoggedOut) {
            return;
        }
-       Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name);
+       Ext.util.Cookies.set(Proxmox.Setup.auth_cookie_name, "", new Date(0), null, null, true);
        window.localStorage.removeItem("ProxmoxUser");
     },
 
@@ -561,7 +568,7 @@ utilities: {
            return;
        }
 
-       let items = container.query('>'); // direct childs
+       let items = container.query('>'); // direct children
        factor = Math.min(factor, items.length);
        container.oldFactor = factor;
 
@@ -680,6 +687,37 @@ utilities: {
        'PB': 1000*1000*1000*1000*1000,
     },
 
+    parse_size_unit: function(val) {
+       //let m = val.match(/([.\d])+\s?([KMGTP]?)(i?)B?\s*$/i);
+       let m = val.match(/(\d+(?:\.\d+)?)\s?([KMGTP]?)(i?)B?\s*$/i);
+       let size = parseFloat(m[1]);
+       let scale = m[2].toUpperCase();
+       let binary = m[3].toLowerCase();
+
+       let unit = `${scale}${binary}B`;
+       let factor = Proxmox.Utils.SizeUnits[unit];
+
+       return { size, factor, unit, binary }; // for convenience return all we got
+    },
+
+    size_unit_to_bytes: function(val) {
+       let { size, factor } = Proxmox.Utils.parse_size_unit(val);
+       return size * factor;
+    },
+
+    autoscale_size_unit: function(val) {
+       let { size, factor, binary } = Proxmox.Utils.parse_size_unit(val);
+       return Proxmox.Utils.format_size(size * factor, binary !== "i");
+    },
+
+    size_unit_ratios: function(a, b) {
+       a = typeof a !== "undefined" ? a : 0;
+       b = typeof b !== "undefined" ? b : Infinity;
+       let aBytes = typeof a === "number" ? a : Proxmox.Utils.size_unit_to_bytes(a);
+       let bBytes = typeof b === "number" ? b : Proxmox.Utils.size_unit_to_bytes(b);
+       return aBytes / (bBytes || Infinity); // avoid division by zero
+    },
+
     render_upid: function(value, metaData, record) {
        let task = record.data;
        let type = task.type || task.worker_type;
@@ -1288,7 +1326,7 @@ Ext.define('Proxmox.Async', {
     singleton: true,
 
     // Returns a Promise resolving to the result of an `API2Request` or rejecting to the error
-    // repsonse on failure
+    // response on failure
     api2: function(reqOpts) {
        return new Promise((resolve, reject) => {
            delete reqOpts.callback; // not allowed in this api