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

namespace yii\bootstrap;

10
use Yii;
11
use yii\helpers\ArrayHelper;
12 13 14 15 16
use yii\helpers\Html;

/**
 * NavBar renders a navbar HTML component.
 *
17 18 19
 * Any content enclosed between the [[begin()]] and [[end()]] calls of NavBar
 * is treated as the content of the navbar. You may use widgets such as [[Nav]]
 * or [[\yii\widgets\Menu]] to build up such content. For example,
20 21
 *
 * ```php
22 23 24
 * use yii\bootstrap\NavBar;
 * use yii\widgets\Menu;
 *
Alexander Makarov committed
25 26 27 28 29 30 31
 * NavBar::begin(['brandLabel' => 'NavBar Test']);
 * echo Nav::widget([
 *     'items' => [
 *         ['label' => 'Home', 'url' => ['/site/index']],
 *         ['label' => 'About', 'url' => ['/site/about']],
 *     ],
 * ]);
32
 * NavBar::end();
33 34
 * ```
 *
MarsuBoss committed
35
 * @see http://getbootstrap.com/components/#navbar
36
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
37
 * @author Alexander Kochetov <creocoder@gmail.com>
38 39 40 41
 * @since 2.0
 */
class NavBar extends Widget
{
42 43 44
	/**
	 * @var array the HTML attributes for the widget container tag. The following special options are recognized:
	 *
45 46
	 * - tag: string, defaults to "nav", the name of the container tag.
	 *
47
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
48 49 50 51 52
	 */
	public $options = [];
	/**
	 * @var array the HTML attributes for the container tag. The following special options are recognized:
	 *
53 54
	 * - tag: string, defaults to "div", the name of the container tag.
	 *
55
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
56 57
	 */
	public $containerOptions = [];
58
	/**
Qiang Xue committed
59
	 * @var string the text of the brand. Note that this is not HTML-encoded.
MarsuBoss committed
60
	 * @see http://getbootstrap.com/components/#navbar
61 62 63
	 */
	public $brandLabel;
	/**
64
	 * @param array|string $url the URL for the brand's hyperlink tag. This parameter will be processed by [[Url::to()]]
65
	 * and will be used for the "href" attribute of the brand link. If not set, [[\yii\web\Application::homeUrl]] will be used.
66
	 */
67
	public $brandUrl;
68 69
	/**
	 * @var array the HTML attributes of the brand link.
70
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
71
	 */
Alexander Makarov committed
72
	public $brandOptions = [];
73 74 75
	/**
	 * @var string text to show for screen readers for the button to toggle the navbar.
	 */
76
	public $screenReaderToggleText = 'Toggle navigation';
77
	/**
78
	 * @var boolean whether the navbar content should be included in an inner div container which by default
79 80 81 82 83
	 * adds left and right padding. Set this to false for a 100% width navbar.
	 */
	public $renderInnerContainer = true;
	/**
	 * @var array the HTML attributes of the inner container.
84
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
85
	 */
86
	public $innerContainerOptions = [];
87 88 89 90 91 92 93

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
94
		$this->clientOptions = false;
95
		Html::addCssClass($this->options, 'navbar');
96
		if ($this->options['class'] === 'navbar') {
97 98
			Html::addCssClass($this->options, 'navbar-default');
		}
99
		Html::addCssClass($this->brandOptions, 'navbar-brand');
100 101 102
		if (empty($this->options['role'])) {
			$this->options['role'] = 'navigation';
		}
103 104 105 106 107 108 109 110
		$options = $this->options;
		$tag = ArrayHelper::remove($options, 'tag', 'nav');
		echo Html::beginTag($tag, $options);
		if ($this->renderInnerContainer) {
			if (!isset($this->innerContainerOptions['class'])) {
				Html::addCssClass($this->innerContainerOptions, 'container');
			}
			echo Html::beginTag('div', $this->innerContainerOptions);
111
		}
Alexander Makarov committed
112
		echo Html::beginTag('div', ['class' => 'navbar-header']);
113 114 115
		if (!isset($this->containerOptions['id'])) {
			$this->containerOptions['id'] = "{$this->options['id']}-collapse";
		}
116
		echo $this->renderToggleButton();
117
		if ($this->brandLabel !== null) {
118
			Html::addCssClass($this->brandOptions, 'navbar-brand');
119
			echo Html::a($this->brandLabel, $this->brandUrl === null ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
120
		}
121
		echo Html::endTag('div');
122 123 124 125 126
		Html::addCssClass($this->containerOptions, 'collapse');
		Html::addCssClass($this->containerOptions, 'navbar-collapse');
		$options = $this->containerOptions;
		$tag = ArrayHelper::remove($options, 'tag', 'div');
		echo Html::beginTag($tag, $options);
127 128 129
	}

	/**
130
	 * Renders the widget.
131
	 */
132
	public function run()
133
	{
134 135 136
		$tag = ArrayHelper::remove($this->containerOptions, 'tag', 'div');
		echo Html::endTag($tag);
		if ($this->renderInnerContainer) {
137 138
			echo Html::endTag('div');
		}
139 140
		$tag = ArrayHelper::remove($this->options, 'tag', 'nav');
		echo Html::endTag($tag, $this->options);
141
		BootstrapPluginAsset::register($this->getView());
142 143 144 145 146 147 148 149
	}

	/**
	 * Renders collapsible toggle button.
	 * @return string the rendering toggle button.
	 */
	protected function renderToggleButton()
	{
Alexander Makarov committed
150
		$bar = Html::tag('span', '', ['class' => 'icon-bar']);
151
		$screenReader = "<span class=\"sr-only\">{$this->screenReaderToggleText}</span>";
Alexander Makarov committed
152
		return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
153
			'class' => 'navbar-toggle',
154
			'data-toggle' => 'collapse',
155
			'data-target' => "#{$this->containerOptions['id']}",
Alexander Makarov committed
156
		]);
157
	}
Qiang Xue committed
158
}