forked from yiidoc/yii2-tagsinput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagsInput.php
68 lines (58 loc) · 1.76 KB
/
TagsInput.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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\tagsinput;
use yii\helpers\Html;
use yii\widgets\InputWidget;
use yii\helpers\Json;
/**
*
* @author Nghia Nguyen <[email protected]>
* @since 2.0
*/
class TagsInput extends InputWidget
{
public $options = ['class' => 'form-control'];
public $clientOptions = [];
public $clientEvents = [];
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->getId();
}
}
TagsInputAsset::register($this->getView());
$this->registerScript();
$this->registerEvent();
}
public function run()
{
if ($this->hasModel()) {
echo Html::activeInput('text', $this->model, $this->attribute, $this->options);
} else {
echo Html::input('text', $this->name, $this->value, $this->options);
}
}
public function registerScript()
{
$clientOptions = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$js = "jQuery('#{$this->options["id"]}').tagsinput({$clientOptions});";
$this->getView()->registerJs($js);
}
public function registerEvent()
{
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));
}
}
}