[pmg-devel] [PATCH] SpamQuarantine: simplify download button by binding its href config

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Oct 25 12:28:12 CEST 2019


This avoids all the strange click event hack. We already have a
button where the users needs to click on, so just use that.

A viewModel with a downloadMailURL formula to assemble the href
allows us to just bind the "href" config from the Download button,
and be done with it..

refactors commit 7ad0de10d630f628b15969ef1478b9276e46197b

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

I've no idea why commit 7ad0de10d630f628b15969ef1478b9276e46197b went this
complicated and seemingly hacky route, the commit message isn't really
telling... So if there were any real reasons this was done this complicated
way: please holler me, I saw none..

 js/SpamQuarantine.js | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)

diff --git a/js/SpamQuarantine.js b/js/SpamQuarantine.js
index d1b32f0..fafe6d1 100644
--- a/js/SpamQuarantine.js
+++ b/js/SpamQuarantine.js
@@ -48,6 +48,15 @@ Ext.define('PMG.SpamQuarantine', {
     // from mail link
     cselect: undefined,
 
+    viewModel: {
+	parent: null,
+	data: {
+	    mailid: '',
+	},
+	formulas: {
+	    downloadMailURL: get => '/api2/json/quarantine/download?mailid=' + encodeURIComponent(get('mailid')),
+	},
+    },
     controller: {
 
 	xclass: 'Ext.app.ViewController',
@@ -147,6 +156,7 @@ Ext.define('PMG.SpamQuarantine', {
 	    }
 	    var rec = list.selModel.getSelection()[0];
 
+	    me.getViewModel().set('mailid', rec.data.id);
 	    me.updatePreview(me.raw || false, rec);
 	    me.lookupReference('spaminfo').setID(rec);
 	},
@@ -156,32 +166,6 @@ Ext.define('PMG.SpamQuarantine', {
 	    grid.setVisible(!grid.isVisible());
 	},
 
-	downloadEmail: function(btn) {
-	    var me = this;
-	    var list = this.lookupReference('list');
-	    var selection = list.selModel.getSelection();
-	    if (selection.length != 1) {
-		return; // multi download is not implemented
-	    }
-	    var rec = selection[0];
-	    var url = "/api2/json/quarantine/download?mailid=" +
-			encodeURIComponent(rec.data.id);
-	    var link = Ext.DomHelper.append(document.body, {
-		tag: 'a',
-		href: url,
-		css: 'display:none;visibility:hidden;height: 0px;',
-		download: rec.data.id + '.eml'
-	    });
-
-	    if (link.fireEvent) {
-		link.fireEvent('onclick');
-	    } else {
-		var evt = document.createEvent("MouseEvents");
-		evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
-		link.dispatchEvent(evt);
-	    }
-	},
-
 	openContextMenu: function(table, record, tr, index, event) {
 	    event.stopEvent();
 	    var me = this;
@@ -235,9 +219,6 @@ Ext.define('PMG.SpamQuarantine', {
 	    'button[reference=spam]': {
 		click: 'toggleSpamInfo'
 	    },
-	    'button[reference=download]': {
-		click: 'downloadEmail'
-	    },
 	    'pmgQuarantineList': {
 		selectionChange: 'onSelectMail',
 		itemkeypress: 'keyPress',
@@ -331,6 +312,7 @@ Ext.define('PMG.SpamQuarantine', {
 			    xtype: 'button',
 			    reference: 'download',
 			    text: gettext('Download'),
+			    bind: { href: '{downloadMailURL}' },
 			    iconCls: 'fa fa-download'
 			},
 			'-',
-- 
2.20.1




More information about the pmg-devel mailing list