Skip to content

Commit 3d07118

Browse files
Merge pull request #3 from sansusan/master
Add extra buttons
2 parents 45a9927 + 1d24f21 commit 3d07118

File tree

1 file changed

+105
-8
lines changed

1 file changed

+105
-8
lines changed

src/Widget.php

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,39 @@ class Widget extends InputWidget
2727
* @var string
2828
*/
2929
public $theme;
30+
31+
/**
32+
* Disable input
33+
* @var bool
34+
*/
35+
public $disabled = false;
36+
37+
/**
38+
* Show group buttons
39+
* @var bool
40+
*/
41+
public $groupBtnShow = false;
42+
43+
/**
44+
* Buttons template
45+
* @var string
46+
*/
47+
public $groupBtnTemplate = '{toggle}{clear}';
48+
49+
/**
50+
* Buttons
51+
* @var array
52+
*/
53+
public $groupBtn = [
54+
'toggle' => [
55+
'btnClass' => 'btn btn-default',
56+
'iconClass' => 'glyphicon glyphicon-calendar',
57+
],
58+
'clear' => [
59+
'btnClass' => 'btn btn-default',
60+
'iconClass' => 'glyphicon glyphicon-remove',
61+
],
62+
];
3063

3164
/**
3265
* @throws \yii\base\InvalidConfigException
@@ -46,35 +79,99 @@ public function init()
4679
*/
4780
public function run()
4881
{
82+
if ($this->groupBtnShow)
83+
$this->clientOptions['wrap'] = true;
84+
else
85+
$this->clientOptions['wrap'] = false;
86+
4987
$this->registerClientScript();
88+
$content = '';
89+
$options['data-input'] = '';
90+
if ($this->disabled)
91+
$options['disabled'] = 'disabled';
5092

51-
if ($this->hasModel()) {
52-
$content = Html::activeTextInput($this->model, $this->attribute, $this->options);
93+
if ($this->groupBtnShow) {
94+
$content .= '<div class="flatpickr-' . $this->options['id'] . ' input-group">';
95+
96+
if ($this->hasModel()) {
97+
$content .= Html::activeTextInput($this->model, $this->attribute, array_merge($this->options, $options));
98+
} else {
99+
$content .= Html::textInput($this->name, $this->value, array_merge($this->options, $options));
100+
}
101+
102+
$content .= '<div class="input-group-btn">';
103+
if (preg_match_all('/{(toggle|clear)}/i', $this->groupBtnTemplate, $matches)) {
104+
foreach ($matches[1] as $btnName)
105+
$content .= $this->renderGroupBtn($btnName);
106+
}
107+
$content .= '</div>';
108+
$content .= '</div>';
53109
} else {
54-
$content = Html::textInput($this->name, $this->value, $this->options);
110+
if ($this->hasModel()) {
111+
$content = Html::activeTextInput($this->model, $this->attribute, array_merge($this->options, $options));
112+
} else {
113+
$content = Html::textInput($this->name, $this->value, array_merge($this->options, $options));
114+
}
55115
}
56116

57117
return $content;
58118
}
59119

60-
/**
120+
/**
61121
* Register widget client scripts.
62122
*/
63123
protected function registerClientScript()
64124
{
65125
$view = $this->getView();
66126

67-
$selector = Json::encode('#' . $this->options['id']);
127+
if ($this->groupBtnShow)
128+
$selector = Json::encode('.flatpickr-' . $this->options['id']);
129+
else
130+
$selector = Json::encode('#' . $this->options['id']);
131+
68132
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
69133

70134
FlatpickrAsset::register($view);
71-
if(!empty($this->theme)) {
72-
FlatpickrAsset::register($view)->css[] = 'themes/' . $this->theme . '.css';
135+
if (!empty($this->theme)) {
136+
FlatpickrAsset::register($view)->css[] = 'themes/' . $this->theme . '.css';
73137
}
74-
if(!empty($this->locale)) {
138+
if (!empty($this->locale)) {
75139
FlatpickrAsset::register($view)->js[] = 'l10n/' . $this->locale . '.js';
76140
}
77141

78142
$view->registerJs("flatpickr($selector, {$options});");
79143
}
144+
145+
146+
147+
/**
148+
* @param string $btnName
149+
* @return string
150+
*/
151+
private function renderGroupBtn($btnName)
152+
{
153+
$content = '';
154+
if (isset($this->groupBtn[$btnName])) {
155+
if (isset($this->groupBtn[$btnName]['btnClass']))
156+
$btnClass = $this->groupBtn[$btnName]['btnClass'];
157+
else
158+
$btnClass = 'btn btn-default';
159+
160+
if (isset($this->groupBtn[$btnName]['iconClass']))
161+
$iconClass = $this->groupBtn[$btnName]['iconClass'];
162+
else
163+
$iconClass = '';
164+
165+
$disabled = '';
166+
if ($this->disabled)
167+
$disabled = 'disabled="disabled"';
168+
169+
$content = <<<HTML
170+
<button class="$btnClass" type="button" $disabled data-$btnName>
171+
<span class="$iconClass"></span>
172+
</button>
173+
HTML;
174+
}
175+
return $content;
176+
}
80177
}

0 commit comments

Comments
 (0)