ButtonDropdown.php 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

resurtm committed
10
use yii\helpers\Html;
11 12 13 14 15 16 17 18

/**
 * ButtonDropdown renders a group or split button dropdown bootstrap component.
 *
 * For example,
 *
 * ```php
 * // a button group using Dropdown widget
Alexander Makarov committed
19
 * echo ButtonDropdown::widget([
20
 *     'label' => 'Action',
Alexander Makarov committed
21 22 23 24 25 26 27
 *     'dropdown' => [
 *         'items' => [
 *             ['label' => 'DropdownA', 'url' => '/'],
 *             ['label' => 'DropdownB', 'url' => '#'],
 *         ],
 *     ],
 * ]);
28 29 30 31 32 33 34 35 36 37 38
 * ```
 * @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
	 */
Qiang Xue committed
39
	public $label = 'Button';
40 41 42
	/**
	 * @var array the HTML attributes of the button.
	 */
Alexander Makarov committed
43
	public $options = [];
44
	/**
45
	 * @var array the configuration array for [[Dropdown]].
46
	 */
Alexander Makarov committed
47
	public $dropdown = [];
48
	/**
Qiang Xue committed
49
	 * @var boolean whether to display a group of split-styled button group.
50 51 52 53 54 55 56 57 58
	 */
	public $split = false;


	/**
	 * Renders the widget.
	 */
	public function run()
	{
59
		echo $this->renderButton() . "\n" . $this->renderDropdown();
60 61 62 63 64 65 66 67 68
		$this->registerPlugin('button');
	}

	/**
	 * Generates the button dropdown.
	 * @return string the rendering result.
	 */
	protected function renderButton()
	{
69
		Html::addCssClass($this->options, 'btn');
70 71
		if ($this->split) {
			$tag = 'button';
72 73 74
			$options = $this->options;
			$this->options['data-toggle'] = 'dropdown';
			Html::addCssClass($this->options, 'dropdown-toggle');
Alexander Makarov committed
75
			$splitButton = Button::widget([
Qiang Xue committed
76 77
				'label' => '<span class="caret"></span>',
				'encodeLabel' => false,
78
				'options' => $this->options,
Alexander Makarov committed
79
			]);
80 81 82
		} else {
			$tag = 'a';
			$this->label .= ' <span class="caret"></span>';
83
			$options = $this->options;
84 85 86
			if (!isset($options['href'])) {
				$options['href'] = '#';
			}
87
			Html::addCssClass($options, 'dropdown-toggle');
88
			$options['data-toggle'] = 'dropdown';
Qiang Xue committed
89
			$splitButton = '';
90
		}
Alexander Makarov committed
91
		return Button::widget([
92 93 94 95
			'tagName' => $tag,
			'label' => $this->label,
			'options' => $options,
			'encodeLabel' => false,
Alexander Makarov committed
96
		]) . "\n" . $splitButton;
97 98 99
	}

	/**
100
	 * Generates the dropdown menu.
101 102
	 * @return string the rendering result.
	 */
Qiang Xue committed
103
	protected function renderDropdown()
104
	{
105 106
		$config = $this->dropdown;
		$config['clientOptions'] = false;
107 108
		return Dropdown::widget($config);
	}
Qiang Xue committed
109
}