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