Only pass the leaflet hash param to the loadLayer function.

This commit is contained in:
David Molineus
2015-01-15 12:45:23 +01:00
parent ae9d70c6c5
commit 52186055d9
3 changed files with 130 additions and 16 deletions

View File

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

View File

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