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(
'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 ''"
),
)
);

View File

@@ -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;

View File

@@ -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;
}
}