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