|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\widgets; |
|
12
|
|
|
|
|
13
|
|
|
use yii\base\Widget; |
|
14
|
|
|
use yii\bootstrap\Html; |
|
15
|
|
|
|
|
16
|
|
|
class DynamicFormCopyButton extends Widget |
|
17
|
|
|
{ |
|
18
|
|
|
public $buttonOptions = ['class' => 'add-item copy btn btn-info btn-sm']; |
|
19
|
|
|
|
|
20
|
|
|
public $icon = '<i class="glyphicon glyphicon-duplicate"></i>'; |
|
21
|
|
|
|
|
22
|
|
|
public $widgetContainer = '.dynamicform_wrapper'; |
|
23
|
|
|
|
|
24
|
|
|
public function run() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->view->registerJs(/** @lang ECMA Script Level 4 */ |
|
27
|
|
|
<<<"JS" |
|
28
|
|
|
(function() { |
|
29
|
|
|
window.prevItem = null; |
|
30
|
|
|
var dynamicFormWrapper = $('{$this->widgetContainer}'); |
|
31
|
|
|
dynamicFormWrapper.on('beforeInsert', function(e, item) { |
|
32
|
|
|
if ($(item).hasClass('copy')) { |
|
33
|
|
|
window.prevItem = $(item).closest('div.item'); |
|
34
|
|
|
} |
|
35
|
|
|
}); |
|
36
|
|
|
dynamicFormWrapper.on('afterInsert', function(e, item) { |
|
37
|
|
|
var currentItem = $(item), prevItem = window.prevItem; |
|
38
|
|
|
if (window.prevItem !== null) { |
|
39
|
|
|
prevItem.find(':input').each(function() { |
|
40
|
|
|
var prevInput = this; |
|
41
|
|
|
if (prevInput.getAttribute("id") !== null) { |
|
42
|
|
|
var needle = prevInput.getAttribute("id").split("-").slice(-1).pop(); |
|
43
|
|
|
var newInput = currentItem.find(":input[id$=\'" + needle + "\']"); |
|
44
|
|
|
if ( newInput.length === 1 ) { |
|
45
|
|
|
if (newInput.attr('data-combo-field')) { // if select2 |
|
46
|
|
|
var prevOption = $(prevInput).find(':selected'); |
|
47
|
|
|
if (prevOption.length) { |
|
48
|
|
|
prevOption.each(function(i, elem) { |
|
49
|
|
|
var elem = $(elem); |
|
50
|
|
|
var newOption = new Option(elem.text(), elem.attr('value'), true, true); |
|
51
|
|
|
newOption.setAttribute('data-select2-id', elem.attr('data-select2-id')); |
|
52
|
|
|
newInput.append(newOption).trigger('change'); |
|
53
|
|
|
newInput.trigger('select2:select'); |
|
54
|
|
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
} else if (newInput.attr('data-amount-with-currency') === 'currency') { // if amount with currency |
|
57
|
|
|
newInput.val(prevInput.value); |
|
58
|
|
|
newInput.closest('.amount-with-currency-widget').find('a[data-value="' + prevInput.value + '"]').click(); |
|
59
|
|
|
} else { |
|
60
|
|
|
newInput.val(prevInput.value); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
}); |
|
65
|
|
|
} |
|
66
|
|
|
window.prevItem = null; |
|
67
|
|
|
}); |
|
68
|
|
|
})(); |
|
69
|
|
|
JS |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return Html::button($this->icon, $this->buttonOptions); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|