Browse Source

refactoring button classes.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
a93f75068e
  1. 10
      framework/yii/bootstrap/Button.php
  2. 25
      framework/yii/bootstrap/ButtonDropdown.php
  3. 66
      framework/yii/bootstrap/ButtonGroup.php

10
framework/yii/bootstrap/Button.php

@ -6,7 +6,6 @@
*/
namespace yii\bootstrap;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
@ -35,7 +34,7 @@ class Button extends Widget
/**
* @var string the button label
*/
public $label;
public $label = 'Button';
/**
* @var boolean whether the label should be HTML-encoded.
*/
@ -45,17 +44,12 @@ class Button extends Widget
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
* @throws InvalidConfigException
*/
public function init()
{
if ($this->label === null) {
throw new InvalidConfigException("The 'label' option is required.");
}
parent::init();
$this->clientOptions = false;
$this->addCssClass($this->options, 'btn');
$this->label = $this->encodeLabel ? Html::encode($this->label) : $this->label;
}
/**
@ -63,7 +57,7 @@ class Button extends Widget
*/
public function run()
{
echo Html::tag($this->tagName, $this->label, $this->options) . "\n";
echo Html::tag($this->tagName, $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->options);
$this->registerPlugin('button');
}
}

25
framework/yii/bootstrap/ButtonDropdown.php

@ -6,8 +6,6 @@
*/
namespace yii\bootstrap;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
@ -61,13 +59,14 @@ class ButtonDropdown extends Widget
/**
* @var string the button label
*/
public $label;
public $label = 'Button';
/**
* @var array the HTML attributes of the button.
*/
public $buttonOptions = array();
/**
* @var array list of menu items in the dropdown. Each array element represents a single
* @var array list of menu items in the dropdown. This will be used to
* set the [[Dropdown::items]] property. Each array element represents a single
* menu with the following structure:
*
* - label: string, required, the label of the item link
@ -81,7 +80,7 @@ class ButtonDropdown extends Widget
*/
public $items = array();
/**
* @var boolean whether to display a group or split styled button group.
* @var boolean whether to display a group of split-styled button group.
*/
public $split = false;
/**
@ -93,13 +92,9 @@ class ButtonDropdown extends Widget
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
* @throws InvalidConfigException
*/
public function init()
{
if ($this->label === null) {
throw new InvalidConfigException("The 'label' option is required.");
}
parent::init();
$this->addCssClass($this->options, 'btn-group');
}
@ -111,7 +106,7 @@ class ButtonDropdown extends Widget
{
echo Html::beginTag('div', $this->options) . "\n";
echo $this->renderButton() . "\n";
echo $this->renderItems() . "\n";
echo $this->renderDropdown() . "\n";
echo Html::endTag('div') . "\n";
$this->registerPlugin('button');
}
@ -123,7 +118,6 @@ class ButtonDropdown extends Widget
protected function renderButton()
{
$this->addCssClass($this->buttonOptions, 'btn');
$splitButton = '';
if ($this->split) {
$tag = 'button';
$options = $this->buttonOptions;
@ -133,8 +127,7 @@ class ButtonDropdown extends Widget
'label' => '<span class="caret"></span>',
'encodeLabel' => false,
'options' => $this->buttonOptions,
)
);
));
} else {
$tag = 'a';
$this->label .= ' <span class="caret"></span>';
@ -144,6 +137,7 @@ class ButtonDropdown extends Widget
}
$this->addCssClass($options, 'dropdown-toggle');
$options['data-toggle'] = 'dropdown';
$splitButton = '';
}
return Button::widget(array(
'tagName' => $tag,
@ -157,11 +151,8 @@ class ButtonDropdown extends Widget
* Generates the dropdown menu as specified on [[items]].
* @return string the rendering result.
*/
protected function renderItems()
protected function renderDropdown()
{
if (is_string($this->items)) {
return $this->items;
}
$config = array('items' => $this->items, 'clientOptions' => false);
return Dropdown::widget($config);
}

66
framework/yii/bootstrap/ButtonGroup.php

@ -20,23 +20,18 @@ use yii\helpers\Html;
* // a button group with items configuration
* echo ButtonGroup::::widget(array(
* 'items' => array(
* array('label'=>'A'),
* array('label'=>'B'),
* array('label' => 'A'),
* array('label' => 'B'),
* )
* ));
*
* // button group with an item as a string
* echo ButtonGroup::::widget(array(
* 'items' => array(
* Button::widget(array('label'=>'A')),
* array('label'=>'B'),
* Button::widget(array('label' => 'A')),
* array('label' => 'B'),
* )
* ));
*
* // button group with body content as string
* ButtonGroup::beging();
* Button::widget(array('label'=>'A')), // you can also use plain string
* ButtonGroup::end();
* ```
* @see http://twitter.github.io/bootstrap/javascript.html#buttons
* @see http://twitter.github.io/bootstrap/components.html#buttonGroups
@ -46,17 +41,15 @@ use yii\helpers\Html;
class ButtonGroup extends Widget
{
/**
* @var array list of buttons. Each array element represents a single
* menu with the following structure:
* @var array list of buttons. Each array element represents a single button
* which can be specified as a string or an array of the following structure:
*
* - label: string, required, the button label.
* - options: array, optional, the HTML attributes of the button.
*
* Optionally, you can also set each item as a string, or even the [[items]] attribute.
*/
public $items = array();
public $buttons = array();
/**
* @var boolean whether the labels for dropdown items should be HTML-encoded.
* @var boolean whether to HTML-encode the button labels.
*/
public $encodeLabels = true;
@ -70,7 +63,6 @@ class ButtonGroup extends Widget
parent::init();
$this->clientOptions = false;
$this->addCssClass($this->options, 'btn-group');
echo $this->renderGroupBegin() . "\n";
}
/**
@ -78,51 +70,29 @@ class ButtonGroup extends Widget
*/
public function run()
{
echo "\n" . $this->renderGroupEnd();
echo Html::tag('div', $this->renderButtons(), $this->options);
$this->registerPlugin('button');
}
/**
* Renders the opening tag of the button group.
* @return string the rendering result.
*/
protected function renderGroupBegin()
{
return Html::beginTag('div', $this->options);
}
/**
* Renders the items and closing tag of the button group.
* @return string the rendering result.
*/
protected function renderGroupEnd()
{
return $this->renderItems() . "\n" . Html::endTag('div');
}
/**
* Generates the buttons that compound the group as specified on [[items]].
* @return string the rendering result.
*/
protected function renderItems()
protected function renderButtons()
{
if (is_string($this->items)) {
return $this->items;
}
$buttons = array();
foreach ($this->items as $item) {
if (is_string($item)) {
$buttons[] = $item;
continue;
}
$label = ArrayHelper::getValue($item, 'label');
$options = ArrayHelper::getValue($item, 'options');
foreach ($this->buttons as $button) {
if (is_array($button)) {
$label = ArrayHelper::getValue($button, 'label');
$options = ArrayHelper::getValue($button, 'options');
$buttons[] = Button::widget(array(
'label' => $label,
'options' => $options,
'encodeLabel' => $this->encodeLabels
)
);
));
} else {
$buttons[] = $button;
}
}
return implode("\n", $buttons);
}

Loading…
Cancel
Save