]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
utils: code cleanup
[pmg-gui.git] / js / Utils.js
1 Ext.ns('PMG');
2
3 console.log("Starting PMG Manager");
4
5
6 Ext.define('PMG.Utils', {
7 singleton: true,
8
9 // this singleton contains miscellaneous utilities
10
11 // use in panels with object spread (...) operator, for example:
12 // ...PMG.Utils.onlineHelpTool('sysadmin_certificate_management'),
13 onlineHelpTool: function(blockid) {
14 let info = Proxmox.Utils.get_help_info(blockid);
15 if (info === undefined) {
16 info = Proxmox.Utils.get_help_info('pmg_documentation_index');
17 if (info === undefined) {
18 throw "get_help_info failed"; // should not happen
19 }
20 }
21
22 let docsURI = window.location.origin + info.link;
23 let title = info.title || gettext('Help');
24 if (info.subtitle) {
25 title += ' - ' + info.subtitle;
26 }
27 return {
28 tools: [
29 {
30 type: 'help',
31 tooltip: title,
32 handler: () => window.open(docsURI),
33 },
34 ],
35 };
36 },
37
38 senderText: gettext('Sender'),
39 receiverText: gettext('Receiver'),
40 scoreText: gettext('Score'),
41
42 user_role_text: {
43 root: gettext('Superuser'),
44 admin: gettext('Administrator'),
45 helpdesk: gettext('Help Desk'),
46 qmanager: gettext('Quarantine Manager'),
47 audit: gettext('Auditor'),
48 },
49
50 format_user_role: function(role) {
51 return PMG.Utils.user_role_text[role] || role;
52 },
53
54 oclass_text: {
55 who: gettext('Who Objects'),
56 what: gettext('What Objects'),
57 when: gettext('When Objects'),
58 action: gettext('Action Objects'),
59 from: gettext('From'),
60 to: gettext('To'),
61 },
62
63 oclass_icon: {
64 who: '<span class="fa fa-fw fa-user-circle"></span> ',
65 what: '<span class="fa fa-fw fa-cube"></span> ',
66 when: '<span class="fa fa-fw fa-clock-o"></span> ',
67 action: '<span class="fa fa-fw fa-flag"></span> ',
68 from: '<span class="fa fa-fw fa-user-circle"></span> ',
69 to: '<span class="fa fa-fw fa-user-circle"></span> ',
70 },
71
72 mail_status_map: {
73 2: 'delivered',
74 4: 'deferred',
75 5: 'bounced',
76 N: 'rejected',
77 G: 'greylisted',
78 A: 'accepted',
79 B: 'blocked',
80 Q: 'quarantine',
81 },
82
83 icon_status_map_class: {
84 2: 'check-circle',
85 4: 'clock-o',
86 5: 'mail-reply',
87 N: 'times-circle',
88 G: 'list',
89 A: 'check',
90 B: 'ban',
91 Q: 'cube',
92 },
93
94 icon_status_map_color: {
95 2: 'green',
96 5: 'gray',
97 A: 'green',
98 B: 'red',
99 },
100
101 format_status_icon: function(status) {
102 var icon = PMG.Utils.icon_status_map_class[status] || 'question-circle';
103 var color = PMG.Utils.icon_status_map_color[status] || '';
104 return '<i class="fa fa-' + icon + ' ' + color + '"></i> ';
105 },
106
107 format_oclass: function(oclass) {
108 var icon = PMG.Utils.oclass_icon[oclass] || '';
109 var text = PMG.Utils.oclass_text[oclass] || oclass;
110 return icon + text;
111 },
112
113 rule_direction_text: {
114 0: gettext('In'),
115 1: gettext('Out'),
116 2: gettext('In & Out'),
117 },
118
119 rule_direction_icon: {
120 0: '<span class="fa fa-fw fa-long-arrow-left"></span> ',
121 1: '<span class="fa fa-fw fa-long-arrow-right"></span> ',
122 2: '<span class="fa fa-fw fa-exchange"></span> ',
123 },
124
125 format_rule_direction: function(dir) {
126 var icon = PMG.Utils.rule_direction_icon[dir] || '';
127 var text = PMG.Utils.rule_direction_text[dir] || dir;
128 return icon + text;
129 },
130
131 format_otype: function(otype) {
132 let editor = PMG.Utils.object_editors[otype];
133 let iconCls = 'fa fa-question-circle';
134 if (editor) {
135 return `<span class="fa-fw ${editor.iconCls || iconCls}"></span> ${editor.subject}`;
136 }
137 return `<span class="fa-fw ${iconCls}"></span> unknown`;
138 },
139
140 format_ldap_protocol: function(p) {
141 if (p === undefined) { return 'LDAP'; }
142 if (p === 'ldap') { return 'LDAP'; }
143 if (p === 'ldaps') { return 'LDAPS'; }
144 if (p === 'ldap+starttls') { return 'LDAP+STARTTLS'; }
145 return 'unknown';
146 },
147
148 convert_field_to_per_min: function(value, record) {
149 return value / (record.data.timespan / 60);
150 },
151
152 object_editors: {
153 1000: {
154 onlineHelp: 'pmg_mailfilter_regex',
155 iconCls: 'fa fa-filter',
156 xtype: 'proxmoxWindowEdit',
157 subdir: 'regex',
158 subject: gettext("Regular Expression"),
159 width: 400,
160 items: [
161 {
162 xtype: 'textfield',
163 name: 'regex',
164 labelWidth: 150,
165 reference: 'regex',
166 fieldLabel: gettext("Regular Expression"),
167 },
168 {
169 xtype: 'pmgRegexTester',
170 fieldLabel: gettext('Test String'),
171 labelWidth: 150,
172 wholeMatch: true,
173 regexFieldReference: 'regex',
174 },
175 ],
176 },
177 1005: {
178 onlineHelp: 'pmgconfig_ldap',
179 iconCls: 'fa fa-users',
180 xtype: 'pmgLDAPGroupEditor',
181 subdir: 'ldap',
182 subject: gettext("LDAP Group"),
183 },
184 1006: {
185 onlineHelp: 'pmgconfig_ldap',
186 iconCls: 'fa fa-user',
187 xtype: 'pmgLDAPUserEditor',
188 subdir: 'ldapuser',
189 subject: gettext("LDAP User"),
190 },
191 1009: {
192 onlineHelp: 'pmg_mailfilter_regex',
193 iconCls: 'fa fa-filter',
194 xtype: 'proxmoxWindowEdit',
195 subdir: 'receiver_regex',
196 subject: gettext("Regular Expression"),
197 receivertest: true,
198 width: 400,
199 items: [
200 {
201 xtype: 'textfield',
202 name: 'regex',
203 labelWidth: 150,
204 fieldLabel: gettext("Regular Expression"),
205 },
206 ],
207 },
208 1001: {
209 onlineHelp: 'pmg_mailfilter_who',
210 iconCls: 'fa fa-envelope-o',
211 xtype: 'proxmoxWindowEdit',
212 subdir: 'email',
213 subject: gettext("E-Mail"),
214 width: 400,
215 items: [
216 {
217 xtype: 'textfield',
218 name: 'email',
219 fieldLabel: gettext("E-Mail"),
220 },
221 ],
222 },
223 1007: {
224 onlineHelp: 'pmg_mailfilter_who',
225 iconCls: 'fa fa-envelope-o',
226 xtype: 'proxmoxWindowEdit',
227 subdir: 'receiver',
228 subject: gettext("E-Mail"),
229 receivertest: true,
230 width: 400,
231 items: [
232 {
233 xtype: 'textfield',
234 name: 'email',
235 fieldLabel: gettext("E-Mail"),
236 },
237 ],
238 },
239 1002: {
240 onlineHelp: 'pmg_mailfilter_who',
241 iconCls: 'fa fa-globe',
242 xtype: 'proxmoxWindowEdit',
243 subdir: 'domain',
244 subject: gettext("Domain"),
245 width: 400,
246 items: [
247 {
248 xtype: 'textfield',
249 name: 'domain',
250 fieldLabel: gettext("Domain"),
251 },
252 ],
253 },
254 1008: {
255 onlineHelp: 'pmg_mailfilter_who',
256 iconCls: 'fa fa-globe',
257 xtype: 'proxmoxWindowEdit',
258 subdir: 'receiver_domain',
259 subject: gettext("Domain"),
260 receivertest: true,
261 width: 400,
262 items: [
263 {
264 xtype: 'textfield',
265 name: 'domain',
266 fieldLabel: gettext("Domain"),
267 },
268 ],
269 },
270 1003: {
271 onlineHelp: 'pmg_mailfilter_who',
272 iconCls: 'fa fa-globe',
273 xtype: 'proxmoxWindowEdit',
274 subdir: 'ip',
275 subject: gettext("IP Address"),
276 width: 400,
277 items: [
278 {
279 xtype: 'textfield',
280 name: 'ip',
281 fieldLabel: gettext("IP Address"),
282 },
283 ],
284 },
285 1004: {
286 onlineHelp: 'pmg_mailfilter_who',
287 iconCls: 'fa fa-globe',
288 xtype: 'proxmoxWindowEdit',
289 subdir: 'network',
290 subject: gettext("IP Network"),
291 width: 400,
292 items: [
293 {
294 xtype: 'textfield',
295 name: 'cidr',
296 fieldLabel: gettext("IP Network"),
297 },
298 ],
299 },
300 2000: {
301 onlineHelp: 'pmg_mailfilter_when',
302 iconCls: 'fa fa-clock-o',
303 xtype: 'proxmoxWindowEdit',
304 subdir: 'timeframe',
305 subject: gettext("TimeFrame"),
306 items: [
307 {
308 xtype: 'timefield',
309 name: 'start',
310 format: 'H:i',
311 fieldLabel: gettext("Start Time"),
312 },
313 {
314 xtype: 'timefield',
315 name: 'end',
316 format: 'H:i',
317 fieldLabel: gettext("End Time"),
318 },
319 ],
320 },
321 3000: {
322 onlineHelp: 'pmg_mailfilter_what',
323 iconCls: 'fa fa-bullhorn',
324 xtype: 'proxmoxWindowEdit',
325 subdir: 'spamfilter',
326 subject: gettext('Spam Filter'),
327 items: [
328 {
329 xtype: 'proxmoxintegerfield',
330 name: 'spamlevel',
331 allowBlank: false,
332 minValue: 0,
333 fieldLabel: gettext('Level'),
334 },
335 ],
336 },
337 3001: {
338 onlineHelp: 'pmg_mailfilter_what',
339 iconCls: 'fa fa-bug',
340 xtype: 'proxmoxWindowEdit',
341 subdir: 'virusfilter',
342 subject: gettext('Virus Filter'),
343 uneditable: true,
344 // there are no parameters to give, so we simply submit it
345 listeners: {
346 show: function(win) {
347 win.submit();
348 },
349 },
350 },
351 3002: {
352 onlineHelp: 'pmg_mailfilter_regex',
353 iconCls: 'fa fa-code',
354 xtype: 'proxmoxWindowEdit',
355 subdir: 'matchfield',
356 subject: gettext('Match Field'),
357 width: 400,
358 items: [
359 {
360 xtype: 'textfield',
361 name: 'field',
362 labelWidth: 150,
363 allowBlank: false,
364 fieldLabel: gettext('Field'),
365 },
366 {
367 xtype: 'textfield',
368 reference: 'value',
369 name: 'value',
370 labelWidth: 150,
371 allowBlank: false,
372 fieldLabel: gettext('Value'),
373 },
374 {
375 labelWidth: 150,
376 fieldLabel: gettext('Test String'),
377 xtype: 'pmgRegexTester',
378 regexFieldReference: 'value',
379 },
380 ],
381 },
382 3003: {
383 onlineHelp: 'pmg_mailfilter_what',
384 iconCls: 'fa fa-file-image-o',
385 xtype: 'proxmoxWindowEdit',
386 subdir: 'contenttype',
387 width: 400,
388 subject: gettext('Content Type Filter'),
389 items: [
390 {
391 xtype: 'combobox',
392 displayField: 'text',
393 labelWidth: 150,
394 valueField: 'mimetype',
395 name: 'contenttype',
396 editable: true,
397 queryMode: 'local',
398 store: {
399 autoLoad: true,
400 proxy: {
401 type: 'proxmox',
402 url: '/api2/json/config/mimetypes',
403 },
404 },
405 fieldLabel: gettext('Content Type'),
406 anyMatch: true,
407 matchFieldWidth: false,
408 listeners: {
409 change: function(cb, value) {
410 var me = this;
411 me.up().down('displayfield').setValue(value);
412 },
413 },
414 },
415 {
416 xtype: 'displayfield',
417 fieldLabel: gettext('Value'),
418 labelWidth: 150,
419 allowBlank: false,
420 reset: Ext.emptyFn,
421 },
422 ],
423 },
424 3004: {
425 onlineHelp: 'pmg_mailfilter_regex',
426 iconCls: 'fa fa-file-o',
427 xtype: 'proxmoxWindowEdit',
428 subdir: 'filenamefilter',
429 width: 400,
430 subject: gettext('Match Filename'),
431 items: [
432 {
433 xtype: 'textfield',
434 name: 'filename',
435 reference: 'filename',
436 fieldLabel: gettext('Filename'),
437 labelWidth: 150,
438 allowBlank: false,
439 },
440 {
441 labelWidth: 150,
442 fieldLabel: gettext('Test String'),
443 wholeMatch: true,
444 xtype: 'pmgRegexTester',
445 regexFieldReference: 'filename',
446 },
447 ],
448 },
449 3005: {
450 onlineHelp: 'pmg_mailfilter_what',
451 iconCls: 'fa fa-file-archive-o',
452 xtype: 'proxmoxWindowEdit',
453 subdir: 'archivefilter',
454 width: 400,
455 subject: gettext('Archive Filter'),
456 items: [
457 {
458 xtype: 'combobox',
459 displayField: 'text',
460 labelWidth: 150,
461 valueField: 'mimetype',
462 name: 'contenttype',
463 editable: true,
464 queryMode: 'local',
465 store: {
466 autoLoad: true,
467 proxy: {
468 type: 'proxmox',
469 url: '/api2/json/config/mimetypes',
470 },
471 },
472 fieldLabel: gettext('Content Type'),
473 anyMatch: true,
474 matchFieldWidth: false,
475 listeners: {
476 change: function(cb, value) {
477 var me = this;
478 me.up().down('displayfield').setValue(value);
479 },
480 },
481 },
482 {
483 xtype: 'displayfield',
484 fieldLabel: gettext('Value'),
485 labelWidth: 150,
486 allowBlank: false,
487 reset: Ext.emptyFn,
488 },
489 ],
490 },
491 3006: {
492 onlineHelp: 'pmg_mailfilter_regex',
493 iconCls: 'fa fa-file-archive-o',
494 xtype: 'proxmoxWindowEdit',
495 subdir: 'archivefilenamefilter',
496 width: 400,
497 subject: gettext('Match Archive Filename'),
498 items: [
499 {
500 xtype: 'textfield',
501 name: 'filename',
502 reference: 'filename',
503 fieldLabel: gettext('Filename'),
504 labelWidth: 150,
505 allowBlank: false,
506 },
507 {
508 labelWidth: 150,
509 fieldLabel: gettext('Test String'),
510 wholeMatch: true,
511 xtype: 'pmgRegexTester',
512 regexFieldReference: 'filename',
513 },
514 ],
515 },
516 4002: {
517 onlineHelp: 'pmg_mailfilter_action',
518 xtype: 'proxmoxWindowEdit',
519 subdir: 'notification',
520 subject: gettext('Notification'),
521 width: 400,
522 items: [
523 {
524 xtype: 'textfield',
525 name: 'name',
526 allowBlank: false,
527 fieldLabel: gettext('Name'),
528 },
529 {
530 xtype: 'textareafield',
531 name: 'info',
532 fieldLabel: gettext("Comment"),
533 },
534 {
535 xtype: 'textfield',
536 name: 'to',
537 allowBlank: false,
538 value: '__ADMIN__',
539 fieldLabel: gettext('Receiver'),
540 },
541 {
542 xtype: 'textfield',
543 name: 'subject',
544 allowBlank: false,
545 value: 'Notification: __SUBJECT__',
546 fieldLabel: gettext('Subject'),
547 },
548 {
549 xtype: 'textarea',
550 name: 'body',
551 allowBlank: false,
552 grow: true,
553 growMax: 250,
554 value:
555 "Proxmox Notifcation:\n\n" +
556 "Sender: __SENDER__\n" +
557 "Receiver: __RECEIVERS__\n" +
558 "Targets: __TARGETS__\n\n" +
559 "Subject: __SUBJECT__\n\n" +
560 "Matching Rule: __RULE__\n\n" +
561 "__RULE_INFO__\n\n" +
562 "__VIRUS_INFO__\n" +
563 "__SPAM_INFO__\n",
564 fieldLabel: gettext('Body'),
565 },
566 {
567 xtype: 'proxmoxcheckbox',
568 name: 'attach',
569 fieldLabel: gettext("Attach orig. Mail"),
570 },
571 ],
572 },
573 4003: {
574 onlineHelp: 'pmg_mailfilter_action',
575 xtype: 'proxmoxWindowEdit',
576 subdir: 'field',
577 subject: gettext('Header Attribute'),
578 width: 400,
579 items: [
580 {
581 xtype: 'textfield',
582 name: 'name',
583 allowBlank: false,
584 fieldLabel: gettext('Name'),
585 },
586 {
587 xtype: 'textareafield',
588 name: 'info',
589 fieldLabel: gettext("Comment"),
590 },
591 {
592 xtype: 'textfield',
593 name: 'field',
594 allowBlank: false,
595 fieldLabel: gettext('Field'),
596 },
597 {
598 xtype: 'textfield',
599 reference: 'value',
600 name: 'value',
601 allowBlank: false,
602 fieldLabel: gettext('Value'),
603 },
604 ],
605 },
606 4005: {
607 onlineHelp: 'pmg_mailfilter_action',
608 xtype: 'proxmoxWindowEdit',
609 subdir: 'bcc',
610 subject: gettext('BCC'),
611 width: 400,
612 items: [
613 {
614 xtype: 'textfield',
615 name: 'name',
616 allowBlank: false,
617 fieldLabel: gettext('Name'),
618 },
619 {
620 xtype: 'textareafield',
621 name: 'info',
622 fieldLabel: gettext("Comment"),
623 },
624 {
625 xtype: 'textfield',
626 name: 'target',
627 allowBlank: false,
628 fieldLabel: gettext("Target"),
629 },
630 {
631 xtype: 'proxmoxcheckbox',
632 checked: true,
633 name: 'original',
634 fieldLabel: gettext("send orig. Mail"),
635 },
636 ],
637
638 },
639 4007: {
640 onlineHelp: 'pmg_mailfilter_action',
641 xtype: 'proxmoxWindowEdit',
642 subdir: 'removeattachments',
643 subject: gettext('Remove Attachments'),
644 width: 500,
645 fieldDefaults: {
646 labelWidth: 150,
647 },
648 items: [
649 {
650 xtype: 'textfield',
651 name: 'name',
652 allowBlank: false,
653 fieldLabel: gettext('Name'),
654 },
655 {
656 xtype: 'textareafield',
657 name: 'info',
658 fieldLabel: gettext("Comment"),
659 },
660 {
661 xtype: 'textareafield',
662 name: 'text',
663 grow: true,
664 growMax: 250,
665 fieldLabel: gettext("Text Replacement"),
666 },
667 {
668 xtype: 'proxmoxcheckbox',
669 checked: true,
670 name: 'all',
671 fieldLabel: gettext("Remove all attachments"),
672 },
673 {
674 xtype: 'proxmoxcheckbox',
675 checked: false,
676 name: 'quarantine',
677 fieldLabel: gettext("Copy original mail to Attachment Quarantine"),
678 },
679 ],
680 },
681 4009: {
682 onlineHelp: 'pmg_mailfilter_action',
683 xtype: 'proxmoxWindowEdit',
684 subdir: 'disclaimer',
685 subject: gettext('Disclaimer'),
686 width: 400,
687 items: [
688 {
689 xtype: 'textfield',
690 name: 'name',
691 allowBlank: false,
692 fieldLabel: gettext('Name'),
693 },
694 {
695 xtype: 'textareafield',
696 name: 'info',
697 fieldLabel: gettext("Comment"),
698 },
699 {
700 xtype: 'textareafield',
701 name: 'disclaimer',
702 grow: true,
703 growMax: 250,
704 fieldLabel: gettext("Disclaimer"),
705 },
706 ],
707 },
708 },
709
710 updateLoginData: function(data) {
711 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
712 Proxmox.UserName = data.username;
713 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true);
714 },
715
716 quarantineActionExtracted: false,
717
718 extractQuarantineAction: function() {
719 if (PMG.Utils.quarantineActionExtracted) {
720 return null;
721 }
722
723 PMG.Utils.quarantineActionExtracted = true;
724
725 let qs = Ext.Object.fromQueryString(location.search);
726
727 let cselect = qs.cselect;
728 let action = qs.action;
729 let dateString = qs.date;
730
731 if (dateString) {
732 let date = new Date(dateString).getTime()/1000;
733
734 // set from date for QuarantineList
735 PMG.QuarantineList.from = date;
736 }
737
738 delete qs.cselect;
739 delete qs.action;
740 delete qs.ticket;
741 delete qs.date;
742
743 var newsearch = Ext.Object.toQueryString(qs);
744
745 var newurl = location.protocol + "//" + location.host + location.pathname;
746 if (newsearch) { newurl += '?' + newsearch; }
747 newurl += location.hash;
748
749 if (window.history) {
750 window.history.pushState({ path: newurl }, '', newurl);
751 }
752
753 if (action || cselect) {
754 return {
755 action: action,
756 cselect: cselect,
757 };
758 }
759 return null;
760 },
761
762 doQuarantineAction: function(action, id, callback) {
763 Proxmox.Utils.API2Request({
764 url: '/quarantine/content/',
765 params: {
766 action: action,
767 id: id,
768 },
769 method: 'POST',
770 failure: function(response, opts) {
771 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
772 },
773 success: function(response, opts) {
774 let count = id.split(';').length;
775 let fmt = count > 1
776 ? gettext("Action '{0}' for '{1}' items successful")
777 : gettext("Action '{0}' successful")
778 ;
779 let message = Ext.String.format(fmt, action, count);
780 let title = Ext.String.format("{0} successful", Ext.String.capitalize(action));
781
782 Ext.toast({
783 html: message,
784 title: title,
785 minWidth: 200,
786 hideDuration: 250,
787 slideBackDuration: 250,
788 slideBackAnimation: 'easeOut',
789 iconCls: 'fa fa-check',
790 shadow: true,
791 align: 'br',
792 });
793
794 if (Ext.isFunction(callback)) {
795 callback();
796 }
797 },
798 });
799 },
800
801 render_filetype: function(value) {
802 let iconCls = 'fa-file-o';
803 let text = Proxmox.Utils.unknownText;
804
805 if (!value) {
806 return `<i class='fa ${iconCls}'></i> ${text}`;
807 }
808
809 text = value.toString().toLowerCase();
810 const type = text.split('/')[0];
811
812 switch (type) {
813 case 'audio':
814 case 'image':
815 case 'video':
816 case 'text':
817 iconCls = `fa-file-${type}-o`;
818 break;
819 case 'application': {
820 const subtypes = ['excel', 'pdf', 'word', 'powerpoint'];
821 let found = subtypes.find(st => text.includes(st));
822 if (found !== undefined) {
823 iconCls = `fa-file-${found}-o`;
824 }
825 } break;
826 default:
827 break;
828 }
829
830 return `<i class='fa ${iconCls}'></i> ${text}`;
831 },
832
833 sender_renderer: function(value, metaData, rec) {
834 var subject = Ext.htmlEncode(value);
835 var from = Ext.htmlEncode(rec.data.from);
836 var sender = Ext.htmlEncode(rec.data.sender);
837 if (sender) {
838 from = Ext.String.format(gettext("{0} on behalf of {1}"),
839 sender, from);
840 }
841 return '<small>' + from + '</small><br>' + subject;
842 },
843
844 constructor: function() {
845 var me = this;
846
847 // do whatever you want here
848 Proxmox.Utils.override_task_descriptions({
849 applycustomscores: ['', gettext('Apply custom SpamAssassin scores')],
850 avupdate: ['', gettext('ClamAV update')],
851 backup: ['', gettext('Backup')],
852 clustercreate: ['', gettext('Create Cluster')],
853 clusterjoin: ['', gettext('Join Cluster')],
854 restore: ['', gettext('Restore')],
855 saupdate: ['', gettext('SpamAssassin update')],
856 });
857 },
858 });
859
860 Ext.define('PMG.Async', {
861 singleton: true,
862
863 // Returns a Promise which executes a quarantine action when awaited.
864 // Shows a Toast message box once completed, if batchNumber and batchTotal
865 // are set, they will be included into the title of that toast.
866 doQAction: function(action, ids, batchNumber, batchTotal) {
867 if (!Ext.isArray(ids)) {
868 ids = [ids];
869 }
870 return Proxmox.Async.api2({
871 url: '/quarantine/content/',
872 params: {
873 action: action,
874 id: ids.join(';'),
875 },
876 method: 'POST',
877 }).then(
878 response => {
879 let count = ids.length;
880 let fmt = count > 1
881 ? gettext("Action '{0}' for '{1}' items successful")
882 : gettext("Action '{0}' successful")
883 ;
884 let message = Ext.String.format(fmt, action, count);
885 let titleFmt = batchNumber !== undefined && batchTotal > 1
886 ? gettext("{0} ({1}/{2}) successful")
887 : gettext("{0} successful")
888 ;
889 let title = Ext.String.format(
890 titleFmt,
891 Ext.String.capitalize(action),
892 batchNumber,
893 batchTotal,
894 );
895
896 Ext.toast({
897 html: message,
898 title: title,
899 minWidth: 200,
900 hideDuration: 250,
901 slideBackDuration: 250,
902 slideBackAnimation: 'easeOut',
903 iconCls: 'fa fa-check',
904 shadow: true,
905 align: 'br',
906 });
907 },
908 response => Proxmox.Utils.alertResponseFailure(response),
909 );
910 },
911 });