Add deferred markers loading.

This commit is contained in:
David Molineus
2015-01-06 15:43:57 +01:00
parent 4e41cc3db3
commit b3d32fef46
3 changed files with 66 additions and 21 deletions

View File

@@ -108,6 +108,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
'default' => array( 'default' => array(
'title' => array('title', 'alias', 'type'), 'title' => array('title', 'alias', 'type'),
'active' => array('active'), 'active' => array('active'),
'expert' => array('deferred'),
), ),
'markers extends default' => array( 'markers extends default' => array(
'+title' => array('markerCluster'), '+title' => array('markerCluster'),
@@ -240,5 +241,13 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array
), ),
'sql' => "varchar(255) NOT NULL default ''" '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 ''"
),
) )
); );

View File

@@ -17,7 +17,7 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\LeafletPHP\Assets; use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; 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\Map;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Leaflet; use Netzmacht\LeafletPHP\Leaflet;

View File

@@ -11,7 +11,6 @@
namespace Netzmacht\Contao\Leaflet\Mapper\Layer; namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper; use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper;
use Netzmacht\Contao\Leaflet\Model\MarkerModel; 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\Group\LayerGroup;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Definition\UI\Marker; use Netzmacht\LeafletPHP\Definition\UI\Marker;
use Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax;
class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
{ {
@@ -37,24 +37,39 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
*/ */
protected static $type = 'markers'; 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( protected function doBuild(
Definition $definition, Definition $definition,
\Model $model, \Model $model,
DefinitionMapper $builder, DefinitionMapper $builder,
LatLngBounds $bounds = null LatLngBounds $bounds = null
) { ) {
if ($definition instanceof LayerGroup) { if ($definition instanceof GeoJsonAjax) {
$collection = MarkerModel::findBy( $base = \Config::get('websitePath') . '/system/modules/leaflet/public/geojson.php?id=';
array('active=1', 'pid=?'), $definition->setUrl($base . $model->id);
array($model->id) } elseif ($definition instanceof LayerGroup) {
); $collection = $this->loadMarkerModels($model);
if ($collection) { if ($collection) {
foreach ($collection as $item) { foreach ($collection as $item) {
$marker = new Marker('marker_' . $item->id, $item->coordinates); $definition->addLayer($this->createMarker($item));
$marker->setTitle($item->tooltip);
$definition->addLayer($marker);
} }
} }
} }
@@ -69,22 +84,43 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
*/ */
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{ {
$feature = new FeatureCollection(); $feature = new FeatureCollection();
$collection = $this->loadMarkerModels($model);
$collection = MarkerModel::findBy(
array('active=1', 'pid=?'),
array($model->id)
);
if ($collection) { if ($collection) {
foreach ($collection as $item) { foreach ($collection as $item) {
$marker = new Marker('marker_' . $item->id, $item->coordinates); $feature->addFeature($this->createMarker($item)->getFeature());
$marker->setTitle($item->tooltip);
$feature->addFeature($marker->getFeature());
} }
} }
return $feature; 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;
}
} }