|
@ -53,7 +53,8 @@ use yii\helpers\Url; |
|
|
* |
|
|
* |
|
|
* |
|
|
* |
|
|
*/ |
|
|
*/ |
|
|
class Menu extends Widget { |
|
|
|
|
|
|
|
|
class Menu extends Widget |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @var array 菜单的item数组。 |
|
|
* @var array 菜单的item数组。 |
|
@ -132,8 +133,8 @@ class Menu extends Widget { |
|
|
public $options = [ |
|
|
public $options = [ |
|
|
'class' => 'nav nav-pills nav-sidebar flex-column', |
|
|
'class' => 'nav nav-pills nav-sidebar flex-column', |
|
|
'data-widget' => 'treeview', |
|
|
'data-widget' => 'treeview', |
|
|
'role'=>'menu', |
|
|
|
|
|
'data-accordion'=>'false' |
|
|
|
|
|
|
|
|
'role' => 'menu', |
|
|
|
|
|
'data-accordion' => 'false' |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -164,7 +165,8 @@ class Menu extends Widget { |
|
|
/** |
|
|
/** |
|
|
* 渲染菜单 |
|
|
* 渲染菜单 |
|
|
*/ |
|
|
*/ |
|
|
public function run() { |
|
|
|
|
|
|
|
|
public function run() |
|
|
|
|
|
{ |
|
|
if ($this->route === null && Yii::$app->controller !== null) { |
|
|
if ($this->route === null && Yii::$app->controller !== null) { |
|
|
$this->route = Yii::$app->controller->getRoute(); |
|
|
$this->route = Yii::$app->controller->getRoute(); |
|
|
} |
|
|
} |
|
@ -186,7 +188,8 @@ class Menu extends Widget { |
|
|
* @param array $items |
|
|
* @param array $items |
|
|
* @return string 渲染结果 |
|
|
* @return string 渲染结果 |
|
|
*/ |
|
|
*/ |
|
|
protected function renderItems($items) { |
|
|
|
|
|
|
|
|
protected function renderItems($items) |
|
|
|
|
|
{ |
|
|
$lines = []; |
|
|
$lines = []; |
|
|
$n = count($items); |
|
|
$n = count($items); |
|
|
foreach ($items as $i => $item) { |
|
|
foreach ($items as $i => $item) { |
|
@ -234,7 +237,8 @@ class Menu extends Widget { |
|
|
* @param array $item |
|
|
* @param array $item |
|
|
* @return string 渲染结果 |
|
|
* @return string 渲染结果 |
|
|
*/ |
|
|
*/ |
|
|
protected function renderItem($item) { |
|
|
|
|
|
|
|
|
protected function renderItem($item) |
|
|
|
|
|
{ |
|
|
if (isset($item['url'])) { |
|
|
if (isset($item['url'])) { |
|
|
if (isset($item['template'])) { |
|
|
if (isset($item['template'])) { |
|
|
$template = $item['template']; |
|
|
$template = $item['template']; |
|
@ -244,7 +248,7 @@ class Menu extends Widget { |
|
|
return strtr($template, [ |
|
|
return strtr($template, [ |
|
|
'{url}' => Html::encode(Url::to($item['url'])), |
|
|
'{url}' => Html::encode(Url::to($item['url'])), |
|
|
'{label}' => Html::encode($item['label']), |
|
|
'{label}' => Html::encode($item['label']), |
|
|
'{class}'=>isset($item['active'])?'active':'', |
|
|
|
|
|
|
|
|
'{class}' => isset($item['active']) ? 'active' : '', |
|
|
'{icon}' => Html::encode($item['icon']) |
|
|
'{icon}' => Html::encode($item['icon']) |
|
|
]); |
|
|
]); |
|
|
} |
|
|
} |
|
@ -253,7 +257,7 @@ class Menu extends Widget { |
|
|
|
|
|
|
|
|
return strtr($template, [ |
|
|
return strtr($template, [ |
|
|
'{label}' => $item['label'], |
|
|
'{label}' => $item['label'], |
|
|
'{class}'=>isset($item['active'])?'active':'' |
|
|
|
|
|
|
|
|
'{class}' => isset($item['active']) ? 'active' : '' |
|
|
]); |
|
|
]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -262,21 +266,31 @@ class Menu extends Widget { |
|
|
* @param $item array |
|
|
* @param $item array |
|
|
* @return boolean $item |
|
|
* @return boolean $item |
|
|
*/ |
|
|
*/ |
|
|
protected function isItemActive($item) { |
|
|
|
|
|
|
|
|
protected function isItemActive($item) |
|
|
|
|
|
{ |
|
|
if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) { |
|
|
if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) { |
|
|
$route = Yii::getAlias($item['url'][0]); |
|
|
$route = Yii::getAlias($item['url'][0]); |
|
|
|
|
|
|
|
|
if ($route[0] !== '/' && Yii::$app->controller) { |
|
|
if ($route[0] !== '/' && Yii::$app->controller) { |
|
|
$route = Yii::$app->controller->module->getUniqueId() . '/' . $route; |
|
|
$route = Yii::$app->controller->module->getUniqueId() . '/' . $route; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (ltrim($route, '/') !== $this->route) { |
|
|
|
|
|
|
|
|
$route = ltrim($route, '/'); |
|
|
|
|
|
if ($route != substr($this->route, 0, strrpos($this->route, '/')) && $route != $this->route && |
|
|
|
|
|
ltrim(Yii::$app->request->url, '/') !== $route) { |
|
|
|
|
|
|
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unset($item['url']['#']); |
|
|
|
|
|
if (count($item['url']) > 1) { |
|
|
|
|
|
foreach (array_splice($item['url'], 1) as $name => $value) { |
|
|
|
|
|
if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -285,7 +299,8 @@ class Menu extends Widget { |
|
|
* @param string $item |
|
|
* @param string $item |
|
|
* @param bool $active |
|
|
* @param bool $active |
|
|
*/ |
|
|
*/ |
|
|
protected function normalizeItems($items, &$active) { |
|
|
|
|
|
|
|
|
protected function normalizeItems($items, &$active) |
|
|
|
|
|
{ |
|
|
foreach ($items as $i => $item) { |
|
|
foreach ($items as $i => $item) { |
|
|
/* 去除visible 为 false的item */ |
|
|
/* 去除visible 为 false的item */ |
|
|
if (isset($item['visible']) && !$item['visible']) { |
|
|
if (isset($item['visible']) && !$item['visible']) { |
|
|