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.
		
		
		
		
			
				
					98 lines
				
				2.3 KiB
			
		
		
			
		
	
	
					98 lines
				
				2.3 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;
 | ||
|  | 
 | ||
| 
											12 years ago
										 | use yii\helpers\ArrayHelper;
 | ||
| 
											13 years ago
										 | use yii\helpers\Html;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * ButtonGroup renders a button group bootstrap component.
 | ||
|  |  *
 | ||
|  |  * For example,
 | ||
|  |  *
 | ||
|  |  * ```php
 | ||
|  |  * // a button group with items configuration
 | ||
| 
											12 years ago
										 |  * echo ButtonGroup::widget([
 | ||
| 
											12 years ago
										 |  *     'buttons' => [
 | ||
|  |  *         ['label' => 'A'],
 | ||
|  |  *         ['label' => 'B'],
 | ||
|  |  *     ]
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * // button group with an item as a string
 | ||
| 
											12 years ago
										 |  * echo ButtonGroup::widget([
 | ||
| 
											12 years ago
										 |  *     'buttons' => [
 | ||
|  |  *         Button::widget(['label' => 'A']),
 | ||
|  |  *         ['label' => 'B'],
 | ||
|  |  *     ]
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  * ```
 | ||
|  |  * @see http://twitter.github.io/bootstrap/javascript.html#buttons
 | ||
|  |  * @see http://twitter.github.io/bootstrap/components.html#buttonGroups
 | ||
|  |  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class ButtonGroup extends Widget
 | ||
|  | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array list of buttons. Each array element represents a single button
 | ||
|  | 	 * which can be specified as a string or an array of the following structure:
 | ||
| 
											13 years ago
										 | 	 *
 | ||
|  | 	 * - label: string, required, the button label.
 | ||
|  | 	 * - options: array, optional, the HTML attributes of the button.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $buttons = [];
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether to HTML-encode the button labels.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $encodeLabels = true;
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Initializes the widget.
 | ||
|  | 	 * If you override this method, make sure you call the parent implementation first.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		parent::init();
 | ||
| 
											13 years ago
										 | 		Html::addCssClass($this->options, 'btn-group');
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		echo Html::tag('div', $this->renderButtons(), $this->options);
 | ||
| 
											12 years ago
										 | 		BootstrapAsset::register($this->getView());
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Generates the buttons that compound the group as specified on [[items]].
 | ||
|  | 	 * @return string the rendering result.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	protected function renderButtons()
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$buttons = [];
 | ||
| 
											13 years ago
										 | 		foreach ($this->buttons as $button) {
 | ||
|  | 			if (is_array($button)) {
 | ||
|  | 				$label = ArrayHelper::getValue($button, 'label');
 | ||
|  | 				$options = ArrayHelper::getValue($button, 'options');
 | ||
| 
											12 years ago
										 | 				$buttons[] = Button::widget([
 | ||
| 
											13 years ago
										 | 					'label' => $label,
 | ||
|  | 					'options' => $options,
 | ||
|  | 					'encodeLabel' => $this->encodeLabels
 | ||
| 
											12 years ago
										 | 				]);
 | ||
| 
											13 years ago
										 | 			} else {
 | ||
|  | 				$buttons[] = $button;
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 		return implode("\n", $buttons);
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |