Linkable graph component (#13263)

* Linkable graph component

* indentation change
This commit is contained in:
Tony Murray
2021-09-28 08:29:38 -05:00
committed by GitHub
parent bfa200f3f7
commit 5a0ed57514
3 changed files with 62 additions and 13 deletions

View File

@@ -45,6 +45,10 @@ class Graph extends Component
* @var int
*/
public $absolute_size;
/**
* @var bool|string
*/
private $link;
/**
* Create a new component instance.
@@ -61,8 +65,20 @@ class Graph extends Component
* @param \App\Models\Device|int|null $device
* @param \App\Models\Port|int|null $port
*/
public function __construct(string $type = '', array $vars = [], $from = '-1d', $to = null, string $legend = 'no', string $aspect = 'normal', ?int $width = null, ?int $height = null, int $absolute_size = 0, $device = null, $port = null)
{
public function __construct(
string $type = '',
array $vars = [],
$from = '-1d',
$to = null,
string $legend = 'no',
string $aspect = 'normal',
?int $width = null,
?int $height = null,
int $absolute_size = 0,
$link = true,
$device = null,
$port = null
) {
$this->type = $type;
$this->vars = $vars;
$this->from = $from;
@@ -71,6 +87,7 @@ class Graph extends Component
$this->absolute_size = $absolute_size;
$this->width = $width ?: ($aspect == 'wide' ? self::DEFAULT_WIDE_WIDTH : self::DEFAULT_NORMAL_WIDTH);
$this->height = $height ?: ($aspect == 'wide' ? self::DEFAULT_WIDE_HEIGHT : self::DEFAULT_NORMAL_HEIGHT);
$this->link = $link;
// handle device and port ids/models for convenience could be set in $vars
if ($device instanceof Device) {
@@ -91,16 +108,47 @@ class Graph extends Component
*/
public function render()
{
return view('components.graph', [
'link' => url('graph.php') . '?' . http_build_query($this->vars + [
'type' => $this->type,
'legend' => $this->legend,
'absolute_size' => $this->absolute_size,
'width' => $this->width,
'height' => $this->height,
'from' => $this->from,
'to' => $this->to,
]),
if ($this->link === false) {
return view('components.graph', [
'src' => $this->getSrc(),
]);
}
return view('components.linked-graph', [
'link' => $this->getLink(),
'src' => $this->getSrc(),
]);
}
private function getSrc(): string
{
return url('graph.php') . '?' . http_build_query($this->vars + [
'type' => $this->type,
'legend' => $this->legend,
'absolute_size' => $this->absolute_size,
'width' => $this->width,
'height' => $this->height,
'from' => $this->from,
'to' => $this->to,
]);
}
private function getLink(): string
{
if ($this->link === true) {
$url = url('graphs') . '/' . http_build_query($this->vars + [
'type' => $this->type,
'from' => $this->from,
'to' => $this->to,
], '', '/');
return $url;
}
if ($this->link === false) {
return '';
}
return $this->link;
}
}

View File

@@ -1 +1 @@
<img width="{{ $width }}" height="{{ $height }}" src="{{ $link }}" alt="{{ $type }}" {{ $attributes->merge(['class' => 'graph-image']) }}>
<img width="{{ $width }}" height="{{ $height }}" src="{{ $src }}" alt="{{ $type }}" {{ $attributes->merge(['class' => 'graph-image']) }}>

View File

@@ -0,0 +1 @@
<a href="{{ $link }}"><img width="{{ $width }}" height="{{ $height }}" src="{{ $src }}" alt="{{ $type }}" {{ $attributes->merge(['class' => 'graph-image']) }}></a>