-
Notifications
You must be signed in to change notification settings - Fork 10
/
DatePicker.php
306 lines (265 loc) · 11 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* @copyright Copyright (c) 2015 Roman Ovchinnikov
* @link https://github.com/RomeroMsk
* @version 1.1.0
*/
namespace nex\datepicker;
use Yii;
use yii\bootstrap\ButtonDropdown;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use yii\widgets\InputWidget;
/**
* DatePicker renders a DatePicker input.
*
* @author Roman Ovchinnikov <[email protected]>
* @link https://github.com/RomeroMsk/yii2-datepicker
*/
class DatePicker extends InputWidget
{
/**
* The name of the jQuery plugin to use for this widget.
*/
const PLUGIN_NAME = 'datetimepicker';
/**
* @var mixed the addon markup if you wish to display the input as a component. If you don't wish to render as a
* component then set it to null or false. But remember that plugin requires a relative positioned container.
*/
public $addon = '<span class="input-group-addon" style="border-left: none;"><i class="glyphicon glyphicon-calendar"></i></span>';
/**
* @var array list of menu items for button dropdown. This dropdown is used to set predefined values of the input.
* If this property is empty or [[addon]] is not set, no button dropdown will be rendered.
* Please refer to the yii\bootstrap\Dropdown::$items documentation for accepted structure.
* Add 'value' element to each item to define value which will be set to input.
*
* ~~~
* $dropdownItems = [
* ['label' => 'Yesterday', 'url' => '#', 'value' => \Yii::$app->formatter->asDate('-1 day')],
* ['label' => 'Tomorrow', 'url' => '#', 'value' => \Yii::$app->formatter->asDate('+1 day')],
* ['label' => 'Some value', 'url' => '#', 'value' => 'Special value'],
* ]
* ~~~
*/
public $dropdownItems = [];
/**
* @var string the template to render the input.
*/
public $template = '{input}{addon}{dropdown}';
/**
* @var boolean whether to set readonly attribute to the input.
*/
public $readonly = false;
/**
* @var string the language to use.
*/
public $language;
/**
* @var array the options for the Bootstrap DatePicker plugin.
* Please refer to the [Datepicker options](http://eonasdan.github.io/bootstrap-datetimepicker/Options/) web page for possible options.
*/
public $clientOptions = [];
/**
* @var array the default options for the Bootstrap DatePicker plugin. Widget will merge [[clientOptions]] with [[defaultClientOptions]].
* Please refer to the [Datepicker options](http://eonasdan.github.io/bootstrap-datetimepicker/Options/) web page for possible options.
*/
public $defaultClientOptions = [
// Show timepicker in the right part of popup
'sideBySide' => true,
// Don't change empty value on picker show
'useCurrent' => false,
// Show picker on input focus
'allowInputToggle' => true,
// Show toolbar at the bottom and some buttons by default
'toolbarPlacement' => 'bottom',
'showTodayButton' => true,
'showClear' => true,
// Don't reset invalid input values
'keepInvalid' => true,
// Use strict mode when trying to parse value as moment
'useStrict' => true,
];
/**
* @var array the event handlers for the Bootstrap 3 DatePicker plugin.
* Please refer to the [Datepicker events](http://eonasdan.github.io/bootstrap-datetimepicker/Events/) web page for possible events.
*
* ~~~
* [
* 'dp.show' => new \yii\web\JsExpression("function () { console.log('It works!'); }"),
* ]
* ~~~
*/
public $clientEvents = [];
/**
* @var string the size of the input ('lg', 'md', 'sm', 'xs').
*/
public $size;
/**
* @var string the placeholder of the input.
*/
public $placeholder;
/**
* @var array HTML attributes to render the container.
*/
public $containerOptions = [];
/**
* @var boolean whether to show decades view in Datepicker. Set it to false to hide this view.
* Author of original plugin is ignoring requests to add an option for this, so we will hide it via the hack.
* @see https://github.com/Eonasdan/bootstrap-datetimepicker/issues/1226
*/
public $showDecades = true;
/**
* @var string the hashed variable to store the pluginOptions.
*/
private $_hashVar;
/**
* Registers widget translations.
*/
private function registerTranslations()
{
Yii::$app->i18n->translations['datepicker'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
'forceTranslation' => true,
];
$this->clientOptions['tooltips'] = [
'today' => Yii::t('datepicker', 'Go to today'),
'clear' => Yii::t('datepicker', 'Clear selection'),
'close' => Yii::t('datepicker', 'Close the picker'),
'selectMonth' => Yii::t('datepicker', 'Select Month'),
'prevMonth' => Yii::t('datepicker', 'Previous Month'),
'nextMonth' => Yii::t('datepicker', 'Next Month'),
'selectYear' => Yii::t('datepicker', 'Select Year'),
'prevYear' => Yii::t('datepicker', 'Previous Year'),
'nextYear' => Yii::t('datepicker', 'Next Year'),
'selectDecade' => Yii::t('datepicker', 'Select Decade'),
'prevDecade' => Yii::t('datepicker', 'Previous Decade'),
'nextDecade' => Yii::t('datepicker', 'Next Decade'),
'prevCentury' => Yii::t('datepicker', 'Previous Century'),
'nextCentury' => Yii::t('datepicker', 'Next Century'),
'pickHour' => Yii::t('datepicker', 'Pick Hour'),
'incrementHour' => Yii::t('datepicker', 'Increment Hour'),
'decrementHour' => Yii::t('datepicker', 'Decrement Hour'),
'pickMinute' => Yii::t('datepicker', 'Pick Minute'),
'incrementMinute' => Yii::t('datepicker', 'Increment Minute'),
'decrementMinute' => Yii::t('datepicker', 'Decrement Minute'),
'pickSecond' => Yii::t('datepicker', 'Pick Second'),
'incrementSecond' => Yii::t('datepicker', 'Increment Second'),
'decrementSecond' => Yii::t('datepicker', 'Decrement Second'),
'togglePeriod' => Yii::t('datepicker', 'Toggle Period'),
'selectTime' => Yii::t('datepicker', 'Select Time'),
];
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->registerTranslations();
$this->clientOptions = ArrayHelper::merge($this->defaultClientOptions, $this->clientOptions);
if ($this->language !== null) {
$this->clientOptions['locale'] = $this->language;
}
if ($this->size) {
Html::addCssClass($this->options, 'input-' . $this->size);
Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
}
if ($this->readonly) {
$this->options['readonly'] = true;
$this->clientOptions['ignoreReadonly'] = true;
}
if ($this->placeholder) {
$this->options['placeholder'] = $this->placeholder;
}
Html::addCssClass($this->options, 'form-control');
Html::addCssClass($this->containerOptions, ['input-group', 'nex-datepicker-container', '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->addon) {
if (!empty($this->dropdownItems)) {
foreach ($this->dropdownItems as &$item) {
if ($value = ArrayHelper::remove($item, 'value')) {
$item['linkOptions']['data-value'] = $value;
}
}
$dropdown = ButtonDropdown::widget([
'label' => '',
'containerOptions' => [
'class' => [
'widget' => 'input-group-btn',
],
],
'options' => [
'class' => 'btn-default',
'type' => 'button',
],
'dropdown' => [
'items' => $this->dropdownItems,
],
]);
} else {
$dropdown = null;
}
$input = strtr($this->template, ['{input}' => $input, '{addon}' => $this->addon, '{dropdown}' => $dropdown]);
$input = Html::tag('div', $input, $this->containerOptions);
}
$this->registerClientScript();
return $input;
}
/**
* Generates a hashed variable to store the plugin `clientOptions`. Helps in reusing the variable for similar
* options passed for other widgets on the same page. The following special data attribute will also be
* added to the input field to allow accessing the client options via javascript:
*
* - 'data-plugin-inputmask' will store the hashed variable storing the plugin options.
*
* @param View $view the view instance
* @author [Thiago Talma](https://github.com/thiagotalma)
*/
protected function hashPluginOptions($view)
{
$encOptions = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
$this->_hashVar = self::PLUGIN_NAME . '_' . hash('crc32', $encOptions);
$this->options['data-plugin-' . self::PLUGIN_NAME] = $this->_hashVar;
$view->registerJs("var {$this->_hashVar} = {$encOptions};\n", View::POS_HEAD);
}
/**
* Registers required script for the plugin to work as DatePicker.
*/
public function registerClientScript()
{
$js = [];
$view = $this->getView();
DatePickerAsset::register($view);
$id = $this->options['id'];
$selector = ";jQuery('#$id')";
if ($this->addon) {
$selector .= ".parent()";
}
$this->hashPluginOptions($view);
$js[] = "$selector." . self::PLUGIN_NAME . "({$this->_hashVar});";
if (!empty($this->dropdownItems)) {
$js[] = "$selector.find('.dropdown-menu a').on('click', function (e) { e.preventDefault(); jQuery('#$id').val(jQuery(this).data('value')); });";
}
if ($this->showDecades === false) {
$js[] = "$selector.on('dp.show dp.update', function () { $(this).find('.datepicker-years .picker-switch').removeAttr('title').css('cursor', 'default').css('background', 'inherit').on('click', function (e) { e.stopPropagation(); }); });";
}
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "$selector.on('$event', $handler);";
}
}
$view->registerJs(implode("\n", $js) . "\n");
}
}