Commit 534aa4f9 by Qiang Xue

Support auto-activation of Nav.

parent 293b1737
<?php <?php
use yii\bootstrap\NavBar;
use yii\helpers\Html; use yii\helpers\Html;
use yii\widgets\Menu; use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs; use yii\widgets\Breadcrumbs;
/** /**
...@@ -28,8 +28,8 @@ app\config\AppAsset::register($this); ...@@ -28,8 +28,8 @@ app\config\AppAsset::register($this);
'class' => 'navbar-inverse navbar-fixed-top', 'class' => 'navbar-inverse navbar-fixed-top',
), ),
)); ));
echo Menu::widget(array( echo Nav::widget(array(
'options' => array('class' => 'nav navbar-nav pull-right'), 'options' => array('class' => 'navbar-nav pull-right'),
'items' => array( 'items' => array(
array('label' => 'Home', 'url' => array('/site/index')), array('label' => 'Home', 'url' => array('/site/index')),
array('label' => 'About', 'url' => array('/site/about')), array('label' => 'About', 'url' => array('/site/about')),
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html; use yii\helpers\Html;
...@@ -21,9 +22,8 @@ use yii\helpers\Html; ...@@ -21,9 +22,8 @@ use yii\helpers\Html;
* 'items' => array( * 'items' => array(
* array( * array(
* 'label' => 'Home', * 'label' => 'Home',
* 'url' => '/', * 'url' => array('site/index'),
* 'linkOptions' => array(...), * 'linkOptions' => array(...),
* 'active' => true,
* ), * ),
* array( * array(
* 'label' => 'Dropdown', * 'label' => 'Dropdown',
...@@ -63,7 +63,7 @@ class Nav extends Widget ...@@ -63,7 +63,7 @@ class Nav extends Widget
* - linkOptions: array, optional, the HTML attributes of the item's link. * - linkOptions: array, optional, the HTML attributes of the item's link.
* - options: array, optional, the HTML attributes of the item container (LI). * - options: array, optional, the HTML attributes of the item container (LI).
* - active: boolean, optional, whether the item should be on active state or not. * - active: boolean, optional, whether the item should be on active state or not.
* - dropdown: array|string, optional, the configuration array for creating a [[Dropdown]] widget, * - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
* or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus. * or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
*/ */
public $items = array(); public $items = array();
...@@ -71,6 +71,26 @@ class Nav extends Widget ...@@ -71,6 +71,26 @@ class Nav extends Widget
* @var boolean whether the nav items labels should be HTML-encoded. * @var boolean whether the nav items labels should be HTML-encoded.
*/ */
public $encodeLabels = true; public $encodeLabels = true;
/**
* @var boolean whether to automatically activate items according to whether their route setting
* matches the currently requested route.
* @see isItemActive
*/
public $activateItems = true;
/**
* @var string the route used to determine if a menu item is active or not.
* If not set, it will use the route of the current request.
* @see params
* @see isItemActive
*/
public $route;
/**
* @var array the parameters used to determine if a menu item is active or not.
* If not set, it will use `$_GET`.
* @see route
* @see isItemActive
*/
public $params;
/** /**
...@@ -79,6 +99,12 @@ class Nav extends Widget ...@@ -79,6 +99,12 @@ class Nav extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
if ($this->route === null && Yii::$app->controller !== null) {
$this->route = Yii::$app->controller->getRoute();
}
if ($this->params === null) {
$this->params = $_GET;
}
Html::addCssClass($this->options, 'nav'); Html::addCssClass($this->options, 'nav');
} }
...@@ -124,7 +150,13 @@ class Nav extends Widget ...@@ -124,7 +150,13 @@ class Nav extends Widget
$url = Html::url(ArrayHelper::getValue($item, 'url', '#')); $url = Html::url(ArrayHelper::getValue($item, 'url', '#'));
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', array()); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
if (ArrayHelper::getValue($item, 'active')) { if (isset($item['active'])) {
$active = ArrayHelper::remove($item, 'active', false);
} else {
$active = $this->isItemActive($item);
}
if ($active) {
Html::addCssClass($options, 'active'); Html::addCssClass($options, 'active');
} }
...@@ -144,4 +176,31 @@ class Nav extends Widget ...@@ -144,4 +176,31 @@ class Nav extends Widget
return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options); return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
} }
/**
* Checks whether a menu item is active.
* This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
* When the `url` option of a menu item is specified in terms of an array, its first element is treated
* as the route for the item and the rest of the elements are the associated parameters.
* Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
* be considered active.
* @param array $item the menu item to be checked
* @return boolean whether the menu item is active
*/
protected function isItemActive($item)
{
if (isset($item['url']) && is_array($item['url']) && trim($item['url'][0], '/') === $this->route) {
unset($item['url']['#']);
if (count($item['url']) > 1) {
foreach (array_splice($item['url'], 1) as $name => $value) {
if (!isset($this->params[$name]) || $this->params[$name] != $value) {
return false;
}
}
}
return true;
}
return false;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment