1 /**
  2  * @fileOverview  
  3  * Copyright (c) 2013 Regione Autonoma della Sardegna
  4  * Published under the GPL license.<br>
  5  * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a>
  6  * for the full text of the license.
  7  * @version 0.1
  8  */
  9 
 10 /**
 11  * @namespace framework.data
 12  */
 13 Ext.namespace("framework.data");
 14 
 15 /** 
 16  *  
 17  *  @name_ DataExporter
 18  *  @class Define functions to help with data export.
 19  *  @constructor
 20  *  @extends <a target="_blank" href="http://docs.sencha.com/ext-js/3-4/#!/api/Ext.DomHelper">Ext.DomHelper</a>
 21  */
 22 framework.data.DataExporter = {
 23 
 24     /** Format data using Ext.ux.Exporter.
 25     * @public
 26     */  
 27     formatStore: function (store, config, base64Encode) {
 28         var formatter = new Ext.ux.Exporter[config.formatter]();
 29         var data = formatter.format(store, config);
 30         if (base64Encode) {
 31             data = Base64.encode(data);
 32         }
 33         return data;
 34     },
 35 
 36     /** Trigger file download by submitting data to a server script.
 37     * @public
 38     */ 
 39     download: function (data, config) {
 40         // See also http://www.sencha.com/forum/showthread.php?81897-FYI-Very-simple-approach-to-JS-triggered-file-downloads
 41         try {
 42             // Cleanup previous form if required
 43             Ext.destroy(Ext.get('hr_uploadForm'));
 44         }
 45         catch (e) {
 46         }
 47 
 48         var form = Ext.DomHelper.append(
 49                 document.body,
 50                 {
 51                     tag: 'form',
 52                     id: 'hr_uploadForm',
 53                     method: 'post',
 54                     /** DA VERIFICARE: Heron CGI URL, see /services/heron.cgi. */
 55                     action: Heron.globals.serviceUrl,
 56                     children: [
 57                         {tag: 'input', type: 'hidden', name: 'data', value: data},
 58                         // Server sends HTTP Header: Content-Disposition: attachment; filename="%s"' % filename
 59                         {tag: 'input', type: 'hidden', name: 'filename', value: config.fileName},
 60                         {tag: 'input', type: 'hidden', name: 'mime', value: config.mimeType}
 61                     ]
 62                 }
 63         );
 64 
 65         // Add Form to document and submit
 66         document.body.appendChild(form);
 67         form.submit();
 68     },
 69 
 70     /** Trigger file download by requesting data as attachment via hidden iframe (mainly GeoServer).
 71     * @public
 72     */
 73    directDownload: function (url) {
 74         // See also http://www.sencha.com/forum/showthread.php?81897-FYI-Very-simple-approach-to-JS-triggered-file-downloads
 75         try {
 76             // Cleanup previous iframe if required
 77             Ext.destroy(Ext.get('hr_directdownload'));
 78         }
 79         catch (e) {
 80         }
 81 
 82         // Create hidden iframe, this prevents that the browser opens a new window/document.
 83         var iframe = Ext.DomHelper.append(
 84                 document.body,
 85                 {
 86                     tag: 'iframe',
 87                     id: 'hr_directdownload',
 88                     name: 'hr_directdownload',
 89                     width: '0px',
 90                     height: '0px',
 91                     border: '0px',
 92                     style: 'width: 0; height: 0; border: none;',
 93                     src: url
 94                 }
 95         );
 96 
 97         // Add iframe to document and submit
 98         document.body.appendChild(iframe);
 99     }
100 };
101