Completed
Push — master ( cfdbb2...faceef )
by Dmitry
14:53
created

AmountWithCurrency   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 2
dl 0
loc 45
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A run() 0 13 1
A initClientScript() 0 12 1
1
<?php
2
/**
3
 * HiPanel core package.
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
class AmountWithCurrency extends \yii\base\Widget
14
{
15
    public static $widgetClass = 'amount-with-currency-widget';
16
17
    public $currencyAttributeName;
18
    public $currencyAttributeOptions;
19
    public $model;
20
    public $form;
21
    public $attribute;
22
    public $inputOptions;
23
24
    public function init()
25
    {
26
        parent::init();
27
28
        $this->inputOptions = array_merge(['class' => 'form-control'], $this->inputOptions);
29
    }
30
31
    public function run()
32
    {
33
        $this->initClientScript();
34
35
        return $this->render((new \ReflectionClass($this))->getShortName(), [
36
            'form' => $this->form,
37
            'model' => $this->model,
38
            'currencyAttributeName' => $this->currencyAttributeName,
39
            'currencyAttributeOptions' => $this->currencyAttributeOptions,
40
            'attribute' => $this->attribute,
41
            'containerClass' => self::$widgetClass,
42
        ]);
43
    }
44
45
    public function initClientScript()
46
    {
47
        $widgetClass = self::$widgetClass;
48
        $this->getView()->registerJs(<<<"JS"
49
        $(document).on('click', '.{$widgetClass} a', function(e) {
50
            var item = $(this);
51
            item.parents('.amount-with-currency-widget').find('.iwd-label').text(item.data('label'));
52
            item.parents('.amount-with-currency-widget').find(':hidden').val(item.data('value')).trigger('change');
53
        });
54
JS
55
        );
56
    }
57
}
58