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.
299 lines
10 KiB
299 lines
10 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
|
|
||
|
/**
|
||
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
|
* ],
|
||
|
* ],
|
||
10 years ago
|
* [
|
||
|
* 'label' => 'Login',
|
||
|
* 'url' => ['site/login'],
|
||
|
* 'visible' => Yii::$app->user->isGuest
|
||
|
* ],
|
||
11 years ago
|
* ],
|
||
10 years ago
|
* 'options' => ['class' =>'nav-pills'], // set this to nav-tab to get tab-styled navigation
|
||
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/#dropdowns
|
||
11 years ago
|
* @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
|
||
|
{
|
||
11 years ago
|
/**
|
||
|
* @var array list of items in the nav widget. Each array element represents a single
|
||
|
* menu item which can be either a string or an array with the following structure:
|
||
|
*
|
||
|
* - label: string, required, the nav item label.
|
||
|
* - url: optional, the item's URL. Defaults to "#".
|
||
7 years ago
|
* - visible: bool, optional, whether this menu item is visible. Defaults to true.
|
||
11 years ago
|
* - linkOptions: array, optional, the HTML attributes of the item's link.
|
||
|
* - options: array, optional, the HTML attributes of the item container (LI).
|
||
7 years ago
|
* - active: bool, optional, whether the item should be on active state or not.
|
||
10 years ago
|
* - dropDownOptions: array, optional, the HTML options that will passed to the [[Dropdown]] widget.
|
||
11 years ago
|
* - 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.
|
||
7 years ago
|
* - encode: bool, optional, whether the label will be HTML-encoded. If set, supersedes the $encodeLabels option for only this item.
|
||
11 years ago
|
*
|
||
|
* If a menu item is a string, it will be rendered directly without HTML encoding.
|
||
|
*/
|
||
|
public $items = [];
|
||
|
/**
|
||
7 years ago
|
* @var bool whether the nav items labels should be HTML-encoded.
|
||
11 years ago
|
*/
|
||
|
public $encodeLabels = true;
|
||
|
/**
|
||
7 years ago
|
* @var bool whether to automatically activate items according to whether their route setting
|
||
11 years ago
|
* matches the currently requested route.
|
||
|
* @see isItemActive
|
||
|
*/
|
||
|
public $activateItems = true;
|
||
|
/**
|
||
7 years ago
|
* @var bool whether to activate parent menu items when one of the corresponding child menu items is active.
|
||
11 years ago
|
*/
|
||
|
public $activateParents = false;
|
||
|
/**
|
||
11 years ago
|
* @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;
|
||
10 years ago
|
/**
|
||
|
* @var string this property allows you to customize the HTML which is used to generate the drop down caret symbol,
|
||
|
* which is displayed next to the button text to indicate the drop down functionality.
|
||
9 years ago
|
* Defaults to `null` which means `<span class="caret"></span>` will be used. To disable the caret, set this property to be an empty string.
|
||
10 years ago
|
*/
|
||
|
public $dropDownCaret;
|
||
9 years ago
|
/**
|
||
8 years ago
|
* @var string name of a class to use for rendering dropdowns within this widget. Defaults to [[Dropdown]].
|
||
8 years ago
|
* @since 2.0.7
|
||
9 years ago
|
*/
|
||
8 years ago
|
public $dropdownClass = 'yii\bootstrap\Dropdown';
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Initializes the widget.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
if ($this->route === null && Yii::$app->controller !== null) {
|
||
|
$this->route = Yii::$app->controller->getRoute();
|
||
|
}
|
||
|
if ($this->params === null) {
|
||
|
$this->params = Yii::$app->request->getQueryParams();
|
||
|
}
|
||
10 years ago
|
if ($this->dropDownCaret === null) {
|
||
9 years ago
|
$this->dropDownCaret = '<span class="caret"></span>';
|
||
10 years ago
|
}
|
||
10 years ago
|
Html::addCssClass($this->options, ['widget' => 'nav']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
|
BootstrapAsset::register($this->getView());
|
||
10 years ago
|
return $this->renderItems();
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders widget items.
|
||
|
*/
|
||
|
public function renderItems()
|
||
|
{
|
||
|
$items = [];
|
||
|
foreach ($this->items as $i => $item) {
|
||
|
if (isset($item['visible']) && !$item['visible']) {
|
||
|
continue;
|
||
|
}
|
||
|
$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.
|
||
|
* @return string the rendering result.
|
||
11 years ago
|
* @throws InvalidConfigException
|
||
|
*/
|
||
|
public function renderItem($item)
|
||
|
{
|
||
|
if (is_string($item)) {
|
||
|
return $item;
|
||
|
}
|
||
|
if (!isset($item['label'])) {
|
||
|
throw new InvalidConfigException("The 'label' option is required.");
|
||
|
}
|
||
11 years ago
|
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
|
||
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
|
||
11 years ago
|
$options = ArrayHelper::getValue($item, 'options', []);
|
||
|
$items = ArrayHelper::getValue($item, 'items');
|
||
|
$url = ArrayHelper::getValue($item, 'url', '#');
|
||
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
|
||
|
|
||
|
if (isset($item['active'])) {
|
||
|
$active = ArrayHelper::remove($item, 'active', false);
|
||
|
} else {
|
||
|
$active = $this->isItemActive($item);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (empty($items)) {
|
||
|
$items = '';
|
||
|
} else {
|
||
11 years ago
|
$linkOptions['data-toggle'] = 'dropdown';
|
||
10 years ago
|
Html::addCssClass($options, ['widget' => 'dropdown']);
|
||
|
Html::addCssClass($linkOptions, ['widget' => 'dropdown-toggle']);
|
||
10 years ago
|
if ($this->dropDownCaret !== '') {
|
||
|
$label .= ' ' . $this->dropDownCaret;
|
||
|
}
|
||
11 years ago
|
if (is_array($items)) {
|
||
8 years ago
|
$items = $this->isChildActive($items, $active);
|
||
10 years ago
|
$items = $this->renderDropdown($items, $item);
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
if ($active) {
|
||
11 years ago
|
Html::addCssClass($options, 'active');
|
||
11 years ago
|
}
|
||
|
|
||
|
return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
10 years ago
|
* Renders the given items as a dropdown.
|
||
|
* This method is called to create sub-menus.
|
||
|
* @param array $items the given items. Please refer to [[Dropdown::items]] for the array structure.
|
||
10 years ago
|
* @param array $parentItem the parent item information. Please refer to [[items]] for the structure of this array.
|
||
10 years ago
|
* @return string the rendering result.
|
||
|
* @since 2.0.1
|
||
|
*/
|
||
10 years ago
|
protected function renderDropdown($items, $parentItem)
|
||
10 years ago
|
{
|
||
8 years ago
|
/** @var Widget $dropdownClass */
|
||
|
$dropdownClass = $this->dropdownClass;
|
||
|
return $dropdownClass::widget([
|
||
10 years ago
|
'options' => ArrayHelper::getValue($parentItem, 'dropDownOptions', []),
|
||
10 years ago
|
'items' => $items,
|
||
|
'encodeLabels' => $this->encodeLabels,
|
||
|
'clientOptions' => false,
|
||
|
'view' => $this->getView(),
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Check to see if a child item is active optionally activating the parent.
|
||
|
* @param array $items @see items
|
||
7 years ago
|
* @param bool $active should the parent be active too
|
||
11 years ago
|
* @return array @see items
|
||
|
*/
|
||
|
protected function isChildActive($items, &$active)
|
||
|
{
|
||
|
foreach ($items as $i => $child) {
|
||
8 years ago
|
if (is_array($child) && !ArrayHelper::getValue($child, 'visible', true)) {
|
||
|
continue;
|
||
|
}
|
||
11 years ago
|
if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) {
|
||
|
Html::addCssClass($items[$i]['options'], 'active');
|
||
|
if ($this->activateParents) {
|
||
|
$active = true;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
$childItems = ArrayHelper::getValue($child, 'items');
|
||
|
if (is_array($childItems)) {
|
||
|
$activeParent = false;
|
||
|
$items[$i]['items'] = $this->isChildActive($childItems, $activeParent);
|
||
|
if ($activeParent) {
|
||
|
Html::addCssClass($items[$i]['options'], 'active');
|
||
|
$active = true;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
return $items;
|
||
|
}
|
||
11 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.
|
||
11 years ago
|
* @param array $item the menu item to be checked
|
||
7 years ago
|
* @return bool whether the menu item is active
|
||
11 years ago
|
*/
|
||
|
protected function isItemActive($item)
|
||
|
{
|
||
8 years ago
|
if (!$this->activateItems) {
|
||
|
return false;
|
||
|
}
|
||
11 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;
|
||
|
}
|
||
|
unset($item['url']['#']);
|
||
|
if (count($item['url']) > 1) {
|
||
10 years ago
|
$params = $item['url'];
|
||
|
unset($params[0]);
|
||
|
foreach ($params as $name => $value) {
|
||
11 years ago
|
if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
12 years ago
|
}
|