mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-01 04:24:02 +01:00
Only pass the leaflet hash param to the loadLayer function.
This commit is contained in:
@@ -25,7 +25,6 @@ L.Contao = new (L.Class.extend({
|
|||||||
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
|
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
|
||||||
|
|
||||||
this.setGeoJsonListeners(L.GeoJSON);
|
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.
|
* 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 type The response content format.
|
||||||
* @param options Parser options
|
* @param options Parser options
|
||||||
* @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(url, type, options, customLayer, map) {
|
loadLayer: function(hash, type, options, customLayer, map) {
|
||||||
|
var url = this.createRequestUrl(hash);
|
||||||
var layer = omnivore[type](url, options, customLayer);
|
var layer = omnivore[type](url, options, customLayer);
|
||||||
|
|
||||||
if (map) {
|
if (map) {
|
||||||
@@ -224,5 +224,39 @@ L.Contao = new (L.Class.extend({
|
|||||||
onEachFeature: this.onEachFeature.bind(this)
|
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('&');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}))();
|
}))();
|
||||||
|
|||||||
@@ -11,32 +11,44 @@
|
|||||||
|
|
||||||
namespace Netzmacht\Contao\Leaflet\Frontend;
|
namespace Netzmacht\Contao\Leaflet\Frontend;
|
||||||
|
|
||||||
use ContaoCommunityAlliance\UrlBuilder\UrlBuilder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class RequestUrl creates the request url.
|
* Class RequestUrl creates the request url.
|
||||||
*
|
*
|
||||||
* @package Netzmacht\Contao\Leaflet\Request
|
* @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;
|
private static $for;
|
||||||
|
|
||||||
public static function setFor($for)
|
/**
|
||||||
{
|
* The leaflet hash.
|
||||||
static::$for = $for;
|
*
|
||||||
}
|
* @var string
|
||||||
|
*/
|
||||||
|
private $hash;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The request url as url path.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $url;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the request url.
|
* Create the request url.
|
||||||
*
|
*
|
||||||
|
* It combines the params and creates an hash for it.
|
||||||
|
*
|
||||||
* @param int $dataId The data object id.
|
* @param int $dataId The data object id.
|
||||||
* @param string|null $type Object type. If empty it assumes a layer.
|
* @param string|null $type Object type. If empty it assumes a layer.
|
||||||
* @param string|null $format Data format. If empty it assumes geojson.
|
* @param string|null $format Data format. If empty it assumes geojson.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return RequestUrl
|
||||||
*/
|
*/
|
||||||
public static function create($dataId, $type = null, $format = null)
|
public static function create($dataId, $type = null, $format = null)
|
||||||
{
|
{
|
||||||
@@ -47,8 +59,69 @@ class RequestUrl
|
|||||||
'format' => $format != 'geojson' ? $format: null
|
'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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Netzmacht\Contao\Leaflet\Subscriber;
|
namespace Netzmacht\Contao\Leaflet\Subscriber;
|
||||||
|
|
||||||
|
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
||||||
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\Javascript\Event\GetReferenceEvent;
|
||||||
@@ -76,11 +77,17 @@ class EncoderSubscriber implements EventSubscriberInterface
|
|||||||
$ref = $encoder->encodeReference($value);
|
$ref = $encoder->encodeReference($value);
|
||||||
|
|
||||||
if ($value instanceof OmnivoreLayer) {
|
if ($value instanceof OmnivoreLayer) {
|
||||||
|
$url = $value->getUrl();
|
||||||
|
|
||||||
|
if ($url instanceof RequestUrl) {
|
||||||
|
$url = $url->getHash();
|
||||||
|
}
|
||||||
|
|
||||||
$event->addLine(
|
$event->addLine(
|
||||||
sprintf(
|
sprintf(
|
||||||
'%s = L.Contao.loadLayer(%s, %s, %s, %s, map);',
|
'%s = L.Contao.loadLayer(%s, %s, %s, %s, map);',
|
||||||
$ref,
|
$ref,
|
||||||
$encoder->encodeValue($value->getUrl()),
|
$encoder->encodeValue($url),
|
||||||
$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)
|
||||||
|
|||||||
Reference in New Issue
Block a user