SimpleExcel.php
[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     doctype: 'pdf' 
12     success : function() {
13         Roo.MessageBox.alert("File has downloaded");
14     }
15 });
16
17 </code></pre>
18
19 * @constructor
20 * @param {Object} cfg   Configuration object.
21 * @cfg {String} url     Location to download from.
22 * @cfg {String} method     GET or POST (default GET), POST will create a form, and post that into the hidden frame.
23 * @cfg {Boolean} newWindow (optional) download to new window
24 * @cfg {String} doctype (optional) download PDF to new window
25 * @cfg {Boolean} limit (optional) limit for grid downloads.
26  
27  * @cfg {String} csvCols  - use '*' to override grid coluns
28  * @cfg {String} csvTitles - use '*' to override grid coluns
29
30  
31  
32 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
33 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
34 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
35
36 */
37
38 Pman.Download = function(cfg)
39 {
40     
41     this.params = {};
42     
43     Roo.apply(this, cfg);
44      
45     if (this.grid) {
46         
47         this.buildFromGrid();
48         Roo.log(this);
49     }
50     
51     
52     if (this.newWindow && this.method == 'GET') {
53         // as ie seems buggy...
54         window.open( this.url + '?' + Roo.urlEncode(this.params || {}), '_blank');
55         return ; 
56         
57     }
58    
59     
60     
61     this.submit = false;
62     this.createCsvFrame();
63     
64     var requested = 0;
65      
66     Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
67     
68     
69     //--- simple method..
70     this.method = this.method || 'GET';
71     
72     if (this.method == 'GET' && !this.params) {
73         (function() {
74             this.submit = true;
75             this.csvFrame.src = cfg.url;
76             //this.cleanup.defer(cfg.timeout || 30000,this);
77         }).defer(100, this);
78         
79        
80         return;
81     }
82     
83     
84     Roo.log("creating form?");
85     
86     var b = Roo.get(document.body);
87     this.form = b.createChild({
88         tag: 'form',
89         method : this.method,
90         action : this.url,
91         target : this.newWindow ? '_new' : this.csvFrame.id,
92         enctype : 'multipart/form-data'
93     });
94 //    
95 //    if(this.doctype == 'pdf'){
96 //        this.pdfEmbed = b.createChild({
97 //            tag: 'embed',
98 //            src : this.url,
99 //            pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
100 //            alt: this.doctype
101 //        });
102 //    }
103  
104     Roo.log(this.params);
105     for(var i in this.params) {
106         
107         var el = this.form.createChild( {
108             ns : 'html',
109             tag : 'input',
110             
111             type: 'hidden',
112             name : i,
113             value : this.params[i]
114         });
115         
116         
117     }
118     
119     (function() {
120         this.submit = true;
121         this.form.dom.submit();
122         this.cleanup.defer(this.timeout || 30000,this);
123     }).defer(100, this);
124     
125      
126  
127 }
128
129 Roo.apply(Pman.Download.prototype, {
130     
131     /**
132      * @type {HTMLIframe} the iframe to download into.
133      */
134      
135     csvFrame : false,
136     
137     // private
138     form : false,
139     
140     limit : 9999,
141     
142     newWindow : false,
143     
144     method : 'GET',
145     
146     // private..
147     createCsvFrame: function()
148     {
149         if (this.csvFrame) {
150             document.body.removeChild(this.csvFrame);
151         }
152             
153         var id = Roo.id();
154         this.csvFrame = document.createElement('iframe');
155         this.csvFrame.id = id;
156         this.csvFrame.name = id;
157         this.csvFrame.className = 'x-hidden';
158         //if(Roo.isIE){
159             this.csvFrame.src = Roo.SSL_SECURE_URL;
160         //}
161         document.body.appendChild(this.csvFrame);
162
163         if(Roo.isIE){
164             document.frames[id].name = id;
165         }
166         
167     },
168     
169     onLoad : function()
170     {
171        // requested++; // second request is real one..
172        // if (requested < 2) {
173        //     return;
174         //} // n
175         Roo.log('onload?');
176         if (!this.submit) {
177             return false;
178         }
179         return false;
180       
181         var frame = this.csvFrame;
182         var success  = true; 
183         try { 
184             var doc = Roo.isIE ? 
185                 frame.contentWindow.document : 
186                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
187             
188             
189             if(doc && doc.body && doc.body.innerHTML.length){
190               //  alert(doc.body.innerHTML);
191                   
192                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
193                 success  = false;
194                  
195                 
196             }
197             
198             Roo.log(doc.body.innerHTML);
199              
200         }
201         catch(e) {
202             Roo.log(e.toString());
203             Roo.log(e);
204         }
205         // we can not actually do anything with the frame... as it may actually still be downloading..
206         return true;
207     
208         this.cleanup();
209         
210         // this will never fire.. see 
211         // 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
212         if (this.success && success) {
213             
214             this.success();
215         }
216         return false;
217         
218
219     },
220     
221     // private - clean up download elements.
222     cleanup :function()
223     {
224         Roo.log('cleanup?');
225         if (this.form) {
226             this.form.remove();
227             this.form= false;
228         
229         }
230         
231         if (this.csvFrame) {
232             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
233             Roo.get(this.csvFrame).remove();
234             this.csvFrame= false;
235         }
236          
237     },
238     
239     buildFromGrid : function()
240     {
241         // get the params from beforeLoad
242         var ds = this.grid.ds;
243         ds.fireEvent('beforeload', ds, {
244             params : this.params
245             
246         });
247         
248          if(ds.sortInfo && ds.remoteSort){
249             var pn = ds.paramNames;
250             this.params[pn["sort"]] = ds.sortInfo.field;
251             this.params[pn["dir"]] = ds.sortInfo.direction;
252         }
253         if (ds.multiSort) {
254             var pn = ds.paramNames;
255             this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
256         }
257         
258         
259         
260         this.url = this.grid.ds.proxy.conn.url;
261         this.method = this.method || this.grid.ds.proxy.conn.method ;
262         var t = this;
263         // work out the cols
264         
265         if (this.csvCols) {
266             t.params.csvCols = this.csvCols;
267             t.params.csvTitles = this.csvTitles;
268         } else {
269             
270             Roo.each(this.grid.cm.config, function(c,i) {
271                 t.params['csvCols['+i+']'] = c.dataIndex;
272                 t.params['csvTitles['+i+']'] = c.header;
273                 
274             });
275         }
276         if (this.grid.loadMask) {
277             this.grid.loadMask.onLoad();
278         }
279         this.params.limit = this.limit;
280         
281         // do it as a post, as args can get long..
282         
283         this.method = this.method || 'POST';
284         if (this.method  == 'POST') {
285             this.params._get = 1;
286         }
287     }
288      
289 });