From c661abb6266db24c25bb9868457ec96ca4f321fa Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 1 Nov 2018 10:26:38 +0100 Subject: [PATCH] Fix dynamic data loading for new request url. --- js/Contao.js | 34 ++++++++++++++++++- js/Mixin.GeoJSON.js | 2 +- .../Resources/public/js/contao-leaflet.js | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/js/Contao.js b/js/Contao.js index 69ce015..91368a5 100644 --- a/js/Contao.js +++ b/js/Contao.js @@ -139,6 +139,7 @@ L.Contao = L.Evented.extend({ * @param map Pass a map object so that the data loading events are passed to the map. */ loadUrl: function (url, type, options, customLayer, map) { + url = this.applyFilterToUrl(url, map); var layer = omnivore[type](url, options, customLayer); if (map) { @@ -147,7 +148,7 @@ L.Contao = L.Evented.extend({ // Add listener for map bounds changes. if (map.options.dynamicLoad && layer.options.boundsMode == 'fit') { - layer.options.requestHash = hash; + layer.options.requestUrl = url; map.on('moveend', layer.refreshData, layer); map.on('layerremove', function(e) { @@ -326,6 +327,37 @@ L.Contao = L.Evented.extend({ } return value; + }, + + + /** + * Apply the filter to a request url. + * + * @param {string} url The request url. + * @param {L.Map} map The map. + * + * @returns {string} + */ + applyFilterToUrl: function (url, map) { + var value, query, bounds; + + if (!map || !map.options.dynamicLoad) { + return url; + } + + url = new URL(url); + query = new URLSearchParams(url.search); + + bounds = map.getBounds(); + value = bounds.getSouth() + ',' + bounds.getWest(); + value += ',' + bounds.getNorth() + ',' + bounds.getEast(); + + query.set('filter', 'bbox'); + query.set('values', value); + + url.search = query.toString(); + + return url.toString(); } }); diff --git a/js/Mixin.GeoJSON.js b/js/Mixin.GeoJSON.js index 21bea31..3e6f4cf 100644 --- a/js/Mixin.GeoJSON.js +++ b/js/Mixin.GeoJSON.js @@ -29,6 +29,6 @@ L.GeoJSON.include({ }); // TODO: Allow other data formats. - omnivore.geojson(L.contao.createRequestUrl(this.options.requestHash, e.target), null, dataLayer); + omnivore.geojson(L.contao.applyFilterToUrl(this.options.requestUrl, e.target), null, dataLayer); } }); diff --git a/src/Bundle/Resources/public/js/contao-leaflet.js b/src/Bundle/Resources/public/js/contao-leaflet.js index 80ce810..cdf4ee8 100644 --- a/src/Bundle/Resources/public/js/contao-leaflet.js +++ b/src/Bundle/Resources/public/js/contao-leaflet.js @@ -1 +1 @@ -L.Contao=L.Evented.extend({statics:{ATTRIBUTION:' | netzmacht'},maps:{},icons:{},initialize:function(){L.Icon.Default.imagePath="assets/leaflet/libs/leaflet/images/",this.setGeoJsonListeners(L.GeoJSON)},addMap:function(t,o){return this.maps[t]=o,this.fire("map:added",{id:t,map:o}),this},getMap:function(t){return void 0===this.maps[t]?null:this.maps[t]},addIcon:function(t,o){return this.icons[t]=o,this.fire("icon:added",{id:t,icon:o}),this},loadIcons:function(t){for(var o=0;onetzmacht'},maps:{},icons:{},initialize:function(){L.Icon.Default.imagePath="assets/leaflet/libs/leaflet/images/",this.setGeoJsonListeners(L.GeoJSON)},addMap:function(t,o){return this.maps[t]=o,this.fire("map:added",{id:t,map:o}),this},getMap:function(t){return void 0===this.maps[t]?null:this.maps[t]},addIcon:function(t,o){return this.icons[t]=o,this.fire("icon:added",{id:t,icon:o}),this},loadIcons:function(t){for(var o=0;o