mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 00:53:48 +00:00
improve script for downloading exports
This commit is contained in:
67
Resources/public/modules/download-report/download-report.js
Normal file
67
Resources/public/modules/download-report/download-report.js
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
var mime = require('mime-types')
|
||||
|
||||
var download_report = (url, container) => {
|
||||
var download_text = container.dataset.downloadText,
|
||||
alias = container.dataset.alias;
|
||||
|
||||
window.fetch(url, { credentials: 'same-origin' })
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
|
||||
return response.blob();
|
||||
}).then(blob => {
|
||||
|
||||
var content = URL.createObjectURL(blob),
|
||||
link = document.createElement("a"),
|
||||
type = blob.type,
|
||||
hasForcedType = 'mimeType' in container.dataset,
|
||||
extension;
|
||||
|
||||
if (hasForcedType) {
|
||||
// force a type
|
||||
type = container.dataset.mimeType;
|
||||
blob = new Blob([ blob ], { 'type': type });
|
||||
content = URL.createObjectURL(blob);
|
||||
}
|
||||
|
||||
extension = mime.extension(type);
|
||||
|
||||
link.appendChild(document.createTextNode(download_text));
|
||||
link.classList.add("sc-button", "btn-action");
|
||||
link.href = content;
|
||||
link.download = alias;
|
||||
if (extension !== false) {
|
||||
link.download = link.download + '.' + extension;
|
||||
}
|
||||
container.innerHTML = "";
|
||||
container.appendChild(link);
|
||||
}).catch(function(error) {
|
||||
console.log(error);
|
||||
var problem_text =
|
||||
document.createTextNode("Problem during download");
|
||||
|
||||
container
|
||||
.replaceChild(problem_text, container.firstChild);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = download_report;
|
18
Resources/public/modules/download-report/index.js
Normal file
18
Resources/public/modules/download-report/index.js
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
chill.download_report = require("./download-report.js");
|
@@ -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 %}
|
Reference in New Issue
Block a user