Widget.php 4.21 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
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\jui;

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
21 22
	 * @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
23
	 */
Qiang Xue committed
24
	public static $theme = 'yii\jui\ThemeAsset';
Alexander Kochetov committed
25 26
	/**
	 * @var array the HTML attributes for the widget container tag.
27
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Alexander Kochetov committed
28
	 */
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
	/**
	 * @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").
42 43 44 45 46 47 48 49 50 51
	 * Keys are the event names and values are javascript code that is passed to the `.on()` function
	 * as the event handler.
	 *
	 * For example you could write the following in your widget configuration:
	 *
	 * ```php
	 * 'clientEvents' => [
	 *     'change' => 'function() { alert('event "change" occured.'); }'
	 * ],
	 * ```
Alexander Kochetov committed
52
	 */
Alexander Makarov committed
53
	public $clientEvents = [];
Alexander Kochetov committed
54

55 56 57 58
	/**
	 * @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.
	 */
59
	protected $clientEventMap = [];
Alexander Kochetov committed
60

61

Alexander Kochetov committed
62 63 64 65 66 67 68 69 70 71 72 73 74
	/**
	 * 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();
		}
	}

	/**
75
	 * Registers a specific jQuery UI widget assets
Qiang Xue committed
76
	 * @param string $assetBundle the asset bundle for the widget
Alexander Kochetov committed
77
	 */
78
	protected function registerAssets($assetBundle)
Alexander Kochetov committed
79
	{
Qiang Xue committed
80
		/** @var \yii\web\AssetBundle $assetBundle */
81
		$assetBundle::register($this->getView());
Qiang Xue committed
82
		/** @var \yii\web\AssetBundle $themeAsset */
83
		$themeAsset = static::$theme;
84 85
		$themeAsset::register($this->getView());
	}
Alexander Kochetov committed
86

87 88 89
	/**
	 * Registers a specific jQuery UI widget options
	 * @param string $name the name of the jQuery UI widget
90
	 * @param string $id the ID of the widget
91
	 */
92
	protected function registerClientOptions($name, $id)
93
	{
Alexander Kochetov committed
94 95 96
		if ($this->clientOptions !== false) {
			$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
			$js = "jQuery('#$id').$name($options);";
97
			$this->getView()->registerJs($js);
Alexander Kochetov committed
98
		}
99
	}
Alexander Kochetov committed
100

101 102 103
	/**
	 * Registers a specific jQuery UI widget events
	 * @param string $name the name of the jQuery UI widget
104
	 * @param string $id the ID of the widget
105
	 */
106
	protected function registerClientEvents($name, $id)
107
	{
Alexander Kochetov committed
108
		if (!empty($this->clientEvents)) {
Alexander Makarov committed
109
			$js = [];
Alexander Kochetov committed
110
			foreach ($this->clientEvents as $event => $handler) {
111 112
				if (isset($this->clientEventMap[$event])) {
					$eventName = $this->clientEventMap[$event];
113
				} else {
114
					$eventName = strtolower($name . $event);
115 116
				}
				$js[] = "jQuery('#$id').on('$eventName', $handler);";
Alexander Kochetov committed
117
			}
118
			$this->getView()->registerJs(implode("\n", $js));
Alexander Kochetov committed
119 120
		}
	}
121 122 123 124 125

	/**
	 * 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
126
	 * @param string $id the ID of the widget. If null, it will use the `id` value of [[options]].
127
	 */
128
	protected function registerWidget($name, $assetBundle, $id = null)
129
	{
130 131 132
		if ($id === null) {
			$id = $this->options['id'];
		}
133
		$this->registerAssets($assetBundle);
134 135
		$this->registerClientOptions($name, $id);
		$this->registerClientEvents($name, $id);
136
	}
Alexander Kochetov committed
137
}