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

25
framework/yii/bootstrap/ButtonDropdown.php

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

66
framework/yii/bootstrap/ButtonGroup.php

@ -20,23 +20,18 @@ use yii\helpers\Html;
* // a button group with items configuration * // a button group with items configuration
* echo ButtonGroup::::widget(array( * echo ButtonGroup::::widget(array(
* 'items' => array( * 'items' => array(
* array('label'=>'A'), * array('label' => 'A'),
* array('label'=>'B'), * array('label' => 'B'),
* ) * )
* )); * ));
* *
* // button group with an item as a string * // button group with an item as a string
* echo ButtonGroup::::widget(array( * echo ButtonGroup::::widget(array(
* 'items' => array( * 'items' => array(
* Button::widget(array('label'=>'A')), * Button::widget(array('label' => 'A')),
* array('label'=>'B'), * 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/javascript.html#buttons
* @see http://twitter.github.io/bootstrap/components.html#buttonGroups * @see http://twitter.github.io/bootstrap/components.html#buttonGroups
@ -46,17 +41,15 @@ use yii\helpers\Html;
class ButtonGroup extends Widget class ButtonGroup extends Widget
{ {
/** /**
* @var array list of buttons. Each array element represents a single * @var array list of buttons. Each array element represents a single button
* menu with the following structure: * which can be specified as a string or an array of the following structure:
* *
* - label: string, required, the button label. * - label: string, required, the button label.
* - options: array, optional, the HTML attributes of the button. * - 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; public $encodeLabels = true;
@ -70,7 +63,6 @@ class ButtonGroup extends Widget
parent::init(); parent::init();
$this->clientOptions = false; $this->clientOptions = false;
$this->addCssClass($this->options, 'btn-group'); $this->addCssClass($this->options, 'btn-group');
echo $this->renderGroupBegin() . "\n";
} }
/** /**
@ -78,51 +70,29 @@ class ButtonGroup extends Widget
*/ */
public function run() public function run()
{ {
echo "\n" . $this->renderGroupEnd(); echo Html::tag('div', $this->renderButtons(), $this->options);
$this->registerPlugin('button'); $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]]. * Generates the buttons that compound the group as specified on [[items]].
* @return string the rendering result. * @return string the rendering result.
*/ */
protected function renderItems() protected function renderButtons()
{ {
if (is_string($this->items)) {
return $this->items;
}
$buttons = array(); $buttons = array();
foreach ($this->items as $item) { foreach ($this->buttons as $button) {
if (is_string($item)) { if (is_array($button)) {
$buttons[] = $item; $label = ArrayHelper::getValue($button, 'label');
continue; $options = ArrayHelper::getValue($button, 'options');
}
$label = ArrayHelper::getValue($item, 'label');
$options = ArrayHelper::getValue($item, 'options');
$buttons[] = Button::widget(array( $buttons[] = Button::widget(array(
'label' => $label, 'label' => $label,
'options' => $options, 'options' => $options,
'encodeLabel' => $this->encodeLabels 'encodeLabel' => $this->encodeLabels
) ));
); } else {
$buttons[] = $button;
}
} }
return implode("\n", $buttons); return implode("\n", $buttons);
} }

Loading…
Cancel
Save