NavBar.php 5.46 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
use yii\helpers\Html;

/**
 * NavBar renders a navbar HTML component.
 *
 * For example:
 *
 * ```php
 * echo NavBar::widget(array(
21
 *     'brandLabel' => 'NavBar Test',
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *     'items' => array(
 *         // a Nav widget
 *         array(
 *             // defaults to Nav anyway.
 *             'class' => 'yii\bootstrap\Nav',
 *             // widget configuration
 *             'options' => array(
 *                 'items' => array(
 *                     array(
 *                         'label' => 'Home',
 *                         'url' => '/',
 *                         'options' => array('class' => 'active'),
 *                     ),
 *                     array(
 *                         'label' => 'Dropdown',
37 38 39 40 41 42 43 44 45 46 47 48
 *                         'content' => new Dropdown(array(
 *                             'items' => array(
 *                                 array(
 *                                     'label' => 'DropdownA',
 *                                     'url' => '#',
 *                                 ),
 *                                 array(
 *                                     'label' => 'DropdownB',
 *                                     'url' => '#'
 *                                 ),
 *                             )
 *                         ),
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 *                     ),
 *                 )
 *             ),
 *         ),
 *         // you can also use strings
 *         '<form class="navbar-search pull-left" action="">' .
 *         '<input type="text" class="search-query" placeholder="Search">' .
 *         '</form>',
 *     ),
 * ));
 * ```
 *
 * @see http://twitter.github.io/bootstrap/components.html#navbar
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class NavBar extends Widget
{
67 68 69 70 71 72 73 74 75
	/**
	 * @var string the text of the brand.
	 * @see http://twitter.github.io/bootstrap/components.html#navbar
	 */
	public $brandLabel;
	/**
	 * @param array|string $url the URL for the brand's hyperlink tag. This parameter will be processed by [[Html::url()]]
	 * and will be used for the "href" attribute of the brand link. Defaults to site root.
	 */
Qiang Xue committed
76
	public $brandUrl = '/';
77 78 79 80
	/**
	 * @var array the HTML attributes of the brand link.
	 */
	public $brandOptions = array();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	/**
	 * @var array list of menu items in the navbar widget. Each array element represents a single
	 * menu item with the following structure:
	 *
	 * ```php
	 * array(
	 *     // optional, the menu item class type of the widget to render. Defaults to "Nav" widget.
	 *     'class' => 'Menu item class type',
	 *     // required, the configuration options of the widget.
	 *     'options'=> array(...),
	 * ),
	 * // optionally, you can pass a string
	 * '<form class="navbar-search pull-left" action="">' .
	 * '<input type="text" class="search-query span2" placeholder="Search">' .
	 * '</form>',
	 * ```
	 *
	 * Optionally, you can also use a plain string instead of an array element.
	 */
	public $items = array();


	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
109
		$this->clientOptions = false;
110
		$this->addCssClass($this->options, 'navbar');
111
		$this->addCssClass($this->brandOptions, 'brand');
112 113 114 115 116 117 118 119 120 121
	}

	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo Html::beginTag('div', $this->options);
		echo $this->renderItems();
		echo Html::endTag('div');
Qiang Xue committed
122
		$this->getView()->registerAssetBundle(self::$responsive ? 'yii/bootstrap/responsive' : 'yii/bootstrap');
123 124 125
	}

	/**
126
	 * Renders the items.
127 128 129 130 131 132 133 134
	 * @return string the rendering items.
	 */
	protected function renderItems()
	{
		$items = array();
		foreach ($this->items as $item) {
			$items[] = $this->renderItem($item);
		}
135
		$contents = implode("\n", $items);
Qiang Xue committed
136 137 138
		$brand = Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);

		if (self::$responsive) {
139
			$this->getView()->registerAssetBundle('yii/bootstrap/collapse');
Qiang Xue committed
140
			$contents = Html::tag('div',
141
					$this->renderToggleButton() .
Qiang Xue committed
142
					$brand . "\n" .
143 144 145
					Html::tag('div', $contents, array('class' => 'nav-collapse collapse navbar-collapse')),
					array('class' => 'container'));
		} else {
Qiang Xue committed
146
			$contents = $brand . "\n" . $contents;
147
		}
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		return Html::tag('div', $contents, array('class' => 'navbar-inner'));
	}

	/**
	 * Renders a item. The item can be a string, a custom class or a Nav widget (defaults if no class specified.
	 * @param mixed $item the item to render. If array, it is assumed the configuration of a widget being `class`
	 * required and if not specified, then defaults to `yii\bootstrap\Nav`.
	 * @return string the rendering result.
	 * @throws InvalidConfigException
	 */
	protected function renderItem($item)
	{
		if (is_string($item)) {
			return $item;
		}
		$config = ArrayHelper::getValue($item, 'options', array());
165 166 167 168 169
		$config['clientOptions'] = false;

		$class = ArrayHelper::getValue($item, 'class', 'yii\bootstrap\Nav');

		return $class::widget($config);
170 171 172 173 174 175 176 177 178 179 180 181
	}

	/**
	 * Renders collapsible toggle button.
	 * @return string the rendering toggle button.
	 */
	protected function renderToggleButton()
	{
		$items = array();
		for ($i = 0; $i < 3; $i++) {
			$items[] = Html::tag('span', '', array('class' => 'icon-bar'));
		}
182
		return Html::a(implode("\n", $items), null, array(
183 184 185
			'class' => 'btn btn-navbar',
			'data-toggle' => 'collapse',
			'data-target' => 'div.navbar-collapse',
186
		));
187
	}
Qiang Xue committed
188
}