Completed
Push — master ( e76d78...462bae )
by Klochok
31:56
created

DynamicFormCopyButton   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 46 1
1
<?php
2
3
namespace hipanel\widgets;
4
5
use yii\base\Widget;
6
use yii\bootstrap\Html;
7
8
class DynamicFormCopyButton extends Widget
9
{
10
    public $buttonOptions = ['class' => 'add-item copy btn btn-info btn-sm'];
11
12
    public $icon = '<i class="glyphicon glyphicon-duplicate"></i>';
13
14
    public $widgetContainer = '.dynamicform_wrapper';
15
16
    public function run()
17
    {
18
        $this->view->registerJs(<<<"JS"
19
            (function() {
20
                window.prevItem = null;
21
                var dynamicFormWrapper = $('{$this->widgetContainer}');
22
                dynamicFormWrapper.on('beforeInsert', function(e, item) {
23
                    if ($(item).hasClass('copy')) {
24
                        window.prevItem = $(item).closest('div.item');    
25
                    }
26
                });
27
                dynamicFormWrapper.on('afterInsert', function(e, item) {
28
                    var currentItem = $(item), prevItem = window.prevItem;
29
                    if (window.prevItem !== null) {
30
                        prevItem.find(':input').each(function() {
31
                            var prevInput = this;
32
                            if (prevInput.getAttribute("id") !== null) {
33
                                var needle = prevInput.getAttribute("id").split("-").slice(-1).pop();
34
                                var newInput = currentItem.find(":input[id$=\'" + needle + "\']");
35
                                if ( newInput.length === 1 ) {
36
                                    if (newInput.attr('data-combo-field')) { // if select2
37
                                        var prevOption = $(prevInput).find(':selected');
38
                                        if (prevOption.length) {
39
                                            var newOption = new Option(prevOption.text(), prevOption.attr('value'), true, true);
40
                                            newOption.setAttribute('data-select2-id', prevOption.attr('data-select2-id'));
41
                                            newInput.append(newOption).trigger('change');
42
                                            newInput.trigger('select2:select');
43
                                        }
44
                                    } else if (newInput.attr('data-amount-with-currency') === 'currency') { // if amount with currency
45
                                        newInput.val(prevInput.value);
46
                                        newInput.closest('.amount-with-currency-widget').find('a[data-value="' + prevInput.value + '"]').click();
47
                                    } else {
48
                                        newInput.val(prevInput.value);
49
                                    }
50
                                }
51
                            }
52
                        });
53
                    }
54
                    window.prevItem = null;
55
                });
56
            })();
57
JS
58
        );
59
60
        return Html::button($this->icon, $this->buttonOptions);
61
    }
62
}
63