]> git.proxmox.com Git - proxmox-widget-toolkit.git/commitdiff
file browser: unify file type schema and avoid switch-case bloat
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Sun, 15 May 2022 08:15:57 +0000 (10:15 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Sun, 15 May 2022 08:15:57 +0000 (10:15 +0200)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/Schema.js
src/window/FileBrowser.js

index bcbc51196bc3b30748d4676cc977f67db2826365..aa39fb1f065cee905b594e63964d7def1424feec 100644 (file)
@@ -24,4 +24,16 @@ Ext.define('Proxmox.Schema', { // a singleton
            Proxmox.Schema.authDomains[key] = value;
        }
     },
+
+    pxarFileTypes: {
+       b: { icon: 'cube', label: gettext('Block Device') },
+       c: { icon: 'tty', label: gettext('Character Device') },
+       d: { icon: 'folder-o', label: gettext('Directory') },
+       f: { icon: 'file-label-o', label: gettext('File') },
+       h: { icon: 'file-o', label: gettext('Hardlink') },
+       l: { icon: 'link', label: gettext('Softlink') },
+       p: { icon: 'exchange', label: gettext('Pipe/Fifo') },
+       s: { icon: 'plug', label: gettext('Socket') },
+       v: { icon: 'cube', label: gettext('Virtual') },
+    },
 });
index 9ad2e81968c4d7885b4bca131fb0d4045538a2a3..570ad8f5a2201f3c7e26b0049660bab264ae1878 100644 (file)
@@ -1,7 +1,8 @@
 Ext.define('proxmox-file-tree', {
     extend: 'Ext.data.Model',
 
-    fields: ['filepath', 'text', 'type', 'size',
+    fields: [
+       'filepath', 'text', 'type', 'size',
        {
            name: 'sizedisplay',
            calculate: data => {
@@ -23,40 +24,10 @@ Ext.define('proxmox-file-tree', {
        {
            name: 'iconCls',
            calculate: function(data) {
-               let icon = 'file-o';
-               switch (data.type) {
-                   case 'b': // block device
-                       icon = 'cube';
-                       break;
-                   case 'c': // char device
-                       icon = 'tty';
-                       break;
-                   case 'd':
-                       icon = data.expanded ? 'folder-open-o' : 'folder-o';
-                       break;
-                   case 'f': //regular file
-                       icon = 'file-text-o';
-                       break;
-                   case 'h': // hardlink
-                       icon = 'file-o';
-                       break;
-                   case 'l': // softlink
-                       icon = 'link';
-                       break;
-                   case 'p': // pipe/fifo
-                       icon = 'exchange';
-                       break;
-                   case 's': // socket
-                       icon = 'plug';
-                       break;
-                   case 'v': // virtual
-                       icon = 'cube';
-                       break;
-                   default:
-                       icon = 'file-o';
-                       break;
+               let icon = Proxmox.Schema.pxarFileTypes[data.type]?.icon ?? 'file-o';
+               if (data.expanded && data.type === 'd') {
+                   icon = 'folder-open-o';
                }
-
                return `fa fa-${icon}`;
            },
        },
@@ -309,20 +280,7 @@ Ext.define("Proxmox.window.FileBrowser", {
                {
                    text: gettext('Type'),
                    dataIndex: 'type',
-                   renderer: function(value) {
-                       switch (value) {
-                           case 'b': return gettext('Block Device');
-                           case 'c': return gettext('Character Device');
-                           case 'd': return gettext('Directory');
-                           case 'f': return gettext('File');
-                           case 'h': return gettext('Hardlink');
-                           case 'l': return gettext('Softlink');
-                           case 'p': return gettext('Pipe/Fifo');
-                           case 's': return gettext('Socket');
-                           case 'v': return gettext('Virtual');
-                           default: return Proxmox.Utils.unknownText;
-                       }
-                   },
+                   renderer: (v) => Proxmox.Schema.pxarFileTypes[v]?.label ?? Proxmox.Utils.unknownText,
                },
            ],
        },