Skip to content

Commit 5648d28

Browse files
committed
Fix #22: Ability to configure picker button options
1 parent e9a1333 commit 5648d28

File tree

6 files changed

+154
-11
lines changed

6 files changed

+154
-11
lines changed

CHANGE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ version 1.3.2
55
- (enh #21): Add new remove button to clear dates. Applicable only for following `DatePicker` types:
66
- `DatePicker::TYPE_COMPONENT_PREPEND` and
77
- `DatePicker::TYPE_COMPONENT_APPEND`
8+
- (enh #22): Ability to configure picker button options. Applicable only for following `DatePicker` types:
9+
- `DatePicker::TYPE_COMPONENT_PREPEND` and
10+
- `DatePicker::TYPE_COMPONENT_APPEND`
811

912
version 1.3.1
1013
=============

DatePicker.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ class DatePicker extends \kartik\base\InputWidget
5858
* - 'label': string the button label. Defaults to `<i class="glyphicon glyphicon-calendar"></i>`
5959
*/
6060
public $buttonOptions = [];
61+
62+
/**
63+
* @var mixed the calendar picker button configuration.
64+
* - if this is passed as a string, it will be displayed as is (will not be HTML encoded).
65+
* - if this is set to false, the picker button will not be displayed.
66+
* - if this is passed as an array (this is the DEFAULT) it will treat this as HTML attributes
67+
* for the button (to be displayed as a Bootstrap addon). The following special keys are recognized;
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'.
70+
*/
71+
public $pickerButton = [];
6172

6273
/**
6374
* @var mixed the calendar remove button configuration - applicable only for type
@@ -135,6 +146,7 @@ class DatePicker extends \kartik\base\InputWidget
135146
*/
136147
public function init()
137148
{
149+
$this->_msgCat = 'kvdate';
138150
parent::init();
139151
$this->_hasAddon = $this->type == self::TYPE_COMPONENT_PREPEND || $this->type == self::TYPE_COMPONENT_APPEND;
140152
if ($this->type === self::TYPE_RANGE && $this->attribute2 === null && $this->name2 === null) {
@@ -156,6 +168,7 @@ public function init()
156168
throw new InvalidConfigException("The 'attribute2' property must be set for a 'range' type markup and a defined 'form' property.");
157169
}
158170
$s = DIRECTORY_SEPARATOR;
171+
$this->initI18N();
159172
$this->setLanguage('bootstrap-datepicker.', __DIR__ . "{$s}assets{$s}", null, '.min.js');
160173
$this->parseDateFormat('date');
161174
$this->_id = ($this->type == self::TYPE_INPUT) ? 'jQuery("#' . $this->options['id'] . '")' : 'jQuery("#' . $this->options['id'] . '").parent()';
@@ -191,21 +204,30 @@ protected function renderInput()
191204
}
192205

193206
/**
194-
* Returns the remove button addon
207+
* Returns the addon to render
208+
*
209+
* @param array $options the HTML attributes for the addon
210+
* @param string $type whether the addon is the picker or remove
195211
* @return string
196212
*/
197-
protected function renderRemoveButton()
213+
protected function renderAddon(&$options, $type = 'picker')
198214
{
199-
$options = $this->removeButton;
200215
if ($options === false) {
201216
return '';
202217
}
203-
Html::addCssClass($options, 'input-group-addon kv-date-remove');
204-
$icon = '<i class="glyphicon glyphicon-' . ArrayHelper::remove($options, 'icon', 'remove') . '"></i>';
218+
if (is_string($options)) {
219+
return $options;
220+
}
221+
$icon = ($type === 'picker') ? 'calendar' : 'remove';
222+
Html::addCssClass($options, 'input-group-addon kv-date-' . $icon);
223+
$icon = '<i class="glyphicon glyphicon-' . ArrayHelper::remove($options, 'icon', $icon) . '"></i>';
205224
if (empty($options['title'])) {
206-
$options['title'] = Yii::t('app', 'Clear field');
225+
$title = ($type === 'picker') ? Yii::t('kvdate', 'Select date') : Yii::t('kvdate', 'Clear field');
226+
if ($title != false) {
227+
$options['title'] = $title;
228+
}
207229
}
208-
return Html::tag('span', $icon, $options);
230+
return Html::tag('span', $icon, $options);
209231
}
210232

211233
/**
@@ -231,11 +253,11 @@ protected function parseMarkup($input)
231253
}
232254
if ($this->_hasAddon) {
233255
Html::addCssClass($this->_container, 'date');
234-
$addon = "<span class='input-group-addon'>{$this->addon}</span>";
235-
$remove = $this->renderRemoveButton();
236-
$content = $addon . $remove . $input;
256+
$picker = $this->renderAddon($this->pickerButton);
257+
$remove = $this->renderAddon($this->removeButton, 'remove');
258+
$content = $picker . $remove . $input;
237259
if ($this->type == self::TYPE_COMPONENT_APPEND) {
238-
$content = $input . $remove . $addon;
260+
$content = $input . $remove . $picker;
239261
}
240262
return Html::tag('div', $content, $this->_container);
241263
}

messages/config.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
return [
4+
// string, required, root directory of all source files
5+
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..',
6+
// string, required, root directory containing message translations.
7+
'messagePath' => __DIR__,
8+
// array, required, list of language codes that the extracted messages
9+
// should be translated to. For example, ['zh-CN', 'de'].
10+
'languages' => ['en', 'de','ru'],
11+
// string, the name of the function for translating messages.
12+
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
13+
// translated. You may use a string for single function name or an array for
14+
// multiple function names.
15+
'translator' => 'Yii::t',
16+
// boolean, whether to sort messages by keys when merging new messages
17+
// with the existing ones. Defaults to false, which means the new (untranslated)
18+
// messages will be separated from the old (translated) ones.
19+
'sort' => false,
20+
// boolean, whether the message file should be overwritten with the merged messages
21+
'overwrite' => true,
22+
// boolean, whether to remove messages that no longer appear in the source code.
23+
// Defaults to false, which means each of these messages will be enclosed with a pair of '' marks.
24+
'removeUnused' => false,
25+
// array, list of patterns that specify which files/directories should NOT be processed.
26+
// If empty or not set, all files/directories will be processed.
27+
// A path matches a pattern if it contains the pattern string at its end. For example,
28+
// '/a/b' will match all files and directories ending with '/a/b';
29+
// the '*.svn' will match all files and directories whose name ends with '.svn'.
30+
// and the '.svn' will match all files and directories named exactly '.svn'.
31+
// Note, the '/' characters in a pattern matches both '/' and '\'.
32+
// See helpers/FileHelper::findFiles() description for more details on pattern matching rules.
33+
'only' => ['*.php'],
34+
// array, list of patterns that specify which files (not directories) should be processed.
35+
// If empty or not set, all files will be processed.
36+
// Please refer to "except" for details about the patterns.
37+
// If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
38+
'except' => [
39+
'.svn',
40+
'.git',
41+
'.gitignore',
42+
'.gitkeep',
43+
'.hgignore',
44+
'.hgkeep',
45+
'/messages',
46+
],
47+
// Generated file format. Can be either "php", "po" or "db".
48+
'format' => 'php',
49+
// When format is "db", you may specify the following two options
50+
//'db' => 'db',
51+
//'sourceMessageTable' => '{{%source_message}}',
52+
];

messages/de/kvdate.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Clear field' => 'Feld löschen',
21+
'Select date' => 'Datum wählen',
22+
];

messages/en/kvdate.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Clear field' => '',
21+
'Select date' => '',
22+
];

messages/ru/kvdate.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Clear field' => 'Очистить поле',
21+
'Select date' => 'Выбрать дату',
22+
];

0 commit comments

Comments
 (0)