Merge branch 'master' of http://git.roojs.com:8081/Pman.Core
[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     success : function() {
12         Roo.MessageBox.alert("File has downloaded");
13     }
14 });
15
16 </code></pre>
17
18 * @constructor
19 * @param {Object} cfg   Configuration object.
20 * @cfg {String} url     Location to download from.
21 * @cfg {String} method     GET or POST (default GET), POST will create a form, and post that into the hidden frame.
22 * @cfg {Boolean} newWindow (optional) download to new window
23 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
24 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
25 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
26
27 */
28
29 Pman.Download = function(cfg)
30 {
31     
32     this.params = {};
33     
34     Roo.apply(this, cfg);
35      
36     if (this.grid) {
37         
38         this.buildFromGrid();
39         Roo.log(this);
40     }
41     
42     
43     if (cfg.newWindow) {
44             // as ie seems buggy...
45         window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
46         return ; 
47         
48     }
49    
50     
51     
52     this.submit = false;
53     this.createCsvFrame();
54     
55     var requested = 0;
56      
57     Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
58     
59     
60     //--- simple method..
61     this.method = this.method || 'GET';
62     
63     if (this.method == 'GET' && !this.params) {
64         (function() {
65             this.submit = true;
66             this.csvFrame.src = cfg.url;
67             //this.cleanup.defer(cfg.timeout || 30000,this);
68         }).defer(100, this);
69         
70        
71         return;
72     }
73     
74     
75     Roo.log("creating form?");
76     
77     var b = Roo.get(document.body);
78     this.form = b.createChild({
79         tag: 'form',
80         method : this.method,
81         action : this.url,
82         target : this.csvFrame.id,
83         enctype : 'multipart/form-data'
84
85
86         
87     });
88  
89     for(var i in this.params) {
90         
91         var el = this.form.createChild( {
92             ns : 'html',
93             tag : 'input',
94             
95             type: 'hidden',
96             name : i,
97             value : this.params[i]
98         });
99         
100         
101     }
102     
103     (function() {
104         this.submit = true;
105         this.form.dom.submit();
106         this.cleanup.defer(this.timeout || 30000,this);
107     }).defer(100, this);
108     
109      
110  
111 }
112
113 Roo.apply(Pman.Download.prototype, {
114     
115     /**
116      * @type {HTMLIframe} the iframe to download into.
117      */
118      
119     csvFrame : false,
120     
121     // private
122     form : false,
123     
124     // private..
125     createCsvFrame: function()
126     {
127         
128         if (this.csvFrame) {
129             document.body.removeChild(this.csvFrame);
130         }
131             
132         var id = Roo.id();
133         this.csvFrame = document.createElement('iframe');
134         this.csvFrame.id = id;
135         this.csvFrame.name = id;
136         this.csvFrame.className = 'x-hidden';
137         //if(Roo.isIE){
138             this.csvFrame.src = Roo.SSL_SECURE_URL;
139         //}
140         document.body.appendChild(this.csvFrame);
141
142         if(Roo.isIE){
143             document.frames[id].name = id;
144         }
145         
146     },
147     
148     onLoad : function()
149     {
150        // requested++; // second request is real one..
151        // if (requested < 2) {
152        //     return;
153         //} // n
154         Roo.log('onload?');
155         if (!this.submit) {
156             return false;
157         }
158         return false;
159       
160         var frame = this.csvFrame;
161         var success  = true; 
162         try { 
163             var doc = Roo.isIE ? 
164                 frame.contentWindow.document : 
165                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
166             
167             
168             if(doc && doc.body && doc.body.innerHTML.length){
169               //  alert(doc.body.innerHTML);
170                   
171                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
172                 success  = false;
173                  
174                 
175             }
176             
177             Roo.log(doc.body.innerHTML);
178              
179         }
180         catch(e) {
181             Roo.log(e.toString());
182             Roo.log(e);
183         }
184         // we can not actually do anything with the frame... as it may actually still be downloading..
185         return true;
186     
187         this.cleanup();
188         
189         // this will never fire.. see 
190         // 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
191         if (this.success && success) {
192             
193             this.success();
194         }
195         return false;
196         
197
198     },
199     
200     // private - clean up download elements.
201     cleanup :function()
202     {
203         Roo.log('cleanup?');
204         if (this.form) {
205             this.form.remove();
206             this.form= false;
207         
208         }
209         
210         if (this.csvFrame) {
211             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
212             Roo.get(this.csvFrame).remove();
213             this.csvFrame= false;
214         }
215          
216     },
217     
218     buildFromGrid : function()
219     {
220         // get the params from beforeLoad
221         this.grid.ds.fireEvent('beforeload', this.grid.ds, {
222             params : this.params
223             
224         });
225         this.url = this.grid.ds.proxy.conn.url;
226         this.method = this.grid.ds.proxy.conn.method ;
227         var t = this;
228         // work out the cols
229         Roo.each(this.grid.cm.config, function(c,i) {
230             t.params['csvCols['+i+']'] = c.dataIndex;
231             t.params['csvTitles['+i+']'] = c.header;
232             
233         });
234         if (this.grid.loadMask) {
235             this.grid.loadMask.onLoad();
236         }
237         this.params.limit = 9999;
238         
239         
240     }
241     
242     
243     
244     
245     
246      
247      
248 });