Refactor download

This commit is contained in:
Norbert Renner 2022-05-27 12:16:05 +02:00
parent 99a00e5948
commit 1d26949770

View file

@ -44,6 +44,26 @@ BR.Export = L.Class.extend({
} }
}, },
_getMimeType: function (format) {
const mimeTypeMap = {
gpx: 'application/gpx+xml',
kml: 'application/vnd.google-earth.kml+xml',
geojson: 'application/vnd.geo+json',
csv: 'text/tab-separated-values',
};
return mimeTypeMap[format];
},
_triggerDownload: function (url, name) {
const link = document.createElement('a');
link.href = url;
if (name) {
link.download = name;
}
link.click();
},
_export: function (e) { _export: function (e) {
var exportForm = document.forms['export']; var exportForm = document.forms['export'];
var format = exportForm['format'].value || $('#export-format input:radio:checked').val(); var format = exportForm['format'].value || $('#export-format input:radio:checked').val();
@ -55,26 +75,17 @@ BR.Export = L.Class.extend({
if (BR.Browser.download) { if (BR.Browser.download) {
const track = this._formatTrack(format, name, includeWaypoints); const track = this._formatTrack(format, name, includeWaypoints);
const fileName = (name || 'brouter') + '.' + format;
const mimeTypeMap = { const mimeType = this._getMimeType(format);
gpx: 'application/gpx+xml',
kml: 'application/vnd.google-earth.kml+xml',
geojson: 'application/vnd.geo+json',
csv: 'text/tab-separated-values',
};
const mimeType = mimeTypeMap[format];
const blob = new Blob([track], { const blob = new Blob([track], {
type: mimeType + ';charset=utf-8', type: mimeType + ';charset=utf-8',
}); });
const objectUrl = URL.createObjectURL(blob); const objectUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = objectUrl; this._triggerDownload(objectUrl, fileName);
link.download = (name || 'brouter') + '.' + format;
link.click();
} else { } else {
var uri = this.router.getUrl( var serverUrl = this.router.getUrl(
this.latLngs, this.latLngs,
null, null,
this.pois.getMarkers(), this.pois.getMarkers(),
@ -83,11 +94,8 @@ BR.Export = L.Class.extend({
nameUri, nameUri,
includeWaypoints includeWaypoints
); );
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); this._triggerDownload(serverUrl);
var link = document.createElement('a');
link.href = uri;
link.dispatchEvent(evt);
} }
}, },