From aeb568bee26f3d257756f5f00ee14833dc0a169b Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 9 Aug 2013 10:55:16 -0400 Subject: [PATCH] refactored NavBar and basic app navbar. --- apps/basic/views/layouts/main.php | 40 +++++----- framework/yii/bootstrap/NavBar.php | 145 +++++++++---------------------------- 2 files changed, 56 insertions(+), 129 deletions(-) diff --git a/apps/basic/views/layouts/main.php b/apps/basic/views/layouts/main.php index 1f8611c..c5ad569 100644 --- a/apps/basic/views/layouts/main.php +++ b/apps/basic/views/layouts/main.php @@ -1,4 +1,6 @@ beginBody(); ?> - + 'My Company', + 'brandUrl' => Yii::$app->homeUrl, + 'options' => array( + 'class' => 'navbar-inverse navbar-fixed-top', + ), + )); + echo Menu::widget(array( + 'options' => array('class' => 'nav navbar-nav pull-right'), + 'items' => array( + array('label' => 'Home', 'url' => array('/site/index')), + array('label' => 'About', 'url' => array('/site/about')), + array('label' => 'Contact', 'url' => array('/site/contact')), + Yii::$app->user->isGuest ? + array('label' => 'Login', 'url' => array('/site/login')) : + array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')), + ))); + NavBar::end(); + ?>
'NavBar Test', + * use yii\bootstrap\NavBar; + * use yii\widgets\Menu; + * + * NavBar::begin(array('brandLabel' => 'NavBar Test')); + * echo Menu::widget(array( * 'items' => array( - * // a Nav widget - * array( - * // defaults to Nav anyway. - * 'class' => 'yii\bootstrap\Nav', - * // widget configuration - * 'options' => array( - * 'items' => array( - * array( - * 'label' => 'Home', - * 'url' => '/', - * 'options' => array('class' => 'active'), - * ), - * array( - * 'label' => 'Dropdown', - * 'content' => new Dropdown(array( - * 'items' => array( - * array( - * 'label' => 'DropdownA', - * 'url' => '#', - * ), - * array( - * 'label' => 'DropdownB', - * 'url' => '#' - * ), - * ) - * ), - * ), - * ) - * ), - * ), - * // you can also use strings - * '', + * array('label' => 'Home', 'url' => array('/site/index')), + * array('label' => 'About', 'url' => array('/site/about')), * ), * )); + * NavBar::end(); * ``` * * @see http://twitter.github.io/bootstrap/components.html#navbar @@ -65,6 +37,10 @@ use yii\helpers\Html; class NavBar extends Widget { /** + * @var boolean whether to enable a collapsing responsive navbar. + */ + public $responsive = true; + /** * @var string the text of the brand. * @see http://twitter.github.io/bootstrap/components.html#navbar */ @@ -78,26 +54,6 @@ class NavBar extends Widget * @var array the HTML attributes of the brand link. */ public $brandOptions = array(); - /** - * @var array list of menu items in the navbar widget. Each array element represents a single - * menu item with the following structure: - * - * ```php - * array( - * // optional, the menu item class type of the widget to render. Defaults to "Nav" widget. - * 'class' => 'Menu item class type', - * // required, the configuration options of the widget. - * 'options' => array(...), - * ), - * // optionally, you can pass a string - * '', - * ``` - * - * Optionally, you can also use a plain string instead of an array element. - */ - public $items = array(); /** @@ -108,60 +64,30 @@ class NavBar extends Widget parent::init(); $this->clientOptions = false; Html::addCssClass($this->options, 'navbar'); - Html::addCssClass($this->brandOptions, 'brand'); - } + Html::addCssClass($this->brandOptions, 'navbar-brand'); - /** - * Renders the widget. - */ - public function run() - { echo Html::beginTag('div', $this->options); - echo $this->renderItems(); - echo Html::endTag('div'); - BootstrapPluginAsset::register($this->getView()); - } - - /** - * Renders the items. - * @return string the rendering items. - */ - protected function renderItems() - { - $items = array(); - foreach ($this->items as $item) { - $items[] = $this->renderItem($item); + if ($this->responsive) { + echo Html::beginTag('div', array('class' => 'container')); + echo $this->renderToggleButton(); + echo Html::beginTag('div', array('class' => 'nav-collapse collapse navbar-responsive-collapse')); + } + if ($this->brandLabel !== null) { + echo Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions); } - $contents = implode("\n", $items); - $brand = Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions); - - $contents = Html::tag('div', - $this->renderToggleButton() . - $brand . "\n" . - Html::tag('div', $contents, array('class' => 'nav-collapse collapse navbar-collapse')), - array('class' => 'container')); - - return Html::tag('div', $contents, array('class' => 'navbar-inner')); } /** - * Renders a item. The item can be a string, a custom class or a Nav widget (defaults if no class specified. - * @param mixed $item the item to render. If array, it is assumed the configuration of a widget being `class` - * required and if not specified, then defaults to `yii\bootstrap\Nav`. - * @return string the rendering result. - * @throws InvalidConfigException + * Renders the widget. */ - protected function renderItem($item) + public function run() { - if (is_string($item)) { - return $item; + if ($this->responsive) { + echo Html::endTag('div'); + echo Html::endTag('div'); } - $config = ArrayHelper::getValue($item, 'options', array()); - $config['clientOptions'] = false; - - $class = ArrayHelper::getValue($item, 'class', 'yii\bootstrap\Nav'); - - return $class::widget($config); + echo Html::endTag('div'); + BootstrapPluginAsset::register($this->getView()); } /** @@ -170,14 +96,11 @@ class NavBar extends Widget */ protected function renderToggleButton() { - $items = array(); - for ($i = 0; $i < 3; $i++) { - $items[] = Html::tag('span', '', array('class' => 'icon-bar')); - } - return Html::a(implode("\n", $items), null, array( - 'class' => 'btn btn-navbar', + $bar = Html::tag('span', '', array('class' => 'icon-bar')); + return Html::button("{$bar}\n{$bar}\n{$bar}", array( + 'class' => 'navbar-toggle', 'data-toggle' => 'collapse', - 'data-target' => 'div.navbar-collapse', + 'data-target' => '.navbar-responsive-collapse', )); } }