You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 satır
5.7KB

  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|boolean the text of the brand of 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. * @param array|string|boolean $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. Default value is false that means
  63. * [[\yii\web\Application::homeUrl]] will be used.
  64. */
  65. public $brandUrl = false;
  66. /**
  67. * @var array the HTML attributes of the brand link.
  68. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  69. */
  70. public $brandOptions = [];
  71. /**
  72. * @var string text to show for screen readers for the button to toggle the navbar.
  73. */
  74. public $screenReaderToggleText = 'Toggle navigation';
  75. /**
  76. * @var boolean whether the navbar content should be included in an inner div container which by default
  77. * adds left and right padding. Set this to false for a 100% width navbar.
  78. */
  79. public $renderInnerContainer = true;
  80. /**
  81. * @var array the HTML attributes of the inner container.
  82. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  83. */
  84. public $innerContainerOptions = [];
  85. /**
  86. * Initializes the widget.
  87. */
  88. public function init()
  89. {
  90. parent::init();
  91. $this->clientOptions = false;
  92. Html::addCssClass($this->options, 'navbar');
  93. if ($this->options['class'] === 'navbar') {
  94. Html::addCssClass($this->options, 'navbar-default');
  95. }
  96. Html::addCssClass($this->brandOptions, 'navbar-brand');
  97. if (empty($this->options['role'])) {
  98. $this->options['role'] = 'navigation';
  99. }
  100. $options = $this->options;
  101. $tag = ArrayHelper::remove($options, 'tag', 'nav');
  102. echo Html::beginTag($tag, $options);
  103. if ($this->renderInnerContainer) {
  104. if (!isset($this->innerContainerOptions['class'])) {
  105. Html::addCssClass($this->innerContainerOptions, 'container');
  106. }
  107. echo Html::beginTag('div', $this->innerContainerOptions);
  108. }
  109. echo Html::beginTag('div', ['class' => 'navbar-header']);
  110. if (!isset($this->containerOptions['id'])) {
  111. $this->containerOptions['id'] = "{$this->options['id']}-collapse";
  112. }
  113. echo $this->renderToggleButton();
  114. if ($this->brandLabel !== false) {
  115. Html::addCssClass($this->brandOptions, 'navbar-brand');
  116. echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
  117. }
  118. echo Html::endTag('div');
  119. Html::addCssClass($this->containerOptions, 'collapse');
  120. Html::addCssClass($this->containerOptions, '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, $this->options);
  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. }