You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
6.1 KiB
215 lines
6.1 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\bootstrap;
|
||
|
|
||
12 years ago
|
use Yii;
|
||
12 years ago
|
use yii\base\InvalidConfigException;
|
||
12 years ago
|
use yii\helpers\ArrayHelper;
|
||
12 years ago
|
use yii\helpers\Html;
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Nav renders a nav HTML component.
|
||
11 years ago
|
*
|
||
12 years ago
|
* For example:
|
||
|
*
|
||
|
* ```php
|
||
11 years ago
|
* echo Nav::widget([
|
||
|
* 'items' => [
|
||
|
* [
|
||
12 years ago
|
* 'label' => 'Home',
|
||
11 years ago
|
* 'url' => ['site/index'],
|
||
|
* 'linkOptions' => [...],
|
||
11 years ago
|
* ],
|
||
11 years ago
|
* [
|
||
12 years ago
|
* 'label' => 'Dropdown',
|
||
11 years ago
|
* 'items' => [
|
||
11 years ago
|
* ['label' => 'Level 1 - Dropdown A', 'url' => '#'],
|
||
11 years ago
|
* '<li class="divider"></li>',
|
||
|
* '<li class="dropdown-header">Dropdown Header</li>',
|
||
11 years ago
|
* ['label' => 'Level 1 - Dropdown B', 'url' => '#'],
|
||
11 years ago
|
* ],
|
||
|
* ],
|
||
|
* ],
|
||
|
* ]);
|
||
12 years ago
|
* ```
|
||
11 years ago
|
*
|
||
11 years ago
|
* Note: Multilevel dropdowns beyond Level 1 are not supported in Bootstrap 3.
|
||
11 years ago
|
*
|
||
11 years ago
|
* @see http://getbootstrap.com/components.html#dropdowns
|
||
|
* @see http://getbootstrap.com/components/#nav
|
||
11 years ago
|
*
|
||
12 years ago
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Nav extends Widget
|
||
|
{
|
||
|
/**
|
||
|
* @var array list of items in the nav widget. Each array element represents a single
|
||
11 years ago
|
* menu item which can be either a string or an array with the following structure:
|
||
12 years ago
|
*
|
||
12 years ago
|
* - label: string, required, the nav item label.
|
||
|
* - url: optional, the item's URL. Defaults to "#".
|
||
12 years ago
|
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
|
||
12 years ago
|
* - linkOptions: array, optional, the HTML attributes of the item's link.
|
||
|
* - options: array, optional, the HTML attributes of the item container (LI).
|
||
|
* - active: boolean, optional, whether the item should be on active state or not.
|
||
12 years ago
|
* - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
|
||
12 years ago
|
* or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
|
||
11 years ago
|
*
|
||
11 years ago
|
* If a menu item is a string, it will be rendered directly without HTML encoding.
|
||
12 years ago
|
*/
|
||
11 years ago
|
public $items = [];
|
||
12 years ago
|
/**
|
||
|
* @var boolean whether the nav items labels should be HTML-encoded.
|
||
|
*/
|
||
|
public $encodeLabels = true;
|
||
12 years ago
|
/**
|
||
|
* @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;
|
||
12 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Initializes the widget.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
12 years ago
|
parent::init();
|
||
12 years ago
|
if ($this->route === null && Yii::$app->controller !== null) {
|
||
|
$this->route = Yii::$app->controller->getRoute();
|
||
|
}
|
||
|
if ($this->params === null) {
|
||
|
$this->params = $_GET;
|
||
|
}
|
||
12 years ago
|
Html::addCssClass($this->options, 'nav');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
|
echo $this->renderItems();
|
||
12 years ago
|
BootstrapAsset::register($this->getView());
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders widget items.
|
||
|
*/
|
||
|
public function renderItems()
|
||
|
{
|
||
11 years ago
|
$items = [];
|
||
12 years ago
|
foreach ($this->items as $i => $item) {
|
||
|
if (isset($item['visible']) && !$item['visible']) {
|
||
|
unset($items[$i]);
|
||
|
continue;
|
||
|
}
|
||
12 years ago
|
$items[] = $this->renderItem($item);
|
||
|
}
|
||
|
|
||
|
return Html::tag('ul', implode("\n", $items), $this->options);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders a widget's item.
|
||
11 years ago
|
* @param string|array $item the item to render.
|
||
12 years ago
|
* @return string the rendering result.
|
||
|
* @throws InvalidConfigException
|
||
|
*/
|
||
|
public function renderItem($item)
|
||
|
{
|
||
|
if (is_string($item)) {
|
||
|
return $item;
|
||
|
}
|
||
|
if (!isset($item['label'])) {
|
||
|
throw new InvalidConfigException("The 'label' option is required.");
|
||
|
}
|
||
12 years ago
|
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
|
||
11 years ago
|
$options = ArrayHelper::getValue($item, 'options', []);
|
||
12 years ago
|
$items = ArrayHelper::getValue($item, 'items');
|
||
12 years ago
|
$url = Html::url(ArrayHelper::getValue($item, 'url', '#'));
|
||
11 years ago
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
|
||
12 years ago
|
|
||
12 years ago
|
if (isset($item['active'])) {
|
||
|
$active = ArrayHelper::remove($item, 'active', false);
|
||
|
} else {
|
||
|
$active = $this->isItemActive($item);
|
||
|
}
|
||
|
|
||
|
if ($active) {
|
||
12 years ago
|
Html::addCssClass($options, 'active');
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if ($items !== null) {
|
||
12 years ago
|
$linkOptions['data-toggle'] = 'dropdown';
|
||
12 years ago
|
Html::addCssClass($options, 'dropdown');
|
||
|
Html::addCssClass($urlOptions, 'dropdown-toggle');
|
||
11 years ago
|
$label .= ' ' . Html::tag('b', '', ['class' => 'caret']);
|
||
12 years ago
|
if (is_array($items)) {
|
||
11 years ago
|
$items = Dropdown::widget([
|
||
12 years ago
|
'items' => $items,
|
||
12 years ago
|
'encodeLabels' => $this->encodeLabels,
|
||
12 years ago
|
'clientOptions' => false,
|
||
11 years ago
|
]);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
|
||
|
/**
|
||
|
* 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)
|
||
|
{
|
||
12 years ago
|
if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
|
||
|
$route = $item['url'][0];
|
||
|
if ($route[0] !== '/' && Yii::$app->controller) {
|
||
|
$route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
|
||
|
}
|
||
|
if (ltrim($route, '/') !== $this->route) {
|
||
|
return false;
|
||
|
}
|
||
12 years ago
|
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;
|
||
|
}
|
||
12 years ago
|
}
|