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         
92     }
93  
94  
95     for(var i in this.params) {
96         
97         var el = this.form.createChild( {
98             ns : 'html',
99             tag : 'input',
100             
101             type: 'hidden',
102             name : i,
103             value : this.params[i]
104         });
105         
106         
107     }
108     
109     (function() {
110         this.submit = true;
111         this.form.dom.submit();
112         this.cleanup.defer(this.timeout || 30000,this);
113     }).defer(100, this);
114     
115      
116  
117 }
118
119 Roo.apply(Pman.Download.prototype, {
120     
121     /**
122      * @type {HTMLIframe} the iframe to download into.
123      */
124      
125     csvFrame : false,
126     
127     // private
128     form : false,
129     
130     limit : 9999,
131     
132     newWindow : false,
133     
134     method : 'GET',
135     
136     // private..
137     createCsvFrame: function()
138     {
139         if (this.csvFrame) {
140             document.body.removeChild(this.csvFrame);
141         }
142             
143         var id = Roo.id();
144         this.csvFrame = document.createElement('iframe');
145         this.csvFrame.id = id;
146         this.csvFrame.name = id;
147         this.csvFrame.className = 'x-hidden';
148         //if(Roo.isIE){
149             this.csvFrame.src = Roo.SSL_SECURE_URL;
150         //}
151         document.body.appendChild(this.csvFrame);
152
153         if(Roo.isIE){
154             document.frames[id].name = id;
155         }
156         
157     },
158     
159     onLoad : function()
160     {
161        // requested++; // second request is real one..
162        // if (requested < 2) {
163        //     return;
164         //} // n
165         Roo.log('onload?');
166         if (!this.submit) {
167             return false;
168         }
169         return false;
170       
171         var frame = this.csvFrame;
172         var success  = true; 
173         try { 
174             var doc = Roo.isIE ? 
175                 frame.contentWindow.document : 
176                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
177             
178             
179             if(doc && doc.body && doc.body.innerHTML.length){
180               //  alert(doc.body.innerHTML);
181                   
182                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
183                 success  = false;
184                  
185                 
186             }
187             
188             Roo.log(doc.body.innerHTML);
189              
190         }
191         catch(e) {
192             Roo.log(e.toString());
193             Roo.log(e);
194         }
195         // we can not actually do anything with the frame... as it may actually still be downloading..
196         return true;
197     
198         this.cleanup();
199         
200         // this will never fire.. see 
201         // 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
202         if (this.success && success) {
203             
204             this.success();
205         }
206         return false;
207         
208
209     },
210     
211     // private - clean up download elements.
212     cleanup :function()
213     {
214         Roo.log('cleanup?');
215         if (this.form) {
216             this.form.remove();
217             this.form= false;
218         
219         }
220         
221         if (this.csvFrame) {
222             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
223             Roo.get(this.csvFrame).remove();
224             this.csvFrame= false;
225         }
226          
227     },
228     
229     buildFromGrid : function()
230     {
231         // get the params from beforeLoad
232         var ds = this.grid.ds;
233         ds.fireEvent('beforeload', ds, {
234             params : this.params
235             
236         });
237         
238          if(ds.sortInfo && ds.remoteSort){
239             var pn = ds.paramNames;
240             this.params[pn["sort"]] = ds.sortInfo.field;
241             this.params[pn["dir"]] = ds.sortInfo.direction;
242         }
243         if (ds.multiSort) {
244             var pn = ds.paramNames;
245             this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
246         }
247         
248         
249         
250         this.url = this.grid.ds.proxy.conn.url;
251         this.method = this.grid.ds.proxy.conn.method ;
252         var t = this;
253         // work out the cols
254         Roo.each(this.grid.cm.config, function(c,i) {
255             t.params['csvCols['+i+']'] = c.dataIndex;
256             t.params['csvTitles['+i+']'] = c.header;
257             
258         });
259         
260         if (this.grid.loadMask) {
261             this.grid.loadMask.onLoad();
262         }
263         this.params.limit = this.limit;
264         
265         // do it as a post, as args can get long..
266         this.params._get = 1;
267         this.method = 'POST';
268         
269     },
270     
271     createPdfEmbed : function()
272     {
273         
274     }
275      
276 });