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