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
12 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.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public static $theme = 'yii\jui\ThemeAsset';
|
||
12 years ago
|
/**
|
||
|
* @var array the HTML attributes for the widget container tag.
|
||
|
*/
|
||
11 years ago
|
public $options = [];
|
||
12 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").
|
||
|
*/
|
||
11 years ago
|
public $clientOptions = [];
|
||
12 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").
|
||
|
*/
|
||
11 years ago
|
public $clientEvents = [];
|
||
12 years ago
|
|
||
11 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 = [];
|
||
12 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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Registers a specific jQuery UI widget assets
|
||
12 years ago
|
* @param string $assetBundle the asset bundle for the widget
|
||
12 years ago
|
*/
|
||
11 years ago
|
protected function registerAssets($assetBundle)
|
||
12 years ago
|
{
|
||
12 years ago
|
/** @var \yii\web\AssetBundle $assetBundle */
|
||
11 years ago
|
$assetBundle::register($this->getView());
|
||
12 years ago
|
/** @var \yii\web\AssetBundle $themeAsset */
|
||
11 years ago
|
$themeAsset = static::$theme;
|
||
11 years ago
|
$themeAsset::register($this->getView());
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Registers a specific jQuery UI widget options
|
||
|
* @param string $name the name of the jQuery UI widget
|
||
|
*/
|
||
|
protected function registerClientOptions($name)
|
||
|
{
|
||
12 years ago
|
if ($this->clientOptions !== false) {
|
||
11 years ago
|
$id = $this->options['id'];
|
||
12 years ago
|
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
|
||
|
$js = "jQuery('#$id').$name($options);";
|
||
11 years ago
|
$this->getView()->registerJs($js);
|
||
12 years ago
|
}
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Registers a specific jQuery UI widget events
|
||
|
* @param string $name the name of the jQuery UI widget
|
||
|
*/
|
||
|
protected function registerClientEvents($name)
|
||
|
{
|
||
12 years ago
|
if (!empty($this->clientEvents)) {
|
||
11 years ago
|
$id = $this->options['id'];
|
||
11 years ago
|
$js = [];
|
||
12 years ago
|
foreach ($this->clientEvents as $event => $handler) {
|
||
11 years ago
|
if (isset($this->clientEventsMap[$event])) {
|
||
|
$eventName = $this->clientEventsMap[$event];
|
||
|
} else {
|
||
|
$eventName = $name.$event;
|
||
|
}
|
||
|
$js[] = "jQuery('#$id').on('$eventName', $handler);";
|
||
12 years ago
|
}
|
||
11 years ago
|
$this->getView()->registerJs(implode("\n", $js));
|
||
12 years ago
|
}
|
||
|
}
|
||
11 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);
|
||
|
}
|
||
12 years ago
|
}
|