Completed
Push — master ( 462bae...29fcb5 )
by Dmitry
25:39 queued 19:35
created

DynamicFormInputsValueInheritor   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 4 1
A registerClientScript() 0 22 1
1
<?php
2
3
namespace hipanel\widgets;
4
5
use yii\base\Widget;
6
use yii\helpers\Json;
7
8
/**
9
 * Class DynamicFormInputsValueInheritor copies value from the latest input group
10
 * to the newly create one.
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class DynamicFormInputsValueInheritor extends Widget
15
{
16
    /**
17
     * @var string
18
     */
19
    public $containerSelector = '.dynamicform_wrapper';
20
21
    public $itemSelector = '.item';
22
23
    public $inputSelectors = [];
24
25
    public function run()
26
    {
27
        $this->registerClientScript();
28
    }
29
30
    protected function registerClientScript()
31
    {
32
        $containerSelector = Json::htmlEncode($this->containerSelector);
33
        $inputSelectors = Json::htmlEncode($this->inputSelectors);
34
        $itemSelector = Json::htmlEncode($this->itemSelector);
35
36
        $this->view->registerJs(<<<JS
37
            $({$containerSelector}).on('afterInsert', function (event, newItem) {
38
                newItem = $(newItem);
39
                var templateItem = $(event.target).find($itemSelector).slice(-2, -1);
40
                
41
                $({$inputSelectors}).each(function (index, selector) {
42
                    var templateInput = templateItem.find(selector);
43
                    var newInput = newItem.find(selector)
44
                    debugger;
45
                    
46
                    newInput.val(templateInput.val());
47
                });
48
            });
49
JS
50
        );
51
    }
52
}
53