diff --git a/assets/maps/contao-leaflet.js b/assets/maps/contao-leaflet.js index 61a3bfc..b8bd7c4 100644 --- a/assets/maps/contao-leaflet.js +++ b/assets/maps/contao-leaflet.js @@ -1,4 +1,4 @@ -L.Contao = L.Class.extend({ +L.Contao = new (L.Class.extend({ includes: L.Mixin.Events, /** @@ -117,10 +117,10 @@ L.Contao = L.Class.extend({ loadLayer: function(url, type, options, customLayer, map) { var layer = omnivore[type](url, options, customLayer); - // Required because Control.Loading tries to get _leafet_id which is created here. - L.stamp(layer); - if (map) { + // Required because Control.Loading tries to get _leafet_id which is created here. + L.stamp(layer); + map.fire('dataloading', { layer: layer }); layer.on('ready', function() { @@ -225,6 +225,4 @@ L.Contao = L.Class.extend({ }; } } -}); - -window.ContaoLeaflet = new L.Contao(); +}))(); diff --git a/module/templates/leaflet_map_html.html5 b/module/templates/leaflet_map_html.html5 index 6d4a46d..303283f 100644 --- a/module/templates/leaflet_map_html.html5 +++ b/module/templates/leaflet_map_html.html5 @@ -1,7 +1,7 @@
-ContaoLeaflet.addMap('{$mapId}', function() { +L.Contao.addMap('{$mapId}', function() { {$javascript} return { map: map, layers: layers, controls: controls, icons: icons }; }()); diff --git a/module/templates/leaflet_map_js.html5 b/module/templates/leaflet_map_js.html5 index 9f08a10..e343576 100644 --- a/module/templates/leaflet_map_js.html5 +++ b/module/templates/leaflet_map_js.html5 @@ -1,4 +1,4 @@ -ContaoLeaflet.addMap('', function() { +L.Contao.addMap('', function() { return { map: map, layers: layers, controls: controls, icons: icons }; diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php index 16ef03e..499ff4c 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php @@ -150,7 +150,7 @@ class BootSubscriber implements EventSubscriberInterface } if ($icons) { - $buffer = sprintf('ContaoLeaflet.loadIcons(%s);', json_encode($icons)); + $buffer = sprintf('L.Contao.loadIcons(%s);', json_encode($icons)); } $file = new \File('assets/leaflet/js/icons.js'); diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php index 7a268d9..3976bd9 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php @@ -13,6 +13,7 @@ namespace Netzmacht\Contao\Leaflet\Subscriber; use Netzmacht\Javascript\Encoder; use Netzmacht\Javascript\Event\EncodeValueEvent; +use Netzmacht\Javascript\Event\GetReferenceEvent; use Netzmacht\LeafletPHP\Definition\Group\GeoJson; use Netzmacht\LeafletPHP\Definition\Type\Icon; use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; @@ -32,14 +33,26 @@ class EncoderSubscriber implements EventSubscriberInterface { return array( EncodeValueEvent::NAME => array( - array('encodeIcons', 100), + array('encodeIcons', 1000), array('loadLayer', 100), ), + GetReferenceEvent::NAME => array('referenceIcon', 100), ); } + public function referenceIcon(GetReferenceEvent $event) + { + $value = $event->getObject(); + + if ($value instanceof Icon) { + $event->setReference('L.Contao.getIcon(\'' . $value->getId() . '\')'); + $event->stopPropagation(); + } + + } + /** - * Force that icons are encoded as reference to the ContaoLeaflet icon registry. + * Force that icons are encoded as reference to the L.Contao icon registry. * * @param EncodeValueEvent $event The subscribed event. * @@ -50,7 +63,8 @@ class EncoderSubscriber implements EventSubscriberInterface $value = $event->getValue(); if ($value instanceof Icon) { - $event->addLine('ContaoLeaflet.getIcon(\'' . $value->getId() . '\')'); + //$event->addLine('L.Contao.getIcon(\'' . $value->getId() . '\')'); + $event->setSuccessful(); $event->stopPropagation(); } } @@ -59,18 +73,27 @@ class EncoderSubscriber implements EventSubscriberInterface { $value = $event->getValue(); $encoder = $event->getEncoder(); + $ref = $encoder->encodeReference($value); if ($value instanceof OmnivoreLayer) { $event->addLine( sprintf( - '%s = ContaoLeaflet.loadLayer(%s, %s, %s, %s, map.map);', - $encoder->encodeReference($value), + '%s = L.Contao.loadLayer(%s, %s, %s, %s, map);', + $ref, $encoder->encodeValue($value->getUrl()), $encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))), $encoder->encodeValue($value->getOptions()), $this->encodeCustomLayer($value, $encoder) ) ); + + foreach ($value->getLayers() as $layer) { + $event->addLine(sprintf('%s.addLayer(%s);', $ref, $encoder->encodeReference($layer))); + } + + foreach ($value->getMethodCalls() as $call) { + $event->addLine($call->encode($encoder, $encoder->getOutput())); + } } }