Skip to content

Commit e53054e

Browse files
committed
[recline.js][b]: Add client side CSV parser
1 parent ece8a07 commit e53054e

File tree

1 file changed

+60
-12
lines changed
  • ckanext/dataexplorer/public/vendor/recline

1 file changed

+60
-12
lines changed

ckanext/dataexplorer/public/vendor/recline/recline.js

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,7 +3070,7 @@ this.recline.View = this.recline.View || {};
30703070
// * message: message to show.
30713071
// * category: warning (default), success, error
30723072
// * persist: if true alert is persistent, o/w hidden after 3s (default = false)
3073-
// * loader: if true show loading spinner
3073+
// * loader: if true show loading progress
30743074
notify: function (flash) {
30753075
var tmplData = _.extend(
30763076
{
@@ -4792,6 +4792,13 @@ this.recline.View = this.recline.View || {};
47924792
var self = this;
47934793
e.preventDefault();
47944794

4795+
//preloads Papaparse and caches it
4796+
var src = "https://unpkg.com/[email protected]/papaparse.min.js";
4797+
$.getScript(src);
4798+
$.ajaxSetup({
4799+
cache: true,
4800+
});
4801+
47954802
// var format = this.$el.find(".select-format").val();
47964803
var fields = self.model.queryState.attributes.fields;
47974804
var query = CKAN._normalizeQuery(self.model.queryState.attributes);
@@ -4812,10 +4819,10 @@ this.recline.View = this.recline.View || {};
48124819
},
48134820
extractFile: function (self, sql_query) {
48144821
var base_path = self.model.attributes.endpoint || self.options.site_url;
4815-
// var endpoint = `${base_path}/3/action/datastore_search_sql?sql=${sql_query}`; USE BASE_PATH IN PRODUCTION
4816-
var endpoint = `https://ckan.nhs.staging.datopian.com/api/3/action/datastore_search_sql?sql=${sql_query}`;
4822+
var endpoint = `${base_path}/3/action/datastore_search_sql?sql=${sql_query}`; // USE BASE_PATH IN PRODUCTION
4823+
// var endpoint = `https://ckan.nhs.staging.datopian.com/api/3/action/datastore_search_sql?sql=${sql_query}`;
4824+
self.progress();
48174825

4818-
self.spinner();
48194826
fetch(endpoint)
48204827
.then(async (resp) => {
48214828
let resource = await resp.json();
@@ -4824,23 +4831,56 @@ this.recline.View = this.recline.View || {};
48244831
resource.result["gc_urls"].forEach((obj) => {
48254832
window.open(obj["url"]);
48264833
});
4827-
self.spinner(true);
4834+
self.progress(true);
48284835
} else {
48294836
//small file, convert json result to CSV
4830-
console.log(resource.result.result.records);
4831-
self.spinner(true);
4837+
this.exportCSVFile(
4838+
resource.result.result.records,
4839+
self.model.attributes.title,
4840+
self
4841+
);
48324842
}
48334843
} else {
4834-
self.spinner(true);
4844+
self.progress(true);
48354845
this.showErrorModal();
48364846
}
48374847
})
48384848
.catch((err) => {
48394849
console.warn(err);
4840-
self.spinner(true);
4850+
self.progress(true);
48414851
this.showErrorModal();
48424852
});
48434853
},
4854+
exportCSVFile: function (resp_json, filename, self) {
4855+
var exported_filename = filename + ".csv";
4856+
try {
4857+
let csv = Papa.unparse(resp_json);
4858+
var blob = new Blob([csv], {
4859+
type: "text/csv;charset=utf-8;",
4860+
});
4861+
if (navigator.msSaveBlob) {
4862+
// IE 10+
4863+
navigator.msSaveBlob(blob, exported_filename);
4864+
} else {
4865+
var link = document.createElement("a");
4866+
if (link.download !== undefined) {
4867+
// Browsers that support HTML5 download attribute
4868+
var url = URL.createObjectURL(blob);
4869+
link.setAttribute("href", url);
4870+
link.setAttribute("download", exported_filename);
4871+
link.style.visibility = "hidden";
4872+
document.body.appendChild(link);
4873+
link.click();
4874+
document.body.removeChild(link);
4875+
}
4876+
}
4877+
self.progress(true);
4878+
} catch (error) {
4879+
console.warn(error);
4880+
self.progress(true);
4881+
self.showErrorModal();
4882+
}
4883+
},
48444884
showErrorModal: function () {
48454885
var modal = document.getElementsByClassName("modal")[0];
48464886
modal.style.display = "flex";
@@ -4854,24 +4894,32 @@ this.recline.View = this.recline.View || {};
48544894
</div>
48554895
</div>
48564896
`;
4897+
var cancelBtn = modal.querySelector("#cancel-btn");
4898+
cancelBtn.onclick = function () {
4899+
modal.style.display = "none";
4900+
};
48574901
},
4858-
spinner: function (hide = false) {
4902+
progress: function (hide = false) {
48594903
var modal = document.getElementsByClassName("modal")[0];
48604904
if (hide) {
48614905
modal.style.display = "none";
4862-
return
4906+
return;
48634907
}
48644908
modal.style.display = "flex";
48654909
document.getElementsByClassName("modal")[0].innerHTML = `
48664910
<div class="modal" style="display: flex;">
48674911
<div class="row">
48684912
<div class="modal-content">
48694913
<h3>Extracting...</h3>
4914+
<button class="btn extract-button modal-btn" id="cancel-btn">Hide</button>
48704915
</div>
4871-
<button class="btn extract-button modal-btn" id="cancel-btn">Hide</button>
48724916
</div>
48734917
</div>
48744918
`;
4919+
var cancelBtn = modal.querySelector("#cancel-btn");
4920+
cancelBtn.onclick = function () {
4921+
modal.style.display = "none";
4922+
};
48754923
},
48764924
jsQueryToSQL: function (query_obj) {
48774925
let query = "";

0 commit comments

Comments
 (0)