Switch to omnivore as default ajax layer.

This commit is contained in:
David Molineus
2015-01-13 14:54:23 +01:00
parent 2d1dadbefc
commit 33e5f1ed8c
4 changed files with 157 additions and 36 deletions

View File

@@ -22,7 +22,6 @@ use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
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 maps the layer model to the markers definition.
@@ -51,12 +50,40 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
protected function getClassName(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
if ($model->deferred) {
return 'Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax';
return 'Netzmacht\LeafletPHP\Plugins\Omnivore\GeoJson';
}
return parent::getClassName($model, $mapper, $bounds);
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
if ($model->deferred) {
if ($model->pointToLayer) {
$layer = new GeoJson($this->getElementId($model, $elementId) . '_1');
$layer->setPointToLayer(new Expression($model->pointToLayer));
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id), array(), $layer);
}
if (!empty($options)) {
}
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id));
}
return parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
}
/**
* {@inheritdoc}
*/
@@ -66,9 +93,7 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
DefinitionMapper $mapper,
LatLngBounds $bounds = null
) {
if ($definition instanceof GeoJsonAjax) {
$definition->setUrl(RequestUrl::create($model->id));
} elseif ($definition instanceof LayerGroup) {
if ($definition instanceof LayerGroup) {
$collection = $this->loadMarkerModels($model);
if ($collection) {

View File

@@ -22,9 +22,9 @@ use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Definition\Group\LayerGroup;
use Netzmacht\LeafletPHP\Definition\Layer;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Definition\Vector;
use Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax;
/**
* Class VectorsLayerMapper maps the layer model for the Vectors layer definition.
@@ -53,12 +53,45 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
protected function getClassName(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
if ($model->deferred) {
return 'Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax';
return 'Netzmacht\LeafletPHP\Plugins\Omnivore\GeoJson';
}
return parent::getClassName($model, $mapper, $bounds);
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
if ($model->deferred) {
$options = array();
if ($model->pointToLayer) {
$options['pointToLayer'] = new Expression($model->pointToLayer);
}
if ($model->onEachFeature) {
$options['onEachFeature'] = new Expression($model->onEachFeature);
}
if (!empty($options)) {
$layer = new GeoJson($this->getElementId($model, $elementId) . '_1');
$layer->setOptions($options);
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id), array(), $layer);
}
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id));
}
return parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
}
/**
* {@inheritdoc}
*/
@@ -68,19 +101,21 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
DefinitionMapper $mapper,
LatLngBounds $bounds = null
) {
if ($definition instanceof GeoJsonAjax) {
$definition->setUrl(RequestUrl::create($model->id));
} elseif ($definition instanceof LayerGroup) {
if ($definition instanceof LayerGroup) {
$collection = $this->loadVectorModels($model);
if ($collection) {
foreach ($collection as $item) {
$definition->addLayer($mapper->handle($item));
$vector = $mapper->handle($item);
if ($vector instanceof Layer) {
$definition->addLayer($vector);
}
}
}
}
if ($definition instanceof GeoJson || $definition instanceof GeoJsonAjax) {
if ($definition instanceof GeoJson) {
if ($model->pointToLayer) {
$definition->setPointToLayer(new Expression($model->pointToLayer));
}
@@ -104,8 +139,10 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
$vector = $mapper->handle($item);
if ($vector instanceof ConvertsToGeoJsonFeature) {
$feature->addFeature($vector->toGeoJsonFeature());
} elseif ($vector instanceof GeoJsonFeature) {
$vector = $vector->toGeoJsonFeature();
}
if ($vector instanceof GeoJsonFeature) {
$feature->addFeature($vector);
}
}

View File

@@ -11,10 +11,13 @@
namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Javascript\Encoder;
use Netzmacht\Javascript\Event\BuildEvent;
use Netzmacht\Javascript\Event\EncodeValueEvent;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@@ -35,7 +38,8 @@ class EncoderSubscriber implements EventSubscriberInterface
array('endWrapper', -1000),
),
EncodeValueEvent::NAME => array(
array('encodeIcons', 100)
array('encodeIcons', 100),
array('loadLayer', 100),
),
);
}
@@ -93,4 +97,48 @@ class EncoderSubscriber implements EventSubscriberInterface
$event->stopPropagation();
}
}
public function loadLayer(EncodeValueEvent $event)
{
$value = $event->getValue();
$encoder = $event->getEncoder();
if ($event->getReferenced() < Encoder::VALUE_REFERENCE_REQUIRED && $value instanceof OmnivoreLayer) {
//$event->stopPropagation();
$event->addLine(
sprintf(
'%s = ContaoLeaflet.loadLayer(%s, %s, %s, %s, map.map);',
$encoder->encodeReference($value),
$encoder->encodeValue($value->getUrl()),
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
$encoder->encodeValue($value->getOptions()),
$this->encodeCustomLayer($value, $encoder)
)
);
}
}
/**
* Encode a custom layer for the omnivore plugin.
*
* @param OmnivoreLayer $layer The layer.
* @param Encoder $encoder The javascript encoder.
*
* @return string
*/
protected function encodeCustomLayer(OmnivoreLayer $layer, Encoder $encoder)
{
$customLayer = $layer->getCustomLayer();
if ($customLayer instanceof GeoJson && !$customLayer->getMethodCalls()) {
return sprintf(
'L.geoJson(null, %s)',
$encoder->encodeValue($customLayer->getOptions())
);
} elseif ($customLayer) {
return $encoder->encodeReference($customLayer);
}
return 'null';
}
}