From 91f6c6cb00948f5ac3a97f550a05727e12f6efa9 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 23 May 2013 20:03:59 +0400 Subject: [PATCH 1/5] jQuery UI DatePicker widget --- framework/yii/jui/DatePicker.php | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 framework/yii/jui/DatePicker.php diff --git a/framework/yii/jui/DatePicker.php b/framework/yii/jui/DatePicker.php new file mode 100644 index 0000000..4f68d56 --- /dev/null +++ b/framework/yii/jui/DatePicker.php @@ -0,0 +1,101 @@ + 'yii/jui/datepicker/i18n/ru', + * 'model' => $model, + * 'attribute' => 'country', + * 'clientOptions' => array( + * 'dateFormat' => 'yy-mm-dd', + * ), + * )); + * ``` + * + * The following example will use the name property instead: + * + * ```php + * echo DatePicker::widget(array( + * 'language' => 'yii/jui/datepicker/i18n/ru', + * 'name' => 'country', + * 'clientOptions' => array( + * 'dateFormat' => 'yy-mm-dd', + * ), + * )); + *``` + * + * @see http://api.jqueryui.com/datepicker/ + * @author Alexander Kochetov + * @since 2.0 + */ +class DatePicker extends Widget +{ + /** + * @var string the jQuery UI datepicker widget language bundle. + */ + public $language = false; + /** + * @var \yii\base\Model the data model that this widget is associated with. + */ + public $model; + /** + * @var string the model attribute that this widget is associated with. + */ + public $attribute; + /** + * @var string the input name. This must be set if [[model]] and [[attribute]] are not set. + */ + public $name; + /** + * @var string the input value. + */ + public $value; + + + /** + * Renders the widget. + */ + public function run() + { + echo $this->renderField(); + $this->registerWidget('datepicker'); + if ($this->language !== false) { + $this->getView()->registerAssetBundle($this->language); + } + } + + /** + * Renders the DatePicker field. If [[model]] has been specified then it will render an active field. + * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to + * the [[name]] attribute. + * @return string the rendering result. + * @throws InvalidConfigException when none of the required attributes are set to render the textInput. + * That is, if [[model]] and [[attribute]] are not set, then [[name]] is required. + */ + public function renderField() + { + if ($this->model instanceof Model && $this->attribute !== null) { + return Html::activeTextInput($this->model, $this->attribute, $this->options); + } elseif ($this->name !== null) { + return Html::textInput($this->name, $this->value, $this->options); + } else { + throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified."); + } + } +} From 548643df0e032e2263a71101a6525d8056d4acac Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sat, 25 May 2013 18:29:44 +0400 Subject: [PATCH 2/5] InputWidget base class --- framework/yii/jui/InputWidget.php | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 framework/yii/jui/InputWidget.php diff --git a/framework/yii/jui/InputWidget.php b/framework/yii/jui/InputWidget.php new file mode 100644 index 0000000..e100d6c --- /dev/null +++ b/framework/yii/jui/InputWidget.php @@ -0,0 +1,59 @@ + + * @since 2.0 + */ +class InputWidget extends Widget +{ + /** + * @var Model the data model that this widget is associated with. + */ + public $model; + /** + * @var string the model attribute that this widget is associated with. + */ + public $attribute; + /** + * @var string the input name. This must be set if [[model]] and [[attribute]] are not set. + */ + public $name; + /** + * @var string the input value. + */ + public $value; + + + /** + * Initializes the widget. + * If you override this method, make sure you call the parent implementation first. + */ + public function init() + { + if (!$this->hasModel() && $this->name === null) { + throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified."); + } + parent::init(); + } + + /** + * @return boolean whether this widget is associated with a data model. + */ + protected function hasModel() + { + return $this->model instanceof Model && $this->attribute !== null; + } +} From 1b417664608f684892b9986e8df2370c9826bff6 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sat, 25 May 2013 18:38:52 +0400 Subject: [PATCH 3/5] DatePicker rework --- framework/yii/jui/DatePicker.php | 55 +++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/framework/yii/jui/DatePicker.php b/framework/yii/jui/DatePicker.php index 4f68d56..6e48418 100644 --- a/framework/yii/jui/DatePicker.php +++ b/framework/yii/jui/DatePicker.php @@ -8,8 +8,6 @@ namespace yii\jui; use Yii; -use yii\base\InvalidConfigException; -use yii\base\Model; use yii\helpers\Html; /** @@ -44,28 +42,16 @@ use yii\helpers\Html; * @author Alexander Kochetov * @since 2.0 */ -class DatePicker extends Widget +class DatePicker extends InputWidget { /** * @var string the jQuery UI datepicker widget language bundle. */ public $language = false; /** - * @var \yii\base\Model the data model that this widget is associated with. + * @var boolean If true, shows the widget as an inline calendar and the input as a hidden field. */ - public $model; - /** - * @var string the model attribute that this widget is associated with. - */ - public $attribute; - /** - * @var string the input name. This must be set if [[model]] and [[attribute]] are not set. - */ - public $name; - /** - * @var string the input value. - */ - public $value; + public $flat = false; /** @@ -73,7 +59,7 @@ class DatePicker extends Widget */ public function run() { - echo $this->renderField(); + echo $this->renderWidget() . "\n"; $this->registerWidget('datepicker'); if ($this->language !== false) { $this->getView()->registerAssetBundle($this->language); @@ -81,21 +67,32 @@ class DatePicker extends Widget } /** - * Renders the DatePicker field. If [[model]] has been specified then it will render an active field. - * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to - * the [[name]] attribute. + * Renders the DatePicker widget. * @return string the rendering result. - * @throws InvalidConfigException when none of the required attributes are set to render the textInput. - * That is, if [[model]] and [[attribute]] are not set, then [[name]] is required. */ - public function renderField() + protected function renderWidget() { - if ($this->model instanceof Model && $this->attribute !== null) { - return Html::activeTextInput($this->model, $this->attribute, $this->options); - } elseif ($this->name !== null) { - return Html::textInput($this->name, $this->value, $this->options); + $contents = array(); + + if ($this->flat === false) { + if ($this->hasModel()) { + $contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options); + } else { + $contents[] = Html::textInput($this->name, $this->value, $this->options); + } } else { - throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified."); + if ($this->hasModel()) { + $contents[] = Html::activeHiddenInput($this->model, $this->attribute, $this->options); + $this->clientOptions['defaultDate'] = $this->model->{$this->attribute}; + } else { + $contents[] = Html::hiddenInput($this->name, $this->value, $this->options); + $this->clientOptions['defaultDate'] = $this->value; + } + $this->clientOptions['altField'] = '#' . $this->options['id']; + $this->options['id'] .= '-container'; + $contents[] = Html::tag('div', null, $this->options); } + + return implode("\n", $contents); } } From ca86a3e27e64700d7d8e33441326e60e9d017cd3 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sat, 25 May 2013 20:44:40 +0400 Subject: [PATCH 4/5] flat => inline --- framework/yii/jui/DatePicker.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/yii/jui/DatePicker.php b/framework/yii/jui/DatePicker.php index 6e48418..17c36f8 100644 --- a/framework/yii/jui/DatePicker.php +++ b/framework/yii/jui/DatePicker.php @@ -51,7 +51,7 @@ class DatePicker extends InputWidget /** * @var boolean If true, shows the widget as an inline calendar and the input as a hidden field. */ - public $flat = false; + public $inline = false; /** @@ -74,7 +74,7 @@ class DatePicker extends InputWidget { $contents = array(); - if ($this->flat === false) { + if ($this->inline === false) { if ($this->hasModel()) { $contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options); } else { From 594341fb163c79f976533d4ece673c9010130869 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sat, 25 May 2013 22:33:50 +0400 Subject: [PATCH 5/5] language as code, not bundle --- framework/yii/jui/DatePicker.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/framework/yii/jui/DatePicker.php b/framework/yii/jui/DatePicker.php index 17c36f8..1138b73 100644 --- a/framework/yii/jui/DatePicker.php +++ b/framework/yii/jui/DatePicker.php @@ -17,7 +17,7 @@ use yii\helpers\Html; * * ```php * echo DatePicker::widget(array( - * 'language' => 'yii/jui/datepicker/i18n/ru', + * 'language' => 'ru', * 'model' => $model, * 'attribute' => 'country', * 'clientOptions' => array( @@ -30,7 +30,7 @@ use yii\helpers\Html; * * ```php * echo DatePicker::widget(array( - * 'language' => 'yii/jui/datepicker/i18n/ru', + * 'language' => 'ru', * 'name' => 'country', * 'clientOptions' => array( * 'dateFormat' => 'yy-mm-dd', @@ -45,7 +45,8 @@ use yii\helpers\Html; class DatePicker extends InputWidget { /** - * @var string the jQuery UI datepicker widget language bundle. + * @var string the locale ID (eg 'fr', 'de') for the language to be used by the date picker. + * If this property set to false, I18N will not be involved. That is, the date picker will show in English. */ public $language = false; /** @@ -62,7 +63,7 @@ class DatePicker extends InputWidget echo $this->renderWidget() . "\n"; $this->registerWidget('datepicker'); if ($this->language !== false) { - $this->getView()->registerAssetBundle($this->language); + $this->getView()->registerAssetBundle("yii/jui/datepicker/i18n/$this->language"); } }