]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/qemu/USBEdit.js
b25f45e69f5ccbc696040e717b052a4ffaf1d3be
[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'; break;
59 case 'hostdevice':
60 case 'port':
61 val = me.down('pveUSBSelector[name=' + type + ']').getUSBValue();
62 break;
63 default:
64 throw "invalid type selected";
65 }
66
67 if (values.usb3) {
68 delete values.usb3;
69 val += ',usb3=1';
70 }
71 values[me.confid] = val;
72 return values;
73 },
74
75 items: [
76 {
77 xtype: 'fieldcontainer',
78 defaultType: 'radiofield',
79 items:[
80 {
81 name: 'usb',
82 inputValue: 'spice',
83 boxLabel: gettext('Spice Port'),
84 submitValue: false,
85 checked: true
86 },
87 {
88 name: 'usb',
89 inputValue: 'hostdevice',
90 boxLabel: gettext('Use USB Vendor/Device ID'),
91 reference: 'hostdevice',
92 submitValue: false
93 },
94 {
95 xtype: 'pveUSBSelector',
96 disabled: true,
97 type: 'device',
98 name: 'hostdevice',
99 cbind: { pveSelNode: '{pveSelNode}' },
100 bind: { disabled: '{!hostdevice.checked}' },
101 editable: true,
102 allowBlank: false,
103 fieldLabel: 'Choose Device',
104 labelAlign: 'right',
105 submitValue: false
106 },
107 {
108 name: 'usb',
109 inputValue: 'port',
110 boxLabel: gettext('Use USB Port'),
111 reference: 'port',
112 submitValue: false
113 },
114 {
115 xtype: 'pveUSBSelector',
116 disabled: true,
117 name: 'port',
118 cbind: { pveSelNode: '{pveSelNode}' },
119 bind: { disabled: '{!port.checked}' },
120 editable: true,
121 type: 'port',
122 allowBlank: false,
123 fieldLabel: gettext('Choose Port'),
124 labelAlign: 'right',
125 submitValue: false
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 });