-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSelectize.php
101 lines (94 loc) · 2.88 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
<?php
namespace yii\selectize;
use yii\helpers\Html;
use yii\widgets\InputWidget;
use yii\helpers\Json;
use yii\jui\JuiAsset;
class Selectize extends InputWidget
{
/**
* @var array
*/
public $items;
/**
* @var array
* @see https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md#options
*/
public $clientOptions;
/**
* @var array
* @see https://github.com/brianreavis/selectize.js/blob/master/docs/events.md
*/
public $clientEvents;
/**
* @inheritdoc
*/
public function init()
{
if (!isset($this->options['id'])) {
if ($this->hasModel()) {
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
} else {
$this->options['id'] = $this->id;
}
}
$this->registerAssetBundle();
$this->registerJs();
$this->registerEvents();
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
if (is_array($this->items)) {
echo Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options);
} else {
echo Html::activeTextInput($this->model, $this->attribute, $this->options);
}
} else {
if (is_array($this->items)) {
echo Html::dropDownList($this->name, $this->value, $this->items, $this->options);
} else {
echo Html::textInput($this->name, $this->value, $this->options);
}
}
}
/**
* Register asset bundles
*/
public function registerAssetBundle()
{
if (isset($this->clientOptions['plugins']) && in_array('drag_drop', $this->clientOptions['plugins'])) {
JuiAsset::register($this->getView());
}
MicroPluginAsset::register($this->getView());
SifterAsset::register($this->getView());
SelectizeAsset::register($this->getView());
}
/**
* Register client script
*/
public function registerJs()
{
if (!isset($this->clientOptions['create']) && empty($this->items)) {
$this->clientOptions['create'] = "function(input) { return { value: input, text: input };}";
}
$clientOptions = (count($this->clientOptions)) ? Json::encode($this->clientOptions) : '';
$this->getView()->registerJs("jQuery('#{$this->options['id']}').selectize({$clientOptions});");
}
/**
* Register client script handles
*/
public function registerEvents()
{
if (!empty($this->clientEvents)) {
$js = [];
foreach ($this->clientEvents as $event => $handle) {
$js[] = "jQuery('#{$this->options['id']}').on('{$event}',{$handle});";
}
$this->getView()->registerJs(implode(PHP_EOL, $js));
}
}
}