|
- <?php
-
-
- namespace yii\bootstrap;
-
- use Yii;
- use yii\helpers\ArrayHelper;
-
-
- class NavBar extends Widget
- {
-
-
- public $options = [];
-
-
- public $containerOptions = [];
-
-
- public $brandLabel = false;
-
-
- public $brandUrl = false;
-
-
- public $brandOptions = [];
-
-
- public $screenReaderToggleText = 'Toggle navigation';
-
-
- public $renderInnerContainer = true;
-
-
- public $innerContainerOptions = [];
-
-
-
-
- public function init()
- {
- parent::init();
- $this->clientOptions = false;
- if (empty($this->options['class'])) {
- Html::addCssClass($this->options, ['navbar', 'navbar-default']);
- } else {
- Html::addCssClass($this->options, ['widget' => 'navbar']);
- }
- 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) {
- Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']);
- echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
- }
- echo Html::endTag('div');
- Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']);
- $options = $this->containerOptions;
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- echo Html::beginTag($tag, $options);
- }
-
-
-
- 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);
- BootstrapPluginAsset::register($this->getView());
- }
-
-
-
- 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']}",
- ]);
- }
- }
|