fix variables on update
[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 * @cfg {Boolean} showDownloading default false - show a dialog indicating that the file is downloading
27  * @cfg {String} csvCols  - use '*' to override grid coluns
28  * @cfg {String} csvTitles - use '*' to override grid coluns
29  * @cfg {String} hiddenCols - default 'show'  (use 'hide' to not display them on download)
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    
42     
43     this.params = {};
44     
45     Roo.apply(this, cfg);
46      
47     if (this.grid) {
48         
49         this.buildFromGrid();
50         Roo.log(this);
51     }
52     
53     
54     if (this.newWindow && this.method == 'GET') {
55         // as ie seems buggy...
56         window.open( this.url + '?' + Roo.urlEncode(this.params || {}), '_blank');
57         return ; 
58         
59     }
60    
61     
62     
63     //this.submit = false;
64     //this.createCsvFrame();
65     
66     //var requested = 0;
67      
68     //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
69     
70     
71     //--- simple method..
72     this.method = this.method || 'GET';
73     
74     if (this.method == 'GET' && !this.params) {
75         (function() {
76             
77             
78             this.createCsvFrame();
79             //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
80             this.submit = true;
81             this.csvFrame.src = cfg.url;
82             //this.cleanup.defer(cfg.timeout || 30000,this);
83         }).defer(100, this);
84         
85        
86         return;
87     }
88     
89     
90     Roo.log("creating form?");
91     
92     this.form = new FormData();
93     /*
94     var b = Roo.get(document.body);
95     this.form = b.createChild({
96         tag: 'form',
97         method : this.method,
98         action : this.url,
99         target : this.newWindow ? '_new' : this.csvFrame.id,
100         enctype : 'multipart/form-data'
101     });
102     **/
103 //    
104 //    if(this.doctype == 'pdf'){
105 //        this.pdfEmbed = b.createChild({
106 //            tag: 'embed',
107 //            src : this.url,
108 //            pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
109 //            alt: this.doctype
110 //        });
111 //    }
112  
113     //Roo.log(this.params);
114     this.params._get = 1; // always do a post request, with _get passed
115     for(var i in this.params) {
116         this.form.append(i, this.params[i]);
117         /*
118         var el = this.form.createChild( {
119             ns : 'html',
120             tag : 'input',
121             
122             type: 'hidden',
123             name : i,
124             value : this.params[i]
125         });
126         */
127         
128         
129     }
130     var req = new XMLHttpRequest();
131     req.responseType = 'blob';
132     
133     req.open('POST', this.url);
134     
135     var _t = this;
136     req.onload = function( ev )
137     {
138         if (req.status == 200) {
139             Roo.log(ev);
140             var cd = req.getResponseHeader('Content-Disposition');
141             
142             var filename = '';
143             var matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(cd);
144             if (matches != null && matches[1]) { 
145                 filename = matches[1].replace(/['"]/g, '');
146             }
147             
148             var blob = new Blob([req.response], {type: req.responseType });
149             
150             var a = document.createElement("a");
151             a.style = "display: none";
152             document.body.appendChild(a);
153             var url = window.URL.createObjectURL(blob);
154             a.href = url;
155             a.download = filename;
156              a.click();
157             //release the reference to the file by revoking the Object URL
158             window.URL.revokeObjectURL(url);
159             
160             _t.success ? _t.success(ev) : '';
161         } else {
162             _t.failure ? _t.failure(ev) : '';
163         }
164         
165     }
166     if (this.showDownloading) {
167         Roo.MessageBox.alert("Downloading", "The file should download shortly");
168     }
169     req.send(this.form);
170     /*
171     (function() {
172         this.submit = true;
173         this.form.dom.submit();
174         this.cleanup.defer(this.timeout || 30000,this);
175     }).defer(100, this);
176     */
177      
178  
179 }
180
181 Roo.apply(Pman.Download.prototype, {
182     
183     /**
184      * @type {HTMLIframe} the iframe to download into.
185      */
186      
187     csvFrame : false,
188     
189     // private
190     form : false,
191     
192     limit : 9999,
193     
194     newWindow : false,
195     
196     method : 'GET',
197     
198     success : false,
199     failure : false,
200     
201     hiddenCols : 'show', // set to 'hide' to hide them..
202     
203     showDownloading : false,
204     // private..
205     //used by simple GET method.
206     createCsvFrame: function()
207     {
208         if (this.csvFrame) {
209             document.body.removeChild(this.csvFrame);
210         }
211             
212         var id = Roo.id();
213         this.csvFrame = document.createElement('iframe');
214         this.csvFrame.id = id;
215         this.csvFrame.name = id;
216         this.csvFrame.className = 'x-hidden';
217         //if(Roo.isIE){
218             this.csvFrame.src = Roo.SSL_SECURE_URL;
219         //}
220         document.body.appendChild(this.csvFrame);
221
222         if(Roo.isIE){
223             document.frames[id].name = id;
224         }
225         
226     },
227     /* not used as it didn't work..
228     onLoad : function()
229     {
230        // requested++; // second request is real one..
231        // if (requested < 2) {
232        //     return;
233         //} // n
234         Roo.log('onload?');
235         if (!this.submit) {
236             return false;
237         }
238         //return false;
239       
240         var frame = this.csvFrame;
241         var success  = true; 
242         try { 
243             var doc = Roo.isIE ? 
244                 frame.contentWindow.document : 
245                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
246             
247             
248             if(doc && doc.body && doc.body.innerHTML.length){
249               //  alert(doc.body.innerHTML);
250                   
251                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
252                 success  = false;
253                 if (this.failure) {
254                     this.failure();
255                 }
256                 return true;
257             }
258             
259             Roo.log(doc.body.innerHTML);
260              
261         }
262         catch(e) {
263             Roo.log(e.toString());
264             Roo.log(e);
265         }
266         if (this.success) {
267             this.success();
268         }
269         // we can not actually do anything with the frame... as it may actually still be downloading..
270         return true;
271     
272         this.cleanup();
273         
274         // this will never fire.. see 
275         // 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
276         if (this.success && success) {
277             
278             this.success();
279         }
280         return false;
281         
282
283     },
284      */
285     // private - clean up download elements.
286     cleanup :function()
287     {
288        /* Roo.log('cleanup?');
289         if (this.form) {
290             this.form.remove();
291             this.form= false;
292         
293         }
294         
295         if (this.csvFrame) {
296             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
297             Roo.get(this.csvFrame).remove();
298             this.csvFrame= false;
299         }
300         */
301          
302     },
303     
304     buildFromGrid : function()
305     {
306         // get the params from beforeLoad
307         var ds = this.grid.ds;
308         ds.fireEvent('beforeload', ds, {
309             params : this.params
310             
311         });
312         
313          if(ds.sortInfo && ds.remoteSort){
314             var pn = ds.paramNames;
315             this.params[pn["sort"]] = ds.sortInfo.field;
316             this.params[pn["dir"]] = ds.sortInfo.direction;
317         }
318         if (ds.multiSort) {
319             var pn = ds.paramNames;
320             this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
321         }
322         
323         
324         
325         this.url = this.grid.ds.proxy.conn.url;
326         this.method = this.method || this.grid.ds.proxy.conn.method ;
327         var t = this;
328         // work out the cols
329         
330         if (this.csvCols) {
331             t.params.csvCols = this.csvCols;
332             t.params.csvTitles = this.csvTitles;
333         } else {
334             
335             Roo.each(this.grid.cm.config, function(c,i) {
336                 
337                 if (t.hiddenCols != 'show' && t.grid.cm.isHidden(i)) {
338                     return;
339                 }
340                 
341                 t.params['csvCols['+i+']'] = c.dataIndex;
342                 t.params['csvTitles['+i+']'] = c.header;
343                 
344             });
345         }
346         if (this.grid.loadMask) {
347             this.grid.loadMask.onLoad();
348         }
349         this.params.limit = this.limit;
350         
351         // do it as a post, as args can get long..
352         
353         this.method =   'POST';
354         if (this.method  == 'POST') {
355             this.params._get = 1;
356         }
357     }
358      
359 });