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