diff --git a/assets/maps/contao-leaflet.js b/assets/maps/contao-leaflet.js index b8bd7c4..d7148b7 100644 --- a/assets/maps/contao-leaflet.js +++ b/assets/maps/contao-leaflet.js @@ -25,7 +25,6 @@ L.Contao = new (L.Class.extend({ L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images'; this.setGeoJsonListeners(L.GeoJSON); - this.setGeoJsonListeners(L.GeoJSON.AJAX); }, /** @@ -108,13 +107,14 @@ L.Contao = new (L.Class.extend({ /** * Layer a url into a layer using omnivore. * - * @param url The url being loaded. + * @param hash The leaflet url hash. * @param type The response content format. * @param options Parser options * @param customLayer optional custom layer. * @param map Pass a map object so that the data loading events are passed to the map. */ - loadLayer: function(url, type, options, customLayer, map) { + loadLayer: function(hash, type, options, customLayer, map) { + var url = this.createRequestUrl(hash); var layer = omnivore[type](url, options, customLayer); if (map) { @@ -224,5 +224,39 @@ L.Contao = new (L.Class.extend({ onEachFeature: this.onEachFeature.bind(this) }; } + }, + + /** + * Create request url by appending the hash to the current url. + * + * @param {string} value The hash + * + * @returns {string} + */ + createRequestUrl: function(value) { + value = encodeURIComponent(value); + + var key = 'leaflet'; + var params = document.location.search.substr(1).split('&'); + + if (params == '') { + return document.location.pathname + '?' + [key, value].join('='); + } else { + var i = params.length; var x; while (i--) { + x = params[i].split('='); + + if (x[0] == key) { + x[1] = value; + params[i] = x.join('='); + break; + } + } + + if (i < 0) { + params[params.length] = [key, value].join('='); + } + + return document.location.pathname + params.join('&'); + } } }))(); diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php b/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php index b8ae470..bda697a 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php @@ -11,32 +11,44 @@ namespace Netzmacht\Contao\Leaflet\Frontend; -use ContaoCommunityAlliance\UrlBuilder\UrlBuilder; - /** * Class RequestUrl creates the request url. * * @package Netzmacht\Contao\Leaflet\Request */ -class RequestUrl +class RequestUrl implements \JsonSerializable { - const BASE = 'assets/leaflet/maps/data.php'; - + /** + * The for param is the identifier to the responsible frontend module or content element. + * + * @var string + */ private static $for; - public static function setFor($for) - { - static::$for = $for; - } + /** + * The leaflet hash. + * + * @var string + */ + private $hash; + + /** + * The request url as url path. + * + * @var string + */ + private $url; /** * Create the request url. * + * It combines the params and creates an hash for it. + * * @param int $dataId The data object id. * @param string|null $type Object type. If empty it assumes a layer. * @param string|null $format Data format. If empty it assumes geojson. * - * @return string + * @return RequestUrl */ public static function create($dataId, $type = null, $format = null) { @@ -47,8 +59,69 @@ class RequestUrl 'format' => $format != 'geojson' ? $format: null ); - $param = base64_encode(implode(',', $params)); + $hash = base64_encode(implode(',', $params)); + $url = \Config::get('websitePath') . '/' . \Frontend::addToUrl('leaflet=' . $hash, false); - return \Config::get('websitePath') . '/' . \Frontend::addToUrl('leaflet=' . $param, false); + return new static($url, $hash); + } + + /** + * Set the for param. + * + * @param $for + */ + public static function setFor($for) + { + static::$for = $for; + } + + /** + * Construct. + * + * @param string $url The request url. + * @param string $hash The leaflet hash. + */ + public function __construct($url, $hash) + { + $this->url = $url; + $this->hash = $hash; + } + + /** + * Get the leaflet url hash. + * + * @return string + */ + public function getHash() + { + return $this->hash; + } + + /** + * Get the whole url. + * + * @return string + */ + public function getUrl() + { + return ; + } + + /** + * Convert to string will always return the whole url. + * + * @return string + */ + public function __toString() + { + return $this->getUrl(); + } + + /** + * {@inheritdoc} + */ + function jsonSerialize() + { + return $this->getUrl(); } } diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php index 3976bd9..f0ca75a 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/EncoderSubscriber.php @@ -11,6 +11,7 @@ namespace Netzmacht\Contao\Leaflet\Subscriber; +use Netzmacht\Contao\Leaflet\Frontend\RequestUrl; use Netzmacht\Javascript\Encoder; use Netzmacht\Javascript\Event\EncodeValueEvent; use Netzmacht\Javascript\Event\GetReferenceEvent; @@ -76,11 +77,17 @@ class EncoderSubscriber implements EventSubscriberInterface $ref = $encoder->encodeReference($value); if ($value instanceof OmnivoreLayer) { + $url = $value->getUrl(); + + if ($url instanceof RequestUrl) { + $url = $url->getHash(); + } + $event->addLine( sprintf( '%s = L.Contao.loadLayer(%s, %s, %s, %s, map);', $ref, - $encoder->encodeValue($value->getUrl()), + $encoder->encodeValue($url), $encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))), $encoder->encodeValue($value->getOptions()), $this->encodeCustomLayer($value, $encoder)