ButtonDropdown.php 3.01 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 19 20

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


	/**
	 * Initializes the widget.
	 * If you override this method, make sure you call the parent implementation first.
	 */
	public function init()
	{
		parent::init();
67
		Html::addCssClass($this->options, 'btn-group');
68 69 70 71 72 73 74 75 76
	}

	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo Html::beginTag('div', $this->options) . "\n";
		echo $this->renderButton() . "\n";
Qiang Xue committed
77
		echo $this->renderDropdown() . "\n";
78 79 80 81 82 83 84 85 86 87
		echo Html::endTag('div') . "\n";
		$this->registerPlugin('button');
	}

	/**
	 * Generates the button dropdown.
	 * @return string the rendering result.
	 */
	protected function renderButton()
	{
88
		Html::addCssClass($this->buttonOptions, 'btn');
89 90 91 92
		if ($this->split) {
			$tag = 'button';
			$options = $this->buttonOptions;
			$this->buttonOptions['data-toggle'] = 'dropdown';
93
			Html::addCssClass($this->buttonOptions, 'dropdown-toggle');
94
			$splitButton = Button::widget(array(
Qiang Xue committed
95 96 97 98
				'label' => '<span class="caret"></span>',
				'encodeLabel' => false,
				'options' => $this->buttonOptions,
			));
99 100 101 102 103 104 105
		} else {
			$tag = 'a';
			$this->label .= ' <span class="caret"></span>';
			$options = $this->buttonOptions;
			if (!isset($options['href'])) {
				$options['href'] = '#';
			}
106
			Html::addCssClass($options, 'dropdown-toggle');
107
			$options['data-toggle'] = 'dropdown';
Qiang Xue committed
108
			$splitButton = '';
109 110 111 112 113 114 115 116 117 118
		}
		return Button::widget(array(
			'tagName' => $tag,
			'label' => $this->label,
			'options' => $options,
			'encodeLabel' => false,
		)) . "\n" . $splitButton;
	}

	/**
119
	 * Generates the dropdown menu.
120 121
	 * @return string the rendering result.
	 */
Qiang Xue committed
122
	protected function renderDropdown()
123
	{
124 125
		$config = $this->dropdown;
		$config['clientOptions'] = false;
126 127
		return Dropdown::widget($config);
	}
Qiang Xue committed
128
}