From bf66036b7b5586b31a3a8d9fa9611529f65b4f8b Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 31 Aug 2023 16:36:34 +0200 Subject: [PATCH] Make sure we load all data before downloading it fix #980 --- umap/static/umap/js/umap.controls.js | 22 +++++++++++++++---- umap/static/umap/js/umap.js | 32 +++++++++++++++++----------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index b5d729d9..42a6dd67 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -986,9 +986,10 @@ L.U.Map.include({ const status = this.permissions.getShareStatusDisplay() name.textContent = this.getDisplayName() // status is not set until map is saved once - if (status) share_status.textContent = L._('Visibility: {status}', { - status: status, - }) + if (status) + share_status.textContent = L._('Visibility: {status}', { + status: status, + }) } update() this.once('saved', L.bind(update, this)) @@ -1129,10 +1130,23 @@ L.U.Map.include({ toggleCaveat() const download = L.DomUtil.create('a', 'button', container) download.textContent = L._('Download data') - L.DomEvent.on(download, 'click', () => this.download(typeInput.value), this) + L.DomEvent.on( + download, + 'click', + () => { + if (typeInput.value === 'umap') this.fullDownload() + else this.download(typeInput.value) + } + ) this.ui.openPanel({ data: { html: container } }) }, + fullDownload: function () { + // Make sure all data is loaded before downloading + this.once('dataloaded', () => this.download()) + this.loadDatalayers(true) // Force load + }, + download: function (mode) { const type = this.EXPORT_TYPES[mode || 'umap'] const content = type.formatter(this) diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 742e3aab..ec412835 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -379,33 +379,41 @@ L.U.Map.include({ }, initDatalayers: function () { - let toload = (dataToload = seen = this.options.datalayers.length) - const self = this + for (let j = 0; j < this.options.datalayers.length; j++) { + this.createDataLayer(this.options.datalayers[j]) + } + this.loadDatalayers() + }, + + loadDatalayers: function (force) { + force = force || L.Util.queryString('download') // In case we are in download mode, let's go strait to loading all data + let toload = (dataToload = total = this.datalayers_index.length) let datalayer const loaded = () => { - self.datalayersLoaded = true - self.fire('datalayersloaded') + this.datalayersLoaded = true + this.fire('datalayersloaded') } const decrementToLoad = () => { toload-- if (toload === 0) loaded() } const dataLoaded = () => { - self.dataLoaded = true - self.fire('dataloaded') + this.dataLoaded = true + this.fire('dataloaded') } const decrementDataToLoad = () => { dataToload-- if (dataToload === 0) dataLoaded() } - for (let j = 0; j < this.options.datalayers.length; j++) { - datalayer = this.createDataLayer(this.options.datalayers[j]) - if (datalayer.displayedOnLoad()) datalayer.onceLoaded(decrementToLoad) + this.eachDataLayer(function (datalayer) { + if (force && !datalayer.hasDataLoaded()) datalayer.show() + if (datalayer.displayedOnLoad() || force) datalayer.onceLoaded(decrementToLoad) else decrementToLoad() - if (datalayer.displayedOnLoad()) datalayer.onceDataLoaded(decrementDataToLoad) + if (datalayer.displayedOnLoad() || force) + datalayer.onceDataLoaded(decrementDataToLoad) else decrementDataToLoad() - } - if (seen === 0) { + }) + if (total === 0) { // no datalayer loaded() dataLoaded()