Implement bounds padding option.

This commit is contained in:
David Molineus
2016-10-11 14:32:43 +02:00
parent 17e7bc5e1c
commit 28490a9ee6
5 changed files with 43 additions and 4 deletions

View File

@@ -1 +1 @@
L.Contao=L.Class.extend({includes:L.Mixin.Events,statics:{ATTRIBUTION:' | <a href="https://netzmacht.de/contao-leaflet" title="Powered by Leaflet extension for Contao CMS developed by netzmacht David Molineus">netzmacht</a>'},maps:{},icons:{},initialize:function(){L.Icon.Default.imagePath="assets/leaflet/libs/leaflet/images/",this.setGeoJsonListeners(L.GeoJSON)},addMap:function(t,e){return this.maps[t]=e,this.fire("map:added",{id:t,map:e}),this},getMap:function(t){return"undefined"==typeof this.maps[t]?null:this.maps[t]},addIcon:function(t,e){return this.icons[t]=e,this.fire("icon:added",{id:t,icon:e}),this},loadIcons:function(t){for(var e=0;e<t.length;e++){var o=L[t[e].type](t[e].options);this.addIcon(t[e].id,o)}},getIcon:function(t){return"undefined"==typeof this.icons[t]?null:this.icons[t]},load:function(t,e,o,n,i){var s=this.createRequestUrl(t,i),r=omnivore[e](s,o,n);return i&&(L.stamp(r),i.options.dynamicLoad&&"fit"==r.options.boundsMode&&(r.options.requestHash=t,i.on("moveend",r.refreshData,r),i.on("layerremove",function(t){t.layer===r&&i.off("moveend",r.updateBounds,r)})),i.fire("dataloading",{layer:r}),r.on("ready",function(){i.calculateFeatureBounds(r),i.fire("dataload",{layer:r})}),r.on("error",function(){i.fire("dataload",{layer:r})})),r},pointToLayer:function(t,e){var o="marker",n=null;if(t.properties&&(t.properties.bounds=!0,t.properties.type&&(o=t.properties.type),t.properties.arguments&&(n=L[o].apply(L[o],t.properties.arguments),L.Util.setOptions(n,t.properties.options))),null===n&&(n=L[o](e,t.properties.options)),t.properties){if(t.properties.radius&&n.setRadius(t.properties.radius),t.properties.icon){var i=this.getIcon(t.properties.icon);i&&n.setIcon(i)}this.bindPopupFromFeature(n,t)}return this.fire("point:added",{marker:n,feature:t,latlng:e,type:o}),n},onEachFeature:function(t,e){t.properties&&(L.Util.setOptions(e,t.properties.options),this.bindPopupFromFeature(e,t),this.fire("feature:added",{feature:t,layer:e}))},bindPopupFromFeature:function(t,e){e.properties&&(e.properties.popup?t.bindPopup(e.properties.popup,e.properties.popupOptions):e.properties.popupContent&&t.bindPopup(e.properties.popupContent,e.properties.popupOptions))},setGeoJsonListeners:function(t){t&&t.prototype&&(t.prototype.options={pointToLayer:this.pointToLayer.bind(this),onEachFeature:this.onEachFeature.bind(this)})},createRequestUrl:function(t,e){var o,n="leaflet",i=document.location.search.substr(1).split("&");if(t=encodeURIComponent(t),""==i)t=document.location.pathname+"?"+[n,t].join("=");else{for(var s,r=i.length;r--;)if(s=i[r].split("="),s[0]==n){s[1]=t,i[r]=s.join("=");break}r<0&&(i[i.length]=[n,t].join("=")),t=document.location.pathname+"?"+i.join("&")}return e&&e.options.dynamicLoad&&(o=e.getBounds(),t+="&f=bbox&v=",t+=o.getSouth()+","+o.getWest(),t+=","+o.getNorth()+","+o.getEast()),t}}),L.contao=new L.Contao,L.Control.Attribution.addInitHook(function(){this.options.prefix+=L.Contao.ATTRIBUTION}),L.Control.Attribution.include({setPrefix:function(t){return t.indexOf(L.Contao.ATTRIBUTION)===-1&&(t+=L.Contao.ATTRIBUTION),this.options.prefix=t,this._update(),this}}),L.GeoJSON.include({refreshData:function(t){var e=L.geoJson(),o=this;e.on("ready",function(){var t,e=o.getLayers();for(t=0;t<e.length;t++)o.removeLayer(e[t]);for(e=this.getLayers(),t=0;t<e.length;t++)this.removeLayer(e[t]),o.addLayer(e[t])}),omnivore.geojson(L.contao.createRequestUrl(this.options.requestHash,t.target),null,e)}}),L.Map.include({_dynamicBounds:null,calculateFeatureBounds:function(t,e){if(t){if(!this.options.adjustBounds&&!e)return;this._scanForBounds(t)}else this.eachLayer(this._scanForBounds,this);this._dynamicBounds&&this.fitBounds(this._dynamicBounds)},_scanForBounds:function(t){var e;!t.feature||t.feature.properties&&t.feature.properties.ignoreForBounds?L.MarkerClusterGroup&&t instanceof L.MarkerClusterGroup&&t.options.boundsMode&&"extend"==t.options.boundsMode?(e=t.getBounds(),e.isValid()&&(this._dynamicBounds?this._dynamicBounds.extend(e):this._dynamicBounds=L.latLngBounds(e.getSouthWest(),e.getNorthEast()))):(!t.options||t.options&&t.options.boundsMode&&!t.options.ignoreForBounds)&&t.eachLayer&&t.eachLayer(this._scanForBounds,this):t.getBounds?(e=t.getBounds(),e.isValid()&&(this._dynamicBounds?this._dynamicBounds.extend(e):this._dynamicBounds=L.latLngBounds(e.getSouthWest(),e.getNorthEast()))):t.getLatLng&&(e=t.getLatLng(),this._dynamicBounds?this._dynamicBounds.extend(e):this._dynamicBounds=L.latLngBounds(e,e))}});
L.Contao=L.Class.extend({includes:L.Mixin.Events,statics:{ATTRIBUTION:' | <a href="https://netzmacht.de/contao-leaflet" title="Powered by Leaflet extension for Contao CMS developed by netzmacht David Molineus">netzmacht</a>'},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"undefined"==typeof 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<t.length;o++){var e=L[t[o].type](t[o].options);this.addIcon(t[o].id,e)}},getIcon:function(t){return"undefined"==typeof this.icons[t]?null:this.icons[t]},load:function(t,o,e,n,i){var s=this.createRequestUrl(t,i),r=omnivore[o](s,e,n);return i&&(L.stamp(r),i.options.dynamicLoad&&"fit"==r.options.boundsMode&&(r.options.requestHash=t,i.on("moveend",r.refreshData,r),i.on("layerremove",function(t){t.layer===r&&i.off("moveend",r.updateBounds,r)})),i.fire("dataloading",{layer:r}),r.on("ready",function(){i.calculateFeatureBounds(r),i.fire("dataload",{layer:r})}),r.on("error",function(){i.fire("dataload",{layer:r})})),r},pointToLayer:function(t,o){var e="marker",n=null;if(t.properties&&(t.properties.bounds=!0,t.properties.type&&(e=t.properties.type),t.properties.arguments&&(n=L[e].apply(L[e],t.properties.arguments),L.Util.setOptions(n,t.properties.options))),null===n&&(n=L[e](o,t.properties.options)),t.properties){if(t.properties.radius&&n.setRadius(t.properties.radius),t.properties.icon){var i=this.getIcon(t.properties.icon);i&&n.setIcon(i)}this.bindPopupFromFeature(n,t)}return this.fire("point:added",{marker:n,feature:t,latlng:o,type:e}),n},onEachFeature:function(t,o){t.properties&&(L.Util.setOptions(o,t.properties.options),this.bindPopupFromFeature(o,t),this.fire("feature:added",{feature:t,layer:o}))},bindPopupFromFeature:function(t,o){o.properties&&(o.properties.popup?t.bindPopup(o.properties.popup,o.properties.popupOptions):o.properties.popupContent&&t.bindPopup(o.properties.popupContent,o.properties.popupOptions))},setGeoJsonListeners:function(t){t&&t.prototype&&(t.prototype.options={pointToLayer:this.pointToLayer.bind(this),onEachFeature:this.onEachFeature.bind(this)})},createRequestUrl:function(t,o){var e,n="leaflet",i=document.location.search.substr(1).split("&");if(t=encodeURIComponent(t),""==i)t=document.location.pathname+"?"+[n,t].join("=");else{for(var s,r=i.length;r--;)if(s=i[r].split("="),s[0]==n){s[1]=t,i[r]=s.join("=");break}r<0&&(i[i.length]=[n,t].join("=")),t=document.location.pathname+"?"+i.join("&")}return o&&o.options.dynamicLoad&&(e=o.getBounds(),t+="&f=bbox&v=",t+=e.getSouth()+","+e.getWest(),t+=","+e.getNorth()+","+e.getEast()),t}}),L.contao=new L.Contao,L.Control.Attribution.addInitHook(function(){this.options.prefix+=L.Contao.ATTRIBUTION}),L.Control.Attribution.include({setPrefix:function(t){return t.indexOf(L.Contao.ATTRIBUTION)===-1&&(t+=L.Contao.ATTRIBUTION),this.options.prefix=t,this._update(),this}}),L.GeoJSON.include({refreshData:function(t){var o=L.geoJson(),e=this;o.on("ready",function(){var t,o=e.getLayers();for(t=0;t<o.length;t++)e.removeLayer(o[t]);for(o=this.getLayers(),t=0;t<o.length;t++)this.removeLayer(o[t]),e.addLayer(o[t])}),omnivore.geojson(L.contao.createRequestUrl(this.options.requestHash,t.target),null,o)}}),L.Map.include({_dynamicBounds:null,calculateFeatureBounds:function(t,o){if(t){if(!this.options.adjustBounds&&!o)return;this._scanForBounds(t)}else this.eachLayer(this._scanForBounds,this);this._dynamicBounds&&(options={},this.options.boundsPadding?options.padding=this.options.boundsPadding:(this.options.boundsPaddingTopLeft&&(options.paddingTopLeft=this.options.boundsPaddingTopLeft),this.options.boundsPaddingBottomRight&&(options.paddingBottomRight=this.options.boundsPaddingBottomRight)),this.fitBounds(this._dynamicBounds,options))},_scanForBounds:function(t){var o;!t.feature||t.feature.properties&&t.feature.properties.ignoreForBounds?L.MarkerClusterGroup&&t instanceof L.MarkerClusterGroup&&t.options.boundsMode&&"extend"==t.options.boundsMode?(o=t.getBounds(),o.isValid()&&(this._dynamicBounds?this._dynamicBounds.extend(o):this._dynamicBounds=L.latLngBounds(o.getSouthWest(),o.getNorthEast()))):(!t.options||t.options&&t.options.boundsMode&&!t.options.ignoreForBounds)&&t.eachLayer&&t.eachLayer(this._scanForBounds,this):t.getBounds?(o=t.getBounds(),o.isValid()&&(this._dynamicBounds?this._dynamicBounds.extend(o):this._dynamicBounds=L.latLngBounds(o.getSouthWest(),o.getNorthEast()))):t.getLatLng&&(o=t.getLatLng(),this._dynamicBounds?this._dynamicBounds.extend(o):this._dynamicBounds=L.latLngBounds(o,o))}});

View File

@@ -30,7 +30,20 @@ L.Map.include({
}
if (this._dynamicBounds) {
this.fitBounds(this._dynamicBounds);
options = {};
if (this.options.boundsPadding) {
options.padding = this.options.boundsPadding;
} else {
if (this.options.boundsPaddingTopLeft) {
options.paddingTopLeft = this.options.boundsPaddingTopLeft;
}
if (this.options.boundsPaddingBottomRight) {
options.paddingBottomRight = this.options.boundsPaddingBottomRight;
}
}
this.fitBounds(this._dynamicBounds, options);
}
},

View File

@@ -97,7 +97,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'metapalettes' => array(
'default' => array(
'title' => array('title', 'alias'),
'zoom' => array('center', 'zoom', 'adjustZoomExtra', 'adjustBounds', 'dynamicLoad'),
'zoom' => array('center', 'zoom', 'adjustZoomExtra', 'adjustBounds', 'dynamicLoad', 'boundsPadding'),
'locate' => array('locate'),
'layers' => array('layers'),
'interaction' => array(
@@ -479,9 +479,22 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'exclude' => true,
'inputType' => 'checkbox',
'default' => false,
'eval' => array('tl_class' => 'w50', 'submitOnChange' => false),
'eval' => array('tl_class' => 'clr w50', 'submitOnChange' => false),
'sql' => "char(1) NOT NULL default ''"
),
'boundsPadding' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['boundsPadding'],
'exclude' => true,
'inputType' => 'text',
'eval' => array(
'maxlength' => 32,
'tl_class' => 'w50',
'includeBlankOption' => true,
'nullIfEmpty' => true
),
'sql' => "varchar(32) NULL"
),
'locate' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['locate'],

View File

@@ -87,6 +87,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_map']['locateMaximumAge'][0] = 'Locate maximu
$GLOBALS['TL_LANG']['tl_leaflet_map']['locateMaximumAge'][1] = 'Maximum age of detected location in milliseconds. Use cached value if time has not passed.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['dynamicLoad'][0] = 'Load data dynamically in bounds';
$GLOBALS['TL_LANG']['tl_leaflet_map']['dynamicLoad'][1] = 'If enabled the map requests data from the layers for the current bounds only and make a new request if bounds has changed.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['boundsPadding'][0] = 'Bounds padding';
$GLOBALS['TL_LANG']['tl_leaflet_map']['boundsPadding'][1] = 'Padding being used then applying bounds. Use comma separated values. If 2 values are given the <em>padding</em> option is set. If 4 values are given <em>paddingTopLeft</em> and <em>paddingBottomRight</em> is set.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['cache'][0] = 'Enable cache';
$GLOBALS['TL_LANG']['tl_leaflet_map']['cache'][1] = 'If enabled the generated map javascript is cached.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['cacheLifeTime'][0] = 'Cache lifetime';

View File

@@ -180,6 +180,17 @@ class MapMapper extends AbstractMapper
$map->setOption('adjustBounds', true);
}
if ($model->boundsPadding) {
$value = array_map('intval', explode(',', $model->boundsPadding, 4));
if (count($value) === 4) {
$map->setOption('boundsPaddingTopLeft', [$value[0], $value[1]]);
$map->setOption('boundsPaddingBottomRight', [$value[2], $value[3]]);
} elseif (count($value) === 2) {
$map->setOption('boundsPadding', $value);
}
}
if (in_array('load', $adjustBounds)) {
$map->calculateFeatureBounds();
}