]> git.proxmox.com Git - pmg-gui.git/blobdiff - js/Utils.js
disclaimer edit: add position comobobox
[pmg-gui.git] / js / Utils.js
index abd910dbe545819f73cf44e749b71d7fb0589e13..974df1fc0f64a94b22461e726855ab2c15960c9a 100644 (file)
@@ -8,6 +8,33 @@ Ext.define('PMG.Utils', {
 
     // this singleton contains miscellaneous utilities
 
+    // use in panels with object spread (...) operator, for example:
+    // ...PMG.Utils.onlineHelpTool('sysadmin_certificate_management'),
+    onlineHelpTool: function(blockid) {
+       let info = Proxmox.Utils.get_help_info(blockid);
+       if (info === undefined) {
+           info = Proxmox.Utils.get_help_info('pmg_documentation_index');
+           if (info === undefined) {
+               throw "get_help_info failed"; // should not happen
+           }
+       }
+
+       let docsURI = window.location.origin + info.link;
+       let title = info.title || gettext('Help');
+       if (info.subtitle) {
+           title += ' - ' + info.subtitle;
+       }
+       return {
+           tools: [
+               {
+                   type: 'help',
+                   tooltip: title,
+                   handler: () => window.open(docsURI),
+               },
+           ],
+       };
+    },
+
     senderText: gettext('Sender'),
     receiverText: gettext('Receiver'),
     scoreText: gettext('Score'),
@@ -102,14 +129,12 @@ Ext.define('PMG.Utils', {
     },
 
     format_otype: function(otype) {
-       var editor = PMG.Utils.object_editors[otype];
-       var iconCls = 'fa fa-question-circle';
+       let editor = PMG.Utils.object_editors[otype];
+       let iconCls = 'fa fa-question-circle';
        if (editor) {
-           var icon = '<span class="fa-fw ' + (editor.iconCls || iconCls) + '"></span> ';
-           return icon + editor.subject;
+           return `<span class="fa-fw ${editor.iconCls || iconCls}"></span> ${editor.subject}`;
        }
-
-       return '<span class="fa-fw ' + iconCls + '"></span> unknown';
+       return `<span class="fa-fw ${iconCls}"></span> unknown`;
     },
 
     format_ldap_protocol: function(p) {
@@ -121,7 +146,7 @@ Ext.define('PMG.Utils', {
     },
 
     convert_field_to_per_min: function(value, record) {
-       return value/(record.data.timespan/60);
+       return value / (record.data.timespan / 60);
     },
 
     object_editors: {
@@ -136,14 +161,12 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'textfield',
                    name: 'regex',
-                   labelWidth: 150,
                    reference: 'regex',
-                   fieldLabel: gettext("Regular Expression"),
+                   fieldLabel: gettext("Regex"),
                },
                {
-                   labelWidth: 150,
-                   fieldLabel: gettext('Test String'),
                    xtype: 'pmgRegexTester',
+                   fieldLabel: gettext('Test String'),
                    wholeMatch: true,
                    regexFieldReference: 'regex',
                },
@@ -175,8 +198,7 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'textfield',
                    name: 'regex',
-                   labelWidth: 150,
-                   fieldLabel: gettext("Regular Expression"),
+                   fieldLabel: gettext("Regex"),
                },
            ],
        },
@@ -334,22 +356,19 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'textfield',
                    name: 'field',
-                   labelWidth: 150,
                    allowBlank: false,
                    fieldLabel: gettext('Field'),
                },
                {
                    xtype: 'textfield',
-                   reference: 'value',
                    name: 'value',
-                   labelWidth: 150,
+                   reference: 'value',
                    allowBlank: false,
                    fieldLabel: gettext('Value'),
                },
                {
-                   labelWidth: 150,
-                   fieldLabel: gettext('Test String'),
                    xtype: 'pmgRegexTester',
+                   fieldLabel: gettext('Test String'),
                    regexFieldReference: 'value',
                },
            ],
@@ -365,7 +384,6 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'combobox',
                    displayField: 'text',
-                   labelWidth: 150,
                    valueField: 'mimetype',
                    name: 'contenttype',
                    editable: true,
@@ -390,7 +408,6 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'displayfield',
                    fieldLabel: gettext('Value'),
-                   labelWidth: 150,
                    allowBlank: false,
                    reset: Ext.emptyFn,
                },
@@ -409,14 +426,12 @@ Ext.define('PMG.Utils', {
                    name: 'filename',
                    reference: 'filename',
                    fieldLabel: gettext('Filename'),
-                   labelWidth: 150,
                    allowBlank: false,
                },
                {
-                   labelWidth: 150,
+                   xtype: 'pmgRegexTester',
                    fieldLabel: gettext('Test String'),
                    wholeMatch: true,
-                   xtype: 'pmgRegexTester',
                    regexFieldReference: 'filename',
                },
            ],
@@ -432,7 +447,6 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'combobox',
                    displayField: 'text',
-                   labelWidth: 150,
                    valueField: 'mimetype',
                    name: 'contenttype',
                    editable: true,
@@ -457,7 +471,6 @@ Ext.define('PMG.Utils', {
                {
                    xtype: 'displayfield',
                    fieldLabel: gettext('Value'),
-                   labelWidth: 150,
                    allowBlank: false,
                    reset: Ext.emptyFn,
                },
@@ -476,14 +489,12 @@ Ext.define('PMG.Utils', {
                    name: 'filename',
                    reference: 'filename',
                    fieldLabel: gettext('Filename'),
-                   labelWidth: 150,
                    allowBlank: false,
                },
                {
-                   labelWidth: 150,
+                   xtype: 'pmgRegexTester',
                    fieldLabel: gettext('Test String'),
                    wholeMatch: true,
-                   xtype: 'pmgRegexTester',
                    regexFieldReference: 'filename',
                },
            ],
@@ -606,7 +617,7 @@ Ext.define('PMG.Utils', {
                    xtype: 'proxmoxcheckbox',
                    checked: true,
                    name: 'original',
-                   fieldLabel: gettext("send orig. Mail"),
+                   boxLabel: gettext("Send Original Mail"),
                },
            ],
 
@@ -617,9 +628,6 @@ Ext.define('PMG.Utils', {
            subdir: 'removeattachments',
            subject: gettext('Remove Attachments'),
            width: 500,
-           fieldDefaults: {
-               labelWidth: 150,
-           },
            items: [
                {
                    xtype: 'textfield',
@@ -643,13 +651,13 @@ Ext.define('PMG.Utils', {
                    xtype: 'proxmoxcheckbox',
                    checked: true,
                    name: 'all',
-                   fieldLabel: gettext("Remove all attachments"),
+                   boxLabel: gettext("Remove all Attachments"),
                },
                {
                    xtype: 'proxmoxcheckbox',
                    checked: false,
                    name: 'quarantine',
-                   fieldLabel: gettext("Copy orignal mail to Attachment Quarantine"),
+                   boxLabel: gettext("Copy original mail to Attachment Quarantine"),
                },
            ],
        },
@@ -678,6 +686,17 @@ Ext.define('PMG.Utils', {
                    growMax: 250,
                    fieldLabel: gettext("Disclaimer"),
                },
+               {
+                   xtype: 'proxmoxKVComboBox',
+                   name: 'position',
+                   fieldLabel: gettext("Position"),
+                   deleteEmpty: false,
+                   value: 'end',
+                   comboItems: [
+                       ['end', gettext('End')],
+                       ['start', gettext('Start')],
+                   ],
+               },
            ],
        },
     },
@@ -763,6 +782,7 @@ Ext.define('PMG.Utils', {
                    slideBackAnimation: 'easeOut',
                    iconCls: 'fa fa-check',
                    shadow: true,
+                   align: 'br',
                });
 
                if (Ext.isFunction(callback)) {
@@ -804,20 +824,97 @@ Ext.define('PMG.Utils', {
        return `<i class='fa ${iconCls}'></i> ${text}`;
     },
 
-    sender_renderer: function(value, metaData, rec) {
-       var subject = Ext.htmlEncode(value);
-       var from = Ext.htmlEncode(rec.data.from);
-       var sender = Ext.htmlEncode(rec.data.sender);
-       if (sender) {
-           from = Ext.String.format(gettext("{0} on behalf of {1}"),
-                                    sender, from);
+    render_envelope: function(value, { data }, render_receiver) {
+       let subject = Ext.htmlEncode(value);
+       let from = Ext.htmlEncode(data.from);
+       if (data.sender) {
+           let sender = Ext.htmlEncode(data.sender);
+           from = Ext.String.format(gettext("{0} on behalf of {1}"), sender, from);
        }
-       return '<small>' + from + '</small><br>' + subject;
+       if (render_receiver) {
+           let receiver = Ext.htmlEncode(data.receiver);
+           return `<small>${from}<br>To: ${receiver}</small><br>${subject}`;
+       }
+       return `<small>${from}</small><br>${subject}`;
     },
 
+    render_sender: (value, _meta, rec) => PMG.Utils.render_envelope(value, rec, false),
+    render_sender_receiver: (value, _meta, rec) => PMG.Utils.render_envelope(value, rec, true),
+
     constructor: function() {
        var me = this;
 
        // do whatever you want here
+       Proxmox.Utils.override_task_descriptions({
+           applycustomscores: ['', gettext('Apply custom SpamAssassin scores')],
+           avupdate: ['', gettext('ClamAV update')],
+           backup: ['', gettext('Backup')],
+           clustercreate: ['', gettext('Create Cluster')],
+           clusterjoin: ['', gettext('Join Cluster')],
+           restore: ['', gettext('Restore')],
+           saupdate: ['', gettext('SpamAssassin update')],
+       });
+    },
+});
+
+Ext.define('PMG.Async', {
+    singleton: true,
+
+    // Returns a Promise which executes a quarantine action when awaited.
+    // Shows a Toast message box once completed, if batchNumber and batchTotal
+    // are set, they will be included into the title of that toast.
+    doQAction: function(action, ids, batchNumber, batchTotal) {
+       if (!Ext.isArray(ids)) {
+           ids = [ids];
+       }
+       return Proxmox.Async.api2({
+           url: '/quarantine/content/',
+           params: {
+               action: action,
+               id: ids.join(';'),
+           },
+           method: 'POST',
+       }).then(
+           response => {
+               let count = ids.length;
+               let fmt = count > 1
+                   ? gettext("Action '{0}' for '{1}' items successful")
+                   : gettext("Action '{0}' successful")
+                   ;
+               let message = Ext.String.format(fmt, action, count);
+               let titleFmt = batchNumber !== undefined && batchTotal > 1
+                   ? gettext("{0} ({1}/{2}) successful")
+                   : gettext("{0} successful")
+                   ;
+               let title = Ext.String.format(
+                   titleFmt,
+                   Ext.String.capitalize(action),
+                   batchNumber,
+                   batchTotal,
+               );
+
+               Ext.toast({
+                   html: message,
+                   title: title,
+                   minWidth: 200,
+                   hideDuration: 250,
+                   slideBackDuration: 250,
+                   slideBackAnimation: 'easeOut',
+                   iconCls: 'fa fa-check',
+                   shadow: true,
+                   align: 'br',
+               });
+           },
+           response => Proxmox.Utils.alertResponseFailure(response),
+       );
+    },
+});
+
+// custom Vtypes
+Ext.apply(Ext.form.field.VTypes, {
+    // matches the pmg-email-address in pmg-api
+    PMGMail: function(v) {
+       return (/[^\s\\@]+@[^\s/\\@]+/).test(v);
     },
+    PMGMailText: gettext('Example') + ": user@example.com",
 });