ButtonDropdown.php 2.8 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
 * ```
MarsuBoss committed
29 30
 * @see http://getbootstrap.com/javascript/#buttons
 * @see http://getbootstrap.com/components/#btn-dropdowns
31 32 33 34 35 36 37 38
 * @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
	 */
	public $split = false;
52 53 54 55 56 57 58 59
	/**
	 * @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;
60 61 62 63 64 65 66


	/**
	 * Renders the widget.
	 */
	public function run()
	{
67
		echo $this->renderButton() . "\n" . $this->renderDropdown();
68 69 70 71 72 73 74 75 76
		$this->registerPlugin('button');
	}

	/**
	 * Generates the button dropdown.
	 * @return string the rendering result.
	 */
	protected function renderButton()
	{
77
		Html::addCssClass($this->options, 'btn');
78 79 80 81
		$label = $this->label;
		if ($this->encodeLabel) {
			$label = Html::encode($label);
		}
82
		if ($this->split) {
83 84 85
			$options = $this->options;
			$this->options['data-toggle'] = 'dropdown';
			Html::addCssClass($this->options, 'dropdown-toggle');
Alexander Makarov committed
86
			$splitButton = Button::widget([
Qiang Xue committed
87 88
				'label' => '<span class="caret"></span>',
				'encodeLabel' => false,
89
				'options' => $this->options,
90
				'view' => $this->getView(),
Alexander Makarov committed
91
			]);
92
		} else {
93
			$label .= ' <span class="caret"></span>';
94
			$options = $this->options;
95 96 97
			if (!isset($options['href'])) {
				$options['href'] = '#';
			}
98
			Html::addCssClass($options, 'dropdown-toggle');
99
			$options['data-toggle'] = 'dropdown';
Qiang Xue committed
100
			$splitButton = '';
101
		}
Alexander Makarov committed
102
		return Button::widget([
103
			'tagName' => $this->tagName,
104
			'label' => $label,
105
			'options' => $options,
106
			'encodeLabel' => false,
107
			'view' => $this->getView(),
Alexander Makarov committed
108
		]) . "\n" . $splitButton;
109 110 111
	}

	/**
112
	 * Generates the dropdown menu.
113 114
	 * @return string the rendering result.
	 */
Qiang Xue committed
115
	protected function renderDropdown()
116
	{
117 118
		$config = $this->dropdown;
		$config['clientOptions'] = false;
119
		$config['view'] = $this->getView();
120 121
		return Dropdown::widget($config);
	}
Qiang Xue committed
122
}