Completed
Push — master ( 834dc7...c72fcf )
by Dmitry
14:24
created

ResourcePriceInput   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 10 1
B registerClientScript() 0 30 1
1
<?php
2
3
namespace hipanel\modules\finance\widgets;
4
5
use yii\base\Widget;
6
7
class ResourcePriceInput extends Widget
8
{
9
    public $resource = [];
10
    public $baseResource = [];
11
    public $activeField;
12
13
    public function run()
14
    {
15
        $this->registerClientScript();
16
17
        return $this->render('ResourcePriceInput', [
18
            'resource' => $this->resource,
19
            'baseResource' => $this->baseResource,
20
            'activeField' => $this->activeField,
21
        ]);
22
    }
23
24
    public function registerClientScript()
25
    {
26
        $this->view->registerJs(<<<'JS'
27
            $('.price-input').on('change mouseup', function () {
28
                var price = parseFloat($(this).val());
29
                if (isNaN(price)) return false;
30
                
31
                var base = $(this).closest('td').find('.base-price');
32
                var basePrice = parseFloat(base.attr('data-original-price'));
33
                var delta = price - basePrice;
34
                
35
                if (delta <= -basePrice) {
36
                    $(this).val('0.01').trigger('change');
37
                    return false;
38
                }
39
                
40
                base.removeClass('text-success text-danger');
41
                
42
                base.text(delta.toFixed(2)).addClass(delta >= 0 ? 'text-success' : 'text-danger');
43
            });
44
        
45
            $('.price-input').trigger('change');
46
JS
47
        );
48
49
        $this->view->registerCss('
50
            .base-price { font-weight: bold; }
51
            .form-group.form-group-sm { margin-bottom: 0; }
52
        ');
53
    }
54
}
55