Nav.php 3.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

use yii\base\InvalidConfigException;
11
use yii\helpers\ArrayHelper;
12 13 14 15 16 17 18 19 20 21 22 23 24
use yii\helpers\Html;

/**
 * Nav renders a nav HTML component.
 *
 * For example:
 *
 * ```php
 * echo Nav::widget(array(
 *     'items' => array(
 *         array(
 *             'label' => 'Home',
 *             'url' => '/',
25
 *             'linkOptions' => array(...),
26 27 28 29
 *             'active' => true,
 *         ),
 *         array(
 *             'label' => 'Dropdown',
Mojtaba Salehi committed
30
 *             'items' => array(
31
 *                  array(
Mojtaba Salehi committed
32
 *                      'label' => 'Level 1 -DropdownA',
33
 *                      'url' => '#',
Mojtaba Salehi committed
34 35 36 37 38 39
 *                      'items' => array(
 *                          array(
 *                              'label' => 'Level 2 -DropdownA',
 *                              'url' => '#',
 *                          ),
 *                      ),
40 41
 *                  ),
 *                  array(
Mojtaba Salehi committed
42
 *                      'label' => 'Level 1 -DropdownB',
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *                      'url' => '#',
 *                  ),
 *             ),
 *         ),
 *     ),
 * ));
 * ```
 *
 * @see http://twitter.github.io/bootstrap/components.html#nav
 * @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
	 * menu item with the following structure:
	 *
61 62 63 64 65
	 * - label: string, required, the nav item label.
	 * - url: optional, the item's URL. Defaults to "#".
	 * - 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.
Qiang Xue committed
66 67
	 * - dropdown: 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.
68 69
	 */
	public $items = array();
70 71 72 73
	/**
	 * @var boolean whether the nav items labels should be HTML-encoded.
	 */
	public $encodeLabels = true;
74 75 76 77 78 79 80


	/**
	 * Initializes the widget.
	 */
	public function init()
	{
81
		parent::init();
82
		Html::addCssClass($this->options, 'nav');
83 84 85 86 87 88 89 90
	}

	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo $this->renderItems();
91
		BootstrapAsset::register($this->getView());
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	}

	/**
	 * Renders widget items.
	 */
	public function renderItems()
	{
		$items = array();
		foreach ($this->items as $item) {
			$items[] = $this->renderItem($item);
		}

		return Html::tag('ul', implode("\n", $items), $this->options);
	}

	/**
	 * Renders a widget's item.
	 * @param mixed $item the item to render.
	 * @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.");
		}
121
		$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
122
		$options = ArrayHelper::getValue($item, 'options', array());
Mojtaba Salehi committed
123
		$items = ArrayHelper::getValue($item, 'items');
124 125 126
		$url = Html::url(ArrayHelper::getValue($item, 'url', '#'));
		$linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());

resurtm committed
127
		if (ArrayHelper::getValue($item, 'active')) {
128
			Html::addCssClass($options, 'active');
129
		}
130

Mojtaba Salehi committed
131
		if ($items !== null) {
132
			$linkOptions['data-toggle'] = 'dropdown';
133 134
			Html::addCssClass($options, 'dropdown');
			Html::addCssClass($urlOptions, 'dropdown-toggle');
135
			$label .= ' ' . Html::tag('b', '', array('class' => 'caret'));
Mojtaba Salehi committed
136 137 138
			if (is_array($items)) {
				$items = Dropdown::widget(array(
					'items' => $items,
yiidevelop committed
139
					'encodeLabels'=>$this->encodeLabels,
Qiang Xue committed
140 141
					'clientOptions' => false,
				));
Qiang Xue committed
142
			}
143 144
		}

Mojtaba Salehi committed
145
		return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
146
	}
Qiang Xue committed
147
}