Completed
Push — master ( d1d9d0...f3d60a )
by Andrii
04:43
created

ChangeFormulaButton::getJsExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 28
cp 0
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\widgets;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\base\Widget;
8
use yii\bootstrap\Modal;
9
use yii\helpers\Html;
10
use yii\web\JsExpression;
11
use yii\web\View;
12
13
class ChangeFormulaButton extends Widget
14
{
15
    public const MOD_ADD = 'add';
16
17
    public const MOD_REPLACE = 'replace';
18
19
    /**
20
     * @var string the modal size. Can be [[MOD_REPLACE]] or [[MOD_ADD]]
21
     */
22
    public $mod;
23
24
    /**
25
     * @var string
26
     */
27
    private $label;
28
29
    /**
30
     * @var int
31
     */
32
    private $modalId;
33
34
    public function init()
35
    {
36
        if (empty($this->mod)) {
37
            throw new InvalidConfigException('`$mod` must be set');
38
        }
39
        $modalId = mt_rand();
40
        $this->modalId = $modalId;
41
        $operation = [
42
            self::MOD_ADD => Yii::t('hipanel:finance', 'Add formula'),
43
            self::MOD_REPLACE => Yii::t('hipanel:finance', 'Replace formulas'),
44
        ];
45
        $this->label = $operation[$this->mod];
46
        $this->view->registerCss('
47
        .modal-body .ace_editor { min-height: 34px; padding: 0; }
48
        .box.box-solid > .box-header .btn:hover, .box.box-solid > .box-header a:hover {
49
            background-color: #008d4c;
50
        }
51
        ');
52
        $this->view->registerJs($this->getJsExpression(), View::POS_END);
53
        $this->view->on(View::EVENT_END_BODY, function () use ($modalId) {
54
            Modal::begin([
55
                'id' => $this->modalId,
56
                'header' => Html::tag('h4', $this->label, ['class' => 'modal-title']),
57
                'toggleButton' => false,
58
                'footer' => Html::button($this->label, [
59
                    'class' => 'btn btn-success',
60
                    'data-dismiss' => 'modal',
61
                ]),
62
            ]);
63
            echo Html::textarea('modal-formula-input', null, ['id' => $modalId . '-modal-formula-input', 'class' => 'form-control formula-input']);
64
            Modal::end();
65
        });
66
67
        parent::init();
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function run(): string
74
    {
75
        return Html::button($this->label, [
76
            'class' => 'btn btn-success btn-sm',
77
            'data' => [
78
                'toggle' => 'modal',
79
                'target' => '#' . $this->modalId,
80
            ],
81
        ]);
82
    }
83
84
    private function getJsExpression(): string
85
    {
86
        $jsExpression = [
87
            self::MOD_ADD => new JsExpression(<<<"JS"
88
$('#{$this->modalId}.modal button').on('click', () => {
89
    const t = $('.modal.in .formula-input').first().data('ace-editor').session;
90
    $(".form-group .formula-input").each((idx, elem) => {
91
        const e = $(elem).data("ace-editor").session;
92
        const n = "\\n" + t.getValue();
93
        e.insert({row: e.getLength(), column: 0}, n);
94
    });
95
    t.setValue('');
96
});
97
JS
98
            ),
99
            self::MOD_REPLACE => new JsExpression(<<<"JS"
100
$('#{$this->modalId}.modal button').on('click', () => {
101
    const t = $('.modal.in .formula-input').first().data('ace-editor').session;
102
    $(".form-group .formula-input").each((idx, elem) => {
103
        $(elem).data("ace-editor").session.setValue(t.getValue());
104
    });
105
    t.setValue('');
106
});
107
JS
108
            ),
109
        ];
110
111
        return $jsExpression[$this->mod];
112
    }
113
}
114