You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.7 KiB
120 lines
2.7 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\bootstrap;
|
||
|
|
||
12 years ago
|
use yii\helpers\Html;
|
||
12 years ago
|
|
||
|
/**
|
||
|
* ButtonDropdown renders a group or split button dropdown bootstrap component.
|
||
|
*
|
||
|
* For example,
|
||
|
*
|
||
|
* ```php
|
||
|
* // a button group using Dropdown widget
|
||
11 years ago
|
* echo ButtonDropdown::widget([
|
||
12 years ago
|
* 'label' => 'Action',
|
||
11 years ago
|
* 'dropdown' => [
|
||
|
* 'items' => [
|
||
|
* ['label' => 'DropdownA', 'url' => '/'],
|
||
|
* ['label' => 'DropdownB', 'url' => '#'],
|
||
|
* ],
|
||
|
* ],
|
||
|
* ]);
|
||
12 years ago
|
* ```
|
||
|
* @see http://twitter.github.io/bootstrap/javascript.html#buttons
|
||
|
* @see http://twitter.github.io/bootstrap/components.html#buttonDropdowns
|
||
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class ButtonDropdown extends Widget
|
||
|
{
|
||
|
/**
|
||
|
* @var string the button label
|
||
|
*/
|
||
12 years ago
|
public $label = 'Button';
|
||
12 years ago
|
/**
|
||
|
* @var array the HTML attributes of the button.
|
||
|
*/
|
||
11 years ago
|
public $options = [];
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var array the configuration array for [[Dropdown]].
|
||
12 years ago
|
*/
|
||
11 years ago
|
public $dropdown = [];
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var boolean whether to display a group of split-styled button group.
|
||
12 years ago
|
*/
|
||
|
public $split = false;
|
||
11 years ago
|
/**
|
||
|
* @var string the tag to use to render the button
|
||
|
*/
|
||
|
public $tagName = 'button';
|
||
|
/**
|
||
|
* @var boolean whether the label should be HTML-encoded.
|
||
|
*/
|
||
|
public $encodeLabel = true;
|
||
12 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
11 years ago
|
echo $this->renderButton() . "\n" . $this->renderDropdown();
|
||
12 years ago
|
$this->registerPlugin('button');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Generates the button dropdown.
|
||
|
* @return string the rendering result.
|
||
|
*/
|
||
|
protected function renderButton()
|
||
|
{
|
||
11 years ago
|
Html::addCssClass($this->options, 'btn');
|
||
11 years ago
|
$label = $this->label;
|
||
|
if ($this->encodeLabel) {
|
||
|
$label = Html::encode($label);
|
||
|
}
|
||
12 years ago
|
if ($this->split) {
|
||
11 years ago
|
$options = $this->options;
|
||
|
$this->options['data-toggle'] = 'dropdown';
|
||
|
Html::addCssClass($this->options, 'dropdown-toggle');
|
||
11 years ago
|
$splitButton = Button::widget([
|
||
12 years ago
|
'label' => '<span class="caret"></span>',
|
||
|
'encodeLabel' => false,
|
||
11 years ago
|
'options' => $this->options,
|
||
11 years ago
|
]);
|
||
12 years ago
|
} else {
|
||
11 years ago
|
$label .= ' <span class="caret"></span>';
|
||
11 years ago
|
$options = $this->options;
|
||
12 years ago
|
if (!isset($options['href'])) {
|
||
|
$options['href'] = '#';
|
||
|
}
|
||
12 years ago
|
Html::addCssClass($options, 'dropdown-toggle');
|
||
12 years ago
|
$options['data-toggle'] = 'dropdown';
|
||
12 years ago
|
$splitButton = '';
|
||
12 years ago
|
}
|
||
11 years ago
|
return Button::widget([
|
||
11 years ago
|
'tagName' => $this->tagName,
|
||
11 years ago
|
'label' => $label,
|
||
12 years ago
|
'options' => $options,
|
||
11 years ago
|
'encodeLabel' => false,
|
||
11 years ago
|
]) . "\n" . $splitButton;
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Generates the dropdown menu.
|
||
12 years ago
|
* @return string the rendering result.
|
||
|
*/
|
||
12 years ago
|
protected function renderDropdown()
|
||
12 years ago
|
{
|
||
12 years ago
|
$config = $this->dropdown;
|
||
|
$config['clientOptions'] = false;
|
||
12 years ago
|
return Dropdown::widget($config);
|
||
|
}
|
||
12 years ago
|
}
|