Pman.Download.js
[Pman.Core] / Pman.Download.js
1 //<script type="text/javascript">
2 /**
3 * @class Pman.Download
4 * Handles file downloads in a hidden frame, or new window.
5 * Usage:
6 <pre><code>
7 var t = new Pman.Download({
8     url: baseURL + '/Images/Download/0/myfile.jpg',
9     newWindow : false,
10     params: { .... },
11     doctype: 'pdf' 
12     success : function() {
13         Roo.MessageBox.alert("File has downloaded");
14     }
15 });
16
17 </code></pre>
18
19 * @constructor
20 * @param {Object} cfg   Configuration object.
21 * @cfg {String} url     Location to download from.
22 * @cfg {String} method     GET or POST (default GET), POST will create a form, and post that into the hidden frame.
23 * @cfg {Boolean} newWindow (optional) download to new window
24 * @cfg {String} doctype (optional) download PDF to new window
25 * @cfg {Boolean} limit (optional) limit for grid downloads.
26  
27  * @cfg {String} csvCols  - use '*' to override grid coluns
28  * @cfg {String} csvTitles - use '*' to override grid coluns
29
30  
31  
32 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
33 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
34 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
35
36 */
37
38 Pman.Download = function(cfg)
39 {
40     
41     this.params = {};
42     
43     Roo.apply(this, cfg);
44      
45     if (this.grid) {
46         
47         this.buildFromGrid();
48         Roo.log(this);
49     }
50     
51     
52     if (this.newWindow && this.method == 'GET') {
53         // as ie seems buggy...
54         window.open( this.url + '?' + Roo.urlEncode(this.params || {}), '_blank');
55         return ; 
56         
57     }
58    
59     
60     
61     this.submit = false;
62     this.createCsvFrame();
63     
64     var requested = 0;
65      
66     Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
67     
68     
69     //--- simple method..
70     this.method = this.method || 'GET';
71     
72     if (this.method == 'GET' && !this.params) {
73         (function() {
74             this.submit = true;
75             this.csvFrame.src = cfg.url;
76             //this.cleanup.defer(cfg.timeout || 30000,this);
77         }).defer(100, this);
78         
79        
80         return;
81     }
82     
83     
84     Roo.log("creating form?");
85     
86     var b = Roo.get(document.body);
87     this.form = b.createChild({
88         tag: 'form',
89         name: 'testForm',
90         method : this.method,
91         action : this.url,
92         target : this.newWindow ? '_new' : this.csvFrame.id,
93         enctype : 'multipart/form-data'
94     });
95 //    
96 //    if(this.doctype == 'pdf'){
97 //        this.pdfEmbed = b.createChild({
98 //            tag: 'embed',
99 //            src : this.url,
100 //            pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
101 //            alt: this.doctype
102 //        });
103 //    }
104  
105     Roo.log(this.params);
106     for(var i in this.params) {
107         
108         var el = this.form.createChild( {
109             ns : 'html',
110             tag : 'input',
111             
112             type: 'hidden',
113             name : i,
114             value : this.params[i]
115         });
116         
117         
118     }
119     Roo.log(document);return;
120     (function() {
121         this.submit = true;
122         this.form.dom.submit();
123         this.cleanup.defer(this.timeout || 30000,this);
124     }).defer(100, this);
125     
126      
127  
128 }
129
130 Roo.apply(Pman.Download.prototype, {
131     
132     /**
133      * @type {HTMLIframe} the iframe to download into.
134      */
135      
136     csvFrame : false,
137     
138     // private
139     form : false,
140     
141     limit : 9999,
142     
143     newWindow : false,
144     
145     method : 'GET',
146     
147     // private..
148     createCsvFrame: function()
149     {
150         if (this.csvFrame) {
151             document.body.removeChild(this.csvFrame);
152         }
153             
154         var id = Roo.id();
155         this.csvFrame = document.createElement('iframe');
156         this.csvFrame.id = id;
157         this.csvFrame.name = id;
158         this.csvFrame.className = 'x-hidden';
159         //if(Roo.isIE){
160             this.csvFrame.src = Roo.SSL_SECURE_URL;
161         //}
162         document.body.appendChild(this.csvFrame);
163
164         if(Roo.isIE){
165             document.frames[id].name = id;
166         }
167         
168     },
169     
170     onLoad : function()
171     {
172        // requested++; // second request is real one..
173        // if (requested < 2) {
174        //     return;
175         //} // n
176         Roo.log('onload?');
177         if (!this.submit) {
178             return false;
179         }
180         return false;
181       
182         var frame = this.csvFrame;
183         var success  = true; 
184         try { 
185             var doc = Roo.isIE ? 
186                 frame.contentWindow.document : 
187                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
188             
189             
190             if(doc && doc.body && doc.body.innerHTML.length){
191               //  alert(doc.body.innerHTML);
192                   
193                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
194                 success  = false;
195                  
196                 
197             }
198             
199             Roo.log(doc.body.innerHTML);
200              
201         }
202         catch(e) {
203             Roo.log(e.toString());
204             Roo.log(e);
205         }
206         // we can not actually do anything with the frame... as it may actually still be downloading..
207         return true;
208     
209         this.cleanup();
210         
211         // this will never fire.. see 
212         // http://www.atalasoft.com/cs/blogs/jake/archive/2009/08/18/events-to-expect-when-dynamically-loading-iframes-in-javascript-take-2-thanks-firefox-3-5.aspx
213         if (this.success && success) {
214             
215             this.success();
216         }
217         return false;
218         
219
220     },
221     
222     // private - clean up download elements.
223     cleanup :function()
224     {
225         Roo.log('cleanup?');
226         if (this.form) {
227             this.form.remove();
228             this.form= false;
229         
230         }
231         
232         if (this.csvFrame) {
233             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
234             Roo.get(this.csvFrame).remove();
235             this.csvFrame= false;
236         }
237          
238     },
239     
240     buildFromGrid : function()
241     {
242         // get the params from beforeLoad
243         var ds = this.grid.ds;
244         ds.fireEvent('beforeload', ds, {
245             params : this.params
246             
247         });
248         
249          if(ds.sortInfo && ds.remoteSort){
250             var pn = ds.paramNames;
251             this.params[pn["sort"]] = ds.sortInfo.field;
252             this.params[pn["dir"]] = ds.sortInfo.direction;
253         }
254         if (ds.multiSort) {
255             var pn = ds.paramNames;
256             this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
257         }
258         
259         
260         
261         this.url = this.grid.ds.proxy.conn.url;
262         this.method = this.method || this.grid.ds.proxy.conn.method ;
263         var t = this;
264         // work out the cols
265         
266         if (this.csvCols) {
267             t.params.csvCols = this.csvCols;
268             t.params.csvTitles = this.csvTitles;
269         } else {
270             
271             Roo.each(this.grid.cm.config, function(c,i) {
272                 t.params['csvCols['+i+']'] = c.dataIndex;
273                 t.params['csvTitles['+i+']'] = c.header;
274                 
275             });
276         }
277         if (this.grid.loadMask) {
278             this.grid.loadMask.onLoad();
279         }
280         this.params.limit = this.limit;
281         
282         // do it as a post, as args can get long..
283         
284         this.method = this.method || 'POST';
285         if (this.method  == 'POST') {
286             this.params._get = 1;
287         }
288     }
289      
290 });