forked from Snck3rs/contao-leaflet-maps
Move javascript wrapping into an template.
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
5
module/templates/leaflet_map_js.html5
Normal file
5
module/templates/leaflet_map_js.html5
Normal file
@@ -0,0 +1,5 @@
|
||||
ContaoLeaflet.addMap('<?php echo $mapId; ?>', function() {
|
||||
<?php echo $javascript; ?>
|
||||
|
||||
return { map: map, layers: layers, controls: controls, icons: icons };
|
||||
}());
|
||||
@@ -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();
|
||||
|
||||
@@ -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);',
|
||||
|
||||
Reference in New Issue
Block a user