From b3d32fef46c6db9d1c662ec2e60a626d1ef0e651 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Tue, 6 Jan 2015 15:43:57 +0100 Subject: [PATCH] Add deferred markers loading. --- module/dca/tl_leaflet_layer.php | 9 +++ src/Netzmacht/Contao/Leaflet/MapService.php | 2 +- .../Mapper/Layer/MarkersLayerMapper.php | 76 ++++++++++++++----- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php index 82c8b2f..c12e5cb 100644 --- a/module/dca/tl_leaflet_layer.php +++ b/module/dca/tl_leaflet_layer.php @@ -108,6 +108,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'default' => array( 'title' => array('title', 'alias', 'type'), 'active' => array('active'), + 'expert' => array('deferred'), ), 'markers extends default' => array( '+title' => array('markerCluster'), @@ -240,5 +241,13 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array ), 'sql' => "varchar(255) NOT NULL default ''" ), + 'deferred' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['deferred'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class' => 'w50', 'submitOnChange' => false), + 'sql' => "char(1) NOT NULL default ''" + ), ) ); diff --git a/src/Netzmacht/Contao/Leaflet/MapService.php b/src/Netzmacht/Contao/Leaflet/MapService.php index f6b433a..ca5f4ce 100644 --- a/src/Netzmacht/Contao/Leaflet/MapService.php +++ b/src/Netzmacht/Contao/Leaflet/MapService.php @@ -17,7 +17,7 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\LeafletPHP\Assets; use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; -use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollectionAggregate; +use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJson; use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; use Netzmacht\LeafletPHP\Leaflet; diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php index 9264b80..9f05e4f 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php @@ -11,7 +11,6 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Layer; - use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper; use Netzmacht\Contao\Leaflet\Model\MarkerModel; @@ -20,6 +19,7 @@ use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; use Netzmacht\LeafletPHP\Definition\Group\LayerGroup; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; use Netzmacht\LeafletPHP\Definition\UI\Marker; +use Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax; class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper { @@ -37,24 +37,39 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper */ protected static $type = 'markers'; + /** + * {@inheritdoc} + */ + protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) + { + if ($model->deferred) { + $reflector = new \ReflectionClass('Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax'); + $instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds)); + + return $instance; + } + + return parent::createInstance($model, $mapper, $bounds); + } + + /** + * {@inheritdoc} + */ protected function doBuild( Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null ) { - if ($definition instanceof LayerGroup) { - $collection = MarkerModel::findBy( - array('active=1', 'pid=?'), - array($model->id) - ); + if ($definition instanceof GeoJsonAjax) { + $base = \Config::get('websitePath') . '/system/modules/leaflet/public/geojson.php?id='; + $definition->setUrl($base . $model->id); + } elseif ($definition instanceof LayerGroup) { + $collection = $this->loadMarkerModels($model); if ($collection) { foreach ($collection as $item) { - $marker = new Marker('marker_' . $item->id, $item->coordinates); - $marker->setTitle($item->tooltip); - - $definition->addLayer($marker); + $definition->addLayer($this->createMarker($item)); } } } @@ -69,22 +84,43 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper */ public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) { - $feature = new FeatureCollection(); - - $collection = MarkerModel::findBy( - array('active=1', 'pid=?'), - array($model->id) - ); + $feature = new FeatureCollection(); + $collection = $this->loadMarkerModels($model); if ($collection) { foreach ($collection as $item) { - $marker = new Marker('marker_' . $item->id, $item->coordinates); - $marker->setTitle($item->tooltip); - - $feature->addFeature($marker->getFeature()); + $feature->addFeature($this->createMarker($item)->getFeature()); } } return $feature; } + + /** + * @param $item + * + * @return Marker + */ + protected function createMarker($item) + { + $marker = new Marker('marker_' . $item->id, $item->coordinates); + $marker->setTitle($item->tooltip); + + return $marker; + } + + /** + * @param \Model $model + * + * @return \Model\Collection|null + */ + protected function loadMarkerModels(\Model $model) + { + $collection = MarkerModel::findBy( + array('active=1', 'pid=?'), + array($model->id) + ); + + return $collection; + } }