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

@@ -1,10 +1,31 @@
L.Contao = L.Class.extend( { L.Contao = L.Class.extend( {
includes: L.Mixin.Events, includes: L.Mixin.Events,
/**
* The map registry.
*/
maps: {}, maps: {},
/**
* The icons registry.
*/
icons: {}, icons: {},
/**
* Initialize Contao leaflet integration.
*/
initialize: function() {
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
},
/**
* Add map to map registry.
*
* @param id The map id.
* @param map The map object.
*
* @returns {L.Contao}
*/
addMap: function (id, map) { addMap: function (id, map) {
this.maps[id] = map; this.maps[id] = map;
@@ -13,6 +34,13 @@ L.Contao = L.Class.extend( {
return this; return this;
}, },
/**
* Get a map from the icon map. Returns null if not set.
*
* @param id The mapobject
*
* @returns {L.Map}|{*}
*/
getMap: function (id) { getMap: function (id) {
if (typeof (this.maps[id]) === 'undefined') { if (typeof (this.maps[id]) === 'undefined') {
return null; return null;
@@ -21,6 +49,14 @@ L.Contao = L.Class.extend( {
return this.maps[id]; return this.maps[id];
}, },
/**
* Add an icon to the icon registry.
*
* @param id The icon id.
* @param icon The icon object.
*
* @returns {L.Contao}
*/
addIcon: function(id, icon) { addIcon: function(id, icon) {
this.icons[id] = icon; this.icons[id] = icon;
this.fire('icon:added', { id: id, icon: icon}); this.fire('icon:added', { id: id, icon: icon});
@@ -28,6 +64,27 @@ L.Contao = L.Class.extend( {
return this; return this;
}, },
/**
* Load icon definitions.
*
* @param icons List of icon definitions.
*
* @return void
*/
loadIcons: function(icons) {
for (var i = 0; i < icons.length; i++) {
var icon = L[icons[i].type](icons[i].options);
this.addIcon(icons[i].id, icon);
}
},
/**
* Get an icon by its id.
*
* @param id Icon id.
*
* @returns {L.Icon}|{L.DivIcon}|{*}
*/
getIcon: function(id) { getIcon: function(id) {
if (typeof (this.icons[id]) === 'undefined') { if (typeof (this.icons[id]) === 'undefined') {
return null; return null;
@@ -36,6 +93,14 @@ L.Contao = L.Class.extend( {
return this.icons[id]; return this.icons[id];
}, },
/**
* Point to layer callback. Adds a geo json point to the layer.
*
* @param feature The geo json feature.
* @param latlng The converted latlng.
*
* @returns {L.Marker}|{*}
*/
pointToLayer: function(feature, latlng) { pointToLayer: function(feature, latlng) {
var marker = L.marker(latlng, feature.properties.options); var marker = L.marker(latlng, feature.properties.options);
@@ -53,6 +118,12 @@ L.Contao = L.Class.extend( {
return marker; return marker;
}, },
/**
* Apply feature methods.
*
* @param obj
* @param feature
*/
applyFeatureMethods: function(obj, feature) { applyFeatureMethods: function(obj, feature) {
if (feature.properties && feature.properties.methods) { if (feature.properties && feature.properties.methods) {
for (var i=0; i < feature.properties.methods.length; i++) { for (var i=0; i < feature.properties.methods.length; i++) {
@@ -64,6 +135,4 @@ L.Contao = L.Class.extend( {
} }
}); });
L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images';
window.ContaoLeaflet = new L.Contao(); window.ContaoLeaflet = new L.Contao();

View File

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

View File

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

View File

@@ -131,31 +131,34 @@ class BootSubscriber implements EventSubscriberInterface
if ($collection) { if ($collection) {
/** @var DefinitionMapper $mapper */ /** @var DefinitionMapper $mapper */
$buffer = ''; $mapper = $GLOBALS['container']['leaflet.definition.mapper'];
$mapper = $GLOBALS['container']['leaflet.definition.mapper']; $buffer = '';
$icons = array();
/** @var Leaflet $builder */ /** @var Leaflet $builder */
$builder = $GLOBALS['container']['leaflet.definition.builder']; $builder = $GLOBALS['container']['leaflet.definition.builder'];
$encoder = $builder->getBuilder()->getEncoder(); $encoder = $builder->getBuilder()->getEncoder();
foreach ($collection as $model) { foreach ($collection as $model) {
/** @var Icon $icon */ /** @var Icon $icon */
$icon = $mapper->handle($model); $icon = $mapper->handle($model);
$icons[] = array(
$buffer .= sprintf( 'id' => $icon->getId(),
'ContaoLeaflet.addIcon(\'%s\', L.icon(%s));' . "\n", 'type' => lcfirst($icon->getType()),
$model->alias ?: ('icon_' . $model->id), 'options' => $icon->getOptions(),
$encoder->encodeValue($icon->getOptions())
); );
} }
if ($buffer) { if ($icons) {
$file = new \File('assets/leaflet/js/icons.js'); $buffer = sprintf('ContaoLeaflet.loadIcons(%s);', $encoder->encodeValue($icons));
$file->write($buffer);
$file->close();
// TODO: Cache it.
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js|static';
} }
$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';
} }
} }
} }