2013-09-09 13:55:29 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-09-09 13:55:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Chart\Primitive;
|
|
|
|
|
2014-07-09 18:04:03 +02:00
|
|
|
use DOMElement;
|
2023-08-17 12:28:42 +02:00
|
|
|
use Icinga\Util\Csp;
|
|
|
|
use ipl\Web\Style;
|
2013-09-23 12:09:40 +02:00
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Base class for stylable drawables
|
|
|
|
*/
|
|
|
|
class Styleable
|
|
|
|
{
|
2013-09-09 13:55:29 +02:00
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* The stroke width to use
|
2013-09-25 16:32:28 +02:00
|
|
|
*
|
2023-08-17 16:06:24 +02:00
|
|
|
* @var int|float
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public $strokeWidth = 0;
|
2013-09-21 17:35:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The stroke color to use
|
2013-09-25 16:32:28 +02:00
|
|
|
*
|
2013-09-21 17:35:18 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public $strokeColor = '#000';
|
2013-09-21 17:35:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The fill color to use
|
2013-09-25 16:32:28 +02:00
|
|
|
*
|
2013-09-21 17:35:18 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public $fill = 'none';
|
2013-09-21 17:35:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional styles to be appended to the style attribute
|
2013-09-25 16:32:28 +02:00
|
|
|
*
|
2023-08-17 12:28:42 +02:00
|
|
|
* @var array<string, string>
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2023-08-17 12:28:42 +02:00
|
|
|
public $additionalStyle = [];
|
2013-09-09 13:55:29 +02:00
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* The id of this element
|
2013-09-25 16:32:28 +02:00
|
|
|
*
|
2023-08-17 12:28:42 +02:00
|
|
|
* @var ?string
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
|
|
|
public $id = null;
|
2013-09-09 13:55:29 +02:00
|
|
|
|
2013-09-23 12:09:40 +02:00
|
|
|
/**
|
|
|
|
* Additional attributes to be set
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $attributes = array();
|
|
|
|
|
2013-09-09 13:55:29 +02:00
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Set the stroke width for this drawable
|
|
|
|
*
|
2023-08-17 16:06:24 +02:00
|
|
|
* @param int|float $width The stroke with unit
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this Fluid interface
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
public function setStrokeWidth($width)
|
|
|
|
{
|
|
|
|
$this->strokeWidth = $width;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Set the color for the stroke or none for no stroke
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @param string $color The color to set for the stroke
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this Fluid interface
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public function setStrokeColor($color)
|
|
|
|
{
|
|
|
|
$this->strokeColor = $color ? $color : 'none';
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Set additional styles for this drawable
|
|
|
|
*
|
2023-08-17 12:28:42 +02:00
|
|
|
* @param array<string, string> $styles The styles to set additionally
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this Fluid interface
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public function setAdditionalStyle($styles)
|
|
|
|
{
|
|
|
|
$this->additionalStyle = $styles;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Set the fill for this styleable
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @param string $color The color to use for filling or null to use no fill
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this Fluid interface
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
|
|
|
public function setFill($color = null)
|
|
|
|
{
|
2013-09-09 13:55:29 +02:00
|
|
|
$this->fill = $color ? $color : 'none';
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Set the id for this element
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @param string $id The id to set for this element
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this Fluid interface
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
|
|
|
public function setId($id)
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 12:28:42 +02:00
|
|
|
* Return the ruleset used for styling the DOMNode
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2023-08-17 12:28:42 +02:00
|
|
|
* @return Style A ruleset containing styles
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public function getStyle()
|
|
|
|
{
|
2023-08-17 12:28:42 +02:00
|
|
|
$styles = $this->additionalStyle;
|
|
|
|
$styles['fill'] = $this->fill;
|
|
|
|
$styles['stroke'] = $this->strokeColor;
|
|
|
|
$styles['stroke-width'] = (string) $this->strokeWidth;
|
|
|
|
|
|
|
|
return (new Style())
|
|
|
|
->setNonce(Csp::getStyleNonce())
|
|
|
|
->add("#$this->id", $styles);
|
2013-09-09 13:55:29 +02:00
|
|
|
}
|
2013-09-23 12:09:40 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-25 16:32:28 +02:00
|
|
|
* Add an additional attribute to this element
|
2013-09-23 12:09:40 +02:00
|
|
|
*/
|
|
|
|
public function setAttribute($key, $value)
|
|
|
|
{
|
|
|
|
$this->attributes[$key] = $value;
|
|
|
|
}
|
|
|
|
|
2013-09-25 16:32:28 +02:00
|
|
|
/**
|
|
|
|
* Apply attribute to a DOMElement
|
|
|
|
*
|
|
|
|
* @param DOMElement $el Element to apply attributes
|
|
|
|
*/
|
2013-09-23 12:09:40 +02:00
|
|
|
protected function applyAttributes(DOMElement $el)
|
|
|
|
{
|
|
|
|
foreach ($this->attributes as $name => $value) {
|
|
|
|
$el->setAttribute($name, $value);
|
|
|
|
}
|
|
|
|
}
|
2013-09-21 17:35:18 +02:00
|
|
|
}
|