From 33e5f1ed8c68c659d444b771752ae9c4c2e28771 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Tue, 13 Jan 2015 14:54:23 +0100 Subject: [PATCH] Switch to omnivore as default ajax layer. --- assets/maps/contao-leaflet.js | 53 +++++++++++------- .../Mapper/Layer/MarkersLayerMapper.php | 35 ++++++++++-- .../Mapper/Layer/VectorsLayerMapper.php | 55 ++++++++++++++++--- .../Leaflet/Subscriber/EncoderSubscriber.php | 50 ++++++++++++++++- 4 files changed, 157 insertions(+), 36 deletions(-) diff --git a/assets/maps/contao-leaflet.js b/assets/maps/contao-leaflet.js index 6a3136c..eeae03b 100644 --- a/assets/maps/contao-leaflet.js +++ b/assets/maps/contao-leaflet.js @@ -24,7 +24,6 @@ L.Contao = L.Class.extend({ initialize: function() { L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images'; - this.bindDataLoadingEvents(); this.setGeoJsonListeners(L.GeoJSON); this.setGeoJsonListeners(L.GeoJSON.AJAX); }, @@ -106,6 +105,38 @@ L.Contao = L.Class.extend({ return this.icons[id]; }, + /** + * Layer a url into a layer using omnivore. + * + * @param url The url being loaded. + * @param type The response content format. + * @param options Parser options + * @param customLayer optional custom layer. + * @param map Pass a map object so that the data loading events are passed to the map. + */ + loadLayer: function(url, type, options, customLayer, map) { + if (map) { + map.fire('dataloading'); + } + + var layer = omnivore[type](url, options, customLayer); + + layer.on('ready', function(e) { + if (map) { + map.fire('dataload'); + } + }); + + layer.on('error', function(e) { + if (map) { + map.fire('dataload'); + } + + }); + + return layer; + }, + /** * Point to layer callback. Adds a geo json point to the layer. * @@ -183,25 +214,6 @@ L.Contao = L.Class.extend({ } }, - /** - * Bind triggered data:loading and data:loaded events to the map. - * - * These events are fired by leaflet.ajax. The loading indicator listens to the map dataloading and dataloaded - * events which is also used by the tile layers. - */ - bindDataLoadingEvents: function() { - L.Map.addInitHook(function () { - var map = this; - - this.on('layeradd', function(e) { - if (e.layer.on) { - e.layer.on('data:loading', function() { map.fire('dataloading'); }); - e.layer.on('data:loaded', function() { map.fire('dataload'); }); - } - }); - }); - }, - /** * Set the default geojson listeners to the prototype. * @@ -215,7 +227,6 @@ L.Contao = L.Class.extend({ }; } } - }); window.ContaoLeaflet = new L.Contao(); diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php index 9d2d4ea..de8af0f 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php @@ -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) { diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php index 7247701..0d503f3 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php @@ -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); } } diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php index bc957e8..9cc943d 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php @@ -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'; + } }