]>
Commit | Line | Data |
---|---|---|
1 | Ext.define('PMG.RuleInfo', { | |
2 | extend: 'Ext.panel.Panel', | |
3 | xtype: 'pmgRuleInfo', | |
4 | ||
5 | controller: { | |
6 | xclass: 'Ext.app.ViewController', | |
7 | ||
8 | setBaseUrl: function(baseurl) { | |
9 | var me = this; | |
10 | me.getViewModel().set('baseurl', baseurl); | |
11 | me.reload(); | |
12 | }, | |
13 | ||
14 | reload: function() { | |
15 | var me = this; | |
16 | var viewmodel = me.getViewModel(); | |
17 | var baseurl = viewmodel.get('baseurl'); | |
18 | ||
19 | if (!baseurl) { | |
20 | me.setRuleInfo(undefined); | |
21 | return; | |
22 | } | |
23 | ||
24 | Proxmox.Utils.API2Request({ | |
25 | url: baseurl + "/config", | |
26 | method: 'GET', | |
27 | success: function(response, opts) { | |
28 | me.setRuleInfo(response.result.data); | |
29 | }, | |
30 | failure: function(response, opts) { | |
31 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
32 | }, | |
33 | }); | |
34 | }, | |
35 | ||
36 | removeObjectGroup: function(rec) { | |
37 | var me = this; | |
38 | Ext.Msg.confirm( | |
39 | gettext('Confirm'), | |
40 | Ext.String.format( | |
41 | gettext('Are you sure you want to remove entry {0}'), | |
42 | "'" + rec.data.name + "'"), | |
43 | function(button) { | |
44 | if (button === 'yes') { | |
45 | Proxmox.Utils.API2Request({ | |
46 | url: me.getViewModel().get('baseurl') + '/' + rec.data.oclass + '/'+ rec.data.typeid, | |
47 | method: 'DELETE', | |
48 | waitMsgTarget: me.getView(), | |
49 | callback: function() { | |
50 | me.reload(); | |
51 | }, | |
52 | failure: function(response, opts) { | |
53 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
54 | }, | |
55 | }); | |
56 | } | |
57 | }, | |
58 | ); | |
59 | }, | |
60 | ||
61 | addObjectGroup: function(type, record) { | |
62 | var me = this; | |
63 | var baseurl = me.getViewModel().get('baseurl'); | |
64 | var url = baseurl + '/' + type; | |
65 | var id = type === 'action'?record.data.ogroup:record.data.id; | |
66 | Proxmox.Utils.API2Request({ | |
67 | url: url, | |
68 | params: { ogroup: id }, | |
69 | method: 'POST', | |
70 | waitMsgTarget: me.getView(), | |
71 | callback: function() { | |
72 | me.reload(); | |
73 | }, | |
74 | failure: function(response, opts) { | |
75 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
76 | }, | |
77 | }); | |
78 | }, | |
79 | ||
80 | setRuleInfo: function(ruledata) { | |
81 | var me = this; | |
82 | ||
83 | var viewmodel = me.getViewModel(); | |
84 | ||
85 | if (ruledata === undefined) { | |
86 | viewmodel.set('selectedRule', null); | |
87 | viewmodel.get('objects').setData([]); | |
88 | } else { | |
89 | ruledata.name = Ext.String.htmlEncode(ruledata.name); | |
90 | viewmodel.set('selectedRule', ruledata); | |
91 | ||
92 | var data = []; | |
93 | Ext.Array.each(['from', 'to', 'when', 'what', 'action'], function(oc) { | |
94 | var store = viewmodel.get(oc + 'objects'); | |
95 | if (ruledata[oc] === undefined || store === undefined) { return; } | |
96 | ||
97 | // we build a filter for the objects, | |
98 | // which are already added to the rule, | |
99 | // so what we only show the ones, | |
100 | // which are still available | |
101 | ||
102 | var ids = Ext.Array.pluck(ruledata[oc], 'id'); | |
103 | // for the actions, we have a different id field | |
104 | var idField = oc === 'action'?'ogroup':'id'; | |
105 | store.clearFilter(); | |
106 | store.addFilter({ | |
107 | filterFn: function(record) { | |
108 | // FIXME | |
109 | // actions have the ogroup as a string | |
110 | // -> parseInt | |
111 | return ids.indexOf(parseInt(record.data[idField], 10)) === -1; | |
112 | }, | |
113 | }); | |
114 | store.load(); | |
115 | Ext.Array.each(ruledata[oc], function(og) { | |
116 | data.push({ oclass: oc, name: og.name, typeid: og.id }); | |
117 | }); | |
118 | }); | |
119 | ||
120 | viewmodel.get('objects').setData(data); | |
121 | } | |
122 | }, | |
123 | ||
124 | removeIconClick: function(gridView, rowindex, colindex, button, event, record) { | |
125 | var me = this; | |
126 | me.removeObjectGroup(record); | |
127 | }, | |
128 | ||
129 | removeDrop: function(gridView, data, overModel) { | |
130 | var me = this; | |
131 | var record = data.records[0]; // only one | |
132 | me.removeObjectGroup(record); | |
133 | return true; | |
134 | }, | |
135 | ||
136 | addIconClick: function(gridView, rowindex, colindex, button, event, record) { | |
137 | var me = this; | |
138 | me.addObjectGroup(gridView.panel.type, record); | |
139 | return true; | |
140 | }, | |
141 | ||
142 | addDrop: function(gridView, data, overModel) { | |
143 | var me = this; | |
144 | var record = data.records[0]; // only one | |
145 | me.addObjectGroup(data.view.panel.type, record); | |
146 | return true; | |
147 | }, | |
148 | ||
149 | control: { | |
150 | 'grid[reference=usedobjects]': { | |
151 | drop: 'addDrop', | |
152 | }, | |
153 | 'tabpanel[reference=availobjects] > grid': { | |
154 | drop: 'removeDrop', | |
155 | }, | |
156 | }, | |
157 | }, | |
158 | ||
159 | viewModel: { | |
160 | data: { | |
161 | baseurl: '', | |
162 | }, | |
163 | ||
164 | stores: { | |
165 | objects: { | |
166 | fields: ['oclass', 'name', 'typeid'], | |
167 | groupField: 'oclass', | |
168 | sorters: 'name', | |
169 | }, | |
170 | ||
171 | actionobjects: { | |
172 | model: 'pmg-action-list', | |
173 | proxy: { | |
174 | type: 'proxmox', | |
175 | url: "/api2/json/config/ruledb/action/objects", | |
176 | }, | |
177 | sorters: 'name', | |
178 | }, | |
179 | fromobjects: { | |
180 | model: 'pmg-object-group', | |
181 | proxy: { | |
182 | type: 'proxmox', | |
183 | url: "/api2/json/config/ruledb/who", | |
184 | }, | |
185 | sorters: 'name', | |
186 | }, | |
187 | toobjects: { | |
188 | model: 'pmg-object-group', | |
189 | proxy: { | |
190 | type: 'proxmox', | |
191 | url: "/api2/json/config/ruledb/who", | |
192 | }, | |
193 | sorters: 'name', | |
194 | }, | |
195 | whatobjects: { | |
196 | model: 'pmg-object-group', | |
197 | proxy: { | |
198 | type: 'proxmox', | |
199 | url: "/api2/json/config/ruledb/what", | |
200 | }, | |
201 | sorters: 'name', | |
202 | }, | |
203 | whenobjects: { | |
204 | model: 'pmg-object-group', | |
205 | proxy: { | |
206 | type: 'proxmox', | |
207 | url: "/api2/json/config/ruledb/when", | |
208 | }, | |
209 | sorters: 'name', | |
210 | }, | |
211 | }, | |
212 | }, | |
213 | ||
214 | ||
215 | defaults: { | |
216 | padding: '5 10 5 10', | |
217 | }, | |
218 | ||
219 | bodyPadding: '5 0 5 0', | |
220 | ||
221 | layout: { | |
222 | type: 'vbox', | |
223 | align: 'stretch', | |
224 | }, | |
225 | ||
226 | scrollable: true, | |
227 | ||
228 | items: [ | |
229 | { | |
230 | xtype: 'panel', | |
231 | bodyPadding: '10 10 10 10', | |
232 | data: { | |
233 | name: '', | |
234 | }, | |
235 | bind: { | |
236 | data: { | |
237 | name: '{selectedRule.name}', | |
238 | priority: '{selectedRule.priority}', | |
239 | active: '{selectedRule.active}', | |
240 | direction: '{selectedRule.direction}', | |
241 | selected: '{selectedRule}', | |
242 | }, | |
243 | }, | |
244 | tpl: [ | |
245 | '<tpl if="selected">', | |
246 | '<b>{name}</b><br><br>', | |
247 | gettext('Priority') + ': {priority}<br>', | |
248 | gettext('Direction') + ': {[PMG.Utils.format_rule_direction(values.direction)]}<br>', | |
249 | gettext('Active') + ': {[Proxmox.Utils.format_boolean(values.active)]}<br>', | |
250 | '<tpl else>', | |
251 | gettext('Please select a rule.'), | |
252 | '</tpl>', | |
253 | ], | |
254 | }, | |
255 | { | |
256 | xtype: 'grid', | |
257 | reference: 'usedobjects', | |
258 | hidden: true, | |
259 | emptyText: gettext('No Objects'), | |
260 | features: [{ | |
261 | id: 'group', | |
262 | ftype: 'grouping', | |
263 | enableGroupingMenu: false, | |
264 | collapsible: false, | |
265 | groupHeaderTpl: [ | |
266 | '{[PMG.Utils.format_oclass(values.name)]}', | |
267 | ], | |
268 | }], | |
269 | ||
270 | title: gettext('Used Objects'), | |
271 | ||
272 | viewConfig: { | |
273 | plugins: { | |
274 | ptype: 'gridviewdragdrop', | |
275 | copy: true, | |
276 | dragGroup: 'usedobjects', | |
277 | dropGroup: 'unusedobjects', | |
278 | ||
279 | // do not show default grid dragdrop behaviour | |
280 | dropZone: { | |
281 | indicatorHtml: '', | |
282 | indicatorCls: '', | |
283 | handleNodeDrop: Ext.emptyFn, | |
284 | }, | |
285 | }, | |
286 | }, | |
287 | ||
288 | columns: [ | |
289 | { | |
290 | header: gettext('Type'), | |
291 | dataIndex: 'oclass', | |
292 | hidden: true, | |
293 | }, | |
294 | { | |
295 | header: gettext('Name'), | |
296 | dataIndex: 'name', | |
297 | flex: 1, | |
298 | }, | |
299 | { | |
300 | text: '', | |
301 | xtype: 'actioncolumn', | |
302 | align: 'center', | |
303 | width: 40, | |
304 | items: [ | |
305 | { | |
306 | iconCls: 'fa fa-fw fa-minus-circle', | |
307 | tooltip: gettext('Remove'), | |
308 | handler: 'removeIconClick', | |
309 | }, | |
310 | ], | |
311 | }, | |
312 | ], | |
313 | ||
314 | bind: { | |
315 | store: '{objects}', | |
316 | hidden: '{!selectedRule}', | |
317 | }, | |
318 | }, | |
319 | { | |
320 | xtype: 'tabpanel', | |
321 | title: gettext('Available Objects'), | |
322 | reference: 'availobjects', | |
323 | hidden: true, | |
324 | bind: { | |
325 | hidden: '{!selectedRule}', | |
326 | }, | |
327 | defaults: { | |
328 | xtype: 'grid', | |
329 | emptyText: gettext('No Objects'), | |
330 | viewConfig: { | |
331 | plugins: { | |
332 | ptype: 'gridviewdragdrop', | |
333 | dragGroup: 'unusedobjects', | |
334 | dropGroup: 'usedobjects', | |
335 | ||
336 | // do not show default grid dragdrop behaviour | |
337 | dropZone: { | |
338 | indicatorHtml: '', | |
339 | indicatorCls: '', | |
340 | handleNodeDrop: Ext.emptyFn, | |
341 | }, | |
342 | }, | |
343 | }, | |
344 | columns: [ | |
345 | { | |
346 | header: gettext('Name'), | |
347 | dataIndex: 'name', | |
348 | flex: 1, | |
349 | }, | |
350 | { | |
351 | text: '', | |
352 | xtype: 'actioncolumn', | |
353 | align: 'center', | |
354 | width: 40, | |
355 | items: [ | |
356 | { | |
357 | iconCls: 'fa fa-fw fa-plus-circle', | |
358 | tooltip: gettext('Add'), | |
359 | handler: 'addIconClick', | |
360 | }, | |
361 | ], | |
362 | }, | |
363 | ], | |
364 | }, | |
365 | items: [ | |
366 | { | |
367 | title: gettext('Action'), | |
368 | bind: { | |
369 | store: '{actionobjects}', | |
370 | }, | |
371 | type: 'action', | |
372 | iconCls: 'fa fa-flag', | |
373 | }, | |
374 | { | |
375 | title: gettext('From'), | |
376 | iconCls: 'fa fa-user-circle', | |
377 | type: 'from', | |
378 | bind: { | |
379 | store: '{fromobjects}', | |
380 | }, | |
381 | }, | |
382 | { | |
383 | title: gettext('To'), | |
384 | iconCls: 'fa fa-user-circle', | |
385 | type: 'to', | |
386 | bind: { | |
387 | store: '{toobjects}', | |
388 | }, | |
389 | }, | |
390 | { | |
391 | title: gettext('What'), | |
392 | iconCls: 'fa fa-cube', | |
393 | type: 'what', | |
394 | bind: { | |
395 | store: '{whatobjects}', | |
396 | }, | |
397 | }, | |
398 | { | |
399 | title: gettext('When'), | |
400 | iconCls: 'fa fa-clock-o', | |
401 | type: 'when', | |
402 | bind: { | |
403 | store: '{whenobjects}', | |
404 | }, | |
405 | }, | |
406 | ], | |
407 | }, | |
408 | ], | |
409 | }); |