Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

162 lines
5.8KB

  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 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|boolean the text of the brand or false if it's not used. Note that this is not HTML-encoded.
  57. * @see http://getbootstrap.com/components/#navbar
  58. */
  59. public $brandLabel = false;
  60. /**
  61. * @var array|string|boolean $url the URL for the brand's hyperlink tag. This parameter will be processed by [[\yii\helpers\Url::to()]]
  62. * and will be used for the "href" attribute of the brand link. Default value is false that means
  63. * [[\yii\web\Application::homeUrl]] will be used.
  64. * You may set it to `null` if you want to have no link at all.
  65. */
  66. public $brandUrl = false;
  67. /**
  68. * @var array the HTML attributes of the brand link.
  69. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  70. */
  71. public $brandOptions = [];
  72. /**
  73. * @var string text to show for screen readers for the button to toggle the navbar.
  74. */
  75. public $screenReaderToggleText = 'Toggle navigation';
  76. /**
  77. * @var boolean whether the navbar content should be included in an inner div container which by default
  78. * adds left and right padding. Set this to false for a 100% width navbar.
  79. */
  80. public $renderInnerContainer = true;
  81. /**
  82. * @var array the HTML attributes of the inner container.
  83. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  84. */
  85. public $innerContainerOptions = [];
  86. /**
  87. * Initializes the widget.
  88. */
  89. public function init()
  90. {
  91. parent::init();
  92. $this->clientOptions = false;
  93. if (empty($this->options['class'])) {
  94. Html::addCssClass($this->options, ['navbar', 'navbar-default']);
  95. } else {
  96. Html::addCssClass($this->options, ['widget' => 'navbar']);
  97. }
  98. if (empty($this->options['role'])) {
  99. $this->options['role'] = 'navigation';
  100. }
  101. $options = $this->options;
  102. $tag = ArrayHelper::remove($options, 'tag', 'nav');
  103. echo Html::beginTag($tag, $options);
  104. if ($this->renderInnerContainer) {
  105. if (!isset($this->innerContainerOptions['class'])) {
  106. Html::addCssClass($this->innerContainerOptions, 'container');
  107. }
  108. echo Html::beginTag('div', $this->innerContainerOptions);
  109. }
  110. echo Html::beginTag('div', ['class' => 'navbar-header']);
  111. if (!isset($this->containerOptions['id'])) {
  112. $this->containerOptions['id'] = "{$this->options['id']}-collapse";
  113. }
  114. echo $this->renderToggleButton();
  115. if ($this->brandLabel !== false) {
  116. Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']);
  117. echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
  118. }
  119. echo Html::endTag('div');
  120. Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']);
  121. $options = $this->containerOptions;
  122. $tag = ArrayHelper::remove($options, 'tag', 'div');
  123. echo Html::beginTag($tag, $options);
  124. }
  125. /**
  126. * Renders the widget.
  127. */
  128. public function run()
  129. {
  130. $tag = ArrayHelper::remove($this->containerOptions, 'tag', 'div');
  131. echo Html::endTag($tag);
  132. if ($this->renderInnerContainer) {
  133. echo Html::endTag('div');
  134. }
  135. $tag = ArrayHelper::remove($this->options, 'tag', 'nav');
  136. echo Html::endTag($tag);
  137. BootstrapPluginAsset::register($this->getView());
  138. }
  139. /**
  140. * Renders collapsible toggle button.
  141. * @return string the rendering toggle button.
  142. */
  143. protected function renderToggleButton()
  144. {
  145. $bar = Html::tag('span', '', ['class' => 'icon-bar']);
  146. $screenReader = "<span class=\"sr-only\">{$this->screenReaderToggleText}</span>";
  147. return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
  148. 'class' => 'navbar-toggle',
  149. 'data-toggle' => 'collapse',
  150. 'data-target' => "#{$this->containerOptions['id']}",
  151. ]);
  152. }
  153. }