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