Commit 0ff8518c by Qiang Xue

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

parent a126419e
......@@ -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
-----------------------------
......
......@@ -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);
......
......@@ -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();
}
......
......@@ -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"));');
}
}
......@@ -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);
}
}
......@@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Bug #1500: Log messages exported to files are not separated by newlines (omnilight, qiangxue)
- Bug #1509: The SQL for creating Postgres RBAC tables is incorrect (qiangxue)
- Bug #1545: It was not possible to execute db Query twice, params where missing (cebe)
- Bug #1550: fixed the issue that JUI input widgets did not property input IDs.
- Bug #1591: StringValidator is accessing undefined property (qiangxue)
- Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark)
- Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark)
......@@ -24,7 +25,8 @@ Yii Framework 2 Change Log
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
- Enh: Support for file aliases in console command 'message' (omnilight)
- Enh: Sort and Paginiation can now create absolute URLs (cebe)
- Chg: Renamed yii\jui\Widget::clientEventsMap to clientEventMap (qiangxue)
- Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue)
- Chg: Added `yii\widgets\InputWidget::options` (qiangxue)
- New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul)
- New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo)
......
......@@ -39,10 +39,6 @@ class Captcha extends InputWidget
*/
public $captchaAction = 'site/captcha';
/**
* @var array HTML attributes to be applied to the text input field.
*/
public $options = [];
/**
* @var array HTML attributes to be applied to the CAPTCHA image tag.
*/
public $imageOptions = [];
......@@ -62,9 +58,6 @@ class Captcha extends InputWidget
$this->checkRequirements();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
if (!isset($this->imageOptions['id'])) {
$this->imageOptions['id'] = $this->options['id'] . '-image';
}
......
......@@ -11,6 +11,7 @@ use Yii;
use yii\base\Widget;
use yii\base\Model;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
/**
* InputWidget is the base class for widgets that collect user inputs.
......@@ -40,6 +41,10 @@ class InputWidget extends Widget
* @var string the input value.
*/
public $value;
/**
* @var array the HTML attributes for the input tag.
*/
public $options = [];
/**
......@@ -49,7 +54,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 (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
parent::init();
}
......
......@@ -61,10 +61,6 @@ class MaskedInput extends InputWidget
* @var string a JavaScript function callback that will be invoked when user finishes the input.
*/
public $completed;
/**
* @var array the HTML attributes for the input tag.
*/
public $options = [];
/**
......@@ -77,10 +73,6 @@ class MaskedInput extends InputWidget
if (empty($this->mask)) {
throw new InvalidConfigException('The "mask" property must be set.');
}
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment