diff --git a/src/app/core/layers/layer.js b/src/app/core/layers/layer.js index 178ff0854..59f0700ba 100644 --- a/src/app/core/layers/layer.js +++ b/src/app/core/layers/layer.js @@ -1,5 +1,6 @@ import { GEOMETRY_FIELDS as geometryFields } from 'app/constant'; import ApplicationState from 'store/application-state'; +import GUI from 'services/gui'; import DataRouterService from 'services/data'; import ProjectsRegistry from 'store/projects'; import DownloadMixin from 'core/layers/mixins/download'; @@ -360,6 +361,55 @@ proto.isWmsUseLayerIds = function() { return this.config.wms_use_layer_ids; }; +/** + * @TODO error handling + * + * [FILE DOWNLOAD] Export layer in different formats + * + * @param { 'shp' | 'shapefile' | 'xls' | 'csv' | 'gpx' | 'gpkg' | 'geotiff' } type + * @param { Object } opts + * @param opts.data + * @param opts.options + * + * @returns { Promise } + * + * @listens GUI~before_download_layer since 3.9.0 + * @fires GUI~choose_layer_fields_to_download since 3.9.0 + */ +proto.downloadAsFile = function(type, { + data = {}, + options, +} = {}) { + return new Promise((resolve, reject) => { + GUI.once( + 'before_download_layer', + async (fields) => { + data.ftod = fields.map(field => field.name).join(); + data.filtertoken = this.getFilterToken(); //from SelectionMixin + // alias + if ('shapefile' === type) { + type = 'shp'; + } + // allowed download types + if (['shp', 'xls', 'csv', 'gpx', 'gpkg', 'geotiff'].includes(type)) { + resolve( + XHR.fileDownload({ + url: this.getUrl(type), + data, + httpMethod: "POST", + }) + ); + } + } + ); + GUI.emit( + 'choose_layer_fields_to_download', + this.getTableFields().map(({ name, label }) => ({ name, label, selected: true })) + ); + }) + .catch(console.warn); +}; + /** * @returns {*|null} source type of layer */ @@ -1372,4 +1422,9 @@ Layer.EDITOPS = { */ Layer.SELECTION_STATE = SELECTION_STATE; +/** + * @deprecated since 3.9.0. Will be deleted in 4.x. Use Layer.downloadAsFile(type, options) instead + */ +Layer.prototype.getDownloadFilefromDownloadDataType = deprecate(Layer.prototype.downloadAsFile, '[G3W-CLIENT] Layer::getDownloadFilefromDownloadDataType(type, options) is deprecated'); + module.exports = Layer; diff --git a/src/app/gui/queryresults/queryresultsservice.js b/src/app/gui/queryresults/queryresultsservice.js index 27b0fd626..bea581c1c 100644 --- a/src/app/gui/queryresults/queryresultsservice.js +++ b/src/app/gui/queryresults/queryresultsservice.js @@ -1535,7 +1535,7 @@ class QueryResultsService extends G3WObject { ({layer, type, data}= {}) => { return CatalogLayersStoresRegistry .getLayerById(layer.id) - .getDownloadFilefromDownloadDataType(type, { data }) || + .downloadAsFile(type, { data }) || Promise.resolve(); }, { @@ -1649,7 +1649,7 @@ class QueryResultsService extends G3WObject { downloadGpx({ id: layerId } = {}, feature) { CatalogLayersStoresRegistry .getLayerById(layerId) - .getGpx({ fid: feature ? feature.attributes[G3W_FID] : null }) + .downloadAsFile('gpx', {data:{ fid: feature ? feature.attributes[G3W_FID] : null }}) .catch((err) => { GUI.notify.error(t("info.server_error")); }) .finally(() => { this.layerMenu.loading.shp = false; this._hideMenu(); }) } @@ -1660,7 +1660,8 @@ class QueryResultsService extends G3WObject { downloadXls({ id: layerId } = {}, feature) { CatalogLayersStoresRegistry .getLayerById(layerId) - .getXls({ fid: feature ? feature.attributes[G3W_FID] : null }) + .downloadAsFile('xls', + { data: {fid: feature ? feature.attributes[G3W_FID] : null }}) .catch(err => { GUI.notify.error(t("info.server_error")); }) .finally(() => { this.layerMenu.loading.shp = false; this._hideMenu(); }) } diff --git a/src/components/CatalogLayerContextMenu.vue b/src/components/CatalogLayerContextMenu.vue index 85c5cfbcc..6ffcab673 100644 --- a/src/components/CatalogLayerContextMenu.vue +++ b/src/components/CatalogLayerContextMenu.vue @@ -552,24 +552,64 @@ + diff --git a/src/locales/de.js b/src/locales/de.js index 8d149d177..7b763a0ac 100644 --- a/src/locales/de.js +++ b/src/locales/de.js @@ -542,6 +542,12 @@ export default { no_data: "Keine Daten", nodatafilterd: "Keine passenden Datensätze gefunden", infoFiltered: "(gefiltert aus _MAX_ Gesamtsätzen)" - } + }, + download: { + fields: { + all: 'All', + title: 'Select fields to download' + } + }, }, }; \ No newline at end of file diff --git a/src/locales/en.js b/src/locales/en.js index 6a59f414d..c8df8710a 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -542,6 +542,12 @@ export default { no_data: "No data", nodatafilterd: "No matching records found", infoFiltered: "(filtered from _MAX_ total records)" - } + }, + download: { + fields: { + all: 'All', + title: 'Select fields to download' + } + }, }, }; diff --git a/src/locales/fi.js b/src/locales/fi.js index bf76ab967..a588fa6e8 100644 --- a/src/locales/fi.js +++ b/src/locales/fi.js @@ -542,6 +542,12 @@ export default { no_data: "Ei tietoja", nodatafilterd: "Vastaavia tietueita ei löytynyt", infoFiltered: "(filtered from _MAX_ total records)" - } + }, + download: { + fields: { + all: 'All', + title: 'Select fields to download' + } + }, }, }; \ No newline at end of file diff --git a/src/locales/fr.js b/src/locales/fr.js index 90dd68b04..9e0d35f91 100644 --- a/src/locales/fr.js +++ b/src/locales/fr.js @@ -543,6 +543,12 @@ export default { info: "Afficher _START_ à _END_ sur _TOTAL_ lignes", nodatafilterd: "Aucun résultat trouvé", infoFiltered: "(Filtré par _MAX_ rangs totaux)" + }, + download: { + fields: { + all: 'All', + title: 'Select fields to download' + } } }, }; \ No newline at end of file diff --git a/src/locales/it.js b/src/locales/it.js index 314d99bbc..f940cbee4 100644 --- a/src/locales/it.js +++ b/src/locales/it.js @@ -548,5 +548,11 @@ export default { nodatafilterd: "Nessun risultato trovato", infoFiltered: "(Filtrati da _MAX_ total righe)" }, + download: { + fields: { + all: 'Tutti', + title: 'Seleziona campi da scaricare' + } + }, }, }; \ No newline at end of file diff --git a/src/locales/ro.js b/src/locales/ro.js index ec64a09dd..c5785968e 100644 --- a/src/locales/ro.js +++ b/src/locales/ro.js @@ -542,6 +542,12 @@ export default { no_data: "Fără date", nodatafilterd: "Niciun rezultat", infoFiltered: "(filtrat din _MAX_ de rezultate totale)" + }, + download: { + fields: { + all: 'All', + title: 'Select fields to download' + } } }, }; \ No newline at end of file diff --git a/src/locales/se.js b/src/locales/se.js index 948ef851b..eac56cc07 100644 --- a/src/locales/se.js +++ b/src/locales/se.js @@ -542,6 +542,12 @@ export default { no_data: "Inga uppgifter", nodatafilterd: "Inga motsvarande poster hittades", infoFiltered: "(filtered from _MAX_ total records)" + }, + download: { + fields: { + all: 'All', + title: 'Select fields to download' + } } }, };