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> <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
|
|
|
|