mirror of
https://github.com/Icinga/icingaweb2-module-businessprocess.git
synced 2026-05-28 04:34:08 -04:00
Breadcrumb: cleanup and externalize
This commit is contained in:
parent
bbcc6eaecf
commit
ee99549341
3 changed files with 61 additions and 53 deletions
57
library/Businessprocess/Renderer/Breadcrumb.php
Normal file
57
library/Businessprocess/Renderer/Breadcrumb.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Businessprocess\Renderer;
|
||||
|
||||
use Icinga\Module\Businessprocess\BpNode;
|
||||
use Icinga\Module\Businessprocess\Html\BaseElement;
|
||||
use Icinga\Module\Businessprocess\Html\Element;
|
||||
use Icinga\Module\Businessprocess\Html\Link;
|
||||
use Icinga\Module\Businessprocess\Renderer\TileRenderer\NodeTile;
|
||||
|
||||
class Breadcrumb extends BaseElement
|
||||
{
|
||||
protected $tag = 'ul';
|
||||
|
||||
protected $defaultAttributes = array(
|
||||
'class' => 'breadcrumb',
|
||||
'data-base-target' => '_main'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param Renderer $renderer
|
||||
* @return static
|
||||
*/
|
||||
public static function create(Renderer $renderer)
|
||||
{
|
||||
$bp = $renderer->getBusinessProcess();
|
||||
$breadcrumb = new static;
|
||||
$breadcrumb->add(Element::create('li')->add(
|
||||
Link::create($bp->getTitle(), $renderer->getBaseUrl())
|
||||
));
|
||||
$path = $renderer->getCurrentPath();
|
||||
|
||||
$parts = array();
|
||||
while ($node = array_pop($path)) {
|
||||
array_unshift(
|
||||
$parts,
|
||||
static::renderNode($bp->getNode($node), $path, $renderer)
|
||||
);
|
||||
}
|
||||
$breadcrumb->addContent($parts);
|
||||
|
||||
return $breadcrumb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BpNode $parent
|
||||
* @return NodeTile
|
||||
*/
|
||||
protected static function renderNode(BpNode $node, $path, Renderer $renderer)
|
||||
{
|
||||
// TODO: something more generic than NodeTile?
|
||||
$p = new NodeTile($renderer, (string) $node, $node, $path);
|
||||
$p->attributes()->add('class', $renderer->getNodeClasses($node));
|
||||
$p->setTag('li');
|
||||
return $p;
|
||||
}
|
||||
}
|
||||
|
|
@ -157,7 +157,7 @@ abstract class Renderer extends Html
|
|||
return $this->path;
|
||||
}
|
||||
|
||||
public function getMyPath()
|
||||
public function getCurrentPath()
|
||||
{
|
||||
$path = $this->getPath();
|
||||
if ($this->rendersSubNode()) {
|
||||
|
|
|
|||
|
|
@ -37,67 +37,18 @@ class TileRenderer extends Renderer
|
|||
$this->add(new AddNewTile($this));
|
||||
}
|
||||
|
||||
$path = $this->getCurrentPath();
|
||||
foreach ($nodes as $name => $node) {
|
||||
$this->add(new NodeTile($this, $name, $node));
|
||||
$this->add(new NodeTile($this, $name, $node, $path));
|
||||
}
|
||||
|
||||
$nodesDiv->addContent($this->getContent());
|
||||
$this->setContent($this->renderBreadCrumb())
|
||||
$this->setContent(Breadcrumb::create($this))
|
||||
->addContent($nodesDiv);
|
||||
|
||||
return parent::render();
|
||||
}
|
||||
|
||||
public function renderBreadCrumb()
|
||||
{
|
||||
$breadcrumb = Element::create('ul', array(
|
||||
'class' => 'breadcrumb',
|
||||
'data-base-target' => '_main'
|
||||
));
|
||||
|
||||
$breadcrumb->add(Element::create('li')->add(
|
||||
Link::create($this->bp->getTitle(), $this->getBaseUrl())
|
||||
));
|
||||
$bp = $this->bp;
|
||||
$path = $this->getMyPath();
|
||||
$max = 20;
|
||||
$chosen = array();
|
||||
for ($i = 1; $i <= $max; $i++) {
|
||||
if (! empty($path)) {
|
||||
$chosen[] = array_pop($path);
|
||||
}
|
||||
}
|
||||
$chosen = array_reverse($chosen);
|
||||
$consumed = array();
|
||||
while ($parent = array_shift($chosen)) {
|
||||
$breadcrumb->add($this->renderParent($bp->getNode($parent), $consumed));
|
||||
$consumed[] = $parent;
|
||||
}
|
||||
|
||||
return $breadcrumb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BpNode $parent
|
||||
*/
|
||||
public function renderParent(BpNode $parent, $path)
|
||||
{
|
||||
$p = new NodeTile($this, (string) $parent, $parent, $path);
|
||||
$p->attributes()->add('class', $this->getNodeClasses($parent));
|
||||
$p->setTag('li');
|
||||
return $p;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultAttributes()
|
||||
{
|
||||
return array(
|
||||
'class' => 'tiles aaaa' . $this->howMany()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A CSS class giving a rough indication of how many nodes we have
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue