diff --git a/module/config/autoload.php b/module/config/autoload.php index 299c6ab..a8f174a 100644 --- a/module/config/autoload.php +++ b/module/config/autoload.php @@ -12,6 +12,7 @@ TemplateLoader::addFiles( array( 'ce_leaflet_map' => 'system/modules/leaflet/templates', + 'leaflet_map_js' => 'system/modules/leaflet/templates', 'mod_leaflet_map' => 'system/modules/leaflet/templates', 'be_leaflet_geocode' => 'system/modules/leaflet/templates', 'be_leaflet_credits' => 'system/modules/leaflet/templates', diff --git a/module/config/services.php b/module/config/services.php index 39b4979..e84481f 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -74,9 +74,7 @@ $container['leaflet.definition.builder'] = $container->share(function($container $boot = $container['leaflet.boot']; $dispatcher = $container['leaflet.definition.builder.event-dispatcher']; - $encoder = new Encoder($dispatcher, new Output(), JSON_UNESCAPED_SLASHES); - $builder = new Builder($encoder, $dispatcher); - $leaflet = new Leaflet($builder); + $leaflet = new Leaflet($dispatcher, array(), JSON_UNESCAPED_SLASHES); return $boot->initializeLeafletBuilder($leaflet); }); diff --git a/module/templates/leaflet_map_js.html5 b/module/templates/leaflet_map_js.html5 new file mode 100644 index 0000000..9f08a10 --- /dev/null +++ b/module/templates/leaflet_map_js.html5 @@ -0,0 +1,5 @@ +ContaoLeaflet.addMap('', function() { + + +return { map: map, layers: layers, controls: controls, icons: icons }; +}()); diff --git a/src/Netzmacht/Contao/Leaflet/MapService.php b/src/Netzmacht/Contao/Leaflet/MapService.php index 3ba1f1f..be756c8 100644 --- a/src/Netzmacht/Contao/Leaflet/MapService.php +++ b/src/Netzmacht/Contao/Leaflet/MapService.php @@ -113,13 +113,21 @@ class MapService * * @return string */ - public function getJavascript($mapId, LatLngBounds $bounds = null, $elementId = null) + public function getJavascript($mapId, LatLngBounds $bounds = null, $elementId = null, $template = 'leaflet_map_js') { $definition = $this->getDefinition($mapId, $bounds, $elementId); $assets = new ContaoAssets(); - $javascript = $this->leaflet->build($definition, $assets); - $event = new GetJavascriptEvent($definition, $javascript); + $template = \Controller::getTemplate($template); + $javascript = $this->leaflet->build($definition, $assets); + $mapId = $definition->getId(); + + ob_start(); + include $template; + $content = ob_get_contents(); + ob_end_clean(); + + $event = new GetJavascriptEvent($definition, $content); $this->eventDispatcher->dispatch($event::NAME, $event); return $event->getJavascript(); diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php index d7fae03..7a268d9 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php @@ -12,10 +12,8 @@ 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; @@ -33,10 +31,6 @@ class EncoderSubscriber implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - BuildEvent::NAME => array( - array('startWrapper', 1000), - array('endWrapper', -1000), - ), EncodeValueEvent::NAME => array( array('encodeIcons', 100), array('loadLayer', 100), @@ -44,43 +38,6 @@ class EncoderSubscriber implements EventSubscriberInterface ); } - /** - * Start the wrapper. - * - * The encoded map is wrapped so that it is added to window.ContaoLeaflet. You can subscribe the - * "mapadded" event on window.ContaoLeaflet if you can to do some customize stuff. - * - * @param BuildEvent $event The subscribed event. - * - * @return void - */ - public function startWrapper(BuildEvent $event) - { - $object = $event->getObject(); - - if ($object instanceof Map) { - $line = sprintf('ContaoLeaflet.addMap(\'%s\', (function() {', $object->getId()); - $event->getOutput()->addLine($line); - } - } - - /** - * End the wrapper. - * - * @param BuildEvent $event The subscribed event. - * - * @return void - */ - public function endWrapper(BuildEvent $event) - { - $object = $event->getObject(); - - if ($object instanceof Map) { - $line = 'return map; })());'; - $event->getOutput()->addLine($line); - } - } - /** * Force that icons are encoded as reference to the ContaoLeaflet icon registry. * @@ -103,8 +60,7 @@ class EncoderSubscriber implements EventSubscriberInterface $value = $event->getValue(); $encoder = $event->getEncoder(); - if ($event->getReferenced() < Encoder::REFERENCE_REQUIRED && $value instanceof OmnivoreLayer) { - //$event->stopPropagation(); + if ($value instanceof OmnivoreLayer) { $event->addLine( sprintf( '%s = ContaoLeaflet.loadLayer(%s, %s, %s, %s, map.map);',