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