InputWidget   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 43
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 4 1
A registerClientScript() 0 14 2
1
<?php
2
/**
3
 * @link https://github.com/2amigos/yii2-selectize-widget
4
 * @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace dosamigos\selectize;
9
10
use yii\helpers\Json;
11
use yii\helpers\Url;
12
use yii\web\JsExpression;
13
14
/**
15
 * InputWidget
16
 *
17
 * @author 2amigos.us <[email protected]>
18
 */
19
class InputWidget extends \yii\widgets\InputWidget
20
{
21
    /**
22
     * @var string
23
     */
24
    public $loadUrl;
25
    
26
    /**
27
     * @var string the parameter name
28
     */
29
    public $queryParam = 'query'; 
30
31
    /**
32
     * @var array
33
     */
34
    public $clientOptions;
35
36
    /**
37
     * @inheritdoc
38
     */
39 6
    public function run()
40
    {
41 6
        $this->registerClientScript();
42 6
    }
43
44
    /**
45
     * Registers the needed JavaScript.
46
     */
47 6
    public function registerClientScript()
48
    {
49 6
        $id = $this->options['id'];
50
51 6
        if ($this->loadUrl !== null) {
52 6
            $url = Url::to($this->loadUrl);
53 6
            $this->clientOptions['load'] = new JsExpression("function (query, callback) { if (!query.length) return callback(); $.getJSON('$url', { {$this->queryParam}: query }, function (data) { callback(data); }).fail(function () { callback(); }); }");
54 2
        }
55
56 6
        $options = Json::encode($this->clientOptions);
57 6
        $view = $this->getView();
58 6
        SelectizeAsset::register($view);
59 6
        $view->registerJs("jQuery('#$id').selectize($options);");
60 6
    }
61
}
62