1 <?php
2
3 4 5 6 7 8 9
10 class ESelect2 extends CInputWidget
11 {
12
13 14 15
16 public $options = array();
17
18 19 20
21 public $data = array();
22
23 24 25
26 public $selector;
27
28 29 30
31 public $events = array();
32
33 34 35
36 public $sortable = false;
37
38 protected $defaultOptions = array();
39
40 public function init()
41 {
42 $this->defaultOptions = array(
43 'formatNoMatches' => 'js:function(){return "' . Yii::t('ESelect2.select2', 'No matches found') . '";}',
44 'formatInputTooShort' => 'js:function(input,min){return "' . Yii::t('ESelect2.select2', 'Please enter {chars} more characters', array('{chars}' => '"+(min-input.length)+"')) . '";}',
45 'formatInputTooLong' => 'js:function(input,max){return "' . Yii::t('ESelect2.select2', 'Please enter {chars} less characters', array('{chars}' => '"+(input.length-max)+"')) . '";}',
46 'formatSelectionTooBig' => 'js:function(limit){return "' . Yii::t('ESelect2.select2', 'You can only select {count} items', array('{count}' => '"+limit+"')) . '";}',
47 'formatLoadMore' => 'js:function(pageNumber){return "' . Yii::t('ESelect2.select2', 'Loading more results...') . '";}',
48 'formatSearching' => 'js:function(){return "' . Yii::t('ESelect2.select2', 'Searching...') . '";}',
49 );
50 }
51
52 public function run()
53 {
54 if ($this->selector == null) {
55 list($this->name, $this->id) = $this->resolveNameId();
56 $this->selector = '#' . $this->id;
57
58 if (isset($this->htmlOptions['placeholder']))
59 $this->options['placeholder'] = $this->htmlOptions['placeholder'];
60
61 if (!isset($this->htmlOptions['multiple'])) {
62 $data = array();
63 if (isset($this->options['placeholder']))
64 $data[''] = '';
65 $this->data = $data + $this->data;
66 }
67
68 if ($this->hasModel()) {
69 echo CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
70 } else {
71 $this->htmlOptions['id'] = $this->id;
72 echo CHtml::dropDownList($this->name, $this->value, $this->data, $this->htmlOptions);
73 }
74 }
75
76 $bu = Yii::app()->assetManager->publish(dirname(__FILE__) . '/assets/');
77 $cs = Yii::app()->clientScript;
78 $cs->registerCssFile($bu . '/select2.css');
79
80 if (YII_DEBUG)
81 $cs->registerScriptFile($bu . '/select2.js');
82 else
83 $cs->registerScriptFile($bu . '/select2.min.js');
84
85 if ($this->sortable) {
86 $cs->registerCoreScript('jquery.ui');
87 }
88
89 $options = CJavaScript::encode(CMap::mergeArray($this->defaultOptions, $this->options));
90 ob_start();
91 echo "jQuery('{$this->selector}').select2({$options})";
92 foreach ($this->events as $event => $handler)
93 echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
94 echo ';';
95 if ($this->sortable) {
96 echo <<<JavaScript
97 jQuery('{$this->selector}').select2("container").find("ul.select2-choices").sortable({
98 containment: 'parent',
99 start: function() { jQuery('{$this->selector}').select2("onSortStart"); },
100 update: function() { jQuery('{$this->selector}').select2("onSortEnd"); }
101 });
102 JavaScript;
103 }
104
105 $cs->registerScript(__CLASS__ . '#' . $this->id, ob_get_clean());
106
107 }
108
109 }
110