]>
Commit | Line | Data |
---|---|---|
1 | Ext.define('PVE.form.ControllerSelector', { | |
2 | extend: 'Ext.form.FieldContainer', | |
3 | alias: 'widget.pveControllerSelector', | |
4 | ||
5 | noVirtIO: false, | |
6 | ||
7 | vmconfig: {}, // used to check for existing devices | |
8 | ||
9 | sortByPreviousUsage: function(vmconfig, controllerList) { | |
10 | let usedControllers = {}; | |
11 | for (const type of Object.keys(PVE.Utils.diskControllerMaxIDs)) { | |
12 | usedControllers[type] = 0; | |
13 | } | |
14 | ||
15 | for (const property of Object.keys(vmconfig)) { | |
16 | if (property.match(PVE.Utils.bus_match) && !vmconfig[property].match(/media=cdrom/)) { | |
17 | const foundController = property.match(PVE.Utils.bus_match)[1]; | |
18 | usedControllers[foundController]++; | |
19 | } | |
20 | } | |
21 | ||
22 | var sortPriority = PVE.qemu.OSDefaults.getDefaults(vmconfig.ostype).busPriority; | |
23 | ||
24 | var sortedList = Ext.clone(controllerList); | |
25 | sortedList.sort(function(a, b) { | |
26 | if (usedControllers[b] == usedControllers[a]) { | |
27 | return sortPriority[b] - sortPriority[a]; | |
28 | } | |
29 | return usedControllers[b] - usedControllers[a]; | |
30 | }); | |
31 | ||
32 | return sortedList; | |
33 | }, | |
34 | ||
35 | setVMConfig: function(vmconfig, autoSelect) { | |
36 | var me = this; | |
37 | ||
38 | me.vmconfig = Ext.apply({}, vmconfig); | |
39 | ||
40 | var clist = ['ide', 'virtio', 'scsi', 'sata']; | |
41 | var bussel = me.down('field[name=controller]'); | |
42 | var deviceid = me.down('field[name=deviceid]'); | |
43 | ||
44 | if (autoSelect === 'cdrom') { | |
45 | if (!Ext.isDefined(me.vmconfig.ide2)) { | |
46 | bussel.setValue('ide'); | |
47 | deviceid.setValue(2); | |
48 | return; | |
49 | } | |
50 | clist = ['ide', 'scsi', 'sata']; | |
51 | } else { | |
52 | // in most cases we want to add a disk to the same controller | |
53 | // we previously used | |
54 | clist = me.sortByPreviousUsage(me.vmconfig, clist); | |
55 | } | |
56 | ||
57 | clist_loop: | |
58 | for (const controller of clist) { | |
59 | bussel.setValue(controller); | |
60 | for (let i = 0; i < PVE.Utils.diskControllerMaxIDs[controller]; i++) { | |
61 | let confid = controller + i.toString(); | |
62 | if (!Ext.isDefined(me.vmconfig[confid])) { | |
63 | deviceid.setValue(i); | |
64 | break clist_loop; // we found the desired controller/id combo | |
65 | } | |
66 | } | |
67 | } | |
68 | deviceid.validate(); | |
69 | }, | |
70 | ||
71 | initComponent: function() { | |
72 | var me = this; | |
73 | ||
74 | Ext.apply(me, { | |
75 | fieldLabel: gettext('Bus/Device'), | |
76 | layout: 'hbox', | |
77 | defaults: { | |
78 | hideLabel: true, | |
79 | }, | |
80 | items: [ | |
81 | { | |
82 | xtype: 'pveBusSelector', | |
83 | name: 'controller', | |
84 | value: PVE.qemu.OSDefaults.generic.busType, | |
85 | noVirtIO: me.noVirtIO, | |
86 | allowBlank: false, | |
87 | flex: 2, | |
88 | listeners: { | |
89 | change: function(t, value) { | |
90 | if (!value) { | |
91 | return; | |
92 | } | |
93 | var field = me.down('field[name=deviceid]'); | |
94 | field.setMaxValue(PVE.Utils.diskControllerMaxIDs[value]); | |
95 | field.validate(); | |
96 | }, | |
97 | }, | |
98 | }, | |
99 | { | |
100 | xtype: 'proxmoxintegerfield', | |
101 | name: 'deviceid', | |
102 | minValue: 0, | |
103 | maxValue: PVE.Utils.diskControllerMaxIDs.ide, | |
104 | value: '0', | |
105 | flex: 1, | |
106 | allowBlank: false, | |
107 | validator: function(value) { | |
108 | if (!me.rendered) { | |
109 | return; | |
110 | } | |
111 | var field = me.down('field[name=controller]'); | |
112 | var controller = field.getValue(); | |
113 | var confid = controller + value; | |
114 | if (Ext.isDefined(me.vmconfig[confid])) { | |
115 | return "This device is already in use."; | |
116 | } | |
117 | return true; | |
118 | }, | |
119 | }, | |
120 | ], | |
121 | }); | |
122 | ||
123 | me.callParent(); | |
124 | }, | |
125 | }); |