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

DynamicFormInputsValueInheritor::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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