Add vector style management.

This commit is contained in:
David Molineus
2015-01-09 11:53:58 +01:00
parent 9c689b072d
commit 37851110b0
23 changed files with 955 additions and 218 deletions

View File

@@ -12,10 +12,25 @@
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\StyleModel;
class Vector
{
public function generateRow($row)
{
return sprintf('%s <span class="tl_gray">[%s]</span>', $row['title'], $row['type']);
}
/**
* Get all styles.
*
* @return array
*/
public function getStyles()
{
$collection = StyleModel::findAll(array('order' => 'title'));
return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions();
}
}

View File

@@ -0,0 +1,32 @@
<?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\Definition;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Vector\Path;
/**
* Interface Style describes a style definition.
*
* @package Netzmacht\Contao\Leaflet\Definition
*/
interface Style extends Definition
{
/**
* Apply style to a given vector.
*
* @param Path $vector The vector path.
*
* @return $this
*/
public function apply(Path $vector);
}

View File

@@ -0,0 +1,287 @@
<?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\Definition\Style;
use Netzmacht\Contao\Leaflet\Definition\Style;
use Netzmacht\LeafletPHP\Definition\AbstractDefinition;
use Netzmacht\LeafletPHP\Definition\OptionsTrait;
use Netzmacht\LeafletPHP\Definition\Vector\Path;
class FixedStyle extends AbstractDefinition implements Style
{
use OptionsTrait;
/**
* {@inheritdoc}
*/
public static function getType()
{
return 'Style';
}
/**
* Set the stroke value.
*
* @param bool $value If true a stroke is drwan.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-stroke
*/
public function setStroke($value)
{
return $this->setOption('stroke', (bool) $value);
}
/**
* Check if stroke is enabled.
*
* @return bool
* @see http://leafletjs.com/reference.html#path-stroke
*/
public function hasStroke()
{
return $this->getOption('stroke', true);
}
/**
* Set the stroke color.
*
* @param string $value Stroke color.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-color
*/
public function setColor($value)
{
return $this->setOption('color', $value);
}
/**
* Get the stroke color.
*
* @return string
* @see http://leafletjs.com/reference.html#path-color
*/
public function getColor()
{
return $this->getOption('color', '#03f');
}
/**
* Set the stroke weight.
*
* @param int $value Stroke weight.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-weight
*/
public function setWeight($value)
{
return $this->setOption('weight', (int) $value);
}
/**
* Get the stroke weight.
*
* @return int
* @see http://leafletjs.com/reference.html#path-weight
*/
public function getWeight()
{
return $this->getOption('weight', 5);
}
/**
* Set the opacity.
*
* @param float $value Path opacity.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-opacity
*/
public function setOpacity($value)
{
return $this->setOption('opacity', (float) $value);
}
/**
* Get opacity.
*
* @return float
* @see http://leafletjs.com/reference.html#path-opacity
*/
public function getOpacity()
{
return $this->getOption('opacity', 0.5);
}
/**
* Fill the path.
*
* @param bool|string $value If true a fill is drwan.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-fill
*/
public function setFill($value)
{
return $this->setOption('fill', (bool) $value);
}
/**
* Check if fill is enabled.
*
* @return bool|string
* @see http://leafletjs.com/reference.html#path-fill
*/
public function isFill()
{
return $this->getOption('fill', 'depends');
}
/**
* Set the stroke fill color.
*
* @param string $value Stroke fill color.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-fillcolor
*/
public function setFillColor($value)
{
return $this->setOption('fillColor', $value);
}
/**
* Get the stroke fill color.
*
* @return string
* @see http://leafletjs.com/reference.html#path-fillcolor
*/
public function getFillColor()
{
return $this->getOption('fillColor', '#03f');
}
/**
* Set the fill opacity.
*
* @param float $value Fill opacity.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-fillopacity
*/
public function setFillOpacity($value)
{
return $this->setOption('fillOpacity', (float) $value);
}
/**
* Get fill opacity.
*
* @return float
* @see http://leafletjs.com/reference.html#path-fillopacity
*/
public function getFillOpacity()
{
return $this->getOption('fillOpacity', 0.2);
}
/**
* Set the stroke dash pattern.
*
* @param string $value The dash pattern.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-dasharray
* @see https://developer.mozilla.org/en/SVG/Attribute/stroke-dasharray
*/
public function setDashArray($value)
{
return $this->setOption('dashArray', $value);
}
/**
* Get stroke dash pattern.
*
* @return float
* @see http://leafletjs.com/reference.html#path-dasharray
* @see https://developer.mozilla.org/en/SVG/Attribute/stroke-dasharray
*/
public function getDashArray()
{
return $this->getOption('dashArray', null);
}
/**
* Set line cap string.
*
* @param string $value The line cap.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-linecap
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap
*/
public function setLineCap($value)
{
return $this->setOption('lineCap', $value);
}
/**
* Get the line cap string.
*
* @return float
* @see http://leafletjs.com/reference.html#path-linecap
* @see https://developer.mozilla.org/en/SVG/Attribute/stroke-linecap
*/
public function getLineCap()
{
return $this->getOption('lineCap', null);
}
/**
* Set line cap string.
*
* @param string $value The line cap.
*
* @return $this
* @see http://leafletjs.com/reference.html#path-linejoin
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linejoin
*/
public function setLineJoin($value)
{
return $this->setOption('lineJoin', $value);
}
/**
* Get the line cap string.
*
* @return float
* @see http://leafletjs.com/reference.html#path-linejoin
* @see https://developer.mozilla.org/en/SVG/Attribute/stroke-linejoin
*/
public function getLineJoin()
{
return $this->getOption('lineJoin', null);
}
/**
* {@inheritdoc}
*/
public function apply(Path $vector)
{
$vector->setOptions($this->getOptions());
return $this;
}
}

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\Mapper\Style;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
abstract class AbstractStyleMapper extends AbstractTypeMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\StyleModel';
}

View File

@@ -0,0 +1,48 @@
<?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\Style;
use Netzmacht\LeafletPHP\Definition;
class FixedStyleMapper extends AbstractStyleMapper
{
/**
* Definition class.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\Contao\Leaflet\Definition\Style\FixedStyle';
/**
* Style type.
*
* @var string
*/
protected static $type = 'fixed';
/**
* {@inheritdoc}
*/
protected function initialize()
{
parent::initialize();
$this
->addOptions('stroke', 'weight', 'opacity', 'clickable', 'className')
->addConditionalOption('color')
->addConditionalOption('lineCap')
->addConditionalOption('lineJoin')
->addConditionalOption('dashArray')
->addConditionalOptions('fill', array('fillColor', 'fillOpacity'))
->addOption('fill');
}
}

View File

@@ -12,10 +12,14 @@
namespace Netzmacht\Contao\Leaflet\Mapper\Vector;
use Netzmacht\Contao\Leaflet\Definition\Style;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\StyleModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\HasPopup;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Definition\Vector\Path;
class AbstractVectorMapper extends AbstractTypeMapper
{
@@ -30,25 +34,37 @@ class AbstractVectorMapper extends AbstractTypeMapper
{
parent::initialize();
$this
->addOptions('stroke', 'weight', 'opacity', 'clickable', 'className')
->addConditionalOption('color')
->addConditionalOption('lineCap')
->addConditionalOption('lineJoin')
->addConditionalOption('dashArray')
->addConditionalOptions('fill', array('fill', 'fillColor', 'fillOpacity'))
;
// $this
// ->addOptions('stroke', 'weight', 'opacity', 'clickable', 'className')
// ->addConditionalOption('color')
// ->addConditionalOption('lineCap')
// ->addConditionalOption('lineJoin')
// ->addConditionalOption('dashArray')
// ->addConditionalOptions('fill', array('fill', 'fillColor', 'fillOpacity'))
// ;
}
protected function build(
Definition $definition,
\Model $model,
DefinitionMapper $builder,
DefinitionMapper $mapper,
LatLngBounds $bounds = null
) {
parent::build($definition, $model, $builder, $bounds);
parent::build($definition, $model, $mapper, $bounds);
if ($definition instanceof Definition\HasPopup && $model->addPopup) {
if ($definition instanceof Path && $model->style) {
$styleModel = StyleModel::findActiveByPk($model->style);
if ($styleModel) {
$style = $mapper->handle($styleModel);
if ($style instanceof Style) {
$style->apply($definition);
}
}
}
if ($definition instanceof HasPopup && $model->addPopup) {
$definition->bindPopup($model->popupContent);
}
}

View File

@@ -0,0 +1,45 @@
<?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;
trait ActiveTrait
{
/**
*
* @param int $modelId
* @param array $options
*
* @return \Model|null
*/
public static function findActiveByPK($modelId, $options = array())
{
return static::findOneBy('active=1 AND id', $modelId, $options);
}
/**
*
* @param int $modelId
* @param array $options
*
* @return \Model|null
*/
public static function findActiveByPid($modelId, $options = array())
{
return static::findBy('active=1 AND pid', $modelId, $options);
}
public static function findActivated($options = array())
{
return static::findBy('active', '1', $options);
}
}

View File

@@ -23,5 +23,7 @@ namespace Netzmacht\Contao\Leaflet\Model;
*/
class IconModel extends \Model
{
use ActiveTrait;
protected static $strTable = 'tl_leaflet_icon';
}

View File

@@ -0,0 +1,20 @@
<?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;
class StyleModel extends \Model
{
use ActiveTrait;
protected static $strTable = 'tl_leaflet_style';
}