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,
/**
@@ -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();
}))();

View File

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

View File

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

View File

@@ -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');

View File

@@ -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()));
}
}
}