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.
		
		
		
		
			
				
					123 lines
				
				3.6 KiB
			
		
		
			
		
	
	
					123 lines
				
				3.6 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\jui;
 | ||
|  | 
 | ||
|  | use Yii;
 | ||
|  | use yii\helpers\Json;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * \yii\jui\Widget is the base class for all jQuery UI widgets.
 | ||
|  |  *
 | ||
|  |  * @author Alexander Kochetov <creocoder@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class Widget extends \yii\base\Widget
 | ||
|  | {
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @var string the jQuery UI theme. This refers to an asset bundle class
 | ||
|  | 	 * representing the JUI theme. The default theme is the official "Smoothness" theme.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public static $theme = 'yii\jui\ThemeAsset';
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var array the HTML attributes for the widget container tag.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $options = [];
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var array the options for the underlying jQuery UI widget.
 | ||
|  | 	 * Please refer to the corresponding jQuery UI widget Web page for possible options.
 | ||
|  | 	 * For example, [this page](http://api.jqueryui.com/accordion/) shows
 | ||
|  | 	 * how to use the "Accordion" widget and the supported options (e.g. "header").
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $clientOptions = [];
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var array the event handlers for the underlying jQuery UI widget.
 | ||
|  | 	 * Please refer to the corresponding jQuery UI widget Web page for possible events.
 | ||
|  | 	 * For example, [this page](http://api.jqueryui.com/accordion/) shows
 | ||
|  | 	 * how to use the "Accordion" widget and the supported events (e.g. "create").
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $clientEvents = [];
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var array event names mapped to what should be specified in .on(
 | ||
|  | 	 * If empty, it is assumed that event passed to clientEvents is prefixed with widget name.
 | ||
|  | 	 */
 | ||
|  | 	protected $clientEventsMap = [];
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * Initializes the widget.
 | ||
|  | 	 * If you override this method, make sure you call the parent implementation first.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		parent::init();
 | ||
|  | 		if (!isset($this->options['id'])) {
 | ||
|  | 			$this->options['id'] = $this->getId();
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Registers a specific jQuery UI widget assets
 | ||
| 
											12 years ago
										 | 	 * @param string $assetBundle the asset bundle for the widget
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	protected function registerAssets($assetBundle)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		/** @var \yii\web\AssetBundle $assetBundle */
 | ||
| 
											12 years ago
										 | 		$assetBundle::register($this->getView());
 | ||
| 
											12 years ago
										 | 		/** @var \yii\web\AssetBundle $themeAsset */
 | ||
| 
											12 years ago
										 | 		$themeAsset = static::$theme;
 | ||
| 
											12 years ago
										 | 		$themeAsset::register($this->getView());
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * Registers a specific jQuery UI widget options
 | ||
|  | 	 * @param string $name the name of the jQuery UI widget
 | ||
|  | 	 */
 | ||
|  | 	protected function registerClientOptions($name)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->clientOptions !== false) {
 | ||
| 
											12 years ago
										 | 			$id = $this->options['id'];
 | ||
| 
											13 years ago
										 | 			$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
 | ||
|  | 			$js = "jQuery('#$id').$name($options);";
 | ||
| 
											12 years ago
										 | 			$this->getView()->registerJs($js);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * Registers a specific jQuery UI widget events
 | ||
|  | 	 * @param string $name the name of the jQuery UI widget
 | ||
|  | 	 */
 | ||
|  | 	protected function registerClientEvents($name)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if (!empty($this->clientEvents)) {
 | ||
| 
											12 years ago
										 | 			$id = $this->options['id'];
 | ||
| 
											12 years ago
										 | 			$js = [];
 | ||
| 
											13 years ago
										 | 			foreach ($this->clientEvents as $event => $handler) {
 | ||
| 
											12 years ago
										 | 				if (isset($this->clientEventsMap[$event])) {
 | ||
|  | 					$eventName = $this->clientEventsMap[$event];
 | ||
|  | 				} else {
 | ||
|  | 					$eventName = $name.$event;
 | ||
|  | 				}
 | ||
|  | 				$js[] = "jQuery('#$id').on('$eventName', $handler);";
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											12 years ago
										 | 			$this->getView()->registerJs(implode("\n", $js));
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events
 | ||
|  | 	 * @param string $name the name of the jQuery UI widget
 | ||
|  | 	 * @param string $assetBundle the asset bundle for the widget
 | ||
|  | 	 */
 | ||
|  | 	protected function registerWidget($name, $assetBundle)
 | ||
|  | 	{
 | ||
|  | 		$this->registerAssets($assetBundle);
 | ||
|  | 		$this->registerClientOptions($name);
 | ||
|  | 		$this->registerClientEvents($name);
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |