mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-29 08:44:10 +02:00
Merge branch 'bugfix/menu-item-renderer-deduplication-7658'
fixes #7658
This commit is contained in:
commit
5f3aafd3bd
@ -9,15 +9,9 @@ use Icinga\Web\Url;
|
|||||||
/**
|
/**
|
||||||
* A menu item with a link that surpasses the regular navigation link behavior
|
* A menu item with a link that surpasses the regular navigation link behavior
|
||||||
*/
|
*/
|
||||||
class ForeignMenuItemRenderer implements MenuItemRenderer {
|
class ForeignMenuItemRenderer extends MenuItemRenderer
|
||||||
|
{
|
||||||
public function render(Menu $menu)
|
protected $attributes = array(
|
||||||
{
|
'target' => '_self'
|
||||||
return sprintf(
|
);
|
||||||
'<a href="%s" target="_self">%s%s<span></span></a>',
|
|
||||||
$menu->getUrl() ?: '#',
|
|
||||||
$menu->getIcon() ? '<img aria-hidden="true" src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
|
|
||||||
htmlspecialchars($menu->getTitle())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,107 @@
|
|||||||
|
|
||||||
namespace Icinga\Web\Menu;
|
namespace Icinga\Web\Menu;
|
||||||
|
|
||||||
|
use Icinga\Application\Icinga;
|
||||||
use Icinga\Web\Menu;
|
use Icinga\Web\Menu;
|
||||||
|
use Icinga\Web\View;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the html content of a single menu item
|
* Default MenuItemRenderer class
|
||||||
*/
|
*/
|
||||||
interface MenuItemRenderer {
|
class MenuItemRenderer
|
||||||
public function render(Menu $menu);
|
{
|
||||||
|
/**
|
||||||
|
* Contains <a> element specific attributes
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $attributes = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* View
|
||||||
|
*
|
||||||
|
* @var View|null
|
||||||
|
*/
|
||||||
|
protected $view;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the view
|
||||||
|
*
|
||||||
|
* @param View $view
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setView(View $view)
|
||||||
|
{
|
||||||
|
$this->view = $view;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the view
|
||||||
|
*
|
||||||
|
* @return View
|
||||||
|
*/
|
||||||
|
public function getView()
|
||||||
|
{
|
||||||
|
if ($this->view === null) {
|
||||||
|
$this->view = Icinga::app()->getViewRenderer()->view;
|
||||||
|
}
|
||||||
|
return $this->view;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the html content of a single menu item
|
||||||
|
*
|
||||||
|
* @param Menu $menu
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function render(Menu $menu)
|
||||||
|
{
|
||||||
|
return $this->createLink($menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a menu item link element
|
||||||
|
*
|
||||||
|
* @param Menu $menu
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function createLink(Menu $menu)
|
||||||
|
{
|
||||||
|
if ($menu->getIcon() && strpos($menu->getIcon(), '.') === false) {
|
||||||
|
return sprintf(
|
||||||
|
'<a href="%s"%s><i aria-hidden="true" class="icon-%s"></i>%s</a>',
|
||||||
|
$menu->getUrl() ? : '#',
|
||||||
|
$this->getAttributes(),
|
||||||
|
$menu->getIcon(),
|
||||||
|
$this->getView()->escape($menu->getTitle())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sprintf(
|
||||||
|
'<a href="%s"%s>%s%s<span></span></a>',
|
||||||
|
$menu->getUrl() ? : '#',
|
||||||
|
$this->getAttributes(),
|
||||||
|
$menu->getIcon() ? '<img aria-hidden="true" src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
|
||||||
|
$this->getView()->escape($menu->getTitle())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns <a> element specific attributes if present
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getAttributes()
|
||||||
|
{
|
||||||
|
$attributes = '';
|
||||||
|
$view = $this->getView();
|
||||||
|
foreach ($this->attributes as $attribute => $value) {
|
||||||
|
$attributes .= ' ' . $view->escape($attribute) . '="' . $view->escape($value) . '"';
|
||||||
|
}
|
||||||
|
return $attributes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
namespace Icinga\Web;
|
namespace Icinga\Web;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Icinga\Web\Menu\MenuItemRenderer;
|
||||||
use RecursiveIteratorIterator;
|
use RecursiveIteratorIterator;
|
||||||
use Icinga\Application\Logger;
|
use Icinga\Application\Logger;
|
||||||
use Icinga\Web\Menu\PermittedMenuItemFilter;
|
use Icinga\Web\Menu\PermittedMenuItemFilter;
|
||||||
@ -32,6 +33,11 @@ class MenuRenderer extends RecursiveIteratorIterator
|
|||||||
*/
|
*/
|
||||||
protected $useCustomRenderer = false;
|
protected $useCustomRenderer = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var MenuItemRenderer
|
||||||
|
*/
|
||||||
|
protected $defaultRenderer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new MenuRenderer
|
* Create a new MenuRenderer
|
||||||
*
|
*
|
||||||
@ -45,6 +51,7 @@ class MenuRenderer extends RecursiveIteratorIterator
|
|||||||
} else {
|
} else {
|
||||||
$this->url = Url::fromPath($url);
|
$this->url = Url::fromPath($url);
|
||||||
}
|
}
|
||||||
|
$this->defaultRenderer = new MenuItemRenderer();
|
||||||
parent::__construct(new PermittedMenuItemFilter($menu), RecursiveIteratorIterator::CHILD_FIRST);
|
parent::__construct(new PermittedMenuItemFilter($menu), RecursiveIteratorIterator::CHILD_FIRST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,22 +121,8 @@ class MenuRenderer extends RecursiveIteratorIterator
|
|||||||
Logger::error('Could not invoke custom renderer. Exception: '. $e->getMessage());
|
Logger::error('Could not invoke custom renderer. Exception: '. $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($child->getIcon() && strpos($child->getIcon(), '.') === false) {
|
|
||||||
return sprintf(
|
return $this->defaultRenderer->render($child);
|
||||||
'<a href="%s"><i aria-hidden="true" class="icon-%s"></i>%s</a>',
|
|
||||||
$child->getUrl() ?: '#',
|
|
||||||
$child->getIcon(),
|
|
||||||
htmlspecialchars($child->getTitle())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return sprintf(
|
|
||||||
'<a href="%s">%s%s</a>',
|
|
||||||
$child->getUrl() ?: '#',
|
|
||||||
$child->getIcon()
|
|
||||||
? '<img aria-hidden="true" src="' . Url::fromPath($child->getIcon()) . '" class="icon" /> '
|
|
||||||
: '',
|
|
||||||
htmlspecialchars($child->getTitle())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,10 +6,9 @@ namespace Icinga\Module\Monitoring\Web\Menu;
|
|||||||
use Icinga\Web\Menu as Menu;
|
use Icinga\Web\Menu as Menu;
|
||||||
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
|
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
|
||||||
use Icinga\Web\Menu\MenuItemRenderer;
|
use Icinga\Web\Menu\MenuItemRenderer;
|
||||||
use Icinga\Web\Url;
|
|
||||||
|
|
||||||
class MonitoringMenuItemRenderer implements MenuItemRenderer {
|
|
||||||
|
|
||||||
|
class MonitoringMenuItemRenderer extends MenuItemRenderer
|
||||||
|
{
|
||||||
protected static $summary;
|
protected static $summary;
|
||||||
|
|
||||||
protected $columns = array();
|
protected $columns = array();
|
||||||
@ -70,31 +69,18 @@ class MonitoringMenuItemRenderer implements MenuItemRenderer {
|
|||||||
|
|
||||||
public function render(Menu $menu)
|
public function render(Menu $menu)
|
||||||
{
|
{
|
||||||
$count = $this->countItems();
|
return $this->getBadge() . $this->createLink($menu);
|
||||||
$badge = '';
|
}
|
||||||
if ($count) {
|
|
||||||
$badge = sprintf(
|
protected function getBadge()
|
||||||
|
{
|
||||||
|
if ($count = $this->countItems()) {
|
||||||
|
return sprintf(
|
||||||
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
|
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
|
||||||
$this->getBadgeTitle(),
|
$this->getBadgeTitle(),
|
||||||
$count
|
$count
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if ($menu->getIcon() && strpos($menu->getIcon(), '.') === false) {
|
return '';
|
||||||
return sprintf(
|
|
||||||
'%s <a href="%s"><i aria-hidden="true" class="icon-%s"></i>%s</a>',
|
|
||||||
$badge,
|
|
||||||
$menu->getUrl() ?: '#',
|
|
||||||
$menu->getIcon(),
|
|
||||||
htmlspecialchars($menu->getTitle())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return sprintf(
|
|
||||||
'%s<a href="%s">%s%s<span></span></a>',
|
|
||||||
$badge,
|
|
||||||
$menu->getUrl() ?: '#',
|
|
||||||
$menu->getIcon() ? '<img aria-hidden="true" src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
|
|
||||||
htmlspecialchars($menu->getTitle())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user