improve script for downloading exports

This commit is contained in:
2018-06-12 17:58:02 +02:00
parent dd05fa0be7
commit 587b248549
6 changed files with 114 additions and 50 deletions

View File

@@ -27,50 +27,8 @@ window.addEventListener("DOMContentLoaded", function(e) {
query = window.location.search,
container = document.querySelector("#download_container")
;
window.fetch(url+query, { credentials: 'same-origin' })
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response.blob();
}).then(function(blob) {
var content = URL.createObjectURL(blob),
link = document.createElement("a"),
suffix_file;
switch (blob.type) {
case 'application/vnd.oasis.opendocument.spreadsheet':
suffix_file = '.ods';
break;
case 'text/csv':
suffix_file = '.csv';
break;
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
suffix_file = '.xlsx';
break;
default:
suffix_file = '';
}
link.appendChild(document.createTextNode("{{ "Download your report"|trans }}"));
link.classList.add("sc-button", "btn-action");
link.href = content;
link.download = "{{ alias }}"+suffix_file;
let waiting_text = container.querySelector("#waiting_text");
container.removeChild(waiting_text);
container.appendChild(link);
}).catch(function(error) {
var problem_text =
document.createTextNode("{{ "Problem during download"|trans }}");
container
.replaceChild(problem_text, container.firstChild);
})
;
chill.download_report(url+query, container);
});
</script>
{% endblock %}
@@ -79,6 +37,6 @@ window.addEventListener("DOMContentLoaded", function(e) {
<h1>{{ "Download export"|trans }}</h1>
<div id="download_container"><span id="waiting_text">{{ "Waiting for your report"|trans }}...</span></div>
<div id="download_container" data-alias="{{ alias|escape('html_attr') }}" {% if mime_type is defined %}data-mime-type="{{ mime_type|escape('html_attr') }}"{% endif %} data-download-text="{{ "Download your report"|trans|escape('html_attr') }}"><span id="waiting_text">{{ "Waiting for your report"|trans }}...</span></div>
{% endblock %}