]> git.proxmox.com Git - pmg-gui.git/blob - js/RuleConfiguration.js
jslint fix: do not use single statements when using if
[pmg-gui.git] / js / RuleConfiguration.js
1 /*global Proxmox*/
2 /*jslint confusion: true*/
3 /* create is a function and boolean,
4 * bind is a function and object,
5 * callback is a function and string
6 */
7 Ext.define('pmg-rule-list', {
8 extend: 'Ext.data.Model',
9 fields: [
10 'id', 'name',
11 { name: 'active', type: 'boolean' },
12 { name: 'direction', type: 'integer' },
13 { name: 'priority', type: 'integer' }
14 ],
15 idProperty: 'id'
16 });
17
18 Ext.define('PMG.RulesConfiguration', {
19 extend: 'Ext.container.Container',
20 xtype: 'pmgRuleConfiguration',
21
22 layout: 'border',
23 border: false,
24 defaults: {
25 border: false
26 },
27
28 controller: {
29 xclass: 'Ext.app.ViewController',
30
31 selectedRuleChange: function(grid, selected, eOpts) {
32 var me = this;
33 var infoPanel = me.lookupReference('infopanel');
34 var baseurl = '';
35
36 if (selected.length > 0) {
37 baseurl = '/config/ruledb/rules/' + selected[0].data.id;
38 }
39
40 infoPanel.getController().setBaseUrl(baseurl);
41 },
42
43 editIconClick: function(gridView, rowindex, colindex, column, e, record) {
44 var me = this;
45 me.showEditWindow(gridView, record);
46 },
47
48 showEditWindow: function(gridView, record) {
49 var me = this;
50 var win = Ext.create('PMG.RuleEditor', {
51 url: '/api2/extjs/config/ruledb/rules/' + record.data.id + '/config',
52 listeners: {
53 destroy: function() {
54 gridView.getStore().load();
55 }
56 }
57 });
58 win.load();
59 win.show();
60 },
61
62 toggleIconClick: function(gridView, rowindex, colindex, column, e, record) {
63 var me = this;
64 Proxmox.Utils.API2Request({
65 url: '/config/ruledb/rules/' + record.data.id + '/config',
66 params: {
67 active: record.data.active ? 0 : 1
68 },
69 method: 'PUT',
70 callback: function() {
71 gridView.getStore().load();
72 },
73 failure: function(response, opts) {
74 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
75 }
76 });
77 },
78
79 reload: function(){
80 var me = this;
81 me.lookupReference('rulegrid').getStore().load();
82 },
83
84 addRule: function() {
85 var me = this;
86 var win = Ext.create('PMG.RuleEditor', {
87 url: '/api2/extjs/config/ruledb/rules/',
88 method: 'POST',
89 create: true,
90 listeners: {
91 destroy: function() {
92 me.lookupReference('rulegrid').getStore().load();
93 }
94 }
95 });
96 win.load();
97 win.show();
98 },
99
100 onFactoryDefaults: function() {
101 var me = this;
102
103 Ext.Msg.confirm(
104 gettext('Confirm'),
105 gettext('Reset rule database to factory defaults?'),
106 function(button) {
107 if (button !== 'yes') {
108 return;
109 }
110 var url = '/config/ruledb';
111 Proxmox.Utils.API2Request({
112 url: '/config/ruledb',
113 method: 'POST',
114 waitMsgTarget: me.getView(),
115 callback: function() {
116 me.reload();
117 },
118 failure: function (response, opts) {
119 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
120 }
121 });
122 }
123 );
124 },
125
126 init: function(view) {
127 var grid = this.lookupReference('rulegrid');
128 Proxmox.Utils.monStoreErrors(grid, grid.getStore(), true);
129 },
130
131 control: {
132 'grid[reference=rulegrid]': {
133 itemdblclick: 'showEditWindow',
134 selectionchange: 'selectedRuleChange'
135 },
136 'button[reference=addButton]': {
137 click: 'addRule'
138 }
139 }
140 },
141
142 viewModel: {
143 data: {
144 selectedRule: undefined,
145 baseUrl: '/config/ruledb/rules'
146 }
147 },
148
149 items: [
150 {
151 xtype: 'grid',
152 layout: 'fit',
153 title: 'Rules',
154 reference: 'rulegrid',
155 region: 'center',
156
157 bind: {
158 selection: '{selectedRule}'
159 },
160
161 dockedItems:{
162 xtype: 'toolbar',
163 reference: 'mytb',
164 items: [
165 {
166 xtype: 'button',
167 text: gettext('Add'),
168 iconCls: 'fa fa-plus-circle',
169 reference: 'addButton'
170 },
171 {
172 xtype: 'proxmoxStdRemoveButton',
173 text: gettext('Remove'),
174 iconCls: 'fa fa-minus-circle',
175 reference: 'removeButton',
176 callback: 'reload',
177 getRecordName: function(rec) { return rec.data.name; },
178 bind: {
179 baseurl: '{baseUrl}'
180 }
181 },
182 '->',
183 {
184 text: gettext('Factory Defaults'),
185 handler: 'onFactoryDefaults'
186 }
187 ]
188 },
189
190 viewConfig: {
191 getRowClass: function(record, rowIndex) {
192 return record.get('active') ? 'enabled' : 'disabled';
193 }
194 },
195
196 store: {
197 autoLoad: true,
198 model: 'pmg-rule-list',
199 reference: 'rulesStore',
200 proxy: {
201 type: 'proxmox',
202 url: '/api2/json/config/ruledb/rules'
203 },
204 sorters: [
205 {
206 property: 'priority',
207 direction: 'DESC'
208 },
209 {
210 property: 'name'
211 }
212 ]
213 },
214
215 sortableColumns: false,
216 columns: [
217 {
218 text: 'Active',
219 dataIndex: 'active',
220 hidden : true
221 },
222 {
223 text: 'Name',
224 dataIndex: 'name',
225 flex: 1
226 },
227 {
228 text: 'Priority',
229 dataIndex: 'priority'
230 },
231 {
232 text: 'Direction',
233 dataIndex: 'direction',
234 renderer: PMG.Utils.format_rule_direction
235 },
236 {
237 text: '',
238 xtype: 'actioncolumn',
239 align: 'center',
240 width: 70,
241 items: [
242 {
243 iconCls: 'fa fa-fw fa-pencil',
244 tooltip: 'Edit',
245 handler: 'editIconClick'
246 },
247 {
248 getClass: function(val, meta, rec) {
249 return 'fa fa-fw fa-' + (rec.get('active') ? 'toggle-on' : 'toggle-off');
250 },
251 getTip: function(val, meta, rec) {
252 return (rec.get('active') ? 'Deactivate' : 'Activate');
253 },
254 handler: 'toggleIconClick'
255 }
256 ]
257 }
258 ]
259 },
260 {
261 region: 'east',
262 reference: 'infopanel',
263 xtype: 'pmgRuleInfo',
264 split: true,
265 width: 440
266 }
267 ]
268 });