-
Notifications
You must be signed in to change notification settings - Fork 2
/
Datepicker.php
200 lines (170 loc) · 5.76 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* @link https://github.com/mrlco/yii2-jalali-datepicker#readme
* @license https://github.com/mrlco/yii2-jalali-datepicker/blob/master/LICENSE
* @copyright Copyright (c) 2015 Mrlco
*/
namespace mrlco\datepicker;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* Datepicker renders a Persian Datepicker input.
*
* @author Mehran Barzandeh <[email protected]>
* @see http://babakhani.github.io/PersianWebToolkit/doc/datepicker/
*/
class Datepicker extends InputWidget
{
/**
* @var array the options for the Persian Bootstrap DatePicker plugin.
* Please refer to the Persian Bootstrap DatePicker plugin Web page for possible options.
* @see http://babakhani.github.io/PersianWebToolkit/doc/datepicker/#/options/
*/
public $clientOptions = [];
/**
* @var array the event handlers for the underlying Persian Bootstrap DatePicker plugin.
* Please refer to the [DatePicker](http://babakhani.github.io/PersianWebToolkit/doc/datepicker/#/methods/) plugin
* Web page for possible events.
*/
public $clientEvents = [];
/**
* @var array HTML attributes to render on the container
*/
public $containerOptions = [];
/**
* @var string the Persian Bootstrap DatePicker plugin's theme.
*/
public $theme = 'default';
/**
* @var string the size of the input ('lg', 'sm')
*/
public $size;
/**
* @var string the prepend addon markup if you wish to display the input as a component then set it to
* something like '<span class="input-group-text">Sample Text</span>'.
*/
public $prependAddon = false;
/**
* @var string the append addon markup if you wish to display the input as a component then set it to
* something like '<span class="input-group-text">Sample Text</span>'.
*/
public $appendAddon = false;
/**
* @var string the template to render the input.
*/
public $template = '{prepend}{input}{append}';
/**
* @var bool whether to render the input as an inline calendar
*/
public $inline = false;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->inline) {
$this->options['readonly'] = 'readonly';
Html::addCssClass($this->options, 'text-center');
}
if ($this->size) {
Html::addCssClass($this->options, 'input-' . $this->size);
Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
}
Html::addCssClass($this->options, 'form-control');
Html::addCssClass($this->containerOptions, 'input-group date');
}
/**
* @inheritdoc
*/
public function run()
{
$input = $this->hasModel()
? Html::activeTextInput($this->model, $this->attribute, $this->options)
: Html::textInput($this->name, $this->value, $this->options);
if ($this->inline) {
$input .= '<div></div>';
}
if (($this->prependAddon || $this->appendAddon) && !$this->inline) {
$prepend = $append = '';
if ($this->prependAddon) {
$prepend = Html::tag('span', $this->prependAddon, ['class' => 'input-group-prepend']);
}
if ($this->appendAddon) {
$append = Html::tag('span', $this->appendAddon, ['class' => 'input-group-append']);
}
$input = strtr($this->template, ['{prepend}' => $prepend, '{input}' => $input, '{append}' => $append]);
$input = Html::tag('div', $input, $this->containerOptions);
}
if ($this->inline) {
$input = strtr($this->template, ['{input}' => $input, '{addon}' => '']);
}
echo $input;
$this->registerClientScript();
}
/**
* Registers required script for the plugin to work as DatePicker
*/
public function registerClientScript()
{
$js = [];
$view = $this->getView();
$this->clientOptions['theme'] = $this->theme;
DateAsset::register($view);
if ($this->theme === 'default') {
DatepickerAsset::register($view);
} else {
$this->{'registerDatepicker' . ucfirst($this->theme) . 'ThemeAsset'}($view);
}
$id = $this->options['id'];
$selector = ";jQuery('#$id')";
if ($this->inline) {
$selector .= ".parent()";
}
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
$js[] = "$selector.pDatepicker($options);";
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "$selector.on('$event', $handler);";
}
}
$view->registerJs(implode("\n", $js));
}
/**
* Register Persian Bootstrap Datepicker Blue theme asset into view.
*
* @param $view
*/
protected function registerDatepickerBlueThemeAsset($view)
{
DatepickerBlueThemeAsset::register($view);
}
/**
* Register Persian Bootstrap Datepicker Cheerup theme asset into view.
*
* @param $view
*/
protected function registerDatepickerCheerupThemeAsset($view)
{
DatepickerCheerupThemeAsset::register($view);
}
/**
* Register Persian Bootstrap Datepicker Dark theme asset into view.
*
* @param $view
*/
protected function registerDatepickerDarkThemeAsset($view)
{
DatepickerDarkThemeAsset::register($view);
}
/**
* Register Persian Bootstrap Datepicker Redblack theme asset into view.
*
* @param $view
*/
protected function registerDatepickerRedblackThemeAsset($view)
{
DatepickerRedblackThemeAsset::register($view);
}
}