@@ -4790,8 +4790,17 @@ this.recline.View = this.recline.View || {};
47904790 document . getElementById ( "download-format" ) . innerHTML =
47914791 DATASTORE_SEARCH_ROWS_MAX <= self . model . recordCount
47924792 ? '<option value="compressed-csv">Compressed CSV</option>'
4793- : '<option value="csv">CSV</option><option value="json">JSON</option>' ;
4793+ : '<option value="csv">CSV</option><option value="compressed-csv">Compressed CSV</option><option value=" json">JSON</option>' ;
47944794 } , 2000 ) ;
4795+
4796+ //Pre-Load helper libraries JSZip for zipping files
4797+ $ . getScript (
4798+ "https://cdnjs.cloudflare.com/ajax/libs/jszip/3.5.0/jszip.min.js"
4799+ ) ;
4800+
4801+ $ . ajaxSetup ( {
4802+ cache : true ,
4803+ } ) ;
47954804 } ,
47964805 render : function ( ) {
47974806 var self = this ;
@@ -4860,47 +4869,68 @@ this.recline.View = this.recline.View || {};
48604869 let blob = null ;
48614870 let exported_filename = "" ;
48624871
4863- if ( format == "csv" ) {
4872+ if ( format === "csv" ) {
48644873 exported_filename = filename + ".csv" ;
48654874 let csv = Papa . unparse ( resp_json ) ;
48664875 blob = new Blob ( [ csv ] , {
48674876 type : "text/csv;charset=utf-8;" ,
48684877 } ) ;
4869- } else {
4878+ self . downloadBlob ( blob , exported_filename )
4879+ self . progress ( true ) ;
4880+
4881+ } else if ( format === "json" ) {
48704882 exported_filename = filename + ".json" ;
48714883 let json = JSON . stringify ( resp_json ) ;
48724884 blob = new Blob ( [ json ] , {
48734885 type : "text/plain;charset=utf-8;" ,
48744886 } ) ;
4875- }
4887+ self . downloadBlob ( blob , exported_filename )
4888+ self . progress ( true ) ;
48764889
4877- if ( navigator . msSaveBlob ) {
4878- // IE 10+
4879- navigator . msSaveBlob ( blob , exported_filename ) ;
48804890 } else {
4881- var link = document . createElement ( "a" ) ;
4882- if ( link . download !== undefined ) {
4883- // Browsers that support HTML5 download attribute
4884- var url = URL . createObjectURL ( blob ) ;
4885- link . setAttribute ( "href" , url ) ;
4886- link . setAttribute ( "download" , exported_filename ) ;
4887- link . style . visibility = "hidden" ;
4888- document . body . appendChild ( link ) ;
4889- link . click ( ) ;
4890- document . body . removeChild ( link ) ;
4891- }
4891+ // zip files
4892+ exported_filename = filename + ".csv" ;
4893+ let csv = Papa . unparse ( resp_json ) ;
4894+ let blob_content = new Blob ( [ csv ] , {
4895+ type : "text/csv;charset=utf-8;" ,
4896+ } ) ;
4897+ let zip = new JSZip ( ) ;
4898+ zip . file ( exported_filename , blob_content ) ;
4899+ zip
4900+ . generateAsync ( {
4901+ type :"blob" ,
4902+ } )
4903+ . then ( function ( zipped_blob ) {
4904+ self . downloadBlob ( zipped_blob , filename )
4905+ self . progress ( true ) ;
4906+ } ) ;
48924907 }
4893- self . progress ( true ) ;
4908+
48944909 } catch ( error ) {
48954910 console . warn ( error ) ;
48964911 self . progress ( true ) ;
48974912 self . showErrorModal ( ) ;
48984913 }
48994914 } ) ;
4900- //caches Papaparse
4901- $ . ajaxSetup ( {
4902- cache : true ,
4903- } ) ;
4915+
4916+ } ,
4917+ downloadBlob : function ( blob , exported_filename ) {
4918+ if ( navigator . msSaveBlob ) {
4919+ // IE 10+
4920+ navigator . msSaveBlob ( blob , exported_filename ) ;
4921+ } else {
4922+ var link = document . createElement ( "a" ) ;
4923+ if ( link . download !== undefined ) {
4924+ // Browsers that support HTML5 download attribute
4925+ var url = URL . createObjectURL ( blob ) ;
4926+ link . setAttribute ( "href" , url ) ;
4927+ link . setAttribute ( "download" , exported_filename ) ;
4928+ link . style . visibility = "hidden" ;
4929+ document . body . appendChild ( link ) ;
4930+ link . click ( ) ;
4931+ document . body . removeChild ( link ) ;
4932+ }
4933+ }
49044934 } ,
49054935 showErrorModal : function ( ) {
49064936 var modal = document . getElementsByClassName ( "modal" ) [ 0 ] ;
@@ -5024,13 +5054,11 @@ this.recline.View = this.recline.View || {};
50245054 return where_str ;
50255055 } ,
50265056 get_field_type : function ( value ) {
5027-
50285057 if ( isNaN ( Number ( value ) ) ) {
50295058 return "string" ;
50305059 } else {
50315060 return "num" ;
50325061 }
5033-
50345062 } ,
50355063 } ) ;
50365064} ) ( jQuery , recline . View ) ;
0 commit comments