Widget.php 2.51 KB
Newer Older
Alexander Kochetov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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
{
	/**
Qiang Xue committed
22 23
	 * @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.
Alexander Kochetov committed
24
	 */
Qiang Xue committed
25
	public static $theme = 'yii\jui\ThemeAsset';
Alexander Kochetov committed
26 27 28
	/**
	 * @var array the HTML attributes for the widget container tag.
	 */
Alexander Makarov committed
29
	public $options = [];
Alexander Kochetov committed
30 31 32 33 34 35
	/**
	 * @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").
	 */
Alexander Makarov committed
36
	public $clientOptions = [];
Alexander Kochetov committed
37 38 39 40 41 42
	/**
	 * @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").
	 */
Alexander Makarov committed
43
	public $clientEvents = [];
Alexander Kochetov committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


	/**
	 * 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();
		}
	}

	/**
	 * Registers a specific jQuery UI widget and the related events
	 * @param string $name the name of the jQuery UI widget
Qiang Xue committed
61
	 * @param string $assetBundle the asset bundle for the widget
Alexander Kochetov committed
62
	 */
Qiang Xue committed
63
	protected function registerWidget($name, $assetBundle)
Alexander Kochetov committed
64 65
	{
		$view = $this->getView();
Qiang Xue committed
66 67 68 69 70
		/** @var \yii\web\AssetBundle $assetBundle */
		$assetBundle::register($view);
		/** @var \yii\web\AssetBundle $themeAsset */
		$themeAsset = self::$theme;
		$themeAsset::register($view);
Alexander Kochetov committed
71

Qiang Xue committed
72
		$id = $this->options['id'];
Alexander Kochetov committed
73 74 75 76 77 78 79
		if ($this->clientOptions !== false) {
			$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
			$js = "jQuery('#$id').$name($options);";
			$view->registerJs($js);
		}

		if (!empty($this->clientEvents)) {
Alexander Makarov committed
80
			$js = [];
Alexander Kochetov committed
81 82 83 84 85 86 87
			foreach ($this->clientEvents as $event => $handler) {
				$js[] = "jQuery('#$id').on('$name$event', $handler);";
			}
			$view->registerJs(implode("\n", $js));
		}
	}
}