From 3647c3f12846919630f1ecfc5f62e897350c36c8 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 13:54:47 +0200 Subject: [PATCH] added button group dropdowns --- framework/yii/bootstrap/Button.php | 147 +++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 framework/yii/bootstrap/Button.php diff --git a/framework/yii/bootstrap/Button.php b/framework/yii/bootstrap/Button.php new file mode 100644 index 0000000..bdc29ca --- /dev/null +++ b/framework/yii/bootstrap/Button.php @@ -0,0 +1,147 @@ + 'Action', + * 'items' => Dropdown::widget(array( + * 'clientOptions' => false, + * 'items' => array( + * array( + * 'label' => 'DropdownA', + * 'url' => '/', + * ), + * array( + * 'label' => 'DropdownB', + * 'url' => '#', + * ), + * ), + * ), + * ), + * )); + * + * // split button group using `items` dropdown configuration + * echo \yii\bootstrap\Button::widget(array( + * 'label' => 'Action', + * 'split' => true, + * 'items' => array( + * array( + * 'label' => 'DropdownA', + * 'url' => '/', + * ), + * array( + * 'label' => 'DropdownB', + * 'url' => '#', + * ), + * ), + * )); + * ``` + * @see http://twitter.github.io/bootstrap/javascript.html#buttons + * @see http://twitter.github.io/bootstrap/components.html#buttonDropdowns + * @author Antonio Ramirez + * @since 2.0 + */ +class Button extends Widget +{ + /** + * @var string the button label + */ + public $label; + /** + * @var array list of menu items in the dropdown. Each array element represents a single + * menu with the following structure: + * + * - label: string, required, the label of the item link + * - url: string, optional, the url of the item link. Defaults to "#". + * - linkOptions: array, optional, the HTML attributes of the item link. + * - options: array, optional, the HTML attributes of the item. + * - items: array, optional, the dropdown items configuration array. + * + * @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727 + * @see [[Dropdown]] + */ + public $items = array(); + /** + * @var boolean whether to display a group or split styled button group. + */ + public $split = false; + /** + * @var boolean whether the labels for dropdown items should be HTML-encoded. + */ + public $encodeLabels = true; + + + /** + * Initializes the widget. + * If you override this method, make sure you call the parent implementation first. + * @throws InvalidConfigException + */ + public function init() + { + if ($this->label === null) { + throw new InvalidConfigException("The 'label' option is required."); + } + parent::init(); + $this->addCssClass($this->options, 'btn-group'); + } + + /** + * Renders the widget. + */ + public function run() + { + echo Html::beginTag('div', $this->options) . "\n"; + echo $this->renderLabel() . "\n"; + echo $this->renderItems() . "\n"; + echo Html::endTag('div') . "\n"; + $this->registerPlugin('button'); + } + + /** + * Generates the button label. + * @return string the rendering result. + */ + protected function renderLabel() + { + $label = $this->encodeLabels ? Html::encode($this->label) : $this->label; + $options = array('class' => 'btn dropdown-toggle', 'data-toggle' => 'dropdown'); + if ($this->split) { + $tag = 'button'; + $label .= Html::tag('button', '', $options); + $options = array('class' => 'btn'); + } else { + $tag = 'a'; + $options['href'] = '#'; + } + return Html::tag($tag, $label, $options); + } + + /** + * Generates the dropdown menu as specified on [[items]]. + * @return string the rendering result. + */ + protected function renderItems() + { + if (is_string($this->items)) { + return $this->items; + } + $config = array('items' => $this->items, 'clientOptions' => false); + return Dropdown::widget($config); + } +} \ No newline at end of file