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 {Function} success (optional) MAY fire on download completed (fails on attachments)..
24 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
25 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
26
27 */
28
29 Pman.Download = function(cfg)
30 {
31  
32     Roo.apply(this, cfg);
33      
34     if (this.grid) {
35         
36         this.buildFromGrid();
37         Roo.log(this);
38     }
39     
40     
41     if (cfg.newWindow) {
42             // as ie seems buggy...
43         window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
44         return ; 
45         
46     }
47    
48     
49     
50     var submit = false;
51     this.createCsvFrame();
52     
53     var requested = 0;
54      
55     Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
56     
57     
58     //--- simple method..
59     this.method = this.method || 'GET';
60     
61     if (this.method == 'GET' && !this.params) {
62         (function() {
63             submit = true;
64             this.csvFrame.src = cfg.url;
65             this.cleanup.defer(cfg.timeout || 30000,this);
66         }).defer(100, this);
67         
68        
69         return;
70     }
71     
72     
73     Roo.log("creating form?");
74     
75     var b = Roo.get(document.body);
76     this.form = b.createChild({
77         tag: 'form',
78         method : this.method,
79         action : this.url,
80         target : this.csvFrame.id,
81         enctype : 'multipart/form-data'
82
83
84         
85     });
86  
87     for(var i in this.params) {
88         
89         var el = this.form.createChild( {
90             ns : 'html',
91             tag : 'input',
92             
93             type: 'hidden',
94             name : i,
95             value : this.params[i]
96         });
97         
98         
99     }
100     
101     (function() {
102         submit = true;
103         this.form.dom.submit();
104         this.cleanup.defer(this.timeout || 30000,this);
105     }).defer(100, this);
106     
107      
108  
109 }
110
111 Roo.apply(Pman.Download.prototype, {
112     
113     /**
114      * @type {HTMLIframe} the iframe to download into.
115      */
116      
117     csvFrame : false,
118     
119     // private
120     form : false,
121     
122     // private..
123     createCsvFrame: function()
124     {
125         
126         if (this.csvFrame) {
127             document.body.removeChild(this.csvFrame);
128         }
129             
130         var id = Roo.id();
131         this.csvFrame = document.createElement('iframe');
132         this.csvFrame.id = id;
133         this.csvFrame.name = id;
134         this.csvFrame.className = 'x-hidden';
135         //if(Roo.isIE){
136             this.csvFrame.src = Roo.SSL_SECURE_URL;
137         //}
138         document.body.appendChild(this.csvFrame);
139
140         if(Roo.isIE){
141             document.frames[id].name = id;
142         }
143         
144     },
145     
146     onLoad : function()
147     {
148        // requested++; // second request is real one..
149        // if (requested < 2) {
150        //     return;
151         //} // n
152         if (!this.submit) {
153             return;
154         }
155         
156       
157         var frame = this.csvFrame;
158         var success  = true; 
159         try { 
160             var doc = Roo.isIE ? 
161                 frame.contentWindow.document : 
162                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
163             
164             
165             if(doc && doc.body && doc.body.innerHTML.length){
166               //  alert(doc.body.innerHTML);
167                   
168                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
169                 success  = false;
170                  
171                 
172             }
173             
174             Roo.log(doc.body.innerHTML);
175              
176         }
177         catch(e) {
178             Roo.log(e.toString());
179             Roo.log(e);
180         }
181         
182         this.cleanup();
183         
184         // this will never fire.. see 
185         // 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
186         if (this.success && success) {
187             
188             this.success();
189         }
190        
191         
192
193     },
194     
195     // private - clean up download elements.
196     cleanup :function()
197     {
198         if (this.form) {
199             this.form.remove();
200             this.form= false;
201         
202         }
203         
204         if (this.csvFrame) {
205             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
206             Roo.get(this.csvFrame).remove();
207             this.csvFrame= false;
208         }
209          
210     },
211     
212     buildFromGrid : function()
213     {
214         // get the params from beforeLoad
215         this.grid.ds.fireEvent('beforeload', this.grid.ds, {
216             params : this.params
217             
218         });
219         this.url = this.grid.ds.proxy.conn.url;
220         this.method = this.grid.ds.proxy.conn.method ;
221         
222         // work out the cols
223         Roo.each(this.grid.cm.config, function(c,i) {
224             params['csvCols['+i+']'] = c.dataIndex;
225             params['csvTitles['+i+']'] = c.header;
226             
227         });
228         
229         
230         
231         
232     }
233     
234     
235     
236     
237     
238      
239      
240 });