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