Browse Source

refactored NavBar and basic app navbar.

tags/2.0.0-beta
Qiang Xue 12 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
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\helpers\Html;
use yii\widgets\Menu;
use yii\widgets\Breadcrumbs;
@ -19,24 +21,26 @@ app\config\AppAsset::register($this);
</head>
<body>
<?php $this->beginBody(); ?>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<?php echo Html::a('My Company', Yii::$app->homeUrl, array('class' => 'navbar-brand')); ?>
<div class="nav-collapse collapse pull-right">
<?php echo Menu::widget(array(
'options' => array('class' => 'nav navbar-nav'),
'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')),
),
)); ?>
</div>
</div>
</div>
<?php
NavBar::begin(array(
'brandLabel' => '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();
?>
<div class="container">
<?php echo Breadcrumbs::widget(array(

145
framework/yii/bootstrap/NavBar.php

@ -7,55 +7,27 @@
namespace yii\bootstrap;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* 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
* echo NavBar::widget(array(
* 'brandLabel' => '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
* '<form class="navbar-search pull-left" action="">' .
* '<input type="text" class="search-query" placeholder="Search">' .
* '</form>',
* 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
* '<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();
$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',
));
}
}

Loading…
Cancel
Save