Completed
Push — master ( 988886...2ff342 )
by Dmitry
04:06
created

MenuButton::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 54
ccs 0
cts 51
cp 0
rs 9.6716
c 1
b 0
f 0
cc 2
eloc 18
nc 2
nop 0
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Menus for Yii2.
4
 *
5
 * @link      https://github.com/hiqdev/yii2-menus
6
 * @package   yii2-menus
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\menus\widgets;
12
13
use yii\helpers\Html;
14
15
class MenuButton extends \yii\base\Widget
16
{
17
    public $icon = '<i class="fa fa-bars"></i>&nbsp;&nbsp;<span class="caret"></span>';
18
19
    public $items;
20
21
    public $options;
22
23
    public $menuClass = Menu::class;
24
25
    public function run()
26
    {
27
        $actionsMenu = $this->renderMenuItems();
28
        if ($actionsMenu === '') {
29
            return '';
30
        }
31
32
        $this->getView()->registerCss('
33
        .menu-button .nav > li > a {
34
            padding: 5px 15px;
35
        }
36
        .menu-button .popover-content {
37
            padding: 9px 0px;
38
        }
39
        ');
40
        $this->getView()->registerJs("
41
            // Init popover
42
            ;(function () {
43
                var actionsButton = $('.menu-button button[data-toggle=\"popover\"]');
44
                actionsButton.popover();
45
                // Show one popover and hide other popovers
46
                actionsButton.on('click', function (e) {
47
                    actionsButton.not(this).popover('hide');
48
                });
49
                // hide popover on click outside
50
                $(document).on('click', function (e) {
51
                    actionsButton.each(function () {
52
                        //the 'is' for buttons that trigger popups
53
                        //the 'has' for icons within a button that triggers a popup
54
                        if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
55
                            (($(this).popover('hide').data('bs.popover')||{}).inState||{}).click = false  // fix for BS 3.3.6
56
                        }
57
58
                    });
59
                });
60
            })();
61
        ");
62
        $html = Html::beginTag('div', [
63
            'class' => 'menu-button visible-lg-inline visible-md-inline visible-sm-inline visible-xs-inline',
64
        ]);
65
        $html .= Html::button($this->icon, [
66
            'class' => 'btn btn-default btn-xs',
67
            'data' => [
68
                'toggle' => 'popover',
69
                'trigger' => 'click',
70
                'content' => $actionsMenu,
71
                'html' => 'true',
72
                'placement' => 'bottom',
73
            ],
74
        ]);
75
        $html .= Html::endTag('div');
76
77
        return $html;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    protected function renderMenuItems()
84
    {
85
        $class = $this->menuClass;
86
87
        return $class::widget([
88
            'items' => $this->items,
89
            'options' => [
90
                'class' => 'nav',
91
            ],
92
        ]);
93
    }
94
}
95