Browse Source

refactored NavBar and basic app navbar.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
aeb568bee2
  1. 40
      apps/basic/views/layouts/main.php
  2. 145
      framework/yii/bootstrap/NavBar.php

40
apps/basic/views/layouts/main.php

@ -1,4 +1,6 @@
<?php <?php
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\helpers\Html; use yii\helpers\Html;
use yii\widgets\Menu; use yii\widgets\Menu;
use yii\widgets\Breadcrumbs; use yii\widgets\Breadcrumbs;
@ -19,24 +21,26 @@ app\config\AppAsset::register($this);
</head> </head>
<body> <body>
<?php $this->beginBody(); ?> <?php $this->beginBody(); ?>
<div class="navbar navbar-inverse navbar-fixed-top"> <?php
<div class="container"> NavBar::begin(array(
<?php echo Html::a('My Company', Yii::$app->homeUrl, array('class' => 'navbar-brand')); ?> 'brandLabel' => 'My Company',
<div class="nav-collapse collapse pull-right"> 'brandUrl' => Yii::$app->homeUrl,
<?php echo Menu::widget(array( 'options' => array(
'options' => array('class' => 'nav navbar-nav'), 'class' => 'navbar-inverse navbar-fixed-top',
'items' => array( ),
array('label' => 'Home', 'url' => array('/site/index')), ));
array('label' => 'About', 'url' => array('/site/about')), echo Menu::widget(array(
array('label' => 'Contact', 'url' => array('/site/contact')), 'options' => array('class' => 'nav navbar-nav pull-right'),
Yii::$app->user->isGuest ? 'items' => array(
array('label' => 'Login', 'url' => array('/site/login')) : array('label' => 'Home', 'url' => array('/site/index')),
array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')), array('label' => 'About', 'url' => array('/site/about')),
), array('label' => 'Contact', 'url' => array('/site/contact')),
)); ?> Yii::$app->user->isGuest ?
</div> array('label' => 'Login', 'url' => array('/site/login')) :
</div> array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')),
</div> )));
NavBar::end();
?>
<div class="container"> <div class="container">
<?php echo Breadcrumbs::widget(array( <?php echo Breadcrumbs::widget(array(

145
framework/yii/bootstrap/NavBar.php

@ -7,55 +7,27 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html; use yii\helpers\Html;
/** /**
* NavBar renders a navbar HTML component. * NavBar renders a navbar HTML component.
* *
* For example: * 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 * ```php
* echo NavBar::widget(array( * use yii\bootstrap\NavBar;
* 'brandLabel' => 'NavBar Test', * use yii\widgets\Menu;
*
* NavBar::begin(array('brandLabel' => 'NavBar Test'));
* echo Menu::widget(array(
* 'items' => array( * 'items' => array(
* // a Nav widget * array('label' => 'Home', 'url' => array('/site/index')),
* array( * array('label' => 'About', 'url' => array('/site/about')),
* // 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
* '<form class="navbar-search pull-left" action="">' .
* '<input type="text" class="search-query" placeholder="Search">' .
* '</form>',
* ), * ),
* )); * ));
* NavBar::end();
* ``` * ```
* *
* @see http://twitter.github.io/bootstrap/components.html#navbar * @see http://twitter.github.io/bootstrap/components.html#navbar
@ -65,6 +37,10 @@ use yii\helpers\Html;
class NavBar extends Widget class NavBar extends Widget
{ {
/** /**
* @var boolean whether to enable a collapsing responsive navbar.
*/
public $responsive = true;
/**
* @var string the text of the brand. * @var string the text of the brand.
* @see http://twitter.github.io/bootstrap/components.html#navbar * @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. * @var array the HTML attributes of the brand link.
*/ */
public $brandOptions = array(); 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
* '<form class="navbar-search pull-left" action="">' .
* '<input type="text" class="search-query span2" placeholder="Search">' .
* '</form>',
* ```
*
* 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(); parent::init();
$this->clientOptions = false; $this->clientOptions = false;
Html::addCssClass($this->options, 'navbar'); 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 Html::beginTag('div', $this->options);
echo $this->renderItems(); if ($this->responsive) {
echo Html::endTag('div'); echo Html::beginTag('div', array('class' => 'container'));
BootstrapPluginAsset::register($this->getView()); echo $this->renderToggleButton();
} echo Html::beginTag('div', array('class' => 'nav-collapse collapse navbar-responsive-collapse'));
}
/** if ($this->brandLabel !== null) {
* Renders the items. echo Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);
* @return string the rendering items.
*/
protected function renderItems()
{
$items = array();
foreach ($this->items as $item) {
$items[] = $this->renderItem($item);
} }
$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. * Renders the widget.
* @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
*/ */
protected function renderItem($item) public function run()
{ {
if (is_string($item)) { if ($this->responsive) {
return $item; echo Html::endTag('div');
echo Html::endTag('div');
} }
$config = ArrayHelper::getValue($item, 'options', array()); echo Html::endTag('div');
$config['clientOptions'] = false; BootstrapPluginAsset::register($this->getView());
$class = ArrayHelper::getValue($item, 'class', 'yii\bootstrap\Nav');
return $class::widget($config);
} }
/** /**
@ -170,14 +96,11 @@ class NavBar extends Widget
*/ */
protected function renderToggleButton() protected function renderToggleButton()
{ {
$items = array(); $bar = Html::tag('span', '', array('class' => 'icon-bar'));
for ($i = 0; $i < 3; $i++) { return Html::button("{$bar}\n{$bar}\n{$bar}", array(
$items[] = Html::tag('span', '', array('class' => 'icon-bar')); 'class' => 'navbar-toggle',
}
return Html::a(implode("\n", $items), null, array(
'class' => 'btn btn-navbar',
'data-toggle' => 'collapse', 'data-toggle' => 'collapse',
'data-target' => 'div.navbar-collapse', 'data-target' => '.navbar-responsive-collapse',
)); ));
} }
} }

Loading…
Cancel
Save