<commit_before><commit_msg>Added DynamicFormCopyButton widget<commit_after><?php

namespace hipanel\widgets;

use yii\base\Widget;
use yii\bootstrap\Html;

class DynamicFormCopyButton extends Widget
{
    public $buttonOptions = ['class' => 'add-item copy btn btn-info btn-sm'];

    public $icon = '<i class="glyphicon glyphicon-duplicate"></i>';

    public $widgetContainer = '.dynamicform_wrapper';

    public function run()
    {
        $this->view->registerJs(<<<"JS"
            (function() {
                window.prevItem = null;
                var dynamicFormWrapper = $('{$this->widgetContainer}');
                dynamicFormWrapper.on('beforeInsert', function(e, item) {
                    if ($(item).hasClass('copy')) {
                        window.prevItem = $(item).closest('div.item');    
                    }
                });
                dynamicFormWrapper.on('afterInsert', function(e, item) {
                    var currentItem = $(item), prevItem = window.prevItem;
                    if (window.prevItem !== null) {
                        prevItem.find(':input').each(function() {
                            var prevInput = this;
                            if (prevInput.getAttribute("id") !== null) {
                                var needle = prevInput.getAttribute("id").split("-").slice(-1).pop();
                                var newInput = currentItem.find(":input[id$=\'" + needle + "\']");
                                if ( newInput.length === 1 ) {
                                    if (newInput.attr('data-combo-field')) { // if select2
                                        var prevOption = $(prevInput).find(':selected');
                                        if (prevOption.length) {
                                            var newOption = new Option(prevOption.text(), prevOption.attr('value'), true, true);
                                            newOption.setAttribute('data-select2-id', prevOption.attr('data-select2-id'));
                                            newInput.append(newOption).trigger('change');
                                            newInput.trigger('select2:select');
                                        }
                                    } else if (newInput.attr('data-amount-with-currency') === 'currency') { // if amount with currency
                                        newInput.val(prevInput.value);
                                        newInput.closest('.amount-with-currency-widget').find('a[data-value="' + prevInput.value + '"]').click();
                                    } else {
                                        newInput.val(prevInput.value);
                                    }
                                }
                            }
                        });
                    }
                    window.prevItem = null;
                });
            })();
JS
        );

        return Html::button($this->icon, $this->buttonOptions);
    }
}
{'fraction_non_alphanumeric': 0.1277020285999335, 'fraction_numerical': 0.0019953441968739607, 'mean_word_length': 1.1640287769784172, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '806521', 'n_tokens_mistral': 682, 'n_tokens_neox': 653, 'n_words': 114}