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.
		
		
		
		
			
				
					161 lines
				
				4.7 KiB
			
		
		
			
		
	
	
					161 lines
				
				4.7 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\bootstrap;
 | ||
|  | 
 | ||
| 
											11 years ago
										 | use yii\helpers\ArrayHelper;
 | ||
| 
											8 years ago
										 | use yii\helpers\Url;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | /**
 | ||
|  |  * ButtonDropdown renders a group or split button dropdown bootstrap component.
 | ||
|  |  *
 | ||
|  |  * For example,
 | ||
|  |  *
 | ||
|  |  * ```php
 | ||
|  |  * // a button group using Dropdown widget
 | ||
| 
											12 years ago
										 |  * echo ButtonDropdown::widget([
 | ||
| 
											13 years ago
										 |  *     'label' => 'Action',
 | ||
| 
											12 years ago
										 |  *     'dropdown' => [
 | ||
|  |  *         'items' => [
 | ||
|  |  *             ['label' => 'DropdownA', 'url' => '/'],
 | ||
|  |  *             ['label' => 'DropdownB', 'url' => '#'],
 | ||
|  |  *         ],
 | ||
|  |  *     ],
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  * ```
 | ||
| 
											12 years ago
										 |  * @see http://getbootstrap.com/javascript/#buttons
 | ||
|  |  * @see http://getbootstrap.com/components/#btn-dropdowns
 | ||
| 
											13 years ago
										 |  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class ButtonDropdown extends Widget
 | ||
|  | {
 | ||
| 
											12 years ago
										 |     /**
 | ||
|  |      * @var string the button label
 | ||
|  |      */
 | ||
|  |     public $label = 'Button';
 | ||
|  |     /**
 | ||
| 
											11 years ago
										 |      * @var array the HTML attributes for the container tag. The following special options are recognized:
 | ||
|  |      *
 | ||
|  |      * - tag: string, defaults to "div", the name of the container tag.
 | ||
|  |      *
 | ||
|  |      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 | ||
|  |      * @since 2.0.1
 | ||
|  |      */
 | ||
|  |     public $containerOptions = [];
 | ||
|  |     /**
 | ||
| 
											12 years ago
										 |      * @var array the HTML attributes of the button.
 | ||
|  |      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 | ||
|  |      */
 | ||
|  |     public $options = [];
 | ||
|  |     /**
 | ||
|  |      * @var array the configuration array for [[Dropdown]].
 | ||
|  |      */
 | ||
|  |     public $dropdown = [];
 | ||
|  |     /**
 | ||
| 
											8 years ago
										 |      * @var bool whether to display a group of split-styled button group.
 | ||
| 
											12 years ago
										 |      */
 | ||
|  |     public $split = false;
 | ||
|  |     /**
 | ||
|  |      * @var string the tag to use to render the button
 | ||
|  |      */
 | ||
|  |     public $tagName = 'button';
 | ||
|  |     /**
 | ||
| 
											8 years ago
										 |      * @var bool whether the label should be HTML-encoded.
 | ||
| 
											12 years ago
										 |      */
 | ||
|  |     public $encodeLabel = true;
 | ||
| 
											9 years ago
										 |     /**
 | ||
|  |      * @var string name of a class to use for rendering dropdowns withing this widget. Defaults to [[Dropdown]].
 | ||
|  |      * @since 2.0.7
 | ||
|  |      */
 | ||
|  |     public $dropdownClass = 'yii\bootstrap\Dropdown';
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											11 years ago
										 | 
 | ||
| 
											12 years ago
										 |     /**
 | ||
|  |      * Renders the widget.
 | ||
|  |      */
 | ||
|  |     public function run()
 | ||
|  |     {
 | ||
| 
											11 years ago
										 |         // @todo use [[options]] instead of [[containerOptions]] and introduce [[buttonOptions]] before 2.1 release
 | ||
| 
											11 years ago
										 |         Html::addCssClass($this->containerOptions, ['widget' => 'btn-group']);
 | ||
| 
											11 years ago
										 |         $options = $this->containerOptions;
 | ||
|  |         $tag = ArrayHelper::remove($options, 'tag', 'div');
 | ||
|  | 
 | ||
| 
											8 years ago
										 |         $this->registerPlugin('dropdown');
 | ||
| 
											11 years ago
										 |         return implode("\n", [
 | ||
| 
											11 years ago
										 |             Html::beginTag($tag, $options),
 | ||
| 
											11 years ago
										 |             $this->renderButton(),
 | ||
|  |             $this->renderDropdown(),
 | ||
|  |             Html::endTag($tag)
 | ||
|  |         ]);
 | ||
| 
											12 years ago
										 |     }
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 |     /**
 | ||
|  |      * Generates the button dropdown.
 | ||
|  |      * @return string the rendering result.
 | ||
|  |      */
 | ||
|  |     protected function renderButton()
 | ||
|  |     {
 | ||
| 
											11 years ago
										 |         Html::addCssClass($this->options, ['widget' => 'btn']);
 | ||
| 
											12 years ago
										 |         $label = $this->label;
 | ||
|  |         if ($this->encodeLabel) {
 | ||
|  |             $label = Html::encode($label);
 | ||
|  |         }
 | ||
| 
											8 years ago
										 | 
 | ||
| 
											12 years ago
										 |         if ($this->split) {
 | ||
|  |             $options = $this->options;
 | ||
|  |             $this->options['data-toggle'] = 'dropdown';
 | ||
| 
											11 years ago
										 |             Html::addCssClass($this->options, ['toggle' => 'dropdown-toggle']);
 | ||
| 
											8 years ago
										 |             unset($options['id']);
 | ||
| 
											12 years ago
										 |             $splitButton = Button::widget([
 | ||
|  |                 'label' => '<span class="caret"></span>',
 | ||
|  |                 'encodeLabel' => false,
 | ||
|  |                 'options' => $this->options,
 | ||
|  |                 'view' => $this->getView(),
 | ||
|  |             ]);
 | ||
|  |         } else {
 | ||
|  |             $label .= ' <span class="caret"></span>';
 | ||
|  |             $options = $this->options;
 | ||
| 
											11 years ago
										 |             Html::addCssClass($options, ['toggle' => 'dropdown-toggle']);
 | ||
| 
											12 years ago
										 |             $options['data-toggle'] = 'dropdown';
 | ||
|  |             $splitButton = '';
 | ||
|  |         }
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											8 years ago
										 |         if (isset($options['href'])) {
 | ||
|  |             if (is_array($options['href'])) {
 | ||
|  |                 $options['href'] = Url::to($options['href']);
 | ||
|  |             }
 | ||
|  |         } else {
 | ||
|  |             if ($this->tagName === 'a') {
 | ||
|  |                 $options['href'] = '#';
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  | 
 | ||
| 
											12 years ago
										 |         return Button::widget([
 | ||
|  |             'tagName' => $this->tagName,
 | ||
|  |             'label' => $label,
 | ||
|  |             'options' => $options,
 | ||
|  |             'encodeLabel' => false,
 | ||
|  |             'view' => $this->getView(),
 | ||
|  |         ]) . "\n" . $splitButton;
 | ||
|  |     }
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 |     /**
 | ||
|  |      * Generates the dropdown menu.
 | ||
|  |      * @return string the rendering result.
 | ||
|  |      */
 | ||
|  |     protected function renderDropdown()
 | ||
|  |     {
 | ||
|  |         $config = $this->dropdown;
 | ||
|  |         $config['clientOptions'] = false;
 | ||
|  |         $config['view'] = $this->getView();
 | ||
| 
											9 years ago
										 |         /** @var Widget $dropdownClass */
 | ||
|  |         $dropdownClass = $this->dropdownClass;
 | ||
|  |         return $dropdownClass::widget($config);
 | ||
| 
											12 years ago
										 |     }
 | ||
| 
											13 years ago
										 | }
 |