forked from kartik-v/yii2-widget-select2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Select2.php
263 lines (242 loc) · 9.89 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
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2015
* @package yii2-widgets
* @subpackage yii2-widget-select2
* @version 2.0.1
*/
namespace kartik\select2;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
/**
* Select2 widget is a Yii2 wrapper for the Select2 jQuery plugin. This
* input widget is a jQuery based replacement for select boxes. It supports
* searching, remote data sets, and infinite scrolling of results. The widget
* is specially styled for Bootstrap 3.
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
* @see https://github.com/select2/select2
*/
class Select2 extends \kartik\base\InputWidget
{
const LARGE = 'lg';
const MEDIUM = 'md';
const SMALL = 'sm';
const THEME_DEFAULT = 'default';
const THEME_CLASSIC = 'classic';
const THEME_BOOTSTRAP = 'bootstrap';
const THEME_KRAJEE = 'krajee';
/**
* @var array $data 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()]].
*/
public $data;
/**
* @var string the locale ID (e.g. 'fr', 'de') for the language to be used by the Select2 Widget.
* If this property not set, then the current application language will be used.
*/
public $language;
/**
* @var string the theme name to be used for styling the Select2
*/
public $theme = self::THEME_KRAJEE;
/**
* @var string|array, the displayed text in the dropdown for the initial
* value when you do not set or provide `data` (e.g. using with ajax).
* If options['multiple'] is set to `true`, you can set this as an array of
* text descriptions for each item in the dropdown `value`.
*/
public $initValueText;
/**
* @var bool whether to hide the search control and render it as a simple select. Defaults to `false`.
*/
public $hideSearch = false;
/**
* @var array addon to prepend or append to the Select2 widget
* - prepend: array the prepend addon configuration
* - content: string the prepend addon content
* - asButton: boolean whether the addon is a button or button group. Defaults to false.
* - append: array the append addon configuration
* - content: string the append addon content
* - asButton: boolean whether the addon is a button or button group. Defaults to false.
* - groupOptions: array HTML options for the input group
* - contentBefore: string content placed before addon
* - contentAfter: string content placed after addon
*/
public $addon = [];
/**
* @var string Size of the Select2 input, must be one of the
* [[LARGE]], [[MEDIUM]] or [[SMALL]]. Defaults to [[MEDIUM]]
*/
public $size = self::MEDIUM;
/**
* @var array the HTML attributes for the input tag. The following options are important:
* - multiple: boolean whether multiple or single item should be selected. Defaults to false.
* - placeholder: string placeholder for the select item.
*/
public $options = [];
protected static $_inbuiltThemes = [
self::THEME_DEFAULT,
self::THEME_CLASSIC,
self::THEME_BOOTSTRAP,
self::THEME_KRAJEE,
];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->pluginOptions['theme'] = $this->theme;
if (!empty($this->addon) && ($this->theme === self::THEME_KRAJEE || $this->theme === self::THEME_BOOTSTRAP)) {
$this->pluginOptions['width'] = '100%';
}
$multiple = ArrayHelper::getValue($this->pluginOptions, 'multiple', false);
unset($this->pluginOptions['multiple']);
$multiple = ArrayHelper::getValue($this->pluginOptions, 'tags', false) ? true :
ArrayHelper::getValue($this->options, 'multiple', $multiple);
$this->options['multiple'] = $multiple;
if ($this->hideSearch) {
$css = ArrayHelper::getValue($this->pluginOptions, 'dropdownCssClass', '');
$css .= ' kv-hide-search';
$this->pluginOptions['dropdownCssClass'] = $css;
}
$this->initPlaceholder();
if (!isset($this->data)) {
if (empty($this->value) && empty($this->initValueText)) {
$this->data = [];
} else {
$key = empty($this->value) ? ($multiple ? [] : '') : $this->value;
$val = empty($this->initValueText) ? $key : $this->initValueText;
$this->data = $multiple ? array_combine($key, $val) : [$key => $val];
}
}
Html::addCssClass($this->options, 'form-control');
$this->initLanguage('language', true);
$this->registerAssets();
$this->renderInput();
}
/**
* Initializes the placeholder for Select2
*/
protected function initPlaceholder()
{
$isMultiple = ArrayHelper::getValue($this->options, 'multiple', false);
if (!empty($this->options['prompt']) && empty($this->pluginOptions['placeholder'])) {
$this->pluginOptions['placeholder'] = $this->options['prompt'];
if ($isMultiple) {
unset($this->options['prompt']);
}
return;
}
if (!empty($this->options['placeholder'])) {
$this->pluginOptions['placeholder'] = $this->options['placeholder'];
unset($this->options['placeholder']);
}
if (!empty($this->pluginOptions['placeholder']) && is_string($this->pluginOptions['placeholder']) && !$isMultiple) {
$this->options['prompt'] = $this->pluginOptions['placeholder'];
}
}
/**
* Embeds the input group addon
*
* @param string $input
*
* @return string
*/
protected function embedAddon($input)
{
if (!isset($this->size) && empty($this->addon)) {
return $input;
}
$group = ArrayHelper::getValue($this->addon, 'groupOptions', []);
$size = isset($this->size) ? ' input-group-' . $this->size : '';
Html::addCssClass($group, 'input-group' . $size);
if (empty($this->addon)) {
return Html::tag('div', $input, $group);
}
$prepend = ArrayHelper::getValue($this->addon, 'prepend', '');
$append = ArrayHelper::getValue($this->addon, 'append', '');
if ($this->pluginLoading) {
Html::addCssClass($group, 'kv-input-group-hide');
Html::addCssClass($group, 'group-' . $this->options['id']);
}
if (is_array($prepend)) {
$content = ArrayHelper::getValue($prepend, 'content', '');
if (isset($prepend['asButton']) && $prepend['asButton'] == true) {
$prepend = Html::tag('div', $content, ['class' => 'input-group-btn']);
} else {
$prepend = Html::tag('span', $content, ['class' => 'input-group-addon']);
}
Html::addCssClass($group, 'select2-bootstrap-prepend');
}
if (is_array($append)) {
$content = ArrayHelper::getValue($append, 'content', '');
if (isset($append['asButton']) && $append['asButton'] == true) {
$append = Html::tag('div', $content, ['class' => 'input-group-btn']);
} else {
$append = Html::tag('span', $content, ['class' => 'input-group-addon']);
}
Html::addCssClass($group, 'select2-bootstrap-append');
}
$addonText = $prepend . $input . $append;
$contentBefore = ArrayHelper::getValue($this->addon, 'contentBefore', '');
$contentAfter = ArrayHelper::getValue($this->addon, 'contentAfter', '');
return Html::tag('div', $contentBefore . $addonText . $contentAfter, $group);
}
/**
* Renders the source Input for the Select2 plugin.
* Graceful fallback to a normal HTML select dropdown
* or text input - in case JQuery is not supported by
* the browser
*/
protected function renderInput()
{
if ($this->pluginLoading) {
$this->_loadIndicator = '<div class="kv-plugin-loading loading-' . $this->options['id'] . '"> </div>';
Html::addCssStyle($this->options, 'display:none');
}
$input = $this->getInput('dropDownList', true);
echo $this->_loadIndicator . $this->embedAddon($input);
}
/**
* Registers the asset bundle and locale
*/
public function registerAssetBundle()
{
$view = $this->getView();
$lang = isset($this->language) ? $this->language : '';
Select2Asset::register($view)->addLanguage($lang, '', 'js/i18n');
if (in_array($this->theme, self::$_inbuiltThemes)) {
$bundleClass = __NAMESPACE__ . '\Theme' . ucfirst($this->theme) . 'Asset';
$bundleClass::register($view);
}
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$id = $this->options['id'];
$this->registerAssetBundle();
// validate bootstrap has-success & has-error states
$clear = 'kv_close_' . str_replace('-', '_', $id);
$this->pluginEvents += [
'select2:opening' => "function(event){initSelect2DropStyle('{$id}', '{$clear}', event);}",
'select2:unselect' => "function(){window.{$clear} = true;}"
];
// register plugin
if ($this->pluginLoading) {
$this->registerPlugin('select2', "jQuery('#{$id}')",
"initSelect2Loading('{$id}', '.select2-container--{$this->theme}')");
} else {
$this->registerPlugin('select2');
}
}
}