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