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