forked from alxgh/yii2-jdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatePicker.php
122 lines (102 loc) · 2.53 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
<?php
namespace jDate;
use yii\helpers\Json;
use yii\helpers\Html;
use yii\widgets\InputWidget;
/**
* Jalali date & time.
* @author Mohammad Mahdi Gholomian.
* @copyright 2014 [email protected]
*/
class DatePicker extends InputWidget
{
/**
* @var array Date picker options.
*/
public $clientOptions = ['formatDate' => "YYYY/0M/0D"];
public $theme = 'default';
public function init()
{
parent::init();
}
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 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()
{
$name = 'persianDatepicker';
$id = $this->options['id'];
$this->clientOptions['theme'] = $this->theme;
if(! isset($this->clientOptions['onSelect'])) {
$this->clientOptions['onSelect'] = "function(){
$('#$id').trigger('change');
}";
}
if(in_array($this->theme, ['default', 'dark'])) {
$this->{"register" . ucfirst($this->theme) . "ThemeAsset"}();
} else {
$this->registerNoThemeAsset();
}
$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);";
$this->getView()->registerJs($js);
}
}