]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/form/ControllerSelector.js
ui: qm: disk selection: add optional selection of unused
[pve-manager.git] / www / manager6 / form / ControllerSelector.js
1 Ext.define('PVE.form.ControllerSelector', {
2 extend: 'Ext.form.FieldContainer',
3 alias: 'widget.pveControllerSelector',
4
5 noVirtIO: false,
6 withUnused: false,
7
8 vmconfig: {}, // used to check for existing devices
9
10 setToFree: function(controllers, busField, deviceIDField) {
11 let me = this;
12 let freeId = PVE.Utils.nextFreeDisk(controllers, me.vmconfig);
13
14 if (freeId !== undefined) {
15 busField.setValue(freeId.controller);
16 deviceIDField.setValue(freeId.id);
17 }
18 },
19
20 updateVMConfig: function(vmconfig) {
21 let me = this;
22 me.vmconfig = Ext.apply({}, vmconfig);
23
24 me.down('field[name=deviceid]').validate();
25 },
26
27 setVMConfig: function(vmconfig, autoSelect) {
28 let me = this;
29
30 me.vmconfig = Ext.apply({}, vmconfig);
31
32 let bussel = me.down('field[name=controller]');
33 let deviceid = me.down('field[name=deviceid]');
34
35 let clist;
36 if (autoSelect === 'cdrom') {
37 if (!Ext.isDefined(me.vmconfig.ide2)) {
38 bussel.setValue('ide');
39 deviceid.setValue(2);
40 return;
41 }
42 clist = ['ide', 'scsi', 'sata'];
43 } else {
44 // in most cases we want to add a disk to the same controller we previously used
45 clist = PVE.Utils.sortByPreviousUsage(me.vmconfig);
46 }
47
48 me.setToFree(clist, bussel, deviceid);
49
50 deviceid.validate();
51 },
52
53 getConfId: function() {
54 let me = this;
55 let controller = me.getComponent('controller').getValue() || 'ide';
56 let id = me.getComponent('deviceid').getValue() || 0;
57
58 return `${controller}${id}`;
59 },
60
61 initComponent: function() {
62 var me = this;
63
64 Ext.apply(me, {
65 fieldLabel: gettext('Bus/Device'),
66 layout: 'hbox',
67 defaults: {
68 hideLabel: true,
69 },
70 items: [
71 {
72 xtype: 'pveBusSelector',
73 name: 'controller',
74 itemId: 'controller',
75 value: PVE.qemu.OSDefaults.generic.busType,
76 noVirtIO: me.noVirtIO,
77 withUnused: me.withUnused,
78 allowBlank: false,
79 flex: 2,
80 listeners: {
81 change: function(t, value) {
82 if (!value) {
83 return;
84 }
85 let field = me.down('field[name=deviceid]');
86 field.setMaxValue(PVE.Utils.diskControllerMaxIDs[value] - 1);
87 field.validate();
88 },
89 },
90 },
91 {
92 xtype: 'proxmoxintegerfield',
93 name: 'deviceid',
94 itemId: 'deviceid',
95 minValue: 0,
96 maxValue: PVE.Utils.diskControllerMaxIDs.ide - 1,
97 value: '0',
98 flex: 1,
99 allowBlank: false,
100 validator: function(value) {
101 if (!me.rendered) {
102 return undefined;
103 }
104 let controller = me.down('field[name=controller]').getValue();
105 let confid = controller + value;
106 if (Ext.isDefined(me.vmconfig[confid])) {
107 return "This device is already in use.";
108 }
109 return true;
110 },
111 },
112 ],
113 });
114
115 me.callParent();
116
117 if (me.selectFree) {
118 me.setVMConfig(me.vmconfig);
119 }
120 },
121 });