From 1314627573c557ead90f4a56480b933a4528dbb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Oli=C5=A1ar?= Date: Fri, 5 Feb 2016 21:28:06 +0100 Subject: [PATCH] Clickable static map with callable [close #9] --- docs/en/map.md | 12 ++++++++++++ src/MapAPI.php | 33 +++++++++++++++++++++++++++------ src/static.latte | 4 ++-- tests/GoogleAPI/MapAPITest.phpt | 18 ++++++++++++++---- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/docs/en/map.md b/docs/en/map.md index bc7cd97..412839d 100644 --- a/docs/en/map.md +++ b/docs/en/map.md @@ -49,6 +49,18 @@ This method creates map as a picture. When the map is static, only `height`, `wi If scrollable is `TRUE`, when mouse is over the map and a user is scrolling, it has no effect. +### isClickable($clickable = TRUE) + +Clickable can be boolean or callable. If clickable is boolean it generates link like this: +``. If clickable is callable +it must return \Nette\Utils\Html. Callable accept 2 parameters. `$url` and `$coordinates`. + +``` +isClickable(function ($url, $coordinates) { + return Nette\Utils\Html::el('a', ['class' => 'foo'])->href($url); +}) +``` + ### addMarkers(Markers $markers) This method must be called, if you want to put the markers to the map. It just transmit markers from `$markers` to the map. diff --git a/src/MapAPI.php b/src/MapAPI.php index f6cd7c4..e633487 100644 --- a/src/MapAPI.php +++ b/src/MapAPI.php @@ -11,6 +11,7 @@ use Nette\Application\UI\Control; use Nette\Application\Responses\JsonResponse; use Nette\Application\UI\ITemplate; +use Nette\Utils\Html; use Nette\Utils\Json; @@ -57,7 +58,7 @@ class MapAPI extends Control private $staticMap = FALSE; /** - * @var bool + * @var bool|string|callable */ private $clickable = FALSE; @@ -337,7 +338,7 @@ public function getIsStaticMap() /** - * @param bool $clickable + * @param bool|callable $clickable * @return $this * @throws InvalidArgumentException */ @@ -348,12 +349,22 @@ public function isClickable($clickable = TRUE) throw new InvalidArgumentException("the 'clickable' option only applies to static map"); } - if (!is_bool($clickable)) + if (!is_bool($clickable) && !is_callable($clickable)) { - throw new InvalidArgumentException("clickable must be boolean, $clickable (".gettype($clickable).") was given"); + throw new InvalidArgumentException( + "clickable must be boolean or callable, $clickable (".gettype($clickable).") was given" + ); + } + + if (is_callable($clickable)) { + $this->clickable = $clickable; + + } else if ($clickable !== FALSE) + { + $this->clickable = ''; } - $this->clickable = $clickable; return $this; } @@ -423,12 +434,22 @@ public function render() { if ($this->staticMap) { + if (is_callable($this->clickable)) + { + $this->clickable = call_user_func_array($this->clickable, [ + 'https://maps.google.com/maps/place/' . $this->coordinates[0] . ',' . + $this->coordinates[1] . '/', + $this->getCoordinates() + ]); + } + $this->template->height = $this->height; $this->template->width = $this->width; $this->template->zoom = $this->zoom; $this->template->position = $this->coordinates; $this->template->markers = $this->markers; - $this->template->clickable = $this->clickable; + $this->template->clickable = $this->clickable instanceof Html ? $this->clickable->startTag() : + $this->clickable; $this->template->setFile(dirname(__FILE__) . '/static.latte'); } else { diff --git a/src/static.latte b/src/static.latte index 6c8eb83..4879522 100644 --- a/src/static.latte +++ b/src/static.latte @@ -1,4 +1,4 @@ -{if $clickable}{/if} +{if !is_null($clickable)}{$clickable|noescape}{/if} -{if $clickable}{/if} +{if !is_null($clickable)}{/if} diff --git a/tests/GoogleAPI/MapAPITest.phpt b/tests/GoogleAPI/MapAPITest.phpt index 149f8e8..7bbc521 100644 --- a/tests/GoogleAPI/MapAPITest.phpt +++ b/tests/GoogleAPI/MapAPITest.phpt @@ -9,6 +9,7 @@ namespace Oli\GoogleAPI; +use Nette\Utils\Html; use Tester\TestCase; use Tester\Assert; @@ -136,7 +137,7 @@ class MapAPITest extends TestCase $this->map->isStaticMap(FALSE)->isClickable(); }, InvalidArgumentException::class, "the 'clickable' option only applies to static map"); - $this->map->isStaticMap(); + $this->map->setCoordinates(array(50.250718,14.583435))->isStaticMap(); Assert::false($this->map->getIsClicable()); @@ -144,14 +145,23 @@ class MapAPITest extends TestCase Assert::false($this->map->getIsClicable()); $this->map->isClickable(); - Assert::true($this->map->getIsClicable()); + Assert::same('', + $this->map->getIsClicable()); $this->map->isClickable(TRUE); - Assert::true($this->map->getIsClicable()); + Assert::same('', + $this->map->getIsClicable()); + + + $function = function ($url) { + return Html::el('a', ['class' => 'btn btn-default'])->href($url); + }; + $this->map->isClickable($function); + Assert::same($function, $this->map->getIsClicable()); Assert::exception(function () { $this->map->isClickable('foo'); - }, InvalidArgumentException::class, 'clickable must be boolean, foo (string) was given'); + }, InvalidArgumentException::class, 'clickable must be boolean or callable, foo (string) was given'); }