]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/qemu/USBEdit.js
ui: qemu/USBEdit: add 'mapped' device case
[pve-manager.git] / www / manager6 / qemu / USBEdit.js
1 Ext.define('PVE.qemu.USBInputPanel', {
2 extend: 'Proxmox.panel.InputPanel',
3 mixins: ['Proxmox.Mixin.CBind'],
4
5 autoComplete: false,
6 onlineHelp: 'qm_usb_passthrough',
7
8 cbindData: function(initialConfig) {
9 let me = this;
10 if (!me.pveSelNode) {
11 throw "no pveSelNode given";
12 }
13
14 return { nodename: me.pveSelNode.data.node };
15 },
16
17 viewModel: {
18 data: {},
19 },
20
21 setVMConfig: function(vmconfig) {
22 var me = this;
23 me.vmconfig = vmconfig;
24 let max_usb = PVE.Utils.get_max_usb_count(me.vmconfig.ostype, me.vmconfig.machine);
25 if (max_usb > PVE.Utils.hardware_counts.usb_old) {
26 me.down('field[name=usb3]').setDisabled(true);
27 }
28 },
29
30 onGetValues: function(values) {
31 var me = this;
32 if (!me.confid) {
33 let max_usb = PVE.Utils.get_max_usb_count(me.vmconfig.ostype, me.vmconfig.machine);
34 for (let i = 0; i < max_usb; i++) {
35 let id = 'usb' + i.toString();
36 if (!me.vmconfig[id]) {
37 me.confid = id;
38 break;
39 }
40 }
41 }
42 var val = "";
43 var type = me.down('radiofield').getGroupValue();
44 switch (type) {
45 case 'spice':
46 val = 'spice';
47 break;
48 case 'mapped':
49 val = `mapping=${values[type]}`;
50 delete values.mapped;
51 break;
52 case 'hostdevice':
53 case 'port':
54 val = 'host=' + values[type];
55 delete values[type];
56 break;
57 default:
58 throw "invalid type selected";
59 }
60
61 if (values.usb3) {
62 delete values.usb3;
63 val += ',usb3=1';
64 }
65 values[me.confid] = val;
66 return values;
67 },
68
69 items: [
70 {
71 xtype: 'fieldcontainer',
72 defaultType: 'radiofield',
73 layout: 'fit',
74 items: [
75 {
76 name: 'usb',
77 inputValue: 'spice',
78 boxLabel: gettext('Spice Port'),
79 submitValue: false,
80 checked: true,
81 },
82 {
83 name: 'usb',
84 inputValue: 'mapped',
85 boxLabel: gettext('Use mapped Device'),
86 reference: 'mapped',
87 submitValue: false,
88 },
89 {
90 xtype: 'pveUSBMapSelector',
91 disabled: true,
92 name: 'mapped',
93 cbind: { nodename: '{nodename}' },
94 bind: { disabled: '{!mapped.checked}' },
95 allowBlank: false,
96 fieldLabel: gettext('Choose Device'),
97 labelAlign: 'right',
98 },
99 {
100 name: 'usb',
101 inputValue: 'hostdevice',
102 boxLabel: gettext('Use USB Vendor/Device ID'),
103 reference: 'hostdevice',
104 submitValue: false,
105 },
106 {
107 xtype: 'pveUSBSelector',
108 disabled: true,
109 type: 'device',
110 name: 'hostdevice',
111 cbind: { pveSelNode: '{pveSelNode}' },
112 bind: { disabled: '{!hostdevice.checked}' },
113 editable: true,
114 allowBlank: false,
115 fieldLabel: gettext('Choose Device'),
116 labelAlign: 'right',
117 },
118 {
119 name: 'usb',
120 inputValue: 'port',
121 boxLabel: gettext('Use USB Port'),
122 reference: 'port',
123 submitValue: false,
124 },
125 {
126 xtype: 'pveUSBSelector',
127 disabled: true,
128 name: 'port',
129 cbind: { pveSelNode: '{pveSelNode}' },
130 bind: { disabled: '{!port.checked}' },
131 editable: true,
132 type: 'port',
133 allowBlank: false,
134 fieldLabel: gettext('Choose Port'),
135 labelAlign: 'right',
136 },
137 {
138 xtype: 'checkbox',
139 name: 'usb3',
140 inputValue: true,
141 checked: true,
142 reference: 'usb3',
143 fieldLabel: gettext('Use USB3'),
144 },
145 ],
146 },
147 ],
148 });
149
150 Ext.define('PVE.qemu.USBEdit', {
151 extend: 'Proxmox.window.Edit',
152
153 vmconfig: undefined,
154
155 isAdd: true,
156 width: 400,
157 subject: gettext('USB Device'),
158
159 initComponent: function() {
160 var me = this;
161
162 me.isCreate = !me.confid;
163
164 var ipanel = Ext.create('PVE.qemu.USBInputPanel', {
165 confid: me.confid,
166 pveSelNode: me.pveSelNode,
167 });
168
169 Ext.apply(me, {
170 items: [ipanel],
171 });
172
173 me.callParent();
174
175 me.load({
176 success: function(response, options) {
177 ipanel.setVMConfig(response.result.data);
178 if (me.isCreate) {
179 return;
180 }
181
182 let data = PVE.Parser.parsePropertyString(response.result.data[me.confid], 'host');
183 let port, hostdevice, mapped, usb3 = false;
184 let usb;
185
186 if (data.host) {
187 if (/^(0x)?[a-zA-Z0-9]{4}:(0x)?[a-zA-Z0-9]{4}$/.test(data.host)) {
188 hostdevice = data.host.replace('0x', '');
189 usb = 'hostdevice';
190 } else if (/^(\d+)-(\d+(\.\d+)*)$/.test(data.host)) {
191 port = data.host;
192 usb = 'port';
193 } else if (/^spice$/i.test(data.host)) {
194 usb = 'spice';
195 }
196 } else if (data.mapping) {
197 mapped = data.mapping;
198 usb = 'mapped';
199 }
200
201 usb3 = data.usb3 ?? false;
202
203 var values = {
204 usb,
205 hostdevice,
206 port,
207 usb3,
208 mapped,
209 };
210
211 ipanel.setValues(values);
212 },
213 });
214 },
215 });