forked from alxgh/yii2-jdate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatePicker.php
137 lines (124 loc) · 2.99 KB
/
DatePicker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace mjm\jdate;
use yii\helpers\Json;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use yii\widgets\InputWidget;
/**
* Jalali date & time.
* @author Mohammad Mahdi Gholomian.
* @copyright 2014 mm.gholamian@yahoo.com
*/
class DatePicker extends InputWidget
{
/**
* @var array Date picker options.
*/
public $clientOptions = ['formatDate' => "YYYY/0M/0D"];
/**
* @var string Theme name.
* Date picker currently have 2 theme : dark, default.
*/
public $theme = 'default';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if(! is_string($this->theme)) {
throw new InvalidConfigException(
"Theme name must be a string."
);
}
}
/**
* Executes the widget.
*/
function run()
{
echo $this->renderInput();
$this->renderJsCode();
}
/**
* Register datepicker default theme asset into view.
*/
function registerDefaultThemeAsset()
{
DatePickerDefaultThemeAsset::register($this->getView());
}
/**
* Register datepicker dark theme asset into view.
*/
function registerDarkThemeAsset()
{
DatePickerDarkThemeAsset::register($this->getView());
}
/**
* Register datepicker assets without css into view.
*/
function registerNoThemeAsset()
{
DatePickerAsset::register($this->getView());
}
/**
* Render input.
*/
function renderInput()
{
if ($this->hasModel()) {
return Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
return Html::textInput($this->name, $this->value, $this->options);
}
}
/**
* Render Js code.
*/
function renderJsCode()
{
//Jquery plugin name.
$name = 'persianDatepicker';
$id = $this->options['id'];
// Set theme name in clientOptions.
$this->clientOptions['theme'] = $this->theme;
// Build onSelect event.
if(! isset($this->clientOptions['onSelect'])) {
$this->clientOptions['onSelect'] = "function(){
$('#$id').trigger('change');
}";
}
// Register theme assets.
if(in_array($this->theme, ['default', 'dark'])) {
$this->{"register" . ucfirst($this->theme) . "ThemeAsset"}();
} else {
$this->registerNoThemeAsset();
}
// Add events.
$onSelect = $this->clientOptions['onSelect'];
if(isset($this->clientOptions['onShow'])) {
$onShow = $this->clientOptions['onSelect'];
}
if(isset($this->clientOptions['onHide'])) {
$onShow = $this->clientOptions['onHide'];
}
unset($this->clientOptions['onHide'], $this->clientOptions['onShow'], $this->clientOptions['onSelect']);
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
if(isset($onShow) || isset($onHide) || isset($onSelect)) {
$options = substr($options, 0, -1);
if(isset($onShow)) {
$options .= ', "onShow" : ' . $onShow;
}
if(isset($onHide)) {
$options .= ', "onHide" : ' . $onHide;
}
if(isset($onSelect)) {
$options .= ', "onSelect" : ' . $onSelect;
}
$options .= '}';
}
$js = "jQuery('#$id').$name($options);";
// Register js code.
$this->getView()->registerJs($js);
}
}