forked from Snck3rs/contao-leaflet-maps
Improve handling of omnivore data loading.
This commit is contained in:
@@ -113,7 +113,7 @@ L.contao = new (L.Class.extend({
|
|||||||
* @param customLayer optional custom layer.
|
* @param customLayer optional custom layer.
|
||||||
* @param map Pass a map object so that the data loading events are passed to the map.
|
* @param map Pass a map object so that the data loading events are passed to the map.
|
||||||
*/
|
*/
|
||||||
loadLayer: function(hash, type, options, customLayer, map) {
|
load: function(hash, type, options, customLayer, map) {
|
||||||
var url = this.createRequestUrl(hash);
|
var url = this.createRequestUrl(hash);
|
||||||
var layer = omnivore[type](url, options, customLayer);
|
var layer = omnivore[type](url, options, customLayer);
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ class EncoderSubscriber implements EventSubscriberInterface
|
|||||||
{
|
{
|
||||||
$value = $event->getValue();
|
$value = $event->getValue();
|
||||||
$encoder = $event->getEncoder();
|
$encoder = $event->getEncoder();
|
||||||
$ref = $encoder->encodeReference($value);
|
$template = 'L.contao.load(%s, %s, %s, %s, map);';
|
||||||
|
|
||||||
if ($value instanceof OmnivoreLayer) {
|
if ($value instanceof OmnivoreLayer) {
|
||||||
$url = $value->getUrl();
|
$url = $value->getUrl();
|
||||||
@@ -97,18 +97,24 @@ class EncoderSubscriber implements EventSubscriberInterface
|
|||||||
if ($url instanceof RequestUrl) {
|
if ($url instanceof RequestUrl) {
|
||||||
$url = $url->getHash();
|
$url = $url->getHash();
|
||||||
} elseif (strpos($url, '/') !== false) {
|
} elseif (strpos($url, '/') !== false) {
|
||||||
// Slash found, not contao leaflet hash, do not replace encoding.
|
// Slash found, not a Contao leaflet hash, do not replace encoding.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($value->getCustomLayer()) {
|
||||||
|
$ref = $encoder->encodeReference($value->getCustomLayer());
|
||||||
|
} else {
|
||||||
|
$template = $encoder->encodeReference($value) . ' = ' . $template;
|
||||||
|
$ref = 'null';
|
||||||
|
}
|
||||||
|
|
||||||
$event->addLine(
|
$event->addLine(
|
||||||
sprintf(
|
sprintf(
|
||||||
'%s = L.contao.loadLayer(%s, %s, %s, %s, map);',
|
$template,
|
||||||
$ref,
|
|
||||||
$encoder->encodeValue($url),
|
$encoder->encodeValue($url),
|
||||||
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
|
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
|
||||||
$encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT),
|
$encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT),
|
||||||
$this->encodeCustomLayer($value, $encoder)
|
$ref
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -121,28 +127,4 @@ class EncoderSubscriber implements EventSubscriberInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a custom layer for the omnivore plugin.
|
|
||||||
*
|
|
||||||
* @param OmnivoreLayer $layer The layer.
|
|
||||||
* @param Encoder $encoder The javascript encoder.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function encodeCustomLayer(OmnivoreLayer $layer, Encoder $encoder)
|
|
||||||
{
|
|
||||||
$customLayer = $layer->getCustomLayer();
|
|
||||||
|
|
||||||
if ($customLayer instanceof GeoJson && !$customLayer->getMethodCalls()) {
|
|
||||||
return sprintf(
|
|
||||||
'L.geoJson(null, %s)',
|
|
||||||
$encoder->encodeValue($customLayer->getOptions())
|
|
||||||
);
|
|
||||||
} elseif ($customLayer) {
|
|
||||||
return $encoder->encodeReference($customLayer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 'null';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user