From 4e30bcaec76b27c8ea5863b204e1021fa538bcd3 Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <renaud.legac@free.fr>
Date: Sun, 27 Mar 2011 16:42:47 +0000
Subject: [PATCH] Add a new class to download file pressing a button.

---
 static/plugin_dbui/src/appbuttondownload.js | 62 +++++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 static/plugin_dbui/src/appbuttondownload.js

diff --git a/static/plugin_dbui/src/appbuttondownload.js b/static/plugin_dbui/src/appbuttondownload.js
new file mode 100644
index 00000000..2b940e79
--- /dev/null
+++ b/static/plugin_dbui/src/appbuttondownload.js
@@ -0,0 +1,62 @@
+/**
+ * A button to download file from the server
+ * 
+ * @extends Ext.Button
+ * @version $Id$
+ *
+ */
+
+Ext.namespace('App');
+
+App.ButtonDownload = Ext.extend(Ext.Button, {
+
+    /**
+     * @cfg{String} url  pointing to the server controller  
+     * which will return the file
+     */    
+    url: undefined,
+    
+     /**
+     * private method requests by the component model of ExtJS
+     */
+    initComponent: function () {
+        
+        // initialize the underlying class
+        App.ButtonDownload.superclass.initComponent.call(this);
+        
+        // download when the button is pressed
+        this.on('click', this.onDownload, this);
+    },
+    
+    /**
+     * Handler to download the file
+     * The scope is the button.
+     * 
+     * NOTE: this method add and iframe at the end of the DOM document
+     * the source of the iframe is the url pointing on the server conroller.
+     * The latter returun a document which is automatically loaded.
+     * 
+     * Method suggested on the sencha forum:
+     * www.sencha.com/forum/showthread.php?26471-Download-File-on%28-click-%29
+     */
+    onDownload: function (button, event) {
+    
+        // remove existing iframe
+        try {
+            Ext.destroy(Ext.get('downloadIframe'));
+        } catch (err) {}
+        
+        // create a fresh iframe
+        Ext.DomHelper.append(document.body, {
+            tag: 'iframe',
+            id: 'downloadIframe',
+            frameBorder: 0,
+            width: 0,
+            height: 0,
+            css: 'display:none;visibility:hidden;height:0px;',
+            src: this.url
+        });
+    }   
+});
+
+Ext.reg('xbuttondownload', App.ButtonDownload);
-- 
GitLab