Add icon support.

This commit is contained in:
David Molineus
2015-01-06 21:30:57 +01:00
parent 3608f7cd48
commit adc8de55ae
11 changed files with 367 additions and 136 deletions

View File

@@ -12,6 +12,9 @@
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\IconModel;
class Marker
{
public function generateRow($row)
@@ -19,4 +22,17 @@ class Marker
return $row['title'];
}
public function getIcons()
{
$collection = IconModel::findAll(array('order' => 'title'));
$builder = OptionsBuilder::fromCollection(
$collection, 'id',
function($model) {
return sprintf('%s [%s]', $model['title'], $model['type']);
}
);
return $builder->getOptions();
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Type;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
class AbstractIconMapper extends AbstractTypeMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\IconModel';
}

View File

@@ -0,0 +1,132 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Type;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
class ImageIconMapper extends AbstractIconMapper
{
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Type\Icon';
/**
* Layer type.
*
* @var string
*/
protected static $type = 'image';
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
if ($model->iconImage) {
$file = \FilesModel::findByUuid($model->iconImage);
if ($file) {
$arguments[] = $file->path;
}
}
return $arguments;
}
protected function doBuild(
Definition $definition,
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null
) {
if ($definition instanceof Icon) {
$this->addIcon($definition, $model);
$this->addShadow($definition, $model);
}
}
/**
* @param Icon $definition
* @param IconModel $model
*/
private function addIcon(Icon $definition, IconModel $model)
{
if ($model->iconImage) {
$file = \FilesModel::findByUuid($model->iconImage);
if ($file) {
$definition->setIconUrl($file->path);
$file = new \File($file->path);
$definition->setIconSize(array($file->width, $file->height));
if (!$model->iconAnchor) {
$definition->setIconAnchor(array($file->width / 2, $file->height));
}
if (!$model->popupAnchor) {
$definition->setPopupAnchor(array(0, 10 - $file->height));
}
}
}
if ($model->iconAnchor) {
$definition->setIconAnchor(array_map('intval', explode(',', $model->iconAnchor)));
}
if ($model->iconRetinaImage) {
$file = \FilesModel::findByUuid($model->iconRetinaImage);
if ($file) {
$definition->setIconRetinaUrl($file->path);
}
}
}
private function addShadow(Icon $definition, $model)
{
if ($model->shadowImage) {
$file = \FilesModel::findByUuid($model->shadowImage);
if ($file) {
$definition->setShadowUrl($file->path);
$file = new \File($file->path);
$definition->setShadowSize(array($file->width, $file->height));
if (!$model->shadowAnchor) {
$definition->setShadowAnchor(array($file->width / 2, $file->height));
}
}
}
if ($model->shadowAnchor) {
$definition->setShadowAnchor(array_map('intval', explode(',', $model->shadowAnchor)));
}
if ($model->shadowRetinaImage) {
$file = \FilesModel::findByUuid($model->shadowRetinaImage);
if ($file) {
$definition->setShadowRetinaUrl($file->path);
}
}
}
}

View File

@@ -14,7 +14,9 @@ namespace Netzmacht\Contao\Leaflet\Mapper\UI;
use Netzmacht\Contao\Leaflet\Mapper\AbstractMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Definition\UI\Marker;
@@ -69,6 +71,20 @@ class MarkerMapper extends AbstractMapper
if ($model->addPopup) {
$definition->bindPopup($model->popupContent);
}
if ($model->customIcon) {
$iconModel = IconModel::findBy(
array('id=?', 'active=1'),
array($model->icon),
array('return' => 'Model')
);
if ($iconModel) {
/** @var Icon $icon */
$icon = $builder->handle($iconModel);
$definition->setIcon($icon);
}
}
}
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Model;
/**
* @property mixed|null iconImage
* @property mixed|null iconAnchor
* @property mixed|null popupAnchor
* @property mixed|null iconRetinaImage
*/
class IconModel extends \Model
{
protected static $strTable = 'tl_leaflet_icon';
}

View File

@@ -16,6 +16,11 @@ use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Javascript\Output;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Leaflet;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@@ -34,7 +39,7 @@ class BootSubscriber implements EventSubscriberInterface
InitializeDefinitionMapperEvent::NAME => 'initializeDefinitionMapper',
InitializeEventDispatcherEvent::NAME => 'initializeEventDispatcher',
InitializeLeafletBuilderEvent::NAME => 'initializeLeafletBuilder',
GetJavascriptEvent::NAME => 'loadAssets'
GetJavascriptEvent::NAME => array(array('loadAssets'), array('loadIcons')),
);
}
@@ -114,4 +119,43 @@ class BootSubscriber implements EventSubscriberInterface
{
$GLOBALS['TL_JAVASCRIPT'][] = 'system/modules/leaflet/assets/js/contao-leaflet.js|static';
}
/**
* Load icons.
*
* @throws \Netzmacht\Javascript\Exception\EncodeValueFailed
*/
public function loadIcons()
{
$collection = IconModel::findBy('active', true);
if ($collection) {
/** @var DefinitionMapper $mapper */
$buffer = '';
$mapper = $GLOBALS['container']['leaflet.definition.mapper'];
/** @var Leaflet $builder */
$builder = $GLOBALS['container']['leaflet.definition.builder'];
$encoder = $builder->getBuilder()->getEncoder();
foreach ($collection as $model) {
/** @var Icon $icon */
$icon = $mapper->handle($model);
$buffer .= sprintf(
'ContaoLeaflet.addIcon(\'%s\', L.icon(%s));' . "\n",
$model->alias ?: ('icon_' . $model->id),
$encoder->encodeValue($icon->getOptions())
);
}
if ($buffer) {
$file = new \File('assets/leaflet/js/icons.js');
$file->write($buffer);
$file->close();
// TODO: Cache it.
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js|static';
}
}
}
}