Rework libraries handling so that libraries don't have to be ported to contao 4.4 only yet.

This commit is contained in:
David Molineus committed 2017-10-09 15:36:11 +02:00
1 parent e6ed2c456d
commit edf212b850
8 files changed
+199 -10

No files matched your search

@@ -0,0 +1,95 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
* @filesource
*/
declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Frontend\Assets;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface as ContaoFramework;
use Traversable;
/**
* Class LibrariesConfiguration
*
* @package Netzmacht\Contao\Leaflet\Frontend\Assets
*/
class LibrariesConfiguration implements \IteratorAggregate, \ArrayAccess
{
/**
* @var ContaoFramework
*/
private $framework;
/**
* LibrariesConfiguration constructor.
*
* @param ContaoFramework $framework
*/
public function __construct(ContaoFramework $framework)
{
$this->framework = $framework;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function getIterator()
{
$this->framework->initialize();
return new \ArrayIterator($GLOBALS['LEAFLET_LIBRARIES']);
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function offsetExists($offset)
{
$this->framework->initialize();
return isset($GLOBALS['LEAFLET_LIBRARIES'][$offset]);
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function offsetGet($offset)
{
return $GLOBALS['LEAFLET_LIBRARIES'][$offset];
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function offsetSet($offset, $value)
{
$GLOBALS['LEAFLET_LIBRARIES'][$offset] = $value;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function offsetUnset($offset)
{
unset($GLOBALS['LEAFLET_LIBRARIES'][$offset]);
}
}
+10 -6
View File
@@ -15,6 +15,7 @@ declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Listener;
use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Assets;
@@ -45,18 +46,18 @@ class LoadAssetsListener
/**
* Libraries.
*
* @var array
* @var LibrariesConfiguration
*/
private $libraries;
/**
* LoadAssetsListener constructor.
*
* @param Assets $assets Assets.
* @param DefinitionMapper $definitionMapper Definition mapper.
* @param array $libraries Libraries.
* @param Assets $assets Assets.
* @param DefinitionMapper $definitionMapper Definition mapper.
* @param LibrariesConfiguration $libraries Libraries.
*/
public function __construct(Assets $assets, DefinitionMapper $definitionMapper, array $libraries)
public function __construct(Assets $assets, DefinitionMapper $definitionMapper, LibrariesConfiguration $libraries)
{
$this->assets = $assets;
$this->definitionMapper = $definitionMapper;
@@ -70,7 +71,10 @@ class LoadAssetsListener
*/
public function onGetJavascriptEvent(): void
{
$this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE);
$this->assets->addJavascript(
'web/bundles/netzmachtcontaoleaflet/js/contao-leaflet.js',
ContaoAssets::TYPE_FILE
);
$collection = IconModel::findBy('active', true);
@@ -0,0 +1,72 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
* @filesource
*/
declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Listener;
use Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration;
use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Leaflet;
/**
* Class RegisterLibrariesListener.
*
* @package Netzmacht\Contao\Leaflet\Listener
*/
final class RegisterLibrariesListener
{
/**
* Libraries configuration.
*
* @var LibrariesConfiguration
*/
private $libraries;
/**
* Leaflet builder.
*
* @var Leaflet
*/
private $leaflet;
/**
* RegisterLibrariesListener constructor.
*
* @param LibrariesConfiguration $libraries Libraries configuration.
* @param Leaflet $leaflet Leaflet builder.
*/
public function __construct(LibrariesConfiguration $libraries, Leaflet $leaflet)
{
$this->libraries = $libraries;
$this->leaflet = $leaflet;
}
/**
* Handle the on initialize system hook.
*
* @return void
*/
public function onInitializeSystem(): void
{
foreach ($this->libraries as $name => $assets) {
if (!empty($assets['css'])) {
list ($source, $type) = (array) $assets['css'];
$this->leaflet->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE);
}
if (!empty($assets['javascript'])) {
list ($source, $type) = (array) $assets['javascript'];
$this->leaflet->registerJavascript($name, $source, $type ?: Assets::TYPE_FILE);
}
}
}
}
-3
View File
@@ -58,6 +58,3 @@ parameters:
- 'id'
- 'title'
- 'alias'
# Leaflet libraries files
netzmacht.contao_leaflet_maps.libraries: []
+8 -1
View File
@@ -65,6 +65,13 @@ services:
arguments:
- '@netzmacht.contao_leaflet_maps.map.assets'
- '@netzmacht.contao_leaflet_maps.definition.mapper'
- '%netzmacht.contao_leaflet_maps.libraries%'
- '@netzmacht.contao_leaflet_maps.libraries'
tags:
- { name: 'kernel.event_listener', event: 'netzmacht.contao_leaflet.get_javascript', method: 'onGetJavascriptEvent' }
netzmacht.contao_leaflet_maps.listeners.register_libraries:
class: Netzmacht\Contao\Leaflet\Listener\RegisterLibrariesListener
arguments:
- '@netzmacht.contao_leaflet_maps.libraries'
- '@netzmacht.contao_leaflet_maps.definition.builder'
+5
View File
@@ -17,6 +17,11 @@ services:
- [] # TODO: Rework filter handling.
- '%kernel.debug%'
netzmacht.contao_leaflet_maps.libraries:
class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration
arguments:
- '@contao.framework'
netzmacht.contao_leaflet_maps.cache:
alias: 'netzmacht.contao_leaflet_maps.cache.default'
+8
View File
@@ -84,3 +84,11 @@ $GLOBALS['TL_MODELS']['tl_leaflet_marker'] = \Netzmacht\Contao\Leaflet\Model\Ma
$GLOBALS['TL_MODELS']['tl_leaflet_popup'] = \Netzmacht\Contao\Leaflet\Model\PopupModel::class;
$GLOBALS['TL_MODELS']['tl_leaflet_style'] = \Netzmacht\Contao\Leaflet\Model\StyleModel::class;
$GLOBALS['TL_MODELS']['tl_leaflet_vector'] = \Netzmacht\Contao\Leaflet\Model\VectorModel::class;
/*
* Hooks.
*/
$GLOBALS['TL_HOOKS']['initializeSystem'][] = [
'netzmacht.contao_leaflet_maps.listeners.register_libraries',
'onInitializeSystem'
];
@@ -0,0 +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,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;e="extraMarkers.icon"===t[o].type?L.ExtraMarkers.icon(t[o].options):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&&this.fitBounds(this._dynamicBounds,this.getBoundsOptions())},getBoundsOptions:function(){return 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)),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))}}),L.LatLngBounds.prototype.toOverpassBBoxString=function(){var t=this._southWest,o=this._northEast;return[tLine truncated