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

namespace yii\bootstrap;

use yii\helpers\Html;

/**
 * NavBar renders a navbar HTML component.
 *
15 16 17
 * 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,
18 19
 *
 * ```php
20 21 22
 * use yii\bootstrap\NavBar;
 * use yii\widgets\Menu;
 *
Alexander Makarov committed
23 24 25 26 27 28 29
 * NavBar::begin(['brandLabel' => 'NavBar Test']);
 * echo Nav::widget([
 *     'items' => [
 *         ['label' => 'Home', 'url' => ['/site/index']],
 *         ['label' => 'About', 'url' => ['/site/about']],
 *     ],
 * ]);
30
 * NavBar::end();
31 32 33 34 35 36 37 38
 * ```
 *
 * @see http://twitter.github.io/bootstrap/components.html#navbar
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class NavBar extends Widget
{
39
	/**
Qiang Xue committed
40
	 * @var string the text of the brand. Note that this is not HTML-encoded.
41 42 43 44 45 46 47
	 * @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
48
	public $brandUrl = '/';
49 50 51
	/**
	 * @var array the HTML attributes of the brand link.
	 */
Alexander Makarov committed
52
	public $brandOptions = [];
53 54 55
	/**
	 * @var string text to show for screen readers for the button to toggle the navbar.
	 */
56
	public $screenReaderToggleText = 'Toggle navigation';
57 58 59 60 61
	/**
	 * @var bool whether the navbar content should be included in a `container` div which adds left and right padding.
	 * Set this to false for a 100% width navbar.
	 */
	public $padded = true;
62 63 64 65 66 67 68

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
69
		$this->clientOptions = false;
70 71 72 73
		Html::addCssClass($this->options, 'navbar');
		if ($this->options['class'] == 'navbar') {
			Html::addCssClass($this->options, 'navbar-default');
		}
74
		Html::addCssClass($this->brandOptions, 'navbar-brand');
75 76 77
		if (empty($this->options['role'])) {
			$this->options['role'] = 'navigation';
		}
78

79
		echo Html::beginTag('nav', $this->options);
80 81 82
		if ($this->padded) {
			echo Html::beginTag('div', ['class' => 'container']);
		}
83

Alexander Makarov committed
84
		echo Html::beginTag('div', ['class' => 'navbar-header']);
85
		echo $this->renderToggleButton();
86 87
		if ($this->brandLabel !== null) {
			echo Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);
88
		}
89 90
		echo Html::endTag('div');

Alex-Code committed
91
		echo Html::beginTag('div', ['class' => "collapse navbar-collapse navbar-{$this->options['id']}-collapse"]);
92 93 94
	}

	/**
95
	 * Renders the widget.
96
	 */
97
	public function run()
98
	{
99 100

		echo Html::endTag('div');
101 102 103
		if ($this->padded) {
			echo Html::endTag('div');
		}
104
		echo Html::endTag('nav');
105
		BootstrapPluginAsset::register($this->getView());
106 107 108 109 110 111 112 113
	}

	/**
	 * Renders collapsible toggle button.
	 * @return string the rendering toggle button.
	 */
	protected function renderToggleButton()
	{
Alexander Makarov committed
114
		$bar = Html::tag('span', '', ['class' => 'icon-bar']);
115
		$screenReader = '<span class="sr-only">'.$this->screenReaderToggleText.'</span>';
Alexander Makarov committed
116
		return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
117
			'class' => 'navbar-toggle',
118
			'data-toggle' => 'collapse',
Alex-Code committed
119
			'data-target' => ".navbar-{$this->options['id']}-collapse",
Alexander Makarov committed
120
		]);
121
	}
Qiang Xue committed
122
}