FormulaHelpModal   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 46
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 33 2
A formulaExamplesProvider() 0 4 1
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\widgets;
12
13
use hipanel\modules\finance\providers\FormulaExamplesProvider;
14
use yii\base\Widget;
15
use yii\web\View;
16
17
/**
18
 * Class FormulaHelpModal.
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class FormulaHelpModal extends Widget
23
{
24
    /**
25
     * @var bool
26
     */
27
    private $wasRendered = false;
28
29
    public function run()
30
    {
31
        if (!$this->wasRendered) {
32
            $this->view->registerJs(<<<JS
33
$('#{$this->getId()}').data('onPasteRequested', function() {
34
    let template = $(this).siblings('kbd').text();
35
    let input = $(this).closest('.modal').modal('hide').data('input');
36
    let editor = input.data('ace-editor');
37
    let session = editor.session;
38
39
    let text = template;
40
    if (session.getLength() > 1 || session.getLine(0).length > 0) {
41
        text = '\\n' + text;
42
    }
43
44
    session.insert({
45
       row: session.getLength(),
46
       column: 0
47
    }, text)
48
    editor.selection.moveCursorToPosition({row: session.getLength(), column: 0});
49
    editor.selection.selectLine();
50
    setTimeout(() => editor.focus(), 500);
51
});
52
JS
53
            );
54
            $this->view->on(View::EVENT_END_BODY, function () {
55
                echo $this->render('formulaHelpModal');
56
            });
57
            $this->wasRendered = true;
58
        }
59
60
        return $this->getId();
61
    }
62
63
    public function formulaExamplesProvider(): FormulaExamplesProvider
64
    {
65
        return new FormulaExamplesProvider();
66
    }
67
}
68