]> git.proxmox.com Git - pmg-gui.git/blob - js/ObjectGroup.js
disclaimer edit: add position comobobox
[pmg-gui.git] / js / ObjectGroup.js
1 Ext.define('PMG.ObjectGroup', {
2 extend: 'Ext.grid.GridPanel',
3 alias: 'widget.pmgObjectGroup',
4
5 baseurl: undefined,
6
7 otype_list: [],
8
9 hideGroupInfo: false,
10 showDirection: false, // only important for SMTP Whitelist
11
12 ogdata: undefined,
13 objectClass: undefined,
14
15 emptyText: gettext('Please select an object.'),
16
17 setBaseUrl: function(baseurl) {
18 let me = this;
19
20 me.baseurl = baseurl;
21
22 if (me.baseurl === undefined) {
23 me.store.proxy.setUrl(undefined);
24 me.store.setData([]);
25 me.setButtonState(me.store, [], false);
26 } else {
27 let url = '/api2/json' + me.baseurl + '/objects';
28 me.store.proxy.setUrl(url);
29 me.store.load();
30 }
31 },
32
33 setObjectInfo: function(ogdata) {
34 let me = this;
35
36 let mode = ogdata?.invert ? 'not' : '';
37 mode += ogdata?.and ? 'all' : 'any';
38
39 me.ogdata = ogdata;
40
41 if (me.ogdata === undefined) {
42 me.down('#oginfo').update(me.emptyText);
43 me.down('#modeBox').setHidden(true);
44 me.down('#whatWarning').setHidden(true);
45 } else {
46 let html = '<b style="font-weight: bold;">' + Ext.String.htmlEncode(me.ogdata.name) + '</b>';
47 html += "<br><br>";
48 html += Ext.String.htmlEncode(Ext.String.trim(me.ogdata.info));
49
50 me.down('#oginfo').update(html);
51 me.down('#ogdata').setHidden(false);
52 let modeSelector = me.down('#modeSelector');
53 modeSelector.suspendEvents();
54 me.down('#modeSelector').setValue(mode);
55 modeSelector.resumeEvents();
56 me.down('#modeBox').setHidden(false);
57 me.down('#whatWarning').setHidden(me.objectClass !== 'what' || mode === 'any');
58 }
59 },
60
61 setButtonState: function(store, records, success) {
62 let me = this;
63 if (!success || !me.baseurl) {
64 me.down('#addMenuButton').setDisabled(true);
65 return;
66 }
67 me.down('#addMenuButton').setDisabled(false);
68 },
69
70 initComponent: function() {
71 let me = this;
72
73 me.store = new Ext.data.Store({
74 model: 'pmg-object-list',
75 proxy: {
76 type: 'proxmox',
77 },
78 sorters: [
79 {
80 property: 'receivertest',
81 },
82 {
83 property: 'otype',
84 direction: 'ASC',
85 },
86 ],
87 });
88
89 me.columns = [{
90 header: gettext('Type'),
91 dataIndex: 'otype',
92 renderer: PMG.Utils.format_otype,
93 width: 200,
94 }];
95
96 if (me.showDirection) {
97 me.columns.push({
98 header: gettext('Direction'),
99 dataIndex: 'receivertest',
100 renderer: function(value) {
101 return value ? PMG.Utils.receiverText : PMG.Utils.senderText;
102 },
103 });
104 }
105
106 me.columns.push({
107 header: gettext('Value'),
108 dataIndex: 'descr',
109 renderer: Ext.String.htmlEncode,
110 flex: 1,
111 });
112
113 let reload = function() {
114 me.store.load();
115 };
116
117 me.selModel = Ext.create('Ext.selection.RowModel', {});
118
119 let remove_btn = Ext.createWidget('proxmoxStdRemoveButton', {
120 selModel: me.selModel,
121 getUrl: function(rec) {
122 return me.baseurl + '/objects/' + rec.data.id;
123 },
124 callback: reload,
125 getRecordName: function(rec) {
126 return PMG.Utils.format_otype(rec.data.otype) +
127 ': ' + rec.data.descr;
128 },
129 waitMsgTarget: me,
130 });
131
132 let full_subject = function(subject, receivertest) {
133 if (me.showDirection) {
134 let direction = receivertest
135 ? PMG.Utils.receiverText : PMG.Utils.senderText;
136
137 return subject + ' (' + direction + ')';
138 } else {
139 return subject;
140 }
141 };
142
143 let run_editor = function() {
144 let rec = me.selModel.getSelection()[0];
145 if (!rec) {
146 return;
147 }
148
149 let editor = PMG.Utils.object_editors[rec.data.otype];
150 if (!editor || editor.uneditable) {
151 return;
152 }
153
154 let config = Ext.apply({ method: 'PUT' }, editor);
155 config.subject = full_subject(editor.subject, rec.data.receivertest);
156 config.url = me.baseurl + '/' + editor.subdir + '/' + rec.data.id;
157
158 let win = Ext.createWidget(config);
159
160 win.load();
161 win.on('destroy', reload);
162 win.show();
163 };
164
165 let menu_items = [];
166
167 Ext.Array.each(me.otype_list, function(otype) {
168 let editor = PMG.Utils.object_editors[otype];
169
170 let config = Ext.apply({ method: 'POST' }, editor);
171 config.subject = full_subject(editor.subject, editor.receivertest);
172
173 menu_items.push({
174 text: config.subject,
175 iconCls: config.iconCls || 'fa fa-question-circle',
176 handler: function() {
177 if (me.baseurl === undefined) {
178 return;
179 }
180 config.url = me.baseurl + '/' + editor.subdir;
181 let win = Ext.create(config);
182 win.on('destroy', reload);
183 win.show();
184 },
185 });
186 });
187
188 me.dockedItems = [];
189
190 me.dockedItems.push({
191 xtype: 'toolbar',
192 dock: 'top',
193 items: [
194 {
195 text: gettext('Add'),
196 disabled: true,
197 itemId: 'addMenuButton',
198 menu: {
199 items: menu_items,
200 },
201 },
202 {
203 xtype: 'proxmoxButton',
204 text: gettext('Edit'),
205 disabled: true,
206 selModel: me.selModel,
207 enableFn: function(rec) {
208 let editor = PMG.Utils.object_editors[rec.data.otype];
209 return editor && !editor.uneditable;
210 },
211 handler: run_editor,
212 },
213 remove_btn,
214 '->',
215 {
216 xtype: 'pmgFilterField',
217 filteredFields: [
218 {
219 name: 'otype',
220 renderer: (otype) => PMG.Utils.object_editors[otype].subject,
221 },
222 'descr',
223 ],
224 },
225 ],
226 });
227
228 me.dockedItems.push({
229 dock: 'top',
230 border: 1,
231 layout: 'hbox',
232 hidden: !!me.hideGroupInfo,
233 itemId: 'ogdata',
234 items: [
235 {
236 xtype: 'container',
237 itemId: 'modeBox',
238 hidden: true,
239 width: 220,
240 padding: 10,
241 layout: {
242 type: 'vbox',
243 align: 'stretch',
244 },
245 items: [
246 {
247 xtype: 'box',
248 html: `<b style="font-weight: bold;">${gettext("Match if")}</b>`,
249 },
250 {
251 xtype: 'pmgMatchModeSelector',
252 itemId: 'modeSelector',
253 padding: '10 0 0 0',
254 listeners: {
255 change: function(_field, value) {
256 let invert = value.startsWith('not') ? 1 : 0;
257 let and = value.endsWith('all') ? 1 : 0;
258
259 Proxmox.Utils.API2Request({
260 url: `${me.baseurl}/config`,
261 method: 'PUT',
262 params: {
263 and,
264 invert,
265 },
266 success: () => {
267 me.fireEvent('modeUpdate', me, !!and, !!invert);
268 me.down('#whatWarning')
269 .setHidden(me.objectClass !== 'what' || value === 'any');
270 },
271 });
272 },
273 },
274 },
275 ],
276 },
277 {
278 xtype: 'component',
279 flex: 1,
280 itemId: 'oginfo',
281 style: { 'white-space': 'pre' },
282 padding: 10,
283 html: me.emptyText,
284 listeners: {
285 dblclick: {
286 fn: function(e, t) {
287 if (me.ogdata === undefined) { return; }
288 me.fireEvent('dblclickOGInfo', me, e, t, me.ogdata);
289 },
290 element: 'el',
291 scope: this,
292 },
293 },
294 },
295 ],
296 });
297
298 me.dockedItems.push({
299 dock: 'top',
300 border: 1,
301 hidden: true,
302 itemId: 'whatWarning',
303 bodyPadding: 5,
304 items: {
305 xtype: 'displayfield',
306 margin: 0,
307 value: gettext("Caution: 'What Objects' match each mail part separately, so be careful with any option besides 'Any matches'."),
308 userCls: 'pmx-hint',
309 },
310 });
311
312 Proxmox.Utils.monStoreErrors(me, me.store, true);
313
314 Ext.apply(me, {
315 run_editor: run_editor,
316 listeners: {
317 itemdblclick: run_editor,
318 activate: reload,
319 },
320 });
321
322 me.callParent();
323
324 me.mon(me.store, 'load', me.setButtonState, me);
325
326 if (me.baseurl) {
327 me.setBaseUrl(me.baseurl); // configure store, load()
328 }
329 },
330 });