4
4
5
5
use yii \helpers \Json ;
6
6
use yii \helpers \Html ;
7
+ use yii \base \InvalidConfigException ;
7
8
use yii \widgets \InputWidget ;
8
9
/**
9
10
* Jalali date & time.
@@ -16,13 +17,26 @@ class DatePicker extends InputWidget
16
17
* @var array Date picker options.
17
18
*/
18
19
public $ clientOptions = ['formatDate ' => "YYYY/0M/0D " ];
19
-
20
+ /**
21
+ * @var string Theme name.
22
+ * Date picker currently have 2 theme : dark, default.
23
+ */
20
24
public $ theme = 'default ' ;
25
+ /**
26
+ * @inheritdoc
27
+ */
21
28
public function init ()
22
29
{
23
30
parent ::init ();
31
+ if (! is_string ($ this ->theme )) {
32
+ throw new InvalidConfigException (
33
+ "Theme name must be a string. "
34
+ );
35
+ }
24
36
}
25
-
37
+ /**
38
+ * Executes the widget.
39
+ */
26
40
function run ()
27
41
{
28
42
echo $ this ->renderInput ();
@@ -44,7 +58,7 @@ function registerDarkThemeAsset()
44
58
DatePickerDarkThemeAsset::register ($ this ->getView ());
45
59
}
46
60
/**
47
- * Register datepicker assets without into view.
61
+ * Register datepicker assets without css into view.
48
62
*/
49
63
function registerNoThemeAsset ()
50
64
{
@@ -67,24 +81,25 @@ function renderInput()
67
81
*/
68
82
function renderJsCode ()
69
83
{
84
+ //Jquery plugin name.
70
85
$ name = 'persianDatepicker ' ;
71
86
72
87
$ id = $ this ->options ['id ' ];
73
-
88
+ // Set theme name in clientOptions.
74
89
$ this ->clientOptions ['theme ' ] = $ this ->theme ;
75
-
90
+ // Build onSelect event.
76
91
if (! isset ($ this ->clientOptions ['onSelect ' ])) {
77
92
$ this ->clientOptions ['onSelect ' ] = "function(){
78
93
$('# $ id').trigger('change');
79
94
} " ;
80
95
}
81
-
96
+ // Register theme assets.
82
97
if (in_array ($ this ->theme , ['default ' , 'dark ' ])) {
83
98
$ this ->{"register " . ucfirst ($ this ->theme ) . "ThemeAsset " }();
84
99
} else {
85
100
$ this ->registerNoThemeAsset ();
86
101
}
87
-
102
+ // Add events.
88
103
$ onSelect = $ this ->clientOptions ['onSelect ' ];
89
104
90
105
if (isset ($ this ->clientOptions ['onShow ' ])) {
@@ -116,7 +131,7 @@ function renderJsCode()
116
131
$ options .= '} ' ;
117
132
}
118
133
$ js = "jQuery('# $ id'). $ name( $ options); " ;
119
-
134
+ // Register js code.
120
135
$ this ->getView ()->registerJs ($ js );
121
136
}
122
137
}
0 commit comments