]> git.proxmox.com Git - pve-manager.git/commitdiff
ui: refactor sortByPreviousUsage and nextFreeDisk
authorDominik Csapak <d.csapak@proxmox.com>
Wed, 22 Sep 2021 09:27:40 +0000 (11:27 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 4 Oct 2021 06:55:05 +0000 (08:55 +0200)
we'll use them outside of the controllerSelector soon

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
www/manager6/Utils.js
www/manager6/form/ControllerSelector.js

index 4041c010882515568632d0a1a04fe8a41164ea10..8631a67c878390b532cb3ad1f20d796947cbe3dd 100644 (file)
@@ -1757,6 +1757,52 @@ Ext.define('PVE.Utils', {
 
        return true;
     },
+
+    sortByPreviousUsage: function(vmconfig, controllerList) {
+       if (!controllerList) {
+           controllerList = ['ide', 'virtio', 'scsi', 'sata'];
+       }
+       let usedControllers = {};
+       for (const type of Object.keys(PVE.Utils.diskControllerMaxIDs)) {
+           usedControllers[type] = 0;
+       }
+
+       for (const property of Object.keys(vmconfig)) {
+           if (property.match(PVE.Utils.bus_match) && !vmconfig[property].match(/media=cdrom/)) {
+               const foundController = property.match(PVE.Utils.bus_match)[1];
+               usedControllers[foundController]++;
+           }
+       }
+
+       let sortPriority = PVE.qemu.OSDefaults.getDefaults(vmconfig.ostype).busPriority;
+
+       let sortedList = Ext.clone(controllerList);
+       sortedList.sort(function(a, b) {
+           if (usedControllers[b] === usedControllers[a]) {
+               return sortPriority[b] - sortPriority[a];
+           }
+           return usedControllers[b] - usedControllers[a];
+       });
+
+       return sortedList;
+    },
+
+    nextFreeDisk: function(controllers, config) {
+       for (const controller of controllers) {
+           for (let i = 0; i < PVE.Utils.diskControllerMaxIDs[controller]; i++) {
+               let confid = controller + i.toString();
+               if (!Ext.isDefined(config[confid])) {
+                   return {
+                       controller,
+                       id: i,
+                       confid,
+                   };
+               }
+           }
+       }
+
+       return undefined;
+    },
 },
 
     singleton: true,
index 27c06169620bbdee40a2a1d3486d6ec22e7c516e..fcf625a10fea237e79293b2c22a742f197798c6a 100644 (file)
@@ -6,44 +6,15 @@ Ext.define('PVE.form.ControllerSelector', {
 
     vmconfig: {}, // used to check for existing devices
 
-    sortByPreviousUsage: function(vmconfig, controllerList) {
-       let usedControllers = {};
-       for (const type of Object.keys(PVE.Utils.diskControllerMaxIDs)) {
-           usedControllers[type] = 0;
-       }
-
-       for (const property of Object.keys(vmconfig)) {
-           if (property.match(PVE.Utils.bus_match) && !vmconfig[property].match(/media=cdrom/)) {
-               const foundController = property.match(PVE.Utils.bus_match)[1];
-               usedControllers[foundController]++;
-           }
-       }
-
-       let sortPriority = PVE.qemu.OSDefaults.getDefaults(vmconfig.ostype).busPriority;
-
-       let sortedList = Ext.clone(controllerList);
-       sortedList.sort(function(a, b) {
-           if (usedControllers[b] === usedControllers[a]) {
-               return sortPriority[b] - sortPriority[a];
-           }
-           return usedControllers[b] - usedControllers[a];
-       });
-
-       return sortedList;
-    },
-
     setToFree: function(controllers, busField, deviceIDField) {
        let me = this;
-       for (const controller of controllers) {
-           busField.setValue(controller);
-           for (let i = 0; i < PVE.Utils.diskControllerMaxIDs[controller]; i++) {
-               let confid = controller + i.toString();
-               if (!Ext.isDefined(me.vmconfig[confid])) {
-                   deviceIDField.setValue(i);
-                   return;
-               }
-           }
+       let freeId = PVE.Utils.nextFreeDisk(controllers, me.vmconfig);
+
+       if (freeId !== undefined) {
+           busField.setValue(freeId.controller);
+           deviceIDField.setValue(freeId.id);
        }
+
     },
 
     setVMConfig: function(vmconfig, autoSelect) {
@@ -54,7 +25,7 @@ Ext.define('PVE.form.ControllerSelector', {
        let bussel = me.down('field[name=controller]');
        let deviceid = me.down('field[name=deviceid]');
 
-       let clist = ['ide', 'virtio', 'scsi', 'sata'];
+       let clist;
        if (autoSelect === 'cdrom') {
            if (!Ext.isDefined(me.vmconfig.ide2)) {
                bussel.setValue('ide');
@@ -64,7 +35,7 @@ Ext.define('PVE.form.ControllerSelector', {
            clist = ['ide', 'scsi', 'sata'];
        } else {
            // in most cases we want to add a disk to the same controller we previously used
-           clist = me.sortByPreviousUsage(me.vmconfig, clist);
+           clist = PVE.Utils.sortByPreviousUsage(me.vmconfig);
        }
 
        me.setToFree(clist, bussel, deviceid);