Use L.Contao as reference to the Contao integration object.

This commit is contained in:
David Molineus
2015-01-15 01:10:36 +01:00
parent 8186a0b3ee
commit 6fbc1a9bf3
5 changed files with 36 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
L.Contao = L.Class.extend({ L.Contao = new (L.Class.extend({
includes: L.Mixin.Events, includes: L.Mixin.Events,
/** /**
@@ -117,10 +117,10 @@ L.Contao = L.Class.extend({
loadLayer: function(url, type, options, customLayer, map) { loadLayer: function(url, type, options, customLayer, map) {
var layer = omnivore[type](url, options, customLayer); 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) { if (map) {
// Required because Control.Loading tries to get _leafet_id which is created here.
L.stamp(layer);
map.fire('dataloading', { layer: layer }); map.fire('dataloading', { layer: layer });
layer.on('ready', function() { layer.on('ready', function() {
@@ -225,6 +225,4 @@ L.Contao = L.Class.extend({
}; };
} }
} }
}); }))();
window.ContaoLeaflet = new L.Contao();

View File

@@ -1,7 +1,7 @@
<div id="<?php echo $mapId; ?>" style="<?php echo $style; ?>"></div> <div id="<?php echo $mapId; ?>" style="<?php echo $style; ?>"></div>
<?php $GLOBALS['TL_BODY'][] = <<<HTML <?php $GLOBALS['TL_BODY'][] = <<<HTML
<script> <script>
ContaoLeaflet.addMap('{$mapId}', function() { L.Contao.addMap('{$mapId}', function() {
{$javascript} {$javascript}
return { map: map, layers: layers, controls: controls, icons: icons }; return { map: map, layers: layers, controls: controls, icons: icons };
}());</script> }());</script>

View File

@@ -1,4 +1,4 @@
ContaoLeaflet.addMap('<?php echo $mapId; ?>', function() { L.Contao.addMap('<?php echo $mapId; ?>', function() {
<?php echo $javascript; ?> <?php echo $javascript; ?>
return { map: map, layers: layers, controls: controls, icons: icons }; return { map: map, layers: layers, controls: controls, icons: icons };

View File

@@ -150,7 +150,7 @@ class BootSubscriber implements EventSubscriberInterface
} }
if ($icons) { 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'); $file = new \File('assets/leaflet/js/icons.js');

View File

@@ -13,6 +13,7 @@ namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Javascript\Encoder; use Netzmacht\Javascript\Encoder;
use Netzmacht\Javascript\Event\EncodeValueEvent; use Netzmacht\Javascript\Event\EncodeValueEvent;
use Netzmacht\Javascript\Event\GetReferenceEvent;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson; use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Definition\Type\Icon; use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
@@ -32,14 +33,26 @@ class EncoderSubscriber implements EventSubscriberInterface
{ {
return array( return array(
EncodeValueEvent::NAME => array( EncodeValueEvent::NAME => array(
array('encodeIcons', 100), array('encodeIcons', 1000),
array('loadLayer', 100), 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. * @param EncodeValueEvent $event The subscribed event.
* *
@@ -50,7 +63,8 @@ class EncoderSubscriber implements EventSubscriberInterface
$value = $event->getValue(); $value = $event->getValue();
if ($value instanceof Icon) { if ($value instanceof Icon) {
$event->addLine('ContaoLeaflet.getIcon(\'' . $value->getId() . '\')'); //$event->addLine('L.Contao.getIcon(\'' . $value->getId() . '\')');
$event->setSuccessful();
$event->stopPropagation(); $event->stopPropagation();
} }
} }
@@ -59,18 +73,27 @@ class EncoderSubscriber implements EventSubscriberInterface
{ {
$value = $event->getValue(); $value = $event->getValue();
$encoder = $event->getEncoder(); $encoder = $event->getEncoder();
$ref = $encoder->encodeReference($value);
if ($value instanceof OmnivoreLayer) { if ($value instanceof OmnivoreLayer) {
$event->addLine( $event->addLine(
sprintf( sprintf(
'%s = ContaoLeaflet.loadLayer(%s, %s, %s, %s, map.map);', '%s = L.Contao.loadLayer(%s, %s, %s, %s, map);',
$encoder->encodeReference($value), $ref,
$encoder->encodeValue($value->getUrl()), $encoder->encodeValue($value->getUrl()),
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))), $encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
$encoder->encodeValue($value->getOptions()), $encoder->encodeValue($value->getOptions()),
$this->encodeCustomLayer($value, $encoder) $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()));
}
} }
} }