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