]> git.proxmox.com Git - pmg-gui.git/blame - js/RuleInfo.js
fix #5251: login: set autocomplete on password and user
[pmg-gui.git] / js / RuleInfo.js
CommitLineData
ad834b6f 1Ext.define('PMG.RuleInfo', {
c3339ea1
DC
2 extend: 'Ext.panel.Panel',
3 xtype: 'pmgRuleInfo',
ad834b6f 4
c3339ea1
DC
5 controller: {
6 xclass: 'Ext.app.ViewController',
ad834b6f 7
c3339ea1
DC
8 setBaseUrl: function(baseurl) {
9 var me = this;
10 me.getViewModel().set('baseurl', baseurl);
11 me.reload();
12 },
ad834b6f 13
c3339ea1
DC
14 reload: function() {
15 var me = this;
16 var viewmodel = me.getViewModel();
17 var baseurl = viewmodel.get('baseurl');
ad834b6f 18
c3339ea1
DC
19 if (!baseurl) {
20 me.setRuleInfo(undefined);
21 return;
22 }
ad834b6f 23
c3339ea1
DC
24 Proxmox.Utils.API2Request({
25 url: baseurl + "/config",
26 method: 'GET',
27 success: function(response, opts) {
28 me.setRuleInfo(response.result.data);
29 },
c87d46fb 30 failure: function(response, opts) {
c3339ea1 31 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
c87d46fb 32 },
c3339ea1
DC
33 });
34 },
ad834b6f 35
c3339ea1
DC
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 },
c87d46fb 52 failure: function(response, opts) {
c3339ea1 53 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
c87d46fb 54 },
c3339ea1
DC
55 });
56 }
c87d46fb 57 },
c3339ea1
DC
58 );
59 },
ad834b6f 60
c3339ea1
DC
61 addObjectGroup: function(type, record) {
62 var me = this;
63 var baseurl = me.getViewModel().get('baseurl');
64 var url = baseurl + '/' + type;
c87d46fb 65 var id = type === 'action'?record.data.ogroup:record.data.id;
c3339ea1
DC
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 },
c87d46fb 74 failure: function(response, opts) {
c3339ea1 75 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
c87d46fb 76 },
c3339ea1
DC
77 });
78 },
ad834b6f 79
c3339ea1
DC
80 setRuleInfo: function(ruledata) {
81 var me = this;
ad834b6f 82
c3339ea1 83 var viewmodel = me.getViewModel();
ad834b6f 84
c3339ea1 85 if (ruledata === undefined) {
c3339ea1
DC
86 viewmodel.set('selectedRule', null);
87 viewmodel.get('objects').setData([]);
c3339ea1 88 } else {
c3339ea1 89 viewmodel.set('selectedRule', ruledata);
ad834b6f 90
60875e2d
DC
91 let data = {
92 leaf: false,
93 expanded: true,
94 children: [],
95 };
c3339ea1 96 Ext.Array.each(['from', 'to', 'when', 'what', 'action'], function(oc) {
c3339ea1
DC
97 var store = viewmodel.get(oc + 'objects');
98 if (ruledata[oc] === undefined || store === undefined) { return; }
ad834b6f 99
c3339ea1
DC
100 // we build a filter for the objects,
101 // which are already added to the rule,
102 // so what we only show the ones,
103 // which are still available
ad834b6f 104
c3339ea1
DC
105 var ids = Ext.Array.pluck(ruledata[oc], 'id');
106 // for the actions, we have a different id field
c87d46fb 107 var idField = oc === 'action'?'ogroup':'id';
c3339ea1
DC
108 store.clearFilter();
109 store.addFilter({
c87d46fb 110 filterFn: function(record) {
c3339ea1
DC
111 // FIXME
112 // actions have the ogroup as a string
113 // -> parseInt
c87d46fb
TL
114 return ids.indexOf(parseInt(record.data[idField], 10)) === -1;
115 },
c3339ea1
DC
116 });
117 store.load();
60875e2d
DC
118
119 let group = {
120 name: oc,
121 oclass: oc,
122 type: true,
e3c8d4fd
DC
123 invert: ruledata[`${oc}-invert`],
124 and: ruledata[`${oc}-and`],
60875e2d
DC
125 leaf: false,
126 expanded: true,
127 expandable: false,
128 children: [],
129 };
c3339ea1 130 Ext.Array.each(ruledata[oc], function(og) {
60875e2d 131 group.children.push({ oclass: oc, name: og.name, typeid: og.id, leaf: true });
c3339ea1 132 });
ad834b6f 133
60875e2d
DC
134 if (group.children.length) {
135 data.children.push(group);
136 }
137 });
138 viewmodel.get('objects').setRoot(data);
c3339ea1
DC
139 }
140 },
ad834b6f 141
c3339ea1
DC
142 removeIconClick: function(gridView, rowindex, colindex, button, event, record) {
143 var me = this;
144 me.removeObjectGroup(record);
145 },
146
147 removeDrop: function(gridView, data, overModel) {
148 var me = this;
149 var record = data.records[0]; // only one
150 me.removeObjectGroup(record);
151 return true;
152 },
ad834b6f 153
c3339ea1
DC
154 addIconClick: function(gridView, rowindex, colindex, button, event, record) {
155 var me = this;
156 me.addObjectGroup(gridView.panel.type, record);
157 return true;
158 },
ad834b6f 159
c3339ea1
DC
160 addDrop: function(gridView, data, overModel) {
161 var me = this;
162 var record = data.records[0]; // only one
163 me.addObjectGroup(data.view.panel.type, record);
164 return true;
165 },
ad834b6f 166
e3c8d4fd
DC
167 updateMode: function(field, value) {
168 let me = this;
169 let vm = me.getViewModel();
170 let oclass = field.getWidgetRecord().data.oclass;
171
172 let params = {};
173 params[`${oclass}-invert`] = value.startsWith('not') ? 1 : 0;
174 params[`${oclass}-and`] = value.endsWith('all') ? 1 : 0;
175
176 Proxmox.Utils.API2Request({
177 url: `${vm.get('baseurl')}/config`,
178 method: 'PUT',
179 params,
180 success: () => me.reload(),
181 });
182 },
183
c3339ea1 184 control: {
60875e2d 185 'treepanel[reference=usedobjects]': {
c87d46fb 186 drop: 'addDrop',
ad834b6f 187 },
c3339ea1 188 'tabpanel[reference=availobjects] > grid': {
c87d46fb
TL
189 drop: 'removeDrop',
190 },
e3c8d4fd
DC
191 'pmgMatchModeSelector': {
192 change: 'updateMode',
193 },
c87d46fb 194 },
c3339ea1 195 },
ad834b6f 196
c3339ea1
DC
197 viewModel: {
198 data: {
c87d46fb 199 baseurl: '',
c3339ea1 200 },
ad834b6f 201
c3339ea1
DC
202 stores: {
203 objects: {
60875e2d 204 type: 'tree',
c3339ea1
DC
205 fields: ['oclass', 'name', 'typeid'],
206 groupField: 'oclass',
c87d46fb 207 sorters: 'name',
ad834b6f 208 },
ad834b6f 209
c3339ea1
DC
210 actionobjects: {
211 model: 'pmg-action-list',
212 proxy: {
213 type: 'proxmox',
c87d46fb 214 url: "/api2/json/config/ruledb/action/objects",
f71626c2 215 },
c87d46fb 216 sorters: 'name',
c3339ea1
DC
217 },
218 fromobjects: {
219 model: 'pmg-object-group',
220 proxy: {
221 type: 'proxmox',
c87d46fb 222 url: "/api2/json/config/ruledb/who",
c3339ea1 223 },
c87d46fb 224 sorters: 'name',
c3339ea1
DC
225 },
226 toobjects: {
227 model: 'pmg-object-group',
228 proxy: {
229 type: 'proxmox',
c87d46fb 230 url: "/api2/json/config/ruledb/who",
c3339ea1 231 },
c87d46fb 232 sorters: 'name',
c3339ea1
DC
233 },
234 whatobjects: {
235 model: 'pmg-object-group',
236 proxy: {
237 type: 'proxmox',
c87d46fb 238 url: "/api2/json/config/ruledb/what",
c3339ea1 239 },
c87d46fb 240 sorters: 'name',
c3339ea1
DC
241 },
242 whenobjects: {
243 model: 'pmg-object-group',
244 proxy: {
245 type: 'proxmox',
c87d46fb 246 url: "/api2/json/config/ruledb/when",
c3339ea1 247 },
c87d46fb
TL
248 sorters: 'name',
249 },
250 },
c3339ea1
DC
251 },
252
253
254 defaults: {
c87d46fb 255 padding: '5 10 5 10',
c3339ea1
DC
256 },
257
258 bodyPadding: '5 0 5 0',
259
260 layout: {
261 type: 'vbox',
c87d46fb 262 align: 'stretch',
c3339ea1
DC
263 },
264
265 scrollable: true,
266
267 items: [
268 {
269 xtype: 'panel',
ea07c9aa 270 bodyPadding: '10 10 10 10',
c3339ea1 271 data: {
c87d46fb 272 name: '',
c3339ea1
DC
273 },
274 bind: {
275 data: {
df2a647a 276 name: '{selectedRule.name:htmlEncode}',
c3339ea1
DC
277 priority: '{selectedRule.priority}',
278 active: '{selectedRule.active}',
279 direction: '{selectedRule.direction}',
c87d46fb
TL
280 selected: '{selectedRule}',
281 },
c3339ea1
DC
282 },
283 tpl: [
284 '<tpl if="selected">',
285 '<b>{name}</b><br><br>',
64fb657f
DC
286 gettext('Priority') + ': {priority}<br>',
287 gettext('Direction') + ': {[PMG.Utils.format_rule_direction(values.direction)]}<br>',
288 gettext('Active') + ': {[Proxmox.Utils.format_boolean(values.active)]}<br>',
c3339ea1
DC
289 '<tpl else>',
290 gettext('Please select a rule.'),
c87d46fb
TL
291 '</tpl>',
292 ],
c3339ea1
DC
293 },
294 {
60875e2d 295 xtype: 'treepanel',
c3339ea1
DC
296 reference: 'usedobjects',
297 hidden: true,
298 emptyText: gettext('No Objects'),
f71626c2 299
c3339ea1 300 title: gettext('Used Objects'),
60875e2d
DC
301 rootVisible: false,
302 useArrows: true,
303 rowLines: true,
304 userCls: 'pmx-rule-tree',
ad834b6f 305
c3339ea1 306 viewConfig: {
60875e2d 307 getRowClass: record => record.data.type ? 'pmx-type-row' : '',
c3339ea1
DC
308 plugins: {
309 ptype: 'gridviewdragdrop',
310 copy: true,
311 dragGroup: 'usedobjects',
312 dropGroup: 'unusedobjects',
313
314 // do not show default grid dragdrop behaviour
315 dropZone: {
316 indicatorHtml: '',
317 indicatorCls: '',
c87d46fb
TL
318 handleNodeDrop: Ext.emptyFn,
319 },
320 },
c3339ea1
DC
321 },
322
323 columns: [
f71626c2 324 {
c3339ea1
DC
325 header: gettext('Name'),
326 dataIndex: 'name',
60875e2d
DC
327 xtype: 'treecolumn',
328 renderer: PMG.Utils.format_oclass,
329 sorter: function(a, b) {
330 if (a.data.type && b.data.type) {
331 return a.data.oclass.localeCompare(b.data.oclass);
332 }
333 return a.data.text.localeCompare(b.data.text);
334 },
c87d46fb 335 flex: 1,
f71626c2
DM
336 },
337 {
e3c8d4fd
DC
338 header: gettext('Match if'),
339 xtype: 'widgetcolumn',
340 width: 200,
341 widget: {
342 xtype: 'pmgMatchModeSelector',
343 },
344 onWidgetAttach: function(col, widget, rec) {
345 if (rec.data.type && rec.data.oclass !== 'action') {
346 let mode = rec.data.invert ? 'not' : '';
347 mode += rec.data.and ? 'all' : 'any';
348 widget.suspendEvents();
349 widget.setValue(mode);
350 widget.resumeEvents();
351 widget.setHidden(false);
352 } else {
353 widget.setHidden(true);
354 }
355 },
356 },
357 {
1f639768 358 text: '',
c3339ea1 359 xtype: 'actioncolumn',
1f639768
DC
360 align: 'center',
361 width: 40,
c3339ea1
DC
362 items: [
363 {
c3339ea1 364 tooltip: gettext('Remove'),
60875e2d
DC
365 isActionDisabled: (v, rI, cI, i, rec) => rec.data.type,
366 getClass: (v, mD, { data }) => data.type ? 'pmx-hidden' : 'fa fa-fw fa-minus-circle',
c87d46fb
TL
367 handler: 'removeIconClick',
368 },
369 ],
370 },
c3339ea1
DC
371 ],
372
373 bind: {
374 store: '{objects}',
c87d46fb
TL
375 hidden: '{!selectedRule}',
376 },
c3339ea1
DC
377 },
378 {
379 xtype: 'tabpanel',
380 title: gettext('Available Objects'),
381 reference: 'availobjects',
382 hidden: true,
383 bind: {
c87d46fb 384 hidden: '{!selectedRule}',
c3339ea1
DC
385 },
386 defaults: {
387 xtype: 'grid',
388 emptyText: gettext('No Objects'),
389 viewConfig: {
390 plugins: {
391 ptype: 'gridviewdragdrop',
392 dragGroup: 'unusedobjects',
393 dropGroup: 'usedobjects',
394
395 // do not show default grid dragdrop behaviour
396 dropZone: {
397 indicatorHtml: '',
398 indicatorCls: '',
c87d46fb
TL
399 handleNodeDrop: Ext.emptyFn,
400 },
401 },
f71626c2 402 },
c3339ea1
DC
403 columns: [
404 {
405 header: gettext('Name'),
406 dataIndex: 'name',
c87d46fb 407 flex: 1,
c3339ea1
DC
408 },
409 {
1f639768 410 text: '',
c3339ea1 411 xtype: 'actioncolumn',
1f639768
DC
412 align: 'center',
413 width: 40,
c3339ea1
DC
414 items: [
415 {
2b97521b 416 iconCls: 'fa fa-fw fa-plus-circle',
c3339ea1 417 tooltip: gettext('Add'),
c87d46fb
TL
418 handler: 'addIconClick',
419 },
420 ],
421 },
422 ],
c3339ea1
DC
423 },
424 items: [
425 {
426 title: gettext('Action'),
427 bind: {
c87d46fb 428 store: '{actionobjects}',
c3339ea1
DC
429 },
430 type: 'action',
c87d46fb 431 iconCls: 'fa fa-flag',
ad834b6f 432 },
39700611 433 {
c3339ea1
DC
434 title: gettext('From'),
435 iconCls: 'fa fa-user-circle',
436 type: 'from',
437 bind: {
c87d46fb
TL
438 store: '{fromobjects}',
439 },
39700611 440 },
ad834b6f 441 {
c3339ea1
DC
442 title: gettext('To'),
443 iconCls: 'fa fa-user-circle',
444 type: 'to',
445 bind: {
c87d46fb
TL
446 store: '{toobjects}',
447 },
c3339ea1
DC
448 },
449 {
450 title: gettext('What'),
451 iconCls: 'fa fa-cube',
452 type: 'what',
453 bind: {
c87d46fb
TL
454 store: '{whatobjects}',
455 },
c3339ea1
DC
456 },
457 {
458 title: gettext('When'),
459 iconCls: 'fa fa-clock-o',
460 type: 'when',
461 bind: {
c87d46fb
TL
462 store: '{whenobjects}',
463 },
464 },
465 ],
466 },
467 ],
ad834b6f 468});