@@ -66,7 +66,8 @@ class DatePicker extends \kartik\base\InputWidget
66
66
* - if this is passed as an array (this is the DEFAULT) it will treat this as HTML attributes
67
67
* for the button (to be displayed as a Bootstrap addon). The following special keys are recognized;
68
68
* - icon - string, the bootstrap glyphicon name/suffix. Defaults to 'calendar'.
69
- * - title - string, the title to be displayed on hover. Defaults to 'Select date & time'.
69
+ * - title - string|bool, the title to be displayed on hover. Defaults to 'Select date & time'. To disable,
70
+ * set it to `false`.
70
71
*/
71
72
public $ pickerButton = [];
72
73
@@ -78,7 +79,8 @@ class DatePicker extends \kartik\base\InputWidget
78
79
* - if this is passed as an array (this is the DEFAULT) it will treat this as HTML attributes
79
80
* for the button (to be displayed as a Bootstrap addon). The following special keys are recognized;
80
81
* - icon - string, the bootstrap glyphicon name/suffix. Defaults to 'remove'.
81
- * - title - string, the title to be displayed on hover. Defaults to 'Clear field'.
82
+ * - title - string, the title to be displayed on hover. Defaults to 'Clear field'. To disable,
83
+ * set it to `false`.
82
84
*/
83
85
public $ removeButton = [];
84
86
@@ -88,10 +90,18 @@ class DatePicker extends \kartik\base\InputWidget
88
90
public $ options = [];
89
91
90
92
/**
91
- * @var string The addon that will be prepended/appended for a
92
- * [[TYPE_COMPONENT_PREPEND]] and [[TYPE_COMPONENT_APPEND]]
93
+ * @var array The addon that will be prepended/appended for a [[TYPE_COMPONENT_PREPEND]] and
94
+ * [[TYPE_COMPONENT_APPEND]]. You can set the following array keys:
95
+ * - part1: string, the content to prepend before the [[TYPE_COMPONENT_PREPEND]] OR
96
+ * before input # 1 for [[TYPE_RANGE]].
97
+ * - part2: string, the content to prepend after the [[TYPE_COMPONENT_PREPEND]] OR
98
+ * after input # 1 for [[TYPE_RANGE]].
99
+ * - part3: string, the content to append before the [[TYPE_COMPONENT_APPEND]] OR
100
+ * before input # 2 for [[TYPE_RANGE]].
101
+ * - part4: string, the content to append after the [[TYPE_COMPONENT_APPEND]] OR
102
+ * after input # 2 for [[TYPE_RANGE]].
93
103
*/
94
- public $ addon = self :: CALENDAR_ICON ;
104
+ public $ addon = [] ;
95
105
96
106
/**
97
107
* @var string the model attribute 2 if you are using [[TYPE_RANGE]]
@@ -168,6 +178,9 @@ public function init()
168
178
if (isset ($ this ->form ) && ($ this ->type === self ::TYPE_RANGE ) && (!isset ($ this ->attribute2 ))) {
169
179
throw new InvalidConfigException ("The 'attribute2' property must be set for a 'range' type markup and a defined 'form' property. " );
170
180
}
181
+ if (isset ($ this ->addon ) && !is_array ($ this ->addon )) {
182
+ throw new InvalidConfigException ("The 'addon' property must be setup as an array with 'part1', 'part2', 'part3', and/or 'part4' keys. " );
183
+ }
171
184
$ s = DIRECTORY_SEPARATOR ;
172
185
$ this ->initI18N (__DIR__ );
173
186
$ this ->setLanguage ('bootstrap-datepicker. ' , __DIR__ . "{$ s }assets {$ s }" , null , '.min.js ' );
@@ -222,11 +235,9 @@ protected function renderAddon(&$options, $type = 'picker')
222
235
$ icon = ($ type === 'picker ' ) ? 'calendar ' : 'remove ' ;
223
236
Html::addCssClass ($ options , 'input-group-addon kv-date- ' . $ icon );
224
237
$ icon = '<i class="glyphicon glyphicon- ' . ArrayHelper::remove ($ options , 'icon ' , $ icon ) . '"></i> ' ;
225
- if (empty ($ options ['title ' ])) {
226
- $ title = ($ type === 'picker ' ) ? Yii::t ('kvdate ' , 'Select date ' ) : Yii::t ('kvdate ' , 'Clear field ' );
227
- if ($ title != false ) {
228
- $ options ['title ' ] = $ title ;
229
- }
238
+ $ title = ArrayHelper::getValue ($ options , 'title ' , '' );
239
+ if ($ title !== false && empty ($ title )) {
240
+ $ options ['title ' ] = ($ type === 'picker ' ) ? Yii::t ('kvdate ' , 'Select date ' ) : Yii::t ('kvdate ' , 'Clear field ' );
230
241
}
231
242
return Html::tag ('span ' , $ icon , $ options );
232
243
}
@@ -252,13 +263,21 @@ protected function parseMarkup($input)
252
263
if ($ this ->type == self ::TYPE_INPUT ) {
253
264
return $ input ;
254
265
}
266
+ $ part1 = $ part2 = $ part3 = $ part4 = '' ;
267
+ if (!empty ($ this ->addon ) && ($ this ->_hasAddon || $ this ->type == self ::TYPE_RANGE )) {
268
+ $ part1 = ArrayHelper::getValue ($ this ->addon , 'part1 ' , '' );
269
+ $ part2 = ArrayHelper::getValue ($ this ->addon , 'part2 ' , '' );
270
+ $ part3 = ArrayHelper::getValue ($ this ->addon , 'part3 ' , '' );
271
+ $ part4 = ArrayHelper::getValue ($ this ->addon , 'part4 ' , '' );
272
+ }
255
273
if ($ this ->_hasAddon ) {
256
274
Html::addCssClass ($ this ->_container , 'date ' );
257
275
$ picker = $ this ->renderAddon ($ this ->pickerButton );
258
276
$ remove = $ this ->renderAddon ($ this ->removeButton , 'remove ' );
259
- $ content = $ picker . $ remove . $ input ;
260
277
if ($ this ->type == self ::TYPE_COMPONENT_APPEND ) {
261
- $ content = $ input . $ remove . $ picker ;
278
+ $ content = $ part1 . $ part2 . $ input . $ part3 . $ remove . $ picker . $ part4 ;
279
+ } else {
280
+ $ content = $ part1 . $ picker . $ remove . $ part2 . $ input . $ part3 . $ part4 ;
262
281
}
263
282
return Html::tag ('div ' , $ content , $ this ->_container );
264
283
}
@@ -297,7 +316,9 @@ protected function parseMarkup($input)
297
316
Html::activeTextInput ($ this ->model , $ this ->attribute2 , $ this ->options2 ) :
298
317
Html::textInput ($ this ->name2 , $ this ->value2 , $ this ->options2 );
299
318
}
300
- return Html::tag ('div ' , "{$ input }<span class='input-group-addon kv-field-separator'> {$ this ->separator }</span> {$ input2 }" , $ this ->_container );
319
+ $ content = $ part1 . $ input . $ part2 . "<span class='input-group-addon kv-field-separator'> {$ this ->separator }</span> " .
320
+ $ part3 . $ input2 . $ part4 ;
321
+ return Html::tag ('div ' , $ content , $ this ->_container );
301
322
}
302
323
if ($ this ->type == self ::TYPE_INLINE ) {
303
324
$ this ->_id = $ this ->options ['id ' ] . '-inline ' ;
@@ -332,13 +353,15 @@ public function registerAssets()
332
353
} else {
333
354
$ this ->registerPlugin ($ this ->pluginName , "{$ id }.parent() " );
334
355
}
335
- if ($ this ->removeButton !== false && $ this ->_hasAddon ) {
336
- $ view ->registerJs ("{$ id }.parent().find('.kv-date-remove').on('click', function() {
337
- {$ id }.parent(). {$ this ->pluginName }('clearDates');
338
- }); " );
356
+ if ($ this ->_hasAddon && $ this ->removeButton !== false ) {
357
+ $ view ->registerJs ("initDPRemove(' " . $ this ->options ['id ' ] . "'); " );
358
+ }
359
+ if ($ this ->_hasAddon && !empty ($ this ->addon )) {
360
+ $ view ->registerJs ("initDPAddon(' " . $ this ->options ['id ' ] . "'); " );
339
361
}
340
362
if ($ this ->type === self ::TYPE_RANGE ) {
341
363
\kartik \field \FieldRangeAsset::register ($ view );
364
+ $ view ->registerJs ("initDPRemove(' " . $ this ->options ['id ' ] . "', true); " );
342
365
}
343
366
}
344
367
}
0 commit comments