Fixes #1550: fixed the issue that JUI input widgets did not property input IDs.

This commit is contained in:
Qiang Xue
2013-12-21 17:07:31 -05:00
parent a126419e9e
commit 0ff8518c21
9 changed files with 69 additions and 39 deletions

View File

@@ -4,7 +4,7 @@ Yii Framework 2 jui extension Change Log
2.0.0 beta under development
----------------------------
- no changes in this release.
- Bug #1550: fixed the issue that JUI input widgets did not property input IDs.
2.0.0 alpha, December 1, 2013
-----------------------------

View File

@@ -54,14 +54,30 @@ class DatePicker extends InputWidget
* @var boolean If true, shows the widget as an inline calendar and the input as a hidden field.
*/
public $inline = false;
/**
* @var array the HTML attributes for the container tag. This is only used when [[inline]] is true.
*/
public $containerOptions = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->inline && !isset($this->containerOptions['id'])) {
$this->containerOptions['id'] = $this->options['id'] . '-container';
}
}
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderWidget() . "\n";
$containerID = $this->inline ? $this->containerOptions['id'] : $this->options['id'];
if ($this->language !== false) {
$view = $this->getView();
DatePickerRegionalAsset::register($view);
@@ -71,10 +87,10 @@ class DatePicker extends InputWidget
$options = $this->clientOptions;
$this->clientOptions = false; // the datepicker js widget is already registered
$this->registerWidget('datepicker', DatePickerAsset::className());
$this->registerWidget('datepicker', DatePickerAsset::className(), $containerID);
$this->clientOptions = $options;
} else {
$this->registerWidget('datepicker', DatePickerAsset::className());
$this->registerWidget('datepicker', DatePickerAsset::className(), $containerID);
}
}
@@ -101,8 +117,7 @@ class DatePicker extends InputWidget
$this->clientOptions['defaultDate'] = $this->value;
}
$this->clientOptions['altField'] = '#' . $this->options['id'];
$this->options['id'] .= '-container';
$contents[] = Html::tag('div', null, $this->options);
$contents[] = Html::tag('div', null, $this->containerOptions);
}
return implode("\n", $contents);

View File

@@ -10,6 +10,7 @@ namespace yii\jui;
use Yii;
use yii\base\Model;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
/**
* InputWidget is the base class for all jQuery UI input widgets.
@@ -44,7 +45,10 @@ class InputWidget extends Widget
public function init()
{
if (!$this->hasModel() && $this->name === null) {
throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified.");
throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
}
if ($this->hasModel() && !isset($this->options['id'])) {
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
}
parent::init();
}

View File

@@ -50,30 +50,42 @@ class SliderInput extends InputWidget
'start' => 'slidestart',
'stop' => 'slidestop',
];
/**
* @var array the HTML attributes for the container tag.
*/
public $containerOptions = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!isset($this->containerOptions['id'])) {
$this->containerOptions['id'] = $this->options['id'] . '-container';
}
}
/**
* Executes the widget.
*/
public function run()
{
echo Html::tag('div', '', $this->options);
echo Html::tag('div', '', $this->containerOptions);
$inputId = $this->id.'-input';
$inputOptions = $this->options;
$inputOptions['id'] = $inputId;
if ($this->hasModel()) {
echo Html::activeHiddenInput($this->model, $this->attribute, $inputOptions);
echo Html::activeHiddenInput($this->model, $this->attribute, $this->options);
} else {
echo Html::hiddenInput($this->name, $this->value, $inputOptions);
echo Html::hiddenInput($this->name, $this->value, $this->options);
}
if (!isset($this->clientEvents['slide'])) {
$this->clientEvents['slide'] = 'function(event, ui) {
$("#'.$inputId.'").val(ui.value);
$("#' . $this->options['id'] . '").val(ui.value);
}';
}
$this->registerWidget('slider', SliderAsset::className());
$this->getView()->registerJs('$("#'.$inputId.'").val($("#'.$this->id.'").slider("value"));');
$this->registerWidget('slider', SliderAsset::className(), $this->containerOptions['id']);
$this->getView()->registerJs('$("#' . $this->options['id'] . '").val($("#' . $this->id . '").slider("value"));');
}
}

View File

@@ -76,11 +76,11 @@ class Widget extends \yii\base\Widget
/**
* Registers a specific jQuery UI widget options
* @param string $name the name of the jQuery UI widget
* @param string $id the ID of the widget
*/
protected function registerClientOptions($name)
protected function registerClientOptions($name, $id)
{
if ($this->clientOptions !== false) {
$id = $this->options['id'];
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$js = "jQuery('#$id').$name($options);";
$this->getView()->registerJs($js);
@@ -90,11 +90,11 @@ class Widget extends \yii\base\Widget
/**
* Registers a specific jQuery UI widget events
* @param string $name the name of the jQuery UI widget
* @param string $id the ID of the widget
*/
protected function registerClientEvents($name)
protected function registerClientEvents($name, $id)
{
if (!empty($this->clientEvents)) {
$id = $this->options['id'];
$js = [];
foreach ($this->clientEvents as $event => $handler) {
if (isset($this->clientEventMap[$event])) {
@@ -112,11 +112,15 @@ class Widget extends \yii\base\Widget
* 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
* @param string $id the ID of the widget. If null, it will use the `id` value of [[options]].
*/
protected function registerWidget($name, $assetBundle)
protected function registerWidget($name, $assetBundle, $id = null)
{
if ($id === null) {
$id = $this->options['id'];
}
$this->registerAssets($assetBundle);
$this->registerClientOptions($name);
$this->registerClientEvents($name);
$this->registerClientOptions($name, $id);
$this->registerClientEvents($name, $id);
}
}