NavBar.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  11. * NavBar renders a navbar HTML component.
  12. *
  13. * Any content enclosed between the [[begin()]] and [[end()]] calls of NavBar
  14. * is treated as the content of the navbar. You may use widgets such as [[Nav]]
  15. * or [[\yii\widgets\Menu]] to build up such content. For example,
  16. *
  17. * ```php
  18. * use yii\bootstrap\NavBar;
  19. * use yii\bootstrap\Nav;
  20. *
  21. * NavBar::begin(['brandLabel' => 'NavBar Test']);
  22. * echo Nav::widget([
  23. * 'items' => [
  24. * ['label' => 'Home', 'url' => ['/site/index']],
  25. * ['label' => 'About', 'url' => ['/site/about']],
  26. * ],
  27. * 'options' => ['class' => 'navbar-nav'],
  28. * ]);
  29. * NavBar::end();
  30. * ```
  31. *
  32. * @see https://getbootstrap.com/docs/3.3/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|bool the text of the brand or false if it's not used. Note that this is not HTML-encoded.
  57. * @see https://getbootstrap.com/docs/3.3/components/#navbar
  58. */
  59. public $brandLabel = false;
  60. /**
  61. * @var string|bool src of the brand image or false if it's not used. Note that this param will override `$this->brandLabel` param.
  62. * @see https://getbootstrap.com/docs/3.3/components/#navbar
  63. * @since 2.0.8
  64. */
  65. public $brandImage = false;
  66. /**
  67. * @var array|string|bool $url the URL for the brand's hyperlink tag. This parameter will be processed by [[\yii\helpers\Url::to()]]
  68. * and will be used for the "href" attribute of the brand link. Default value is false that means
  69. * [[\yii\web\Application::homeUrl]] will be used.
  70. * You may set it to `null` if you want to have no link at all.
  71. */
  72. public $brandUrl = false;
  73. /**
  74. * @var array the HTML attributes of the brand link.
  75. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  76. */
  77. public $brandOptions = [];
  78. /**
  79. * @var string HTML content to be added in navbar-header div, for example, mobile search form.
  80. * @since 2.0.8
  81. */
  82. public $headerContent;
  83. /**
  84. * @var string text to show for screen readers for the button to toggle the navbar.
  85. */
  86. public $screenReaderToggleText = 'Toggle navigation';
  87. /**
  88. * @var bool whether the navbar content should be included in an inner div container which by default
  89. * adds left and right padding. Set this to false for a 100% width navbar.
  90. */
  91. public $renderInnerContainer = true;
  92. /**
  93. * @var array the HTML attributes of the inner container.
  94. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  95. */
  96. public $innerContainerOptions = [];
  97. /**
  98. * Initializes the widget.
  99. */
  100. public function init()
  101. {
  102. parent::init();
  103. $this->clientOptions = false;
  104. if (empty($this->options['class'])) {
  105. Html::addCssClass($this->options, ['navbar', 'navbar-default']);
  106. } else {
  107. Html::addCssClass($this->options, ['widget' => 'navbar']);
  108. }
  109. $options = $this->options;
  110. $tag = ArrayHelper::remove($options, 'tag', 'nav');
  111. echo Html::beginTag($tag, $options);
  112. if ($this->renderInnerContainer) {
  113. if (!isset($this->innerContainerOptions['class'])) {
  114. Html::addCssClass($this->innerContainerOptions, 'container');
  115. }
  116. echo Html::beginTag('div', $this->innerContainerOptions);
  117. }
  118. echo Html::beginTag('div', ['class' => 'navbar-header']);
  119. if (!isset($this->containerOptions['id'])) {
  120. $this->containerOptions['id'] = "{$this->options['id']}-collapse";
  121. }
  122. echo $this->renderToggleButton();
  123. if ($this->brandImage !== false) {
  124. $this->brandLabel = Html::img($this->brandImage);
  125. }
  126. if ($this->brandLabel !== false) {
  127. Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']);
  128. echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
  129. }
  130. echo $this->headerContent;
  131. echo Html::endTag('div');
  132. Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']);
  133. $options = $this->containerOptions;
  134. $tag = ArrayHelper::remove($options, 'tag', 'div');
  135. echo Html::beginTag($tag, $options);
  136. }
  137. /**
  138. * Renders the widget.
  139. */
  140. public function run()
  141. {
  142. $tag = ArrayHelper::remove($this->containerOptions, 'tag', 'div');
  143. echo Html::endTag($tag);
  144. if ($this->renderInnerContainer) {
  145. echo Html::endTag('div');
  146. }
  147. $tag = ArrayHelper::remove($this->options, 'tag', 'nav');
  148. echo Html::endTag($tag);
  149. BootstrapPluginAsset::register($this->getView());
  150. }
  151. /**
  152. * Renders collapsible toggle button.
  153. * @return string the rendering toggle button.
  154. */
  155. protected function renderToggleButton()
  156. {
  157. $bar = Html::tag('span', '', ['class' => 'icon-bar']);
  158. $screenReader = "<span class=\"sr-only\">{$this->screenReaderToggleText}</span>";
  159. return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
  160. 'class' => 'navbar-toggle',
  161. 'data-toggle' => 'collapse',
  162. 'data-target' => "#{$this->containerOptions['id']}",
  163. ]);
  164. }
  165. }