Blame view

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