| Current Path : /var/www/html/delima/common/widgets/inspinia/ |
| Current File : /var/www/html/delima/common/widgets/inspinia/SideBar.php |
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace common\widgets\inspinia;
use Yii;
use Closure;
use yii\helpers\Url;
use yii\widgets\Menu;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* Inspinia SideBar Widget
* @package xutl\inspinia
*/
class SideBar extends Menu
{
/**
* @var array the HTML attributes for the menu's container tag. The following special options are recognized:
*
* - tag: string, defaults to "ul", the tag name of the item container tags. Set to false to disable container tag.
* See also [[\yii\helpers\Html::tag()]].
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [
'class' => 'nav metismenu',
'id' => 'side-menu',
];
/**
* @var string the template used to render the body of a menu which is a link.
* In this template, the token `{url}` will be replaced with the corresponding link URL;
* while `{label}` will be replaced with the link text.
* This property will be overridden by the `template` option set in individual menu items via [[items]].
*/
public $linkTemplate = '<a href="{url}">{icon} {label}</a>';
/**
* @var string the template used to render a list of sub-menus.
* In this template, the token `{items}` will be replaced with the rendered sub-menu items.
*/
public $submenuTemplate = "\n<ul class=\"nav nav-second-level collapse\">\n{items}\n</ul>\n";
/**
* @var bool whether to activate parent menu items when one of the corresponding child menu items is active.
* The activated parent menu items will also have its CSS classes appended with [[activeCssClass]].
*/
public $activateParents = true;
/**
* @var string 头部菜单
*/
public $top;
/**
* Renders the menu.
*/
public function run()
{
if ($this->route === null && Yii::$app->controller !== null) {
$this->route = Yii::$app->controller->getRoute();
}
if ($this->params === null) {
$this->params = Yii::$app->request->getQueryParams();
}
$items = $this->normalizeItems($this->items, $hasActiveChild);
if (!empty($items)) {
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'ul');
echo Html::tag($tag, $this->top . $this->renderItems($items), $options);
}
}
/**
* Recursively renders the menu items (without the container tag).
* @param array $items the menu items to be rendered recursively
* @return string the rendering result
*/
protected function renderItems($items)
{
$n = count($items);
$lines = [];
foreach ($items as $i => $item) {
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
$tag = ArrayHelper::remove($options, 'tag', 'li');
$class = [];
if ($item['active']) {
$class[] = $this->activeCssClass;
}
if ($i === 0 && $this->firstItemCssClass !== null) {
$class[] = $this->firstItemCssClass;
}
if ($i === $n - 1 && $this->lastItemCssClass !== null) {
$class[] = $this->lastItemCssClass;
}
if (!empty($class)) {
if (empty($options['class'])) {
$options['class'] = implode(' ', $class);
} else {
$options['class'] .= ' ' . implode(' ', $class);
}
}
$menu = $this->renderItem($item);
if (!empty($item['items'])) {
$submenuTemplate = ArrayHelper::getValue($item, 'submenuTemplate', $this->submenuTemplate);
$menu .= strtr($submenuTemplate, [
'{items}' => $this->renderItems($item['items']),
]);
}
$lines[] = Html::tag($tag, $menu, $options);
}
return implode("\n", $lines);
}
/**
* Renders the content of a menu item.
* Note that the container and the sub-menus are not rendered here.
* @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item.
* @return string the rendering result
*/
protected function renderItem($item)
{
if ($item['parent'] == null && isset($item['items'])) {//如果是顶级菜单并且有子菜单
$linkTemplate = '<a href="{url}">{icon} {label} <span class="fa arrow"></span></a>';
} else {
$linkTemplate = $this->linkTemplate;
}
if (isset($item['url'])) {
$template = ArrayHelper::getValue($item, 'template', $linkTemplate);
return strtr($template, [
'{icon}' => $item['icon'],
'{url}' => Html::encode(Url::to($item['url'])),
'{label}' => $item['label'],
]);
} else {
$template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
return strtr($template, [
'{label}' => $item['label'],
]);
}
}
/**
* Normalizes the [[items]] property to remove invisible items and activate certain items.
* @param array $items the items to be normalized.
* @param bool $active whether there is an active child menu item.
* @return array the normalized menu items
*/
protected function normalizeItems($items, &$active)
{
foreach ($items as $i => $item) {
if (isset($item['visible']) && !$item['visible']) {
unset($items[$i]);
continue;
}
if (!isset($item['label'])) {
$item['label'] = '';
}
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
if ($item['parent'] == null) {//如果是顶级菜单
$items[$i]['label'] = $encodeLabel ? Html::tag('span', Html::encode($item['label']), ['class' => 'nav-label']) : $item['label'];
} else {
$items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
}
$items[$i]['icon'] = isset ($item['icon']) ? Html::tag('i', '', ['class' => 'fa fa-lg fa-fw ' . $item['icon']]) : '';
$hasActiveChild = false;
//判断是否有子菜单
if (isset($item['items'])) {
$items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
if (empty($items[$i]['items']) && $this->hideEmptyItems) {//隐藏空的子菜单
unset($items[$i]['items']);
if (!isset($item['url'])) {
unset($items[$i]);
continue;
}
}
}
if (!isset($item['active'])) {
if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item)) {
$active = $items[$i]['active'] = true;
} else {
$items[$i]['active'] = false;
}
} elseif ($item['active'] instanceof Closure) {
$active = $items[$i]['active'] = call_user_func($item['active'], $item, $hasActiveChild, $this->isItemActive($item), $this);
} elseif ($item['active']) {
$active = true;
}
}
return array_values($items);
}
}