NavBar.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use Yii;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. /**
  12. * NavBar renders a navbar HTML component.
  13. *
  14. * Any content enclosed between the [[begin()]] and [[end()]] calls of NavBar
  15. * is treated as the content of the navbar. You may use widgets such as [[Nav]]
  16. * or [[\yii\widgets\Menu]] to build up such content. For example,
  17. *
  18. * ```php
  19. * use yii\bootstrap\NavBar;
  20. * use yii\widgets\Menu;
  21. *
  22. * NavBar::begin(['brandLabel' => 'NavBar Test']);
  23. * echo Nav::widget([
  24. * 'items' => [
  25. * ['label' => 'Home', 'url' => ['/site/index']],
  26. * ['label' => 'About', 'url' => ['/site/about']],
  27. * ],
  28. * ]);
  29. * NavBar::end();
  30. * ```
  31. *
  32. * @see http://getbootstrap.com/components/#navbar
  33. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  34. * @author Alexander Kochetov <creocoder@gmail.com>
  35. * @since 2.0
  36. */
  37. class NavBar extends Widget
  38. {
  39. /**
  40. * @var array the HTML attributes for the widget container tag. The following special options are recognized:
  41. *
  42. * - tag: string, defaults to "nav", the name of the container tag.
  43. *
  44. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  45. */
  46. public $options = [];
  47. /**
  48. * @var array the HTML attributes for the container tag. The following special options are recognized:
  49. *
  50. * - tag: string, defaults to "div", the name of the container tag.
  51. *
  52. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  53. */
  54. public $containerOptions = [];
  55. /**
  56. * @var string the text of the brand. Note that this is not HTML-encoded.
  57. * @see http://getbootstrap.com/components/#navbar
  58. */
  59. public $brandLabel;
  60. /**
  61. * @param array|string $url the URL for the brand's hyperlink tag. This parameter will be processed by [[Url::to()]]
  62. * and will be used for the "href" attribute of the brand link. If not set, [[\yii\web\Application::homeUrl]] will be used.
  63. */
  64. public $brandUrl;
  65. /**
  66. * @var array the HTML attributes of the brand link.
  67. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  68. */
  69. public $brandOptions = [];
  70. /**
  71. * @var string text to show for screen readers for the button to toggle the navbar.
  72. */
  73. public $screenReaderToggleText = 'Toggle navigation';
  74. /**
  75. * @var boolean whether the navbar content should be included in an inner div container which by default
  76. * adds left and right padding. Set this to false for a 100% width navbar.
  77. */
  78. public $renderInnerContainer = true;
  79. /**
  80. * @var array the HTML attributes of the inner container.
  81. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  82. */
  83. public $innerContainerOptions = [];
  84. /**
  85. * Initializes the widget.
  86. */
  87. public function init()
  88. {
  89. parent::init();
  90. $this->clientOptions = false;
  91. Html::addCssClass($this->options, 'navbar');
  92. if ($this->options['class'] === 'navbar') {
  93. Html::addCssClass($this->options, 'navbar-default');
  94. }
  95. Html::addCssClass($this->brandOptions, 'navbar-brand');
  96. if (empty($this->options['role'])) {
  97. $this->options['role'] = 'navigation';
  98. }
  99. $options = $this->options;
  100. $tag = ArrayHelper::remove($options, 'tag', 'nav');
  101. echo Html::beginTag($tag, $options);
  102. if ($this->renderInnerContainer) {
  103. if (!isset($this->innerContainerOptions['class'])) {
  104. Html::addCssClass($this->innerContainerOptions, 'container');
  105. }
  106. echo Html::beginTag('div', $this->innerContainerOptions);
  107. }
  108. echo Html::beginTag('div', ['class' => 'navbar-header']);
  109. if (!isset($this->containerOptions['id'])) {
  110. $this->containerOptions['id'] = "{$this->options['id']}-collapse";
  111. }
  112. echo $this->renderToggleButton();
  113. if ($this->brandLabel !== null) {
  114. Html::addCssClass($this->brandOptions, 'navbar-brand');
  115. echo Html::a($this->brandLabel, $this->brandUrl === null ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
  116. }
  117. echo Html::endTag('div');
  118. Html::addCssClass($this->containerOptions, 'collapse');
  119. Html::addCssClass($this->containerOptions, 'navbar-collapse');
  120. $options = $this->containerOptions;
  121. $tag = ArrayHelper::remove($options, 'tag', 'div');
  122. echo Html::beginTag($tag, $options);
  123. }
  124. /**
  125. * Renders the widget.
  126. */
  127. public function run()
  128. {
  129. $tag = ArrayHelper::remove($this->containerOptions, 'tag', 'div');
  130. echo Html::endTag($tag);
  131. if ($this->renderInnerContainer) {
  132. echo Html::endTag('div');
  133. }
  134. $tag = ArrayHelper::remove($this->options, 'tag', 'nav');
  135. echo Html::endTag($tag, $this->options);
  136. BootstrapPluginAsset::register($this->getView());
  137. }
  138. /**
  139. * Renders collapsible toggle button.
  140. * @return string the rendering toggle button.
  141. */
  142. protected function renderToggleButton()
  143. {
  144. $bar = Html::tag('span', '', ['class' => 'icon-bar']);
  145. $screenReader = "<span class=\"sr-only\">{$this->screenReaderToggleText}</span>";
  146. return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
  147. 'class' => 'navbar-toggle',
  148. 'data-toggle' => 'collapse',
  149. 'data-target' => "#{$this->containerOptions['id']}",
  150. ]);
  151. }
  152. }