mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-31 01:34:09 +02:00
Add a HTML renderer for Icinga\Web\Menu
The menu was being rendered through recursive partials before. The overhead this fact implicates is not as efficient as standard recursion so there is now a special renderer for Icinga\Web\Menu utilizing the RecursiveIteratorIterator refs #6153
This commit is contained in:
parent
8eac449d56
commit
143a1e44fe
@ -2,6 +2,7 @@
|
|||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
use Icinga\Web\MenuRenderer;
|
||||||
use Icinga\Web\Controller\ActionController;
|
use Icinga\Web\Controller\ActionController;
|
||||||
use Icinga\Web\Hook;
|
use Icinga\Web\Hook;
|
||||||
use Icinga\Web\Menu;
|
use Icinga\Web\Menu;
|
||||||
@ -17,9 +18,7 @@ class LayoutController extends ActionController
|
|||||||
*/
|
*/
|
||||||
public function menuAction()
|
public function menuAction()
|
||||||
{
|
{
|
||||||
$this->view->url = Url::fromRequest()->getRelativeUrl();
|
$this->view->menuRenderer = new MenuRenderer(Menu::fromConfig()->order(), Url::fromRequest()->getRelativeUrl());
|
||||||
$this->view->items = Menu::fromConfig()->getChildren();
|
|
||||||
$this->view->sub = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
if (! $this->level) {
|
|
||||||
$this->level = 0;
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
<ul<?= $this->level === 0 ? ' role="navigation"' : '' ?>>
|
|
||||||
<?php
|
|
||||||
|
|
||||||
foreach ($this->items as $item) {
|
|
||||||
|
|
||||||
printf(
|
|
||||||
' <li%s><a href="%s">%s%s</a>',
|
|
||||||
$this->href($this->url) === $this->href($item->getUrl()) ? ' class="active"' : '',
|
|
||||||
$item->getUrl() ? $this->href($item->getUrl()) : '#',
|
|
||||||
$item->getIcon() ? $this->img(
|
|
||||||
$item->getIcon(),
|
|
||||||
array('class' => 'icon')
|
|
||||||
) . ' ' : '',
|
|
||||||
$this->escape($item->getTitle())
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($item->hasChildren()) {
|
|
||||||
echo $this->partial(
|
|
||||||
'parts/menu.phtml',
|
|
||||||
array('items' => $item->getChildren(), 'url' => $this->url, 'level' => $this->level + 1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "</li>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
</ul>
|
|
@ -2,22 +2,17 @@
|
|||||||
|
|
||||||
use Icinga\Web\Url;
|
use Icinga\Web\Url;
|
||||||
use Icinga\Web\Menu;
|
use Icinga\Web\Menu;
|
||||||
|
use Icinga\Web\MenuRenderer;
|
||||||
|
|
||||||
// Don't render a menu for unauthenticated users unless menu is auth aware
|
// Don't render a menu for unauthenticated users unless menu is auth aware
|
||||||
if (! $this->auth()->isAuthenticated()) {
|
if (! $this->auth()->isAuthenticated()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current url
|
|
||||||
$url = Url::fromRequest()->getRelativeUrl();
|
|
||||||
$menu = Menu::fromConfig();
|
|
||||||
?>
|
?>
|
||||||
<div id="menu" data-base-target="_main">
|
<div id="menu" data-base-target="_main">
|
||||||
<form action="<?= $this->href('search') ?>" method="get" role="search">
|
<form action="<?= $this->href('search') ?>" method="get" role="search">
|
||||||
<input type="text" name="q" class="search autofocus" placeholder="<?= $this->translate('Search...') ?>" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
|
<input type="text" name="q" class="search autofocus" placeholder="<?= $this->translate('Search...') ?>" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
|
||||||
</form>
|
</form>
|
||||||
<?= $this->partial('parts/menu.phtml', array(
|
<?= new MenuRenderer(Menu::fromConfig()->order(), Url::fromRequest()->getRelativeUrl()); ?>
|
||||||
'items' => $menu->getChildren(),
|
|
||||||
'url' => $url
|
|
||||||
)) ?>
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,39 +1 @@
|
|||||||
<ul>
|
<?= $menuRenderer; ?>
|
||||||
<?php foreach ($items as $item): ?>
|
|
||||||
<?php
|
|
||||||
$itemClass = '';
|
|
||||||
if ($sub) {
|
|
||||||
// $itemClass .= 'submenu ';
|
|
||||||
}
|
|
||||||
if ($this->href($url) === $this->href($item->getUrl())) {
|
|
||||||
$itemClass .= 'active ';
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
<li <?php if (!empty($itemClass)): ?>class="<?= $itemClass ?>"<?php endif ?>>
|
|
||||||
<?php if($item->getUrl()): ?>
|
|
||||||
<a href="<?= $this->href($item->getUrl()); ?>" <?php foreach($item->getAttribs() as $attrib => $value): ?> <?= $attrib ?>="<?= $value ?>"<?php endforeach?>>
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php
|
|
||||||
if ($icon = $item->getIcon()) {
|
|
||||||
echo $this->img($icon, array('height' => 16, 'width' => 16));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
<?php if ($iconClass = $item->getIconClass()): ?>
|
|
||||||
<i class="<?= $iconClass ?>"></i>
|
|
||||||
<?php endif ?>
|
|
||||||
<?= $item->getTitle();?>
|
|
||||||
<?php if($item->getUrl()): ?>
|
|
||||||
</a>
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php
|
|
||||||
if($item->hasChildren()) {
|
|
||||||
echo $this->partial(
|
|
||||||
'layout/menu.phtml',
|
|
||||||
'default',
|
|
||||||
array('items' => $item->getChildren(), 'sub' => true, 'url' => $this->url)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
</li>
|
|
||||||
<?php endforeach ?>
|
|
||||||
</ul>
|
|
151
library/Icinga/Web/MenuRenderer.php
Normal file
151
library/Icinga/Web/MenuRenderer.php
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Icinga\Web;
|
||||||
|
|
||||||
|
use RecursiveIteratorIterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A renderer to draw a menu with its sub-menus using an unordered html list
|
||||||
|
*/
|
||||||
|
class MenuRenderer extends RecursiveIteratorIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The relative url of the current request
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The html tags to assemble the menu with
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $tags = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new MenuRenderer
|
||||||
|
*
|
||||||
|
* @param Menu $menu The menu to render
|
||||||
|
* @param string $url A relative url to identify "active" children with
|
||||||
|
*/
|
||||||
|
public function __construct(Menu $menu, $url = null)
|
||||||
|
{
|
||||||
|
$this->url = $url;
|
||||||
|
parent::__construct($menu, RecursiveIteratorIterator::CHILD_FIRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the outer ul opening html-tag
|
||||||
|
*/
|
||||||
|
public function beginIteration()
|
||||||
|
{
|
||||||
|
$this->tags[] = '<ul role="navigation">';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the outer ul closing html-tag
|
||||||
|
*/
|
||||||
|
public function endIteration()
|
||||||
|
{
|
||||||
|
$this->tags[] = '</ul>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a inner ul opening html-tag
|
||||||
|
*/
|
||||||
|
public function beginChildren()
|
||||||
|
{
|
||||||
|
// The iterator runs in mode CHILD_FIRST so we need to remember
|
||||||
|
// where to insert the parent's opening html tag once its rendered
|
||||||
|
$parent = $this->getSubIterator(0)->current();
|
||||||
|
$this->tags[$parent->getId() . '_begin'] = null;
|
||||||
|
|
||||||
|
$this->tags[] = '<ul>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a inner ul closing html-tag
|
||||||
|
*/
|
||||||
|
public function endChildren()
|
||||||
|
{
|
||||||
|
$this->tags[] = '</ul>';
|
||||||
|
|
||||||
|
// Remember the position of the parent's closing html-tag
|
||||||
|
$parent = $this->getSubIterator(0)->current();
|
||||||
|
$this->tags[$parent->getId() . '_end'] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the given child
|
||||||
|
*
|
||||||
|
* @param Menu $child The menu's child to render
|
||||||
|
*
|
||||||
|
* @return string The child rendered as html
|
||||||
|
*/
|
||||||
|
public function renderChild(Menu $child)
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
'<a href="%s">%s%s</a>',
|
||||||
|
$child->getUrl() ? Url::fromPath($child->getUrl()) : '#',
|
||||||
|
$child->getIcon() ? '<img src="' . Url::fromPath($child->getIcon()) . '" class="icon" /> ' : '',
|
||||||
|
htmlspecialchars($child->getTitle())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the menu rendered as html
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function render()
|
||||||
|
{
|
||||||
|
$passedActiveChild = false;
|
||||||
|
foreach ($this as $child) {
|
||||||
|
$childIsActive = $this->isActive($child);
|
||||||
|
if ($childIsActive && $this->getDepth() > 0) {
|
||||||
|
$passedActiveChild = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($childIsActive || ($passedActiveChild && $this->getDepth() === 0)) {
|
||||||
|
$passedActiveChild &= $this->getDepth() !== 0;
|
||||||
|
$openTag = '<li class="active">';
|
||||||
|
} else {
|
||||||
|
$openTag = '<li>';
|
||||||
|
}
|
||||||
|
$content = $this->renderChild($child);
|
||||||
|
$closingTag = '</li>';
|
||||||
|
|
||||||
|
if (array_key_exists($child->getId() . '_begin', $this->tags)) {
|
||||||
|
$this->tags[$child->getId() . '_begin'] = $openTag . $content;
|
||||||
|
$this->tags[$child->getId() . '_end'] = $closingTag;
|
||||||
|
} else {
|
||||||
|
$this->tags[] = $openTag . $content . $closingTag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode("\n", $this->tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see MenuRenderer::render()
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return $this->render();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the current request url references the child's url
|
||||||
|
*
|
||||||
|
* @param Menu $child The menu's child to check
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isActive(Menu $child)
|
||||||
|
{
|
||||||
|
return html_entity_decode(rawurldecode($this->url)) === html_entity_decode(rawurldecode($child->getUrl()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user