Skip to content

Commit fc3a93c

Browse files
committed
Improve comments and add checking $theme type.
1 parent 1a42ca5 commit fc3a93c

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

DatePicker.php

+23-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use yii\helpers\Json;
66
use yii\helpers\Html;
7+
use yii\base\InvalidConfigException;
78
use yii\widgets\InputWidget;
89
/**
910
* Jalali date & time.
@@ -16,13 +17,26 @@ class DatePicker extends InputWidget
1617
* @var array Date picker options.
1718
*/
1819
public $clientOptions = ['formatDate' => "YYYY/0M/0D"];
19-
20+
/**
21+
* @var string Theme name.
22+
* Date picker currently have 2 theme : dark, default.
23+
*/
2024
public $theme = 'default';
25+
/**
26+
* @inheritdoc
27+
*/
2128
public function init()
2229
{
2330
parent::init();
31+
if(! is_string($this->theme)) {
32+
throw new InvalidConfigException(
33+
"Theme name must be a string."
34+
);
35+
}
2436
}
25-
37+
/**
38+
* Executes the widget.
39+
*/
2640
function run()
2741
{
2842
echo $this->renderInput();
@@ -44,7 +58,7 @@ function registerDarkThemeAsset()
4458
DatePickerDarkThemeAsset::register($this->getView());
4559
}
4660
/**
47-
* Register datepicker assets without into view.
61+
* Register datepicker assets without css into view.
4862
*/
4963
function registerNoThemeAsset()
5064
{
@@ -67,24 +81,25 @@ function renderInput()
6781
*/
6882
function renderJsCode()
6983
{
84+
//Jquery plugin name.
7085
$name = 'persianDatepicker';
7186

7287
$id = $this->options['id'];
73-
88+
// Set theme name in clientOptions.
7489
$this->clientOptions['theme'] = $this->theme;
75-
90+
// Build onSelect event.
7691
if(! isset($this->clientOptions['onSelect'])) {
7792
$this->clientOptions['onSelect'] = "function(){
7893
$('#$id').trigger('change');
7994
}";
8095
}
81-
96+
// Register theme assets.
8297
if(in_array($this->theme, ['default', 'dark'])) {
8398
$this->{"register" . ucfirst($this->theme) . "ThemeAsset"}();
8499
} else {
85100
$this->registerNoThemeAsset();
86101
}
87-
102+
// Add events.
88103
$onSelect = $this->clientOptions['onSelect'];
89104

90105
if(isset($this->clientOptions['onShow'])) {
@@ -116,7 +131,7 @@ function renderJsCode()
116131
$options .= '}';
117132
}
118133
$js = "jQuery('#$id').$name($options);";
119-
134+
// Register js code.
120135
$this->getView()->registerJs($js);
121136
}
122137
}

0 commit comments

Comments
 (0)