From ac30e71e74d250a3259ce70ca460e2692ec2629e Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Tue, 1 Aug 2023 14:09:09 +0200 Subject: [PATCH] Do not let advancedFilters control add/remove features from map This should be done by the datalayer itself, which is now the case. --- umap/static/umap/js/umap.controls.js | 34 ++++------------------------ umap/static/umap/js/umap.core.js | 1 - umap/static/umap/js/umap.features.js | 11 +++++++++ umap/static/umap/js/umap.layer.js | 5 ++++ 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index 2b8d20f0..da996445 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -885,38 +885,12 @@ L.U.Map.include({ } const filterFeatures = function () { - let noResults = true + let found = false this.eachBrowsableDataLayer((datalayer) => { - datalayer.eachFeature(function (feature) { - feature.properties.isVisible = true - for (const [property, values] of Object.entries( - this.map.options.advancedFilters - )) { - if (values.length > 0) { - if ( - !feature.properties[property] || - !values.includes(feature.properties[property]) - ) { - feature.properties.isVisible = false - } - } - } - if (feature.properties.isVisible) { - noResults = false - if (!this.isLoaded()) this.fetchData() - this.map.addLayer(feature) - this.fire('show') - } else { - this.map.removeLayer(feature) - this.fire('hide') - } - }) + datalayer.resetLayer(true) + if (datalayer.hasDataVisible()) found = true }) - if (noResults) { - this.help.show('advancedFiltersNoResults') - } else { - this.help.hide() - } + if (!found) this.ui.alert({content: L._('No results for these filters'), level: 'info'}) } propertiesContainer.innerHTML = '' diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 0a3cb7ac..910c4ae4 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -571,7 +571,6 @@ L.U.Help = L.Class.extend({ advancedFilterKey: L._( 'Comma separated list of properties to use for checkbox filtering' ), - advancedFiltersNoResults: L._('No results for these filters'), interactive: L._('If false, the polygon will act as a part of the underlying map.'), outlink: L._('Define link to open in a new window on polygon click.'), dynamicRemoteData: L._('Fetch data each time map view changes.'), diff --git a/umap/static/umap/js/umap.features.js b/umap/static/umap/js/umap.features.js index 56cd620e..cf5aa938 100644 --- a/umap/static/umap/js/umap.features.js +++ b/umap/static/umap/js/umap.features.js @@ -469,6 +469,17 @@ L.U.FeatureMixin = { return false }, + matchAdvancedFilters: function () { + const filters = this.map.options.advancedFilters + for (const [property, expected] of Object.entries(filters)) { + if (expected.length) { + let value = this.properties[property] + if (!value || !expected.includes(value)) return false + } + } + return true + }, + onVertexRawClick: function (e) { new L.Toolbar.Popup(e.latlng, { className: 'leaflet-inplace-toolbar', diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 09317e7e..68a42837 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -285,6 +285,10 @@ L.U.DataLayer = L.Evented.extend({ this.parentPane.appendChild(this.pane) }, + hasDataVisible: function () { + return !!Object.keys(this.layer._layers).length + }, + resetLayer: function (force) { if (this.layer && this.options.type === this.layer._type && !force) return const visible = this.isVisible() @@ -297,6 +301,7 @@ L.U.DataLayer = L.Evented.extend({ filter = this.map.options.filter this.eachLayer(function (layer) { if (filter && !layer.matchFilter(filter, filterKeys)) return + if (!layer.matchAdvancedFilters()) return this.layer.addLayer(layer) }) if (visible) this.map.addLayer(this.layer)