]>
Commit | Line | Data |
---|---|---|
03ab9fba DM |
1 | Ext.define('PVE.IPSetList', { |
2 | extend: 'Ext.grid.Panel', | |
3 | alias: 'widget.pveIPSetList', | |
4 | ||
5 | ipset_panel: undefined, | |
6 | ||
7 | base_url: undefined, | |
8 | ||
9 | addBtn: undefined, | |
10 | removeBtn: undefined, | |
11 | editBtn: undefined, | |
12 | ||
13 | initComponent: function() { | |
14 | /*jslint confusion: true */ | |
15 | var me = this; | |
16 | ||
17 | if (me.ipset_panel == undefined) { | |
18 | throw "no rule panel specified"; | |
19 | } | |
20 | ||
21 | if (me.base_url == undefined) { | |
22 | throw "no base_url specified"; | |
23 | } | |
24 | ||
25 | var store = new Ext.data.Store({ | |
26 | fields: [ 'name', 'comment', 'digest' ], | |
27 | proxy: { | |
28 | type: 'pve', | |
29 | url: "/api2/json" + me.base_url | |
30 | }, | |
31 | idProperty: 'name', | |
32 | sorters: { | |
33 | property: 'name', | |
34 | order: 'DESC' | |
35 | } | |
36 | }); | |
37 | ||
38 | var sm = Ext.create('Ext.selection.RowModel', {}); | |
39 | ||
40 | var reload = function() { | |
41 | var oldrec = sm.getSelection()[0]; | |
42 | store.load(function(records, operation, success) { | |
43 | if (oldrec) { | |
44 | var rec = store.findRecord('name', oldrec.data.name); | |
45 | if (rec) { | |
46 | sm.select(rec); | |
47 | } | |
48 | } | |
49 | }); | |
50 | }; | |
51 | ||
52 | var run_editor = function() { | |
53 | var rec = sm.getSelection()[0]; | |
54 | if (!rec) { | |
55 | return; | |
56 | } | |
57 | var win = Ext.create('PVE.window.Edit', { | |
58 | subject: "IPSet '" + rec.data.name + "'", | |
59 | url: me.base_url, | |
60 | method: 'POST', | |
61 | digest: rec.data.digest, | |
62 | items: [ | |
63 | { | |
64 | xtype: 'hiddenfield', | |
65 | name: 'rename', | |
66 | value: rec.data.name | |
67 | }, | |
68 | { | |
69 | xtype: 'textfield', | |
70 | name: 'name', | |
71 | value: rec.data.name, | |
72 | fieldLabel: gettext('Name'), | |
73 | allowBlank: false | |
74 | }, | |
75 | { | |
76 | xtype: 'textfield', | |
77 | name: 'comment', | |
78 | value: rec.data.comment, | |
79 | fieldLabel: gettext('Comment') | |
80 | } | |
81 | ] | |
82 | }); | |
83 | win.show(); | |
84 | win.on('destroy', reload); | |
85 | }; | |
86 | ||
87 | me.editBtn = new PVE.button.Button({ | |
88 | text: gettext('Edit'), | |
89 | disabled: true, | |
90 | selModel: sm, | |
91 | handler: run_editor | |
92 | }); | |
93 | ||
94 | me.addBtn = new PVE.button.Button({ | |
95 | text: gettext('Create'), | |
96 | handler: function() { | |
97 | sm.deselectAll(); | |
98 | var win = Ext.create('PVE.window.Edit', { | |
99 | subject: 'IPSet', | |
100 | url: me.base_url, | |
101 | method: 'POST', | |
102 | items: [ | |
103 | { | |
104 | xtype: 'textfield', | |
105 | name: 'name', | |
106 | value: '', | |
107 | fieldLabel: gettext('Name'), | |
108 | allowBlank: false | |
109 | }, | |
110 | { | |
111 | xtype: 'textfield', | |
112 | name: 'comment', | |
113 | value: '', | |
114 | fieldLabel: gettext('Comment') | |
115 | } | |
116 | ] | |
117 | }); | |
118 | win.show(); | |
119 | win.on('destroy', reload); | |
120 | ||
121 | } | |
122 | }); | |
123 | ||
124 | me.removeBtn = new PVE.button.Button({ | |
125 | text: gettext('Remove'), | |
126 | selModel: sm, | |
127 | disabled: true, | |
128 | handler: function() { | |
129 | var rec = sm.getSelection()[0]; | |
130 | if (!rec || !me.base_url) { | |
131 | return; | |
132 | } | |
133 | PVE.Utils.API2Request({ | |
134 | url: me.base_url + '/' + rec.data.name, | |
135 | method: 'DELETE', | |
136 | waitMsgTarget: me, | |
137 | failure: function(response, options) { | |
138 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
139 | }, | |
140 | callback: reload | |
141 | }); | |
142 | } | |
143 | }); | |
144 | ||
145 | Ext.apply(me, { | |
146 | store: store, | |
147 | tbar: [ '<b>IPSet:</b>', me.addBtn, me.removeBtn, me.editBtn ], | |
148 | selModel: sm, | |
149 | columns: [ | |
150 | { header: 'IPSet', dataIndex: 'name', width: 100 }, | |
2eb0767b | 151 | { header: gettext('Comment'), dataIndex: 'comment', renderer: Ext.String.htmlEncode, flex: 1 } |
03ab9fba DM |
152 | ], |
153 | listeners: { | |
154 | itemdblclick: run_editor, | |
155 | select: function(sm, rec) { | |
156 | var url = me.base_url + '/' + rec.data.name; | |
157 | me.ipset_panel.setBaseUrl(url); | |
158 | }, | |
159 | deselect: function() { | |
160 | me.ipset_panel.setBaseUrl(undefined); | |
161 | }, | |
162 | show: reload | |
163 | } | |
164 | }); | |
165 | ||
166 | me.callParent(); | |
167 | ||
168 | store.load(); | |
169 | } | |
170 | }); | |
171 | ||
172 | Ext.define('PVE.IPSetCidrEdit', { | |
173 | extend: 'PVE.window.Edit', | |
174 | ||
175 | cidr: undefined, | |
176 | ||
177 | initComponent : function() { | |
178 | /*jslint confusion: true */ | |
179 | var me = this; | |
180 | ||
181 | me.create = (me.cidr === undefined); | |
182 | ||
183 | ||
184 | if (me.create) { | |
185 | me.url = '/api2/extjs' + me.base_url; | |
186 | me.method = 'POST'; | |
187 | } else { | |
188 | me.url = '/api2/extjs' + me.base_url + '/' + me.cidr; | |
189 | me.method = 'PUT'; | |
190 | } | |
191 | ||
192 | var column1 = []; | |
193 | ||
194 | if (me.create) { | |
195 | if (!me.list_refs_url) { | |
196 | throw "no alias_base_url specified"; | |
197 | } | |
198 | ||
199 | column1.push({ | |
200 | xtype: 'pveIPRefSelector', | |
201 | name: 'cidr', | |
202 | ref_type: 'alias', | |
203 | autoSelect: false, | |
204 | editable: true, | |
205 | base_url: me.list_refs_url, | |
206 | value: '', | |
207 | fieldLabel: gettext('IP/CIDR') | |
208 | }); | |
209 | } else { | |
210 | column1.push({ | |
211 | xtype: 'displayfield', | |
212 | name: 'cidr', | |
03ab9fba DM |
213 | value: '', |
214 | fieldLabel: gettext('IP/CIDR') | |
215 | }); | |
216 | } | |
217 | ||
218 | var ipanel = Ext.create('PVE.panel.InputPanel', { | |
219 | create: me.create, | |
220 | column1: column1, | |
221 | column2: [ | |
222 | { | |
223 | xtype: 'pvecheckbox', | |
224 | name: 'nomatch', | |
225 | checked: false, | |
03ab9fba | 226 | uncheckedValue: 0, |
185a77e5 | 227 | fieldLabel: 'nomatch' |
03ab9fba DM |
228 | } |
229 | ], | |
230 | columnB: [ | |
231 | { | |
232 | xtype: 'textfield', | |
233 | name: 'comment', | |
234 | value: '', | |
235 | fieldLabel: gettext('Comment') | |
236 | } | |
237 | ] | |
238 | }); | |
239 | ||
240 | Ext.apply(me, { | |
241 | subject: gettext('IP/CIDR'), | |
242 | items: [ ipanel ] | |
243 | }); | |
244 | ||
245 | me.callParent(); | |
246 | ||
247 | if (!me.create) { | |
248 | me.load({ | |
249 | success: function(response, options) { | |
250 | var values = response.result.data; | |
251 | ipanel.setValues(values); | |
252 | } | |
253 | }); | |
254 | } | |
255 | } | |
256 | }); | |
257 | ||
258 | Ext.define('PVE.IPSetGrid', { | |
259 | extend: 'Ext.grid.Panel', | |
260 | alias: 'widget.pveIPSetGrid', | |
261 | ||
262 | base_url: undefined, | |
263 | list_refs_url: undefined, | |
264 | ||
265 | addBtn: undefined, | |
266 | removeBtn: undefined, | |
267 | editBtn: undefined, | |
268 | ||
269 | setBaseUrl: function(url) { | |
270 | var me = this; | |
271 | ||
272 | me.base_url = url; | |
273 | ||
274 | if (url === undefined) { | |
275 | me.addBtn.setDisabled(true); | |
276 | me.store.removeAll(); | |
277 | } else { | |
278 | me.addBtn.setDisabled(false); | |
279 | me.store.setProxy({ | |
280 | type: 'pve', | |
281 | url: '/api2/json' + url | |
282 | }); | |
283 | ||
284 | me.store.load(); | |
285 | } | |
286 | }, | |
287 | ||
288 | initComponent: function() { | |
289 | /*jslint confusion: true */ | |
290 | var me = this; | |
291 | ||
292 | if (!me.list_refs_url) { | |
293 | throw "no1 list_refs_url specified"; | |
294 | } | |
295 | ||
296 | var store = new Ext.data.Store({ | |
297 | model: 'pve-ipset' | |
298 | }); | |
299 | ||
300 | var reload = function() { | |
301 | store.load(); | |
302 | }; | |
303 | ||
304 | var sm = Ext.create('Ext.selection.RowModel', {}); | |
305 | ||
306 | var run_editor = function() { | |
307 | var rec = sm.getSelection()[0]; | |
308 | if (!rec) { | |
309 | return; | |
310 | } | |
311 | var win = Ext.create('PVE.IPSetCidrEdit', { | |
312 | base_url: me.base_url, | |
313 | cidr: rec.data.cidr | |
314 | }); | |
315 | win.show(); | |
316 | win.on('destroy', reload); | |
317 | }; | |
318 | ||
319 | me.editBtn = new PVE.button.Button({ | |
320 | text: gettext('Edit'), | |
321 | disabled: true, | |
322 | selModel: sm, | |
323 | handler: run_editor | |
324 | }); | |
325 | ||
326 | me.addBtn = new PVE.button.Button({ | |
327 | text: gettext('Add'), | |
328 | disabled: true, | |
329 | handler: function() { | |
330 | if (!me.base_url) { | |
331 | return; | |
332 | } | |
333 | var win = Ext.create('PVE.IPSetCidrEdit', { | |
334 | base_url: me.base_url, | |
335 | list_refs_url: me.list_refs_url | |
336 | }); | |
337 | win.show(); | |
338 | win.on('destroy', reload); | |
339 | } | |
340 | }); | |
341 | ||
342 | me.removeBtn = new PVE.button.Button({ | |
343 | text: gettext('Remove'), | |
344 | selModel: sm, | |
345 | disabled: true, | |
346 | handler: function() { | |
347 | var rec = sm.getSelection()[0]; | |
348 | if (!rec || !me.base_url) { | |
349 | return; | |
350 | } | |
351 | ||
352 | PVE.Utils.API2Request({ | |
353 | url: me.base_url + '/' + rec.data.cidr, | |
354 | method: 'DELETE', | |
355 | waitMsgTarget: me, | |
356 | failure: function(response, options) { | |
357 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
358 | }, | |
359 | callback: reload | |
360 | }); | |
361 | } | |
362 | }); | |
363 | ||
364 | var render_errors = function(value, metaData, record) { | |
365 | var errors = record.data.errors; | |
366 | if (errors) { | |
367 | var msg = errors.cidr || errors.nomatch; | |
368 | if (msg) { | |
369 | metaData.tdCls = 'x-form-invalid-field'; | |
370 | var html = '<p>' + Ext.htmlEncode(msg) + '</p>'; | |
371 | metaData.tdAttr = 'data-qwidth=600 data-qtitle="ERROR" data-qtip="' + | |
372 | html.replace(/\"/g,'"') + '"'; | |
373 | } | |
374 | } | |
375 | return value; | |
376 | }; | |
377 | ||
378 | Ext.apply(me, { | |
379 | tbar: [ '<b>IP/CIDR:</b>', me.addBtn, me.removeBtn, me.editBtn ], | |
380 | store: store, | |
381 | selModel: sm, | |
382 | listeners: { | |
383 | itemdblclick: run_editor | |
384 | }, | |
385 | columns: [ | |
386 | { | |
387 | xtype: 'rownumberer' | |
388 | }, | |
389 | { | |
390 | header: gettext('IP/CIDR'), | |
391 | dataIndex: 'cidr', | |
392 | width: 150, | |
393 | renderer: function(value, metaData, record) { | |
394 | value = render_errors(value, metaData, record); | |
395 | if (record.data.nomatch) { | |
396 | return '<b>! </b>' + value; | |
397 | } | |
398 | return value; | |
399 | } | |
400 | }, | |
401 | { | |
402 | header: gettext('Comment'), | |
403 | dataIndex: 'comment', | |
404 | flex: 1, | |
405 | renderer: function(value) { | |
406 | return Ext.util.Format.htmlEncode(value); | |
407 | } | |
408 | } | |
409 | ] | |
410 | }); | |
411 | ||
412 | me.callParent(); | |
413 | ||
414 | if (me.base_url) { | |
415 | me.setBaseUrl(me.base_url); // load | |
416 | } | |
417 | } | |
418 | }, function() { | |
419 | ||
420 | Ext.define('pve-ipset', { | |
421 | extend: 'Ext.data.Model', | |
422 | fields: [ { name: 'nomatch', type: 'boolean' }, | |
423 | 'cidr', 'comment', 'errors' ], | |
424 | idProperty: 'cidr' | |
425 | }); | |
426 | ||
427 | }); | |
428 | ||
429 | Ext.define('PVE.IPSet', { | |
430 | extend: 'Ext.panel.Panel', | |
431 | alias: 'widget.pveIPSet', | |
432 | ||
433 | title: 'IPSet', | |
434 | ||
435 | list_refs_url: undefined, | |
436 | ||
437 | initComponent: function() { | |
438 | var me = this; | |
439 | ||
440 | if (!me.list_refs_url) { | |
441 | throw "no list_refs_url specified"; | |
442 | } | |
443 | ||
444 | var ipset_panel = Ext.createWidget('pveIPSetGrid', { | |
445 | region: 'center', | |
446 | list_refs_url: me.list_refs_url, | |
447 | flex: 0.5, | |
448 | border: false | |
449 | }); | |
450 | ||
451 | var ipset_list = Ext.createWidget('pveIPSetList', { | |
452 | region: 'west', | |
453 | ipset_panel: ipset_panel, | |
454 | base_url: me.base_url, | |
455 | flex: 0.5, | |
456 | border: false, | |
457 | split: true | |
458 | }); | |
459 | ||
460 | Ext.apply(me, { | |
461 | layout: 'border', | |
462 | items: [ ipset_list, ipset_panel ], | |
463 | listeners: { | |
464 | show: function() { | |
465 | ipset_list.fireEvent('show', ipset_list); | |
466 | } | |
467 | } | |
468 | }); | |
469 | ||
470 | me.callParent(); | |
471 | } | |
472 | }); |