-
Notifications
You must be signed in to change notification settings - Fork 10
/
Selectize.php
133 lines (118 loc) · 3.18 KB
/
Selectize.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
<?php
namespace yii2mod\selectize;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\JsExpression;
use yii\widgets\InputWidget;
/**
* Class Selectize
*
* @package yii2mod\selectize
*/
class Selectize extends InputWidget
{
/**
* @var array|null the option data items
*/
public $items;
/**
* @var array|string the URL where to get the new options to be loaded into the plugin. This attribute is for basic
* usage of the `load` configuration option of the plugin. For a more advanced usage of the `load` option, please
* refer to the Selectize.js usage documentation. Response format must be json.
*/
public $url;
/**
* @var array plugin options
*/
public $pluginOptions = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (isset($this->pluginOptions['plugins'])
&& in_array('drag_drop', $this->pluginOptions['plugins'], true)
) {
Yii::$container->set(SelectizeAsset::class, [
'depends' => [
'yii\web\JqueryAsset',
'yii\jui\JuiAsset',
],
]);
}
}
/**
* @inheritdoc
*/
public function run()
{
if (!is_array($this->items)) {
$this->renderInput();
} else {
$this->renderDropDown();
}
$this->registerAssets();
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
SelectizeAsset::register($view);
$js = '$("#' . $this->getInputId() . '").selectize(' . $this->getPluginOptions() . ');';
$view->registerJs($js, $view::POS_END);
}
/**
* Get plugin options in the json format
*
* @return string
*/
public function getPluginOptions()
{
if ($this->url !== null) {
$url = Url::to($this->url);
$this->pluginOptions['load'] = new JsExpression("
function (query, callback) {
if (!query.length) return callback();
$.getJSON('$url', { query: encodeURIComponent(query) }, function (data) { callback(data); })
.fail(function () { callback(); });
}
");
}
return Json::encode($this->pluginOptions);
}
/**
* Return input id
*
* @return string
*/
public function getInputId()
{
return $this->options['id'];
}
/**
* Render dropdown list
*/
public function renderDropDown()
{
if ($this->hasModel()) {
echo Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options);
} else {
echo Html::dropDownList($this->name, $this->value, $this->items, $this->options);
}
}
/**
* Render text input
*/
public function renderInput()
{
if ($this->hasModel()) {
echo Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
echo Html::textInput($this->name, $this->value, $this->options);
}
}
}