NavBar.php 2.89 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
	public $screenReaderToggleText = 'Toggle navigation';
55 56 57 58 59 60 61

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
62
		$this->clientOptions = false;
63
		Html::addCssClass($this->options, 'navbar navbar-default');
64
		Html::addCssClass($this->brandOptions, 'navbar-brand');
65 66 67
		if (empty($this->options['role'])) {
			$this->options['role'] = 'navigation';
		}
68

69
		echo Html::beginTag('nav', $this->options);
Alexander Makarov committed
70
		echo Html::beginTag('div', ['class' => 'container']);
71

Alexander Makarov committed
72
		echo Html::beginTag('div', ['class' => 'navbar-header']);
73
		echo $this->renderToggleButton();
74 75
		if ($this->brandLabel !== null) {
			echo Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);
76
		}
77 78
		echo Html::endTag('div');

Alexander Makarov committed
79
		echo Html::beginTag('div', ['class' => 'collapse navbar-collapse navbar-ex1-collapse']);
80 81 82
	}

	/**
83
	 * Renders the widget.
84
	 */
85
	public function run()
86
	{
87 88 89

		echo Html::endTag('div');
		echo Html::endTag('div');
90
		echo Html::endTag('nav');
91
		BootstrapPluginAsset::register($this->getView());
92 93 94 95 96 97 98 99
	}

	/**
	 * Renders collapsible toggle button.
	 * @return string the rendering toggle button.
	 */
	protected function renderToggleButton()
	{
Alexander Makarov committed
100
		$bar = Html::tag('span', '', ['class' => 'icon-bar']);
101
		$screenReader = '<span class="sr-only">'.$this->screenReaderToggleText.'</span>';
Alexander Makarov committed
102
		return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
103
			'class' => 'navbar-toggle',
104
			'data-toggle' => 'collapse',
105
			'data-target' => '.navbar-ex1-collapse',
Alexander Makarov committed
106
		]);
107
	}
Qiang Xue committed
108
}