MenuButton   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 90
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 90
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

2 Methods

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