]>
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', | |
213 | height: 22, // hack: set same height as text fields | |
214 | value: '', | |
215 | fieldLabel: gettext('IP/CIDR') | |
216 | }); | |
217 | } | |
218 | ||
219 | var ipanel = Ext.create('PVE.panel.InputPanel', { | |
220 | create: me.create, | |
221 | column1: column1, | |
222 | column2: [ | |
223 | { | |
224 | xtype: 'pvecheckbox', | |
225 | name: 'nomatch', | |
226 | checked: false, | |
227 | height: 22, // hack: set same height as text fields | |
228 | uncheckedValue: 0, | |
185a77e5 | 229 | fieldLabel: 'nomatch' |
03ab9fba DM |
230 | } |
231 | ], | |
232 | columnB: [ | |
233 | { | |
234 | xtype: 'textfield', | |
235 | name: 'comment', | |
236 | value: '', | |
237 | fieldLabel: gettext('Comment') | |
238 | } | |
239 | ] | |
240 | }); | |
241 | ||
242 | Ext.apply(me, { | |
243 | subject: gettext('IP/CIDR'), | |
244 | items: [ ipanel ] | |
245 | }); | |
246 | ||
247 | me.callParent(); | |
248 | ||
249 | if (!me.create) { | |
250 | me.load({ | |
251 | success: function(response, options) { | |
252 | var values = response.result.data; | |
253 | ipanel.setValues(values); | |
254 | } | |
255 | }); | |
256 | } | |
257 | } | |
258 | }); | |
259 | ||
260 | Ext.define('PVE.IPSetGrid', { | |
261 | extend: 'Ext.grid.Panel', | |
262 | alias: 'widget.pveIPSetGrid', | |
263 | ||
264 | base_url: undefined, | |
265 | list_refs_url: undefined, | |
266 | ||
267 | addBtn: undefined, | |
268 | removeBtn: undefined, | |
269 | editBtn: undefined, | |
270 | ||
271 | setBaseUrl: function(url) { | |
272 | var me = this; | |
273 | ||
274 | me.base_url = url; | |
275 | ||
276 | if (url === undefined) { | |
277 | me.addBtn.setDisabled(true); | |
278 | me.store.removeAll(); | |
279 | } else { | |
280 | me.addBtn.setDisabled(false); | |
281 | me.store.setProxy({ | |
282 | type: 'pve', | |
283 | url: '/api2/json' + url | |
284 | }); | |
285 | ||
286 | me.store.load(); | |
287 | } | |
288 | }, | |
289 | ||
290 | initComponent: function() { | |
291 | /*jslint confusion: true */ | |
292 | var me = this; | |
293 | ||
294 | if (!me.list_refs_url) { | |
295 | throw "no1 list_refs_url specified"; | |
296 | } | |
297 | ||
298 | var store = new Ext.data.Store({ | |
299 | model: 'pve-ipset' | |
300 | }); | |
301 | ||
302 | var reload = function() { | |
303 | store.load(); | |
304 | }; | |
305 | ||
306 | var sm = Ext.create('Ext.selection.RowModel', {}); | |
307 | ||
308 | var run_editor = function() { | |
309 | var rec = sm.getSelection()[0]; | |
310 | if (!rec) { | |
311 | return; | |
312 | } | |
313 | var win = Ext.create('PVE.IPSetCidrEdit', { | |
314 | base_url: me.base_url, | |
315 | cidr: rec.data.cidr | |
316 | }); | |
317 | win.show(); | |
318 | win.on('destroy', reload); | |
319 | }; | |
320 | ||
321 | me.editBtn = new PVE.button.Button({ | |
322 | text: gettext('Edit'), | |
323 | disabled: true, | |
324 | selModel: sm, | |
325 | handler: run_editor | |
326 | }); | |
327 | ||
328 | me.addBtn = new PVE.button.Button({ | |
329 | text: gettext('Add'), | |
330 | disabled: true, | |
331 | handler: function() { | |
332 | if (!me.base_url) { | |
333 | return; | |
334 | } | |
335 | var win = Ext.create('PVE.IPSetCidrEdit', { | |
336 | base_url: me.base_url, | |
337 | list_refs_url: me.list_refs_url | |
338 | }); | |
339 | win.show(); | |
340 | win.on('destroy', reload); | |
341 | } | |
342 | }); | |
343 | ||
344 | me.removeBtn = new PVE.button.Button({ | |
345 | text: gettext('Remove'), | |
346 | selModel: sm, | |
347 | disabled: true, | |
348 | handler: function() { | |
349 | var rec = sm.getSelection()[0]; | |
350 | if (!rec || !me.base_url) { | |
351 | return; | |
352 | } | |
353 | ||
354 | PVE.Utils.API2Request({ | |
355 | url: me.base_url + '/' + rec.data.cidr, | |
356 | method: 'DELETE', | |
357 | waitMsgTarget: me, | |
358 | failure: function(response, options) { | |
359 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
360 | }, | |
361 | callback: reload | |
362 | }); | |
363 | } | |
364 | }); | |
365 | ||
366 | var render_errors = function(value, metaData, record) { | |
367 | var errors = record.data.errors; | |
368 | if (errors) { | |
369 | var msg = errors.cidr || errors.nomatch; | |
370 | if (msg) { | |
371 | metaData.tdCls = 'x-form-invalid-field'; | |
372 | var html = '<p>' + Ext.htmlEncode(msg) + '</p>'; | |
373 | metaData.tdAttr = 'data-qwidth=600 data-qtitle="ERROR" data-qtip="' + | |
374 | html.replace(/\"/g,'"') + '"'; | |
375 | } | |
376 | } | |
377 | return value; | |
378 | }; | |
379 | ||
380 | Ext.apply(me, { | |
381 | tbar: [ '<b>IP/CIDR:</b>', me.addBtn, me.removeBtn, me.editBtn ], | |
382 | store: store, | |
383 | selModel: sm, | |
384 | listeners: { | |
385 | itemdblclick: run_editor | |
386 | }, | |
387 | columns: [ | |
388 | { | |
389 | xtype: 'rownumberer' | |
390 | }, | |
391 | { | |
392 | header: gettext('IP/CIDR'), | |
393 | dataIndex: 'cidr', | |
394 | width: 150, | |
395 | renderer: function(value, metaData, record) { | |
396 | value = render_errors(value, metaData, record); | |
397 | if (record.data.nomatch) { | |
398 | return '<b>! </b>' + value; | |
399 | } | |
400 | return value; | |
401 | } | |
402 | }, | |
403 | { | |
404 | header: gettext('Comment'), | |
405 | dataIndex: 'comment', | |
406 | flex: 1, | |
407 | renderer: function(value) { | |
408 | return Ext.util.Format.htmlEncode(value); | |
409 | } | |
410 | } | |
411 | ] | |
412 | }); | |
413 | ||
414 | me.callParent(); | |
415 | ||
416 | if (me.base_url) { | |
417 | me.setBaseUrl(me.base_url); // load | |
418 | } | |
419 | } | |
420 | }, function() { | |
421 | ||
422 | Ext.define('pve-ipset', { | |
423 | extend: 'Ext.data.Model', | |
424 | fields: [ { name: 'nomatch', type: 'boolean' }, | |
425 | 'cidr', 'comment', 'errors' ], | |
426 | idProperty: 'cidr' | |
427 | }); | |
428 | ||
429 | }); | |
430 | ||
431 | Ext.define('PVE.IPSet', { | |
432 | extend: 'Ext.panel.Panel', | |
433 | alias: 'widget.pveIPSet', | |
434 | ||
435 | title: 'IPSet', | |
436 | ||
437 | list_refs_url: undefined, | |
438 | ||
439 | initComponent: function() { | |
440 | var me = this; | |
441 | ||
442 | if (!me.list_refs_url) { | |
443 | throw "no list_refs_url specified"; | |
444 | } | |
445 | ||
446 | var ipset_panel = Ext.createWidget('pveIPSetGrid', { | |
447 | region: 'center', | |
448 | list_refs_url: me.list_refs_url, | |
449 | flex: 0.5, | |
450 | border: false | |
451 | }); | |
452 | ||
453 | var ipset_list = Ext.createWidget('pveIPSetList', { | |
454 | region: 'west', | |
455 | ipset_panel: ipset_panel, | |
456 | base_url: me.base_url, | |
457 | flex: 0.5, | |
458 | border: false, | |
459 | split: true | |
460 | }); | |
461 | ||
462 | Ext.apply(me, { | |
463 | layout: 'border', | |
464 | items: [ ipset_list, ipset_panel ], | |
465 | listeners: { | |
466 | show: function() { | |
467 | ipset_list.fireEvent('show', ipset_list); | |
468 | } | |
469 | } | |
470 | }); | |
471 | ||
472 | me.callParent(); | |
473 | } | |
474 | }); |