Skip to content

Commit

Permalink
Merge pull request magento#799 from magento-firedrakes/MAGETWO-63716
Browse files Browse the repository at this point in the history
[Firedrakes] Add target to admin menu element
  • Loading branch information
Volodymyr Klymenko authored Feb 6, 2017
2 parents 061977e + 2a22a46 commit 06dd04b
Show file tree
Hide file tree
Showing 20 changed files with 907 additions and 110 deletions.
90 changes: 90 additions & 0 deletions app/code/Magento/Backend/Block/AnchorRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Block;

use Magento\Backend\Model\Menu\Item;
use Magento\Framework\Escaper;

/**
* Class AnchorRenderer
*/
class AnchorRenderer
{
/**
* @var MenuItemChecker
*/
private $menuItemChecker;

/**
* @var Escaper
*/
private $escaper;

/**
* @param MenuItemChecker $menuItemChecker
* @param Escaper $escaper
*/
public function __construct(
MenuItemChecker $menuItemChecker,
Escaper $escaper
) {
$this->menuItemChecker = $menuItemChecker;
$this->escaper = $escaper;
}

/**
* Render menu item anchor.
*
* It is used in backend menu to render anchor menu.
*
* @param Item|false $activeItem Can be false if menu item is inaccessible
* but was triggered directly using controller. It is a legacy code behaviour.
* @param Item $menuItem
* @param int $level
* @return string
*/
public function renderAnchor($activeItem, Item $menuItem, $level)
{
if ($level == 1 && $menuItem->getUrl() == '#') {
$output = '<strong class="submenu-group-title" role="presentation">'
. '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle())) . '</span>'
. '</strong>';
} else {
$target = $menuItem->getTarget() ? ('target=' . $menuItem->getTarget()) : '';
$output = '<a href="' . $menuItem->getUrl() . '" ' . $target . ' ' . $this->_renderItemAnchorTitle(
$menuItem
) . $this->_renderItemOnclickFunction(
$menuItem
) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '')
. '">' . '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle()))
. '</span>' . '</a>';
}

return $output;
}

/**
* Render menu item anchor title
*
* @param Item $menuItem
* @return string
*/
private function _renderItemAnchorTitle($menuItem)
{
return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : '';
}

/**
* Render menu item onclick function
*
* @param Item $menuItem
* @return string
*/
private function _renderItemOnclickFunction($menuItem)
{
return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : '';
}
}
121 changes: 26 additions & 95 deletions app/code/Magento/Backend/Block/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Menu extends \Magento\Backend\Block\Template
/**
* Current selected item
*
* @var \Magento\Backend\Model\Menu\Item|null|bool
* @var \Magento\Backend\Model\Menu\Item|false|null
*/
protected $_activeItemModel = null;

Expand All @@ -62,6 +62,16 @@ class Menu extends \Magento\Backend\Block\Template
*/
protected $_localeResolver;

/**
* @var MenuItemChecker
*/
private $menuItemChecker;

/**
* @var AnchorRenderer
*/
private $anchorRenderer;

/**
* @param Template\Context $context
* @param \Magento\Backend\Model\UrlInterface $url
Expand All @@ -70,6 +80,8 @@ class Menu extends \Magento\Backend\Block\Template
* @param \Magento\Backend\Model\Menu\Config $menuConfig
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @param array $data
* @param MenuItemChecker|null $menuItemChecker
* @param AnchorRenderer|null $anchorRenderer
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
Expand All @@ -78,13 +90,17 @@ public function __construct(
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Backend\Model\Menu\Config $menuConfig,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
array $data = []
array $data = [],
MenuItemChecker $menuItemChecker = null,
AnchorRenderer $anchorRenderer = null
) {
$this->_url = $url;
$this->_iteratorFactory = $iteratorFactory;
$this->_authSession = $authSession;
$this->_menuConfig = $menuConfig;
$this->_localeResolver = $localeResolver;
$this->menuItemChecker = $menuItemChecker;
$this->anchorRenderer = $anchorRenderer;
parent::__construct($context, $data);
}

Expand All @@ -99,30 +115,6 @@ protected function _construct()
$this->setCacheTags([self::CACHE_TAGS]);
}

/**
* Check whether given item is currently selected
*
* @param \Magento\Backend\Model\Menu\Item $item
* @param int $level
* @return bool
*/
protected function _isItemActive(\Magento\Backend\Model\Menu\Item $item, $level)
{
$itemModel = $this->getActiveItemModel();
$output = false;

if ($level == 0 &&
$itemModel instanceof \Magento\Backend\Model\Menu\Item &&
($itemModel->getId() == $item->getId() ||
$item->getChildren()->get(
$itemModel->getId()
) !== null)
) {
$output = true;
}
return $output;
}

/**
* Render menu item anchor label
*
Expand All @@ -134,40 +126,6 @@ protected function _getAnchorLabel($menuItem)
return $this->escapeHtml(__($menuItem->getTitle()));
}

/**
* Render menu item anchor title
*
* @param \Magento\Backend\Model\Menu\Item $menuItem
* @return string
*/
protected function _renderItemAnchorTitle($menuItem)
{
return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : '';
}

/**
* Render menu item onclick function
*
* @param \Magento\Backend\Model\Menu\Item $menuItem
* @return string
*/
protected function _renderItemOnclickFunction($menuItem)
{
return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : '';
}

/**
* Render menu item anchor css class
*
* @param \Magento\Backend\Model\Menu\Item $menuItem
* @param int $level
* @return string
*/
protected function _renderAnchorCssClass($menuItem, $level)
{
return $this->_isItemActive($menuItem, $level) ? '_active' : '';
}

/**
* Render menu item mouse events
* @param \Magento\Backend\Model\Menu\Item $menuItem
Expand All @@ -188,10 +146,11 @@ protected function _renderMouseEvent($menuItem)
protected function _renderItemCssClass($menuItem, $level)
{
$isLast = 0 == $level && (bool)$this->getMenuModel()->isLast($menuItem) ? 'last' : '';
$output = ($this->_isItemActive(
$menuItem,
$level
) ? '_current _active' : '') .
$output = ($this->menuItemChecker->isItemActive(
$this->getActiveItemModel(),
$menuItem,
$level
) ? '_current _active' : '') .
' ' .
($menuItem->hasChildren() ? 'parent' : '') .
' ' .
Expand All @@ -202,34 +161,6 @@ protected function _renderItemCssClass($menuItem, $level)
return $output;
}

/**
* Render menu item anchor
* @param \Magento\Backend\Model\Menu\Item $menuItem
* @param int $level
* @return string
*/
protected function _renderAnchor($menuItem, $level)
{
if ($level == 1 && $menuItem->getUrl() == '#') {
$output = '<strong class="submenu-group-title" role="presentation">'
. '<span>' . $this->_getAnchorLabel($menuItem) . '</span>'
. '</strong>';
} else {
$output = '<a href="' . $menuItem->getUrl() . '" ' . $this->_renderItemAnchorTitle(
$menuItem
) . $this->_renderItemOnclickFunction(
$menuItem
) . ' class="' . $this->_renderAnchorCssClass(
$menuItem,
$level
) . '">' . '<span>' . $this->_getAnchorLabel(
$menuItem
) . '</span>' . '</a>';
}

return $output;
}

/**
* Get menu filter iterator
*
Expand Down Expand Up @@ -336,7 +267,7 @@ public function renderMenu($menu, $level = 0)
$menuItem->getId()
) . 'role="menuitem">';

$output .= $this->_renderAnchor($menuItem, $level);
$output .= $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level);

if ($menuItem->hasChildren()) {
$output .= $this->renderMenu($menuItem->getChildren(), $level + 1);
Expand Down Expand Up @@ -456,7 +387,7 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = [])

$id = $this->getJsId($menuItem->getId());
$subMenu = $this->_addSubMenu($menuItem, $level, $limit, $id);
$anchor = $this->_renderAnchor($menuItem, $level);
$anchor = $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level);
$output .= '<li ' . $this->getUiId($menuItem->getId())
. ' class="item-' . $itemClass . ' ' . $this->_renderItemCssClass($menuItem, $level)
. ($level == 0 ? '" id="' . $id . '" aria-haspopup="true' : '')
Expand All @@ -474,7 +405,7 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = [])
/**
* Get current selected menu item
*
* @return \Magento\Backend\Model\Menu\Item|null|bool
* @return \Magento\Backend\Model\Menu\Item|false
*/
public function getActiveItemModel()
{
Expand Down
49 changes: 49 additions & 0 deletions app/code/Magento/Backend/Block/MenuItemChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Block;

use Magento\Backend\Model\Menu\Item;

/**
* Class MenuItemChecker
*/
class MenuItemChecker
{
/**
* Check whether given menu item is currently selected.
*
* It is used in backend menu to highlight active menu item.
*
* @param Item|false $activeItem Can be false if menu item is inaccessible
* but was triggered directly using controller. It is a legacy code behaviour.
* @param Item $item
* @param int $level
* @return bool
*/
public function isItemActive($activeItem, Item $item, $level)
{
$output = false;

if ($level == 0
&& $activeItem instanceof \Magento\Backend\Model\Menu\Item
&& $this->isActiveItemEqualOrChild($activeItem, $item)
) {
$output = true;
}
return $output;
}

/**
* @param Item $activeItem,
* @param Item $item
* @return bool
*/
private function isActiveItemEqualOrChild($activeItem, $item)
{
return ($activeItem->getId() == $item->getId())
|| ($item->getChildren()->get($activeItem->getId()) !== null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@
*/
namespace Magento\Backend\Controller\Adminhtml\System\Store;

use Magento\Framework\Controller\ResultFactory;

/**
* Class Index returns Stores page
*/
class Index extends \Magento\Backend\Controller\Adminhtml\System\Store
{
/**
* Returns Stores page
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_Backend::system_store');
$resultPage->addBreadcrumb(__('Stores'), __('Stores'));
$resultPage->addBreadcrumb(__('All Stores'), __('All Stores'));
$resultPage->getConfig()->getTitle()->prepend(__('Stores'));

return $resultPage;
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/Backend/Model/Menu/Config/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
*/
namespace Magento\Backend\Model\Menu\Config;

/**
* Class Converter converts xml to appropriate array
*/
class Converter implements \Magento\Framework\Config\ConverterInterface
{
/**
* Converts xml to appropriate array
*
* @param mixed $dom
* @return array
*/
Expand All @@ -26,6 +31,7 @@ public function convert($dom)
'resource',
'dependsOnModule',
'dependsOnConfig',
'target'
];
$xpath = new \DOMXPath($dom);
$nodeList = $xpath->query('/config/menu/*');
Expand Down
Loading

0 comments on commit 06dd04b

Please sign in to comment.