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.
		
		
		
		
			
				
					102 lines
				
				3.0 KiB
			
		
		
			
		
	
	
					102 lines
				
				3.0 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\bootstrap;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | use Yii;
 | ||
|  | use yii\base\View;
 | ||
| 
											13 years ago
										 | use yii\helpers\Json;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * \yii\bootstrap\Widget is the base class for all bootstrap widgets.
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 | ||
| 
											13 years ago
										 |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
| 
											13 years ago
										 |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class Widget extends \yii\base\Widget
 | ||
| 
											13 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether to use the responsive version of Bootstrap.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public static $responsive = true;
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array the HTML attributes for the widget container tag.
 | ||
|  | 	 */
 | ||
|  | 	public $options = array();
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array the options for the underlying Bootstrap JS plugin.
 | ||
|  | 	 * Please refer to the corresponding Bootstrap plugin Web page for possible options.
 | ||
|  | 	 * For example, [this page](http://twitter.github.io/bootstrap/javascript.html#modals) shows
 | ||
|  | 	 * how to use the "Modal" plugin and the supported options (e.g. "remote").
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $pluginOptions = array();
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array the event handlers for the underlying Bootstrap JS plugin.
 | ||
|  | 	 * Please refer to the corresponding Bootstrap plugin Web page for possible events.
 | ||
|  | 	 * For example, [this page](http://twitter.github.io/bootstrap/javascript.html#modals) shows
 | ||
|  | 	 * how to use the "Modal" plugin and the supported events (e.g. "shown").
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $pluginEvents = array();
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Initializes the widget.
 | ||
|  | 	 * This method will register the bootstrap asset bundle. If you override this method,
 | ||
|  | 	 * make sure you call the parent implementation first.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function init()
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		parent::init();
 | ||
|  | 		if (!isset($this->options['id'])) {
 | ||
|  | 			$this->options['id'] = $this->getId();
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Registers a specific Bootstrap plugin and the related events
 | ||
|  | 	 * @param string $name the name of the Bootstrap plugin
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	protected function registerPlugin($name)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$id = $this->options['id'];
 | ||
|  | 		$view = $this->getView();
 | ||
| 
											13 years ago
										 | 		$view->registerAssetBundle(static::$responsive ? 'yii/bootstrap/responsive' : 'yii/bootstrap');
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		if ($this->pluginOptions !== false) {
 | ||
|  | 			$options = empty($this->pluginOptions) ? '' : Json::encode($this->pluginOptions);
 | ||
|  | 			$js = "jQuery('#$id').$name($options);";
 | ||
|  | 			$view->registerJs($js);
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		if (!empty($this->pluginEvents)) {
 | ||
|  | 			$js = array();
 | ||
|  | 			foreach ($this->pluginEvents as $event => $handler) {
 | ||
|  | 				$js[] = "jQuery('#$id').on('$event', $handler);";
 | ||
|  | 			}
 | ||
|  | 			$view->registerJs(implode("\n", $js));
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Adds a CSS class to the specified options.
 | ||
|  | 	 * This method will ensure that the CSS class is unique and the "class" option is properly formatted.
 | ||
|  | 	 * @param array $options the options to be modified.
 | ||
|  | 	 * @param string $class the CSS class to be added
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	protected function addCssClass(&$options, $class)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (isset($options['class'])) {
 | ||
|  | 			$classes = preg_split('/\s+/', $options['class'] . ' ' . $class, -1, PREG_SPLIT_NO_EMPTY);
 | ||
|  | 			$options['class'] = implode(' ', array_unique($classes));
 | ||
|  | 		} else {
 | ||
|  | 			$options['class'] = $class;
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |