Do not let advancedFilters control add/remove features from map

This should be done by the datalayer itself, which is now the case.
This commit is contained in:
Yohan Boniface 2023-08-01 14:09:09 +02:00
parent 5058e27496
commit ac30e71e74
4 changed files with 20 additions and 31 deletions

View file

@ -885,38 +885,12 @@ L.U.Map.include({
} }
const filterFeatures = function () { const filterFeatures = function () {
let noResults = true let found = false
this.eachBrowsableDataLayer((datalayer) => { this.eachBrowsableDataLayer((datalayer) => {
datalayer.eachFeature(function (feature) { datalayer.resetLayer(true)
feature.properties.isVisible = true if (datalayer.hasDataVisible()) found = 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')
}
})
}) })
if (noResults) { if (!found) this.ui.alert({content: L._('No results for these filters'), level: 'info'})
this.help.show('advancedFiltersNoResults')
} else {
this.help.hide()
}
} }
propertiesContainer.innerHTML = '' propertiesContainer.innerHTML = ''

View file

@ -571,7 +571,6 @@ L.U.Help = L.Class.extend({
advancedFilterKey: L._( advancedFilterKey: L._(
'Comma separated list of properties to use for checkbox filtering' '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.'), 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.'), outlink: L._('Define link to open in a new window on polygon click.'),
dynamicRemoteData: L._('Fetch data each time map view changes.'), dynamicRemoteData: L._('Fetch data each time map view changes.'),

View file

@ -469,6 +469,17 @@ L.U.FeatureMixin = {
return false 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) { onVertexRawClick: function (e) {
new L.Toolbar.Popup(e.latlng, { new L.Toolbar.Popup(e.latlng, {
className: 'leaflet-inplace-toolbar', className: 'leaflet-inplace-toolbar',

View file

@ -285,6 +285,10 @@ L.U.DataLayer = L.Evented.extend({
this.parentPane.appendChild(this.pane) this.parentPane.appendChild(this.pane)
}, },
hasDataVisible: function () {
return !!Object.keys(this.layer._layers).length
},
resetLayer: function (force) { resetLayer: function (force) {
if (this.layer && this.options.type === this.layer._type && !force) return if (this.layer && this.options.type === this.layer._type && !force) return
const visible = this.isVisible() const visible = this.isVisible()
@ -297,6 +301,7 @@ L.U.DataLayer = L.Evented.extend({
filter = this.map.options.filter filter = this.map.options.filter
this.eachLayer(function (layer) { this.eachLayer(function (layer) {
if (filter && !layer.matchFilter(filter, filterKeys)) return if (filter && !layer.matchFilter(filter, filterKeys)) return
if (!layer.matchAdvancedFilters()) return
this.layer.addLayer(layer) this.layer.addLayer(layer)
}) })
if (visible) this.map.addLayer(this.layer) if (visible) this.map.addLayer(this.layer)