@@ -4762,8 +4762,7 @@ this.recline.View = this.recline.View || {};
47624762 <label>' +
47634763 ckan . i18n . _ ( "Format" ) +
47644764 '</label> \
4765- <select class="select-format form-control"> \
4766- <option value="csv">CSV</option> \
4765+ <select id="download-format" class="select-format form-control"> \
47674766 </select> \
47684767 <input class="form-control extract-data-input" type="hidden" id="extract_data" name="extract_data" value=""> \
47694768 </fieldset> \
@@ -4781,38 +4780,45 @@ this.recline.View = this.recline.View || {};
47814780 "click .extract-button" : "onExtract" ,
47824781 } ,
47834782 initialize : function ( ) {
4783+ var self = this ;
4784+ const DATASTORE_SEARCH_ROWS_MAX = 3200 ; //TODO: Get param from environment/backend
4785+ // console.log(self.options);
47844786 _ . bindAll ( this , "render" ) ;
47854787 this . render ( ) ;
4788+ //Timeout of 2 seconds ensures that the form has been rendered before the select element is accessed
4789+ setTimeout ( ( ) => {
4790+ document . getElementById ( "download-format" ) . innerHTML =
4791+ DATASTORE_SEARCH_ROWS_MAX <= self . model . recordCount
4792+ ? '<option value="compressed-csv">Compressed CSV</option>'
4793+ : '<option value="csv">CSV</option><option value="json">JSON</option>' ;
4794+ } , 2000 ) ;
47864795 } ,
47874796 render : function ( ) {
47884797 var self = this ;
4789-
47904798 var out = Mustache . render ( this . template , { } ) ;
47914799 this . $el . html ( out ) ;
47924800 } ,
47934801 onExtract : function ( e ) {
47944802 var self = this ;
47954803 e . preventDefault ( ) ;
47964804
4797- // var format = this.$el.find(".select -format").val() ;
4805+ var format = document . getElementById ( "download -format") . value ;
47984806 var fields = self . model . queryState . attributes . fields ;
47994807 var query = CKAN . _normalizeQuery ( self . model . queryState . attributes ) ;
48004808 query . ckan_resource_id = self . model . attributes . id ;
48014809 query . resource_id = self . model . attributes . bq_table_name ;
48024810 query . limit = this . model . recordCount ;
4803- // query.format = format;
4804- query . format = "csv" ;
4805-
48064811 query . offset = 0 ;
48074812 query . fields = fields ;
4813+
48084814 var input = this . $el
48094815 . find ( ".extract-data-input" )
48104816 . val ( JSON . stringify ( query ) ) ;
48114817
48124818 var sql_query = this . jsQueryToSQL ( query ) ;
4813- this . extractFile ( self , sql_query ) ;
4819+ this . extractFile ( self , sql_query , format ) ;
48144820 } ,
4815- extractFile : function ( self , sql_query ) {
4821+ extractFile : function ( self , sql_query , format ) {
48164822 var base_path = self . model . attributes . endpoint || self . options . site_url ;
48174823 //console.log(base_path);
48184824 var endpoint = `${ base_path } /3/action/datastore_search_sql?sql=${ sql_query } ` ; // USE BASE_PATH IN PRODUCTION
@@ -4829,9 +4835,9 @@ this.recline.View = this.recline.View || {};
48294835 } ) ;
48304836 self . progress ( true ) ;
48314837 } else {
4832- //small file, convert json result to CSV
4833- this . exportCSVFile (
4838+ this . exportFile (
48344839 resource . result . result . records ,
4840+ format ,
48354841 self . model . attributes . title ,
48364842 self
48374843 ) ;
@@ -4847,15 +4853,27 @@ this.recline.View = this.recline.View || {};
48474853 this . showErrorModal ( ) ;
48484854 } ) ;
48494855 } ,
4850- exportCSVFile : function ( resp_json , filename , self ) {
4851- var exported_filename = filename + ".csv" ;
4856+ exportFile : function ( resp_json , format , filename , self ) {
48524857 var src = "https://unpkg.com/[email protected] /papaparse.min.js" ; 48534858 $ . getScript ( src , function ( ) {
48544859 try {
4855- let csv = Papa . unparse ( resp_json ) ;
4856- var blob = new Blob ( [ csv ] , {
4857- type : "text/csv;charset=utf-8;" ,
4858- } ) ;
4860+ let blob = null ;
4861+ let exported_filename = "" ;
4862+
4863+ if ( format == "csv" ) {
4864+ exported_filename = filename + ".csv" ;
4865+ let csv = Papa . unparse ( resp_json ) ;
4866+ blob = new Blob ( [ csv ] , {
4867+ type : "text/csv;charset=utf-8;" ,
4868+ } ) ;
4869+ } else {
4870+ exported_filename = filename + ".json" ;
4871+ let json = JSON . stringify ( resp_json ) ;
4872+ blob = new Blob ( [ json ] , {
4873+ type : "text/plain;charset=utf-8;" ,
4874+ } ) ;
4875+ }
4876+
48594877 if ( navigator . msSaveBlob ) {
48604878 // IE 10+
48614879 navigator . msSaveBlob ( blob , exported_filename ) ;
@@ -5006,13 +5024,13 @@ this.recline.View = this.recline.View || {};
50065024 return where_str ;
50075025 } ,
50085026 get_field_type : function ( value ) {
5009-
5027+
50105028 if ( isNaN ( Number ( value ) ) ) {
50115029 return "string" ;
50125030 } else {
50135031 return "num" ;
50145032 }
5015-
5033+
50165034 } ,
50175035 } ) ;
50185036} ) ( jQuery , recline . View ) ;
0 commit comments