forked from anggiaj/ESelect2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESelect2.php
111 lines (92 loc) · 3.93 KB
/
ESelect2.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
<?php
/**
* Wrapper for ivaynberg jQuery select2 (https://github.com/ivaynberg/select2)
*
* @author Anggiajuang Patria <[email protected]>
* @link http://git.io/Mg_a-w
* @license http://www.opensource.org/licenses/apache2.0.php
*/
class ESelect2 extends CInputWidget
{
/**
* @var array select2 options
*/
public $options = array();
/**
* @var array CHtml::dropDownList $data param
*/
public $data = array();
/**
* @var string html element selector
*/
public $selector;
/**
* @var array javascript event handlers
*/
public $events = array();
/**
* @var boolean should the items of a multiselect list be sortable using jQuery UI
*/
public $sortable = false;
protected $defaultOptions = array();
public function init()
{
$this->defaultOptions = array(
'formatNoMatches' => 'js:function(){return "' . Yii::t('ESelect2.select2', 'No matches found') . '";}',
'formatInputTooShort' => 'js:function(input,min){return "' . Yii::t('ESelect2.select2', 'Please enter {chars} more characters', array('{chars}' => '"+(min-input.length)+"')) . '";}',
'formatInputTooLong' => 'js:function(input,max){return "' . Yii::t('ESelect2.select2', 'Please enter {chars} less characters', array('{chars}' => '"+(input.length-max)+"')) . '";}',
'formatSelectionTooBig' => 'js:function(limit){return "' . Yii::t('ESelect2.select2', 'You can only select {count} items', array('{count}' => '"+limit+"')) . '";}',
'formatLoadMore' => 'js:function(pageNumber){return "' . Yii::t('ESelect2.select2', 'Loading more results...') . '";}',
'formatSearching' => 'js:function(){return "' . Yii::t('ESelect2.select2', 'Searching...') . '";}',
);
}
public function run()
{
if ($this->selector == null) {
list($this->name, $this->id) = $this->resolveNameId();
$this->selector = '#' . $this->id;
if (isset($this->htmlOptions['placeholder']))
$this->options['placeholder'] = $this->htmlOptions['placeholder'];
if (!isset($this->htmlOptions['multiple'])) {
$data = array();
if (isset($this->options['placeholder']))
$data[''] = '';
$this->data = $data + $this->data;
}
if ($this->hasModel()) {
echo CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
} elseif(!isset($this->options['ajax'])) {
$this->htmlOptions['id'] = $this->id;
echo CHtml::dropDownList($this->name, $this->value, $this->data, $this->htmlOptions);
} else {
echo CHtml::hiddenField($this->name, $this->value, $this->htmlOptions);
}
}
$bu = Yii::app()->assetManager->publish(dirname(__FILE__) . '/assets/');
$cs = Yii::app()->clientScript;
$cs->registerCssFile($bu . '/select2.css');
if (YII_DEBUG)
$cs->registerScriptFile($bu . '/select2.js');
else
$cs->registerScriptFile($bu . '/select2.min.js');
if ($this->sortable) {
$cs->registerCoreScript('jquery.ui');
}
$options = CJavaScript::encode(CMap::mergeArray($this->defaultOptions, $this->options));
ob_start();
echo "jQuery('{$this->selector}').select2({$options})";
foreach ($this->events as $event => $handler)
echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
echo ';';
if ($this->sortable) {
echo <<<JavaScript
jQuery('{$this->selector}').select2("container").find("ul.select2-choices").sortable({
containment: 'parent',
start: function() { jQuery('{$this->selector}').select2("onSortStart"); },
update: function() { jQuery('{$this->selector}').select2("onSortEnd"); }
});
JavaScript;
}
$cs->registerScript(__CLASS__ . '#' . $this->id, ob_get_clean());
}
}