Commit 3ea779af by niskac

improved BootButton and BootButtonGroup

parent 2c3e692c
...@@ -12,8 +12,10 @@ Yii::import('bootstrap.widgets.BootWidget'); ...@@ -12,8 +12,10 @@ Yii::import('bootstrap.widgets.BootWidget');
class BootButton extends BootWidget class BootButton extends BootWidget
{ {
const TAG_LINK = 'a'; const METHOD_LINK = 'link';
const TAG_BUTTON = 'button'; const METHOD_BUTTON = 'button';
const METHOD_AJAXLINK = 'ajaxLink';
const METHOD_AJAXBUTTON = 'ajaxButton';
const TYPE_NORMAL = ''; const TYPE_NORMAL = '';
const TYPE_PRIMARY = 'primary'; const TYPE_PRIMARY = 'primary';
...@@ -27,17 +29,19 @@ class BootButton extends BootWidget ...@@ -27,17 +29,19 @@ class BootButton extends BootWidget
const SIZE_NORMAL = ''; const SIZE_NORMAL = '';
const SIZE_LARGE = 'large'; const SIZE_LARGE = 'large';
public $tag = self::TAG_LINK; public $method = self::METHOD_LINK;
public $type = self::TYPE_NORMAL; public $type = self::TYPE_NORMAL;
public $size = self::SIZE_NORMAL; public $size = self::SIZE_NORMAL;
public $icon; public $icon;
public $label; public $label;
public $url; public $url;
public $active = false;
public $items; public $items;
public $toggle; public $toggle;
public $loadingText; public $loadingText;
public $completeText; public $completeText;
public $encodeLabel = true; public $encodeLabel = true;
public $ajaxOptions = array();
/** /**
* Initializes the widget. * Initializes the widget.
...@@ -57,11 +61,18 @@ class BootButton extends BootWidget ...@@ -57,11 +61,18 @@ class BootButton extends BootWidget
if (isset($this->size) && in_array($this->size, $validSizes)) if (isset($this->size) && in_array($this->size, $validSizes))
$class[] = 'btn-'.$this->size; $class[] = 'btn-'.$this->size;
if ($this->active) {
$class[] = 'active';
}
if ($this->encodeLabel)
$this->label = CHtml::encode($this->label);
if ($this->hasDropdown()) if ($this->hasDropdown())
{ {
$class[] = 'dropdown-toggle'; $class[] = 'dropdown-toggle';
$this->label .= ' <span class="caret"></span>';
$this->htmlOptions['data-toggle'] = 'dropdown'; $this->htmlOptions['data-toggle'] = 'dropdown';
Yii::app()->bootstrap->registerDropdown(); Yii::app()->bootstrap->registerDropdown();
} }
...@@ -72,9 +83,6 @@ class BootButton extends BootWidget ...@@ -72,9 +83,6 @@ class BootButton extends BootWidget
else else
$this->htmlOptions['class'] = $cssClass; $this->htmlOptions['class'] = $cssClass;
if ($this->encodeLabel)
$this->label = CHtml::encode($this->label);
if (isset($this->icon)) if (isset($this->icon))
{ {
if (strpos($this->icon, 'icon') === false) if (strpos($this->icon, 'icon') === false)
...@@ -83,9 +91,14 @@ class BootButton extends BootWidget ...@@ -83,9 +91,14 @@ class BootButton extends BootWidget
$this->label = '<i class="'.$this->icon.'"></i> '.$this->label; $this->label = '<i class="'.$this->icon.'"></i> '.$this->label;
} }
if (!isset($this->url)) $this->initHTML5Data();
$this->url = '#'; }
/**
* Initializes the HTML5 data attributes used by the data-api.
*/
protected function initHTML5Data()
{
if (isset($this->toggle) || isset($this->loadingText) || isset($this->completeText)) if (isset($this->toggle) || isset($this->loadingText) || isset($this->completeText))
{ {
if (isset($this->toggle)) if (isset($this->toggle))
...@@ -106,22 +119,32 @@ class BootButton extends BootWidget ...@@ -106,22 +119,32 @@ class BootButton extends BootWidget
*/ */
public function run() public function run()
{ {
if ($this->tag === self::TAG_LINK) echo $this->createButton();
$this->htmlOptions['href'] = $this->url;
echo CHtml::openTag($this->tag, $this->htmlOptions);
echo $this->label;
if ($this->hasDropdown()) if ($this->hasDropdown())
echo ' <span class="caret"></span>'; $this->controller->widget('bootstrap.widgets.BootDropdown', array('items'=>$this->items));
}
echo CHtml::closeTag($this->tag);
if ($this->hasDropdown()) /**
* Creates the button element.
* @return string the created button.
*/
protected function createButton()
{
switch ($this->method)
{ {
$this->controller->widget('bootstrap.widgets.BootDropdown', array( case self::METHOD_BUTTON:
'items'=>$this->items, return CHtml::htmlButton($this->label, $this->htmlOptions);
));
case self::METHOD_AJAXLINK:
return CHtml::ajaxLink($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions);
case self::METHOD_AJAXBUTTON:
return CHtml::ajaxButton($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions);
default:
case self::METHOD_LINK:
return CHtml::link($this->label, $this->url, $this->htmlOptions);
} }
} }
......
...@@ -16,7 +16,7 @@ class BootButtonGroup extends BootWidget ...@@ -16,7 +16,7 @@ class BootButtonGroup extends BootWidget
const TOGGLE_CHECKBOX = 'checkbox'; const TOGGLE_CHECKBOX = 'checkbox';
const TOGGLE_RADIO = 'radio'; const TOGGLE_RADIO = 'radio';
public $tag = BootButton::TAG_LINK; public $method = BootButton::METHOD_LINK;
public $type = BootButton::TYPE_NORMAL; public $type = BootButton::TYPE_NORMAL;
public $size = BootButton::SIZE_NORMAL; public $size = BootButton::SIZE_NORMAL;
public $encodeLabel = true; public $encodeLabel = true;
...@@ -47,13 +47,15 @@ class BootButtonGroup extends BootWidget ...@@ -47,13 +47,15 @@ class BootButtonGroup extends BootWidget
foreach ($this->buttons as $button) foreach ($this->buttons as $button)
{ {
$this->controller->widget('bootstrap.widgets.BootButton', array( $this->controller->widget('bootstrap.widgets.BootButton', array(
'tag'=>isset($button['tag']) ? $button['tag'] : $this->tag, 'method'=>isset($button['method']) ? $button['method'] : $this->method,
'type'=>$this->type, 'type'=>isset($button['type']) ? $button['type'] : $this->type,
'size'=>$this->size, 'size'=>$this->size,
'icon'=>isset($button['icon']) ? $button['icon'] : null, 'icon'=>isset($button['icon']) ? $button['icon'] : null,
'label'=>isset($button['label']) ? $button['label'] : null, 'label'=>isset($button['label']) ? $button['label'] : null,
'url'=>isset($button['url']) ? $button['url'] : null, 'url'=>isset($button['url']) ? $button['url'] : null,
'active'=>isset($button['active']) ? $button['active'] : false,
'items'=>isset($button['items']) ? $button['items'] : array(), 'items'=>isset($button['items']) ? $button['items'] : array(),
'ajaxOptions'=>isset($button['ajaxOptions']) ? $button['ajaxOptions'] : array(),
'htmlOptions'=>isset($button['htmlOptions']) ? $button['htmlOptions'] : array(), 'htmlOptions'=>isset($button['htmlOptions']) ? $button['htmlOptions'] : array(),
'encodeLabel'=>isset($button['encodeLabel']) ? $button['encodeLabel'] : $this->encodeLabel, 'encodeLabel'=>isset($button['encodeLabel']) ? $button['encodeLabel'] : $this->encodeLabel,
)); ));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment