Improve icon handling.

This commit is contained in:
David Molineus
2015-01-07 09:32:14 +01:00
parent 6d453ffab9
commit df7b8d3c94
4 changed files with 121 additions and 21 deletions

View File

@@ -34,6 +34,17 @@ class ImageIconMapper extends AbstractIconMapper
*/
protected static $type = 'image';
/**
* {@inheritdoc}
*/
protected function initialize()
{
$this->addConditionalOption('className');
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
@@ -49,7 +60,9 @@ class ImageIconMapper extends AbstractIconMapper
return $arguments;
}
/**
* {@inheritdoc}
*/
protected function doBuild(
Definition $definition,
\Model $model,
@@ -63,8 +76,12 @@ class ImageIconMapper extends AbstractIconMapper
}
/**
* @param Icon $definition
* @param IconModel $model
* Add icon image.
*
* @param Icon $definition The icon definition.
* @param IconModel $model The model.
*
* @return void
*/
private function addIcon(Icon $definition, IconModel $model)
{
@@ -82,7 +99,7 @@ class ImageIconMapper extends AbstractIconMapper
}
if (!$model->popupAnchor) {
$definition->setPopupAnchor(array(0, 10 - $file->height));
$definition->setPopupAnchor(array(0, 8 - $file->height));
}
}
}
@@ -100,6 +117,14 @@ class ImageIconMapper extends AbstractIconMapper
}
}
/**
* Add shadow if defined.
*
* @param Icon $definition The icon definition.
* @param IconModel $model The model.
*
* @return void
*/
private function addShadow(Icon $definition, $model)
{
if ($model->shadowImage) {

View File

@@ -17,6 +17,9 @@ namespace Netzmacht\Contao\Leaflet\Model;
* @property mixed|null iconAnchor
* @property mixed|null popupAnchor
* @property mixed|null iconRetinaImage
* @property mixed|null shadowAnchor
* @property mixed|null shadowRetinaImage
* @property mixed|null shadowImage
*/
class IconModel extends \Model
{

View File

@@ -131,31 +131,34 @@ class BootSubscriber implements EventSubscriberInterface
if ($collection) {
/** @var DefinitionMapper $mapper */
$buffer = '';
$mapper = $GLOBALS['container']['leaflet.definition.mapper'];
$mapper = $GLOBALS['container']['leaflet.definition.mapper'];
$buffer = '';
$icons = array();
/** @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())
$icon = $mapper->handle($model);
$icons[] = array(
'id' => $icon->getId(),
'type' => lcfirst($icon->getType()),
'options' => $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';
if ($icons) {
$buffer = sprintf('ContaoLeaflet.loadIcons(%s);', $encoder->encodeValue($icons));
}
$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';
}
}
}