-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelect2.php
executable file
·279 lines (247 loc) · 8.51 KB
/
Select2.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
<?php
namespace moonland\select2;
use Yii;
use yii\helpers\Html;
use yii\widgets\InputWidget;
/**
* Bootstrap select2
* -----------------
*
* example code:
* ~~~
* <?php
* echo Select2::widget([
* 'name' => 'selection-name',
* 'value' => 'first',
* 'items' => ['first' => 'First Option', 'second' => 'Second Option', 'third' => 'Third Option'],
* 'options' => ['class' => 'form-control'],
* 'addon' => ['prepend' => 'Select']
* ]);
*
* echo $form->field($model, 'attribute')->widget(Select2::className(), [
* 'items' => ['first' => 'First Option', 'second' => 'Second Option', 'third' => 'Third Option'],
* 'options' => ['class' => 'form-control'],
* 'addon' => ['prepend' => 'Select']
* ]);
* ~~~
*
* More property see http://ivaynberg.github.io/select2/#documentation
* example `function` or `object` type :
* ~~~
* <?php
* echo Select2::widget([
* //jquery inline function
* 'formatResult' => 'function format(state) { if (!state.id) return state.text;return "<img class='flag' src='images/flags/" + state.id.toLowerCase() + ".png'/>" + state.text;}',
* //call existing function
* 'formatSelection' => 'js:formatSelection()',
* //object
* 'data' => 'js:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}]',
* ]);
* ~~~
*
* @property boolean $multiple set the dropdownlist to multiple selection.
* @property string $size
* @property string $language
* @property array $addon
*
* @author Moh Khoirul Anam <moh.khoirul.anaam@gmail.com>
* @copyright moonlandsoft 2014
* @since 1
* @see http://ivaynberg.github.io/select2/
*/
class Select2 extends InputWidget
{
const LARGE = 'lg';
const SMALL = 'sm';
public $clientOptions = [];
public $multiple = false;
public $language;
public $size;
public $addon = [];
/**
* @var array the event handlers for the underlying Select2 Javascript plugin.
* Please refer to the corresponding Bootstrap plugin Web page for possible events.
*/
public $events = [];
protected $hasGroup = false;
public $groupOptions = [
'class' => 'input-group input-group-select',
];
/**
* @var array the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* If you have a list of data models, you may convert them into the format described above using
* [[\yii\helpers\ArrayHelper::map()]].
*
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* the labels will also be HTML-encoded.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
*
* - prompt: string, a prompt text to be displayed as the first option;
* - options: array, the attributes for the select option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example,
*
* ~~~
* [
* 'value1' => ['disabled' => true],
* 'value2' => ['label' => 'value 2'],
* ];
* ~~~
*
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
* except that the array keys represent the optgroup labels specified in $items.
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
*/
public $items = [];
public function __set($name, $value)
{
if (isset($this->{$name}))
parent::__set($name, $value);
else
$this->clientOptions[$name] = $value;
}
public function init()
{
if (!empty($this->addon))
$this->hasGroup = true;
if (isset($this->addon['append']) && !isset($this->addon['prepend']))
Html::addCssClass($this->groupOptions, 'addon-left');
elseif (isset($this->addon['prepend']) && !isset($this->addon['append']))
Html::addCssClass($this->groupOptions, 'addon-right');
else
Html::addCssClass($this->groupOptions, 'addon-both');
if (isset($this->size) && in_array($this->size, ['lg', 'sm'])) {
Html::addCssClass($this->options, 'input-' . $this->size);
}
parent::init();
}
public function run()
{
$this->registerPlugin();
return $this->renderInput();
}
public function renderInput()
{
if ($this->multiple)
{
$this->options['multiple'] = 'multiple';
}
if ($this->hasModel()) {
$input = Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options);
} else {
$input = Html::dropDownList($this->name, $this->value, $this->items, $this->options);
}
if ($this->hasGroup)
$input = Html::tag('div', $this->prepareAddon($input), $this->groupOptions);
return $input;
}
protected function prepareAddon($input)
{
extract($this->addon);
$template = "{prepend}\n{input}\n{append}";
$appendContent = [];
$prependContent = [];
if (isset($append)) {
if (is_array($append)) {
if (is_string($append[0]) && is_bool($append[1])) {
if ($append[1] == true)
$appendContent[] = Html::tag('span', $append[0], ['class' => 'input-group-btn']);
} else {
foreach ($append as $content) {
$appendContent[] = $this->prepareContent($content);
}
}
} else {
$appendContent[] = Html::tag('span', $append, ['class' => 'input-group-addon']);
}
}
if (isset($prepend)) {
if (is_array($prepend)) {
if (is_string($prepend[0]) && is_bool($prepend[1])) {
if ($prepend[1] == true)
$prependContent[] = Html::tag('span', $prepend[0], ['class' => 'input-group-btn']);
} else {
foreach ($prepend as $content) {
$prependContent[] = $this->prepareContent($content);
}
}
} else {
$prependContent[] = Html::tag('span', $prepend, ['class' => 'input-group-addon']);
}
}
return strtr($template, [
'{prepend}' => implode('', $prependContent),
'{input}' => $input,
'{append}' => implode('', $appendContent),
]);
}
protected function prepareContent($content)
{
if (is_array($content)) {
if (!isset($content['class']) && isset($content[0]))
$content['class'] = ArrayHelper::remove($content, 0);
if (!isset($content['options']) && isset($content[1])) {
if (is_bool($content[1]))
$content['asButton'] = ArrayHelper::remove($content, 1);
else
$content['options'] = ArrayHelper::remove($content, 1);
}
if (!isset($content['asButton']))
$content['asButton'] = ArrayHelper::remove($content, 2, false);
$class = $content['class'];
$options = isset($content['options']) ? $content['options'] : [];
$asButton = $content['asButton'];
if ($asButton)
Html::addCssClass($tagOptions, 'input-group-btn');
else
Html::addCssClass($tagOptions, 'input-group-addon');
if (class_exists($class)) {
$inContent = $class::widget($options);
} else {
$inContent = $class;
}
$content = Html::tag('span', $inContent, $tagOptions);
} else {
if ($content instanceof Widget)
Html::addCssClass($tagOptions, 'input-group-btn');
else
Html::addCssClass($tagOptions, 'input-group-addon');
$content = Html::tag('span', $content, $tagOptions);
}
return $content;
}
protected function registerPlugin()
{
$view = $this->getView();
if (isset($this->language) && is_string($this->language))
Select2Asset::register($view)->js[] = 'js/i18n/' . $this->language . '.js';
else {
$this->clientOptions['language'] = $this->language;
Select2Asset::register($view);
}
$selector = $this->options['id'];
$options = !empty($this->clientOptions) ? \yii\helpers\Json::encode($this->clientOptions) : '';
$view->registerJs("jQuery('#$selector').select2({$options});");
if (!empty($this->events)) {
$js = [];
foreach ($this->events as $event => $handler) {
$js[] = "jQuery('#$selector').on('$event', $handler);";
}
$view->registerJs(implode("\n", $js));
}
}
public static function listData($model, $keyName, $valueName)
{
$data = [];
$provider = new ActiveDataProvider(['query' => $model::find(), 'pagination' => false]);
foreach ($provider->getModels() as $model)
{
$data[$model->{$keyName}] = $model->{$valueName};
}
return $data;
}
}