Skip to content

Commit

Permalink
Clickable static map with callable [close #9]
Browse files Browse the repository at this point in the history
  • Loading branch information
Olicek committed Feb 5, 2016
1 parent 37e53ba commit 1314627
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
12 changes: 12 additions & 0 deletions docs/en/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
`<a href="https://maps.google.com/maps/place/50.250718,14.583435/">`. 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.
Expand Down
33 changes: 27 additions & 6 deletions src/MapAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -57,7 +58,7 @@ class MapAPI extends Control
private $staticMap = FALSE;

/**
* @var bool
* @var bool|string|callable
*/
private $clickable = FALSE;

Expand Down Expand Up @@ -337,7 +338,7 @@ public function getIsStaticMap()


/**
* @param bool $clickable
* @param bool|callable $clickable
* @return $this
* @throws InvalidArgumentException
*/
Expand All @@ -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 = '<a href="https://maps.google.com/maps/place/' .
$this->coordinates[0] . ',' . $this->coordinates[1] . '/">';
}

$this->clickable = $clickable;
return $this;
}

Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/static.latte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{if $clickable}<a href="https://maps.google.com/maps/place/{$position[0]},{$position[1]}/">{/if}
{if !is_null($clickable)}{$clickable|noescape}{/if}
<img src="https://maps.google.com/maps/api/staticmap?center={$position[0]},{$position[1]}&zoom={$zoom}&size={(int) $width}x{(int) $height}&maptype=roadmap&language=
{if $markers}{foreach $markers as $marker}&markers=color:{if array_key_exists('color', $marker)}{$marker['color']}{else}red{/if}|label:true|{$marker['position'][0]},{$marker['position'][1]}{/foreach}{/if}" title="" width="{$width}" height="{$height}">
{if $clickable}</a>{/if}
{if !is_null($clickable)}</a>{/if}
18 changes: 14 additions & 4 deletions tests/GoogleAPI/MapAPITest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Oli\GoogleAPI;


use Nette\Utils\Html;
use Tester\TestCase;
use Tester\Assert;

Expand Down Expand Up @@ -136,22 +137,31 @@ 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());


$this->map->isClickable(FALSE);
Assert::false($this->map->getIsClicable());

$this->map->isClickable();
Assert::true($this->map->getIsClicable());
Assert::same('<a href="https://maps.google.com/maps/place/50.250718,14.583435/">',
$this->map->getIsClicable());

$this->map->isClickable(TRUE);
Assert::true($this->map->getIsClicable());
Assert::same('<a href="https://maps.google.com/maps/place/50.250718,14.583435/">',
$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');
}


Expand Down

0 comments on commit 1314627

Please sign in to comment.