Completed
Push — master ( 61317a...7586e2 )
by Dmitry
12s queued 11s
created

MenuButton::run()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 0
cts 60
cp 0
rs 8.8072
c 0
b 0
f 0
cc 2
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
                var myDefaultWhiteList = $.fn.tooltip.Constructor.DEFAULTS.whiteList;
45
                myDefaultWhiteList['*'].push(/^data-[confirm|params|form|pjax|method|target|toggle|whatever]+/);
46
                actionsButton.popover({
47
                    html : true,
48
                    content: function() {
49
                        var content = $(this).attr('data-popover-content');
50
                        return  $(content).html();
51
                    }
52
                });
53
                // Show one popover and hide other popovers
54
                actionsButton.on('click', function (e) {
55
                    actionsButton.not(this).popover('hide');
56
                });
57
                // hide popover on click outside
58
                $(document).on('click', function (e) {
59
                    actionsButton.each(function () {
60
                        //the 'is' for buttons that trigger popups
61
                        //the 'has' for icons within a button that triggers a popup
62
                        if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
63
                            (($(this).popover('hide').data('bs.popover')||{}).inState||{}).click = false  // fix for BS 3.3.6
64
                        }
65
66
                    });
67
                });
68
            })();
69
        ");
70
        $popoverContentId = 'menu-button-popover-content-' . $this->id;
71
        $html = Html::beginTag('div', [
72
            'class' => 'menu-button visible-lg-inline visible-md-inline visible-sm-inline visible-xs-inline',
73
        ]);
74
        $html .= Html::button($this->icon, [
75
            'class' => 'btn btn-default btn-xs',
76
            'data' => [
77
                'toggle' => 'popover',
78
                'trigger' => 'click',
79
                'popover-content' => '#' . $popoverContentId,
80
                'placement' => 'bottom',
81
            ],
82
        ]);
83
        $html .= Html::tag('div', $actionsMenu, ['id' => $popoverContentId, 'class' => 'hidden']);
84
        $html .= Html::endTag('div');
85
86
        return $html;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected function renderMenuItems()
93
    {
94
        $class = $this->menuClass;
95
96
        return $class::widget([
97
            'items' => $this->items,
98
            'options' => [
99
                'class' => 'nav',
100
            ],
101
        ]);
102
    }
103
}
104