This repository was archived by the owner on Feb 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSelect2.php
executable file
·141 lines (116 loc) · 4.72 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
<?php
/**
* Wrapper Widget to use jQuery Select2 in Yii application.
*
* @author Tonin R. Bolzan <admin@tonybolzan.com>
* @copyright Copyright © 2013 tonybolzan.com
* @package extensions
* @subpackage select2
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version 3.4.3 rev.0
*
* @see https://github.com/ivaynberg/select2 jQuery Select2
*/
class Select2 extends CInputWidget
{
/** @var string Path to assets directory published in init() */
private $assetsDir;
/** @var array Chosen script settings passed to $.fn.chosen() */
private $settings = array();
/** @var bool Multiple or single item should be selected */
public $multiple = false;
/** @var array See CHtml::listData() */
public $data;
/** Publish assets and set default values for properties */
public function init()
{
$dir = dirname(__FILE__) . '/assets';
$this->assetsDir = Yii::app()->assetManager->publish($dir);
if ($this->multiple) {
$this->htmlOptions['multiple'] = true;
} elseif (isset($this->htmlOptions['multiple'])) {
$this->multiple = true;
}
if (isset($this->htmlOptions['placeholder'])) {
$this->settings['placeholder'] = $this->htmlOptions['placeholder'];
} elseif (isset($this->htmlOptions['data-placeholder'])) {
$this->settings['placeholder'] = $this->htmlOptions['data-placeholder'];
}
if (isset($this->htmlOptions['select2Options'])) {
$this->settings = CMap::mergeArray($this->settings, $this->htmlOptions['select2Options']);
unset($this->htmlOptions['select2Options']);
}
}
/** Render widget html and register client scripts */
public function run()
{
list($name, $id) = $this->resolveNameID();
if (isset($this->htmlOptions['id'])) {
$id = $this->htmlOptions['id'];
} else {
$this->htmlOptions['id'] = $id;
}
if (isset($this->htmlOptions['name'])) {
$name = $this->htmlOptions['name'];
}
if (isset($this->settings['ajax'])) {
if (isset($this->model)) {
echo CHtml::textField($name, $this->model->{$this->attribute}, $this->htmlOptions);
} else {
echo CHtml::textField($name, $this->value, $this->htmlOptions);
}
} else {
if (isset($this->model)) {
echo CHtml::dropDownList($name, $this->model->{$this->attribute}, $this->data, $this->htmlOptions);
} else {
echo CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions);
}
}
$this->registerScripts($id);
}
/** Register client scripts */
private function registerScripts($id)
{
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$src = !YII_DEBUG ? '' : '/src';
$min = YII_DEBUG ? '' : '.min';
$cs->registerCssFile($this->assetsDir . $src . '/select2' . $min . '.css');
$cs->registerScriptFile($this->assetsDir . $src . '/select2' . $min . '.js');
$lang = strtoupper(str_replace('_', '-', Yii::app()->language));
$lang[0] = strtolower($lang[0]);
$lang[1] = strtolower($lang[1]);
$cs->registerScriptFile($this->assetsDir . $src . '/select2_locale_' . $lang . $min . '.js');
$settings = CJavaScript::encode($this->settings);
$cs->registerScript("{$id}_select2", "$('#{$id}').select2({$settings});");
}
/** Single item select */
public static function dropDownList($name, $select, $data, $htmlOptions = array())
{
return Yii::app()->getController()->widget(__CLASS__, array(
'name' => $name,
'value' => $select,
'data' => $data,
'htmlOptions' => $htmlOptions,
), true);
}
public static function activeDropDownList($model, $attribute, $data, $htmlOptions = array())
{
return self::dropDownList(CHtml::activeName($model, $attribute), CHtml::value($model, $attribute), $data, $htmlOptions);
}
/** Multiple items select */
public static function multiSelect($name, $select, $data, $htmlOptions = array())
{
return Yii::app()->getController()->widget(__CLASS__, array(
'name' => $name,
'value' => $select,
'data' => $data,
'htmlOptions' => $htmlOptions,
'multiple' => true,
), true);
}
public static function activeMultiSelect($model, $attribute, $data, $htmlOptions = array())
{
return self::multiSelect(CHtml::activeName($model, $attribute) . '[]', CHtml::value($model, $attribute), $data, $htmlOptions);
}
}