From 46e6dcc2fadef70c042103d0ef786859ea268a42 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Tue, 17 Oct 2017 17:43:12 +0200 Subject: [PATCH] Support bounds changing after files got loaded. --- js/Contao.js | 18 ++++++++++++++++-- src/Bundle/Resources/config/layers.yml | 2 ++ .../Resources/contao/dca/tl_leaflet_layer.php | 2 +- .../Resources/public/js/contao-leaflet.js | 2 +- src/Mapper/Layer/FileLayerMapper.php | 4 ++++ src/Subscriber/EncoderSubscriber.php | 8 ++++---- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/js/Contao.js b/js/Contao.js index 268f341..004d65e 100644 --- a/js/Contao.js +++ b/js/Contao.js @@ -124,8 +124,22 @@ L.Contao = L.Evented.extend({ * @param map Pass a map object so that the data loading events are passed to the map. */ load: function (hash, type, options, customLayer, map) { - var url = this.createRequestUrl(hash, map), - layer = omnivore[type](url, options, customLayer); + var url = this.createRequestUrl(hash, map); + + return this.loadFile(url, type, options, customLayer, map); + }, + + /** + * Load data from an url into a layer using omnivore. + * + * @param url A file url. + * @param type The response content format. + * @param options Parser options + * @param customLayer optional custom layer. + * @param map Pass a map object so that the data loading events are passed to the map. + */ + loadFile: function (url, type, options, customLayer, map) { + var layer = omnivore[type](url, options, customLayer); if (map) { // Required because Control.Loading tries to get _leafet_id which is created here. diff --git a/src/Bundle/Resources/config/layers.yml b/src/Bundle/Resources/config/layers.yml index 9350a9f..6a36bf1 100644 --- a/src/Bundle/Resources/config/layers.yml +++ b/src/Bundle/Resources/config/layers.yml @@ -48,6 +48,8 @@ parameters: file: children: false icon: 'bundles/netzmachtcontaoleaflet/img/file.png' + boundsMode: + extend: true services: _defaults: diff --git a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php index 4aa8745..3f2ff03 100644 --- a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php +++ b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php @@ -219,7 +219,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ ], 'file extends default' => [ - '+config' => ['file'], + '+config' => ['file', 'boundsMode'], '+expert' => [ 'onEachFeature', 'pointToLayer', diff --git a/src/Bundle/Resources/public/js/contao-leaflet.js b/src/Bundle/Resources/public/js/contao-leaflet.js index 6e70ebc..e26d02d 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;o=200&&t<300||304===t}function i(){void 0===a.status||n(a.status)?o.call(a,null,a):o.call(a,a,null)}var s=!1;if(void 0===window.XMLHttpRequest)return o(Error("Browser not supported"));if(void 0===e){var r=t.match(/^\s*https?:\/\/[^\/]*/);e=r&&r[0]!==location.protocol+"//"+location.hostname+(location.port?":"+location.port:"")}var a=new window.XMLHttpRequest;if(e&&!("withCredentials"in a)){a=new window.XDomainRequest;var p=o;o=function(){if(s)p.apply(this,arguments);else{var t=this,o=arguments;setTimeout(function(){p.apply(t,o)},0)}}}return"onload"in a?a.onload=i:a.onreadystatechange=function(){4===a.readyState&&i()},a.onerror=function(t){o.call(this,t||!0,null),o=function(){}},a.onprogress=function(){},a.ontimeout=function(t){o.call(this,t,null),o=function(){}},a.onabort=function(t){o.call(this,t,null),o=function(){}},a.open("GET",t,!0),a.send(null),s=!0,a}}); \ No newline at end of file +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;o=200&&t<300||304===t}function i(){void 0===a.status||n(a.status)?o.call(a,null,a):o.call(a,a,null)}var s=!1;if(void 0===window.XMLHttpRequest)return o(Error("Browser not supported"));if(void 0===e){var r=t.match(/^\s*https?:\/\/[^\/]*/);e=r&&r[0]!==location.protocol+"//"+location.hostname+(location.port?":"+location.port:"")}var a=new window.XMLHttpRequest;if(e&&!("withCredentials"in a)){a=new window.XDomainRequest;var p=o;o=function(){if(s)p.apply(this,arguments);else{var t=this,o=arguments;setTimeout(function(){p.apply(t,o)},0)}}}return"onload"in a?a.onload=i:a.onreadystatechange=function(){4===a.readyState&&i()},a.onerror=function(t){o.call(this,t||!0,null),o=function(){}},a.onprogress=function(){},a.ontimeout=function(t){o.call(this,t,null),o=function(){}},a.onabort=function(t){o.call(this,t,null),o=function(){}},a.open("GET",t,!0),a.send(null),s=!0,a}}); \ No newline at end of file diff --git a/src/Mapper/Layer/FileLayerMapper.php b/src/Mapper/Layer/FileLayerMapper.php index f58e764..e1f619a 100644 --- a/src/Mapper/Layer/FileLayerMapper.php +++ b/src/Mapper/Layer/FileLayerMapper.php @@ -124,6 +124,10 @@ class FileLayerMapper extends AbstractLayerMapper $customLayer = $definition->getCustomLayer(); if ($customLayer instanceof GeoJson) { + if ($model->boundsMode) { + $customLayer->setOption('boundsMode', $model->boundsMode); + } + if ($model->pointToLayer) { $customLayer->setPointToLayer(new Expression($model->pointToLayer)); } diff --git a/src/Subscriber/EncoderSubscriber.php b/src/Subscriber/EncoderSubscriber.php index bf0aae6..7890246 100644 --- a/src/Subscriber/EncoderSubscriber.php +++ b/src/Subscriber/EncoderSubscriber.php @@ -89,16 +89,15 @@ class EncoderSubscriber implements EventSubscriberInterface { $value = $event->getValue(); $encoder = $event->getEncoder(); - $template = 'L.contao.load(%s, %s, %s, %s, map);'; + $template = 'L.contao.%s(%s, %s, %s, %s, map);'; + $method = 'loadFile'; if ($value instanceof OmnivoreLayer) { $url = $value->getUrl(); if ($url instanceof RequestUrl) { $url = $url->getHash(); - } elseif (strpos($url, '/') !== false) { - // Slash found, not a Contao leaflet hash, do not replace encoding. - return; + $method = 'load'; } if ($value->getCustomLayer()) { @@ -111,6 +110,7 @@ class EncoderSubscriber implements EventSubscriberInterface $event->addLine( sprintf( $template, + $method, $encoder->encodeValue($url), $encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))), $encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT),