Completed
Push — master ( 2d17d3...8330e8 )
by Alexey
05:52
created

IconPicker::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace dominus77\iconpicker;
4
5
use Yii;
6
use yii\helpers\Html;
7
use yii\helpers\Json;
8
use yii\widgets\InputWidget;
9
10
/**
11
 * Class IconPicker
12
 * @package dominus77\iconpicker
13
 */
14
class IconPicker extends InputWidget
15
{
16
    /**
17
     * Options plugin
18
     * @var array
19
     * @see https://farbelous.github.io/fontawesome-iconpicker/
20
     */
21
    public $clientOptions = [];
22
23
    /**
24
     * @inheritdoc
25
     */
26 2
    public function run()
27
    {
28 2
        if ($this->hasModel()) {
29 1
            echo Html::activeTextInput($this->model, $this->attribute, $this->options);
30
        } else {
31 1
            echo Html::textInput($this->name, $this->value, $this->options);
32
        }
33 2
        $this->registerClientScript();
34 2
    }
35
36
    /**
37
     * Publish resource
38
     */
39 2
    protected function registerClientScript()
40
    {
41 2
        $js = [];
42 2
        $view = $this->getView();
43 2
        IconPickerAsset::register($view);
44 2
        $id = $this->options['id'];
45 2
        $options = Json::encode($this->clientOptions);
46 2
        $js[] = "$('#{$id}').iconpicker($options);";
47 2
        $view->registerJs(implode("\n", $js));
48 2
    }
49
}
50