forked from dmstr/yii2-adminlte-asset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.php
212 lines (201 loc) · 7.99 KB
/
Menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace dmstr\widgets;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\helpers\Html;
/**
* Class Menu
* Theme menu widget.
*/
class Menu extends \yii\widgets\Menu
{
/**
* @inheritdoc
*/
public $linkTemplate = '<a href="{url}">{icon} {label}</a>';
/**
* @inheritdoc
* Styles all labels of items on sidebar by AdminLTE
*/
public $labelTemplate = '<span>{label}</span>';
public $submenuTemplate = "\n<ul class='treeview-menu' {show}>\n{items}\n</ul>\n";
public $activateParents = true;
public $defaultIconHtml = '<i class="fa fa-circle-o"></i> ';
public $options = ['class' => 'sidebar-menu', 'data-widget' => 'tree'];
/**
* @var string is prefix that will be added to $item['icon'] if it exist.
* By default uses for Font Awesome (http://fontawesome.io/)
*/
public static $iconClassPrefix = 'fa fa-';
private $noDefaultAction;
private $noDefaultRoute;
/**
* Renders the menu.
*/
public function run()
{
if ($this->route === null && Yii::$app->controller !== null) {
$this->route = Yii::$app->controller->getRoute();
}
if ($this->params === null) {
$this->params = Yii::$app->request->getQueryParams();
}
$posDefaultAction = strpos($this->route, Yii::$app->controller->defaultAction);
if ($posDefaultAction) {
$this->noDefaultAction = rtrim(substr($this->route, 0, $posDefaultAction), '/');
} else {
$this->noDefaultAction = false;
}
$posDefaultRoute = strpos($this->route, Yii::$app->controller->module->defaultRoute);
if ($posDefaultRoute) {
$this->noDefaultRoute = rtrim(substr($this->route, 0, $posDefaultRoute), '/');
} else {
$this->noDefaultRoute = false;
}
$items = $this->normalizeItems($this->items, $hasActiveChild);
if (!empty($items)) {
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'ul');
echo Html::tag($tag, $this->renderItems($items), $options);
}
}
/**
* @inheritdoc
*/
protected function renderItem($item)
{
if (isset($item['items'])) {
$labelTemplate = '<a href="{url}">{icon} {label} <span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a>';
$linkTemplate = '<a href="{url}">{icon} {label} <span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a>';
} else {
$labelTemplate = $this->labelTemplate;
$linkTemplate = $this->linkTemplate;
}
$replacements = [
'{label}' => strtr($this->labelTemplate, ['{label}' => $item['label'],]),
'{icon}' => empty($item['icon']) ? $this->defaultIconHtml
: '<i class="' . static::$iconClassPrefix . $item['icon'] . '"></i> ',
'{url}' => isset($item['url']) ? Url::to($item['url']) : 'javascript:void(0);',
];
$template = ArrayHelper::getValue($item, 'template', isset($item['url']) ? $linkTemplate : $labelTemplate);
return strtr($template, $replacements);
}
/**
* Recursively renders the menu items (without the container tag).
* @param array $items the menu items to be rendered recursively
* @return string the rendering result
*/
protected function renderItems($items)
{
$n = count($items);
$lines = [];
foreach ($items as $i => $item) {
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
$tag = ArrayHelper::remove($options, 'tag', 'li');
$class = [];
if ($item['active']) {
$class[] = $this->activeCssClass;
}
if ($i === 0 && $this->firstItemCssClass !== null) {
$class[] = $this->firstItemCssClass;
}
if ($i === $n - 1 && $this->lastItemCssClass !== null) {
$class[] = $this->lastItemCssClass;
}
if (!empty($class)) {
if (empty($options['class'])) {
$options['class'] = implode(' ', $class);
} else {
$options['class'] .= ' ' . implode(' ', $class);
}
}
$menu = $this->renderItem($item);
if (!empty($item['items'])) {
$menu .= strtr($this->submenuTemplate, [
'{show}' => $item['active'] ? "style='display: block'" : '',
'{items}' => $this->renderItems($item['items']),
]);
if (isset($options['class'])) {
$options['class'] .= ' treeview';
} else {
$options['class'] = 'treeview';
}
}
$lines[] = Html::tag($tag, $menu, $options);
}
return implode("\n", $lines);
}
/**
* @inheritdoc
*/
protected function normalizeItems($items, &$active)
{
foreach ($items as $i => $item) {
if (isset($item['visible']) && !$item['visible']) {
unset($items[$i]);
continue;
}
if (!isset($item['label'])) {
$item['label'] = '';
}
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$items[$i]['icon'] = isset($item['icon']) ? $item['icon'] : '';
$hasActiveChild = false;
if (isset($item['items'])) {
$items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
if (empty($items[$i]['items']) && $this->hideEmptyItems) {
unset($items[$i]['items']);
if (!isset($item['url'])) {
unset($items[$i]);
continue;
}
}
}
if (!isset($item['active'])) {
if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item)) {
$active = $items[$i]['active'] = true;
} else {
$items[$i]['active'] = false;
}
} elseif ($item['active']) {
$active = true;
}
}
return array_values($items);
}
/**
* Checks whether a menu item is active.
* This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
* When the `url` option of a menu item is specified in terms of an array, its first element is treated
* as the route for the item and the rest of the elements are the associated parameters.
* Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
* be considered active.
* @param array $item the menu item to be checked
* @return boolean whether the menu item is active
*/
protected function isItemActive($item)
{
if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
$route = $item['url'][0];
if (isset($route[0]) && $route[0] !== '/' && Yii::$app->controller) {
$route = ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
}
$route = ltrim($route, '/');
if ($route != $this->route && $route !== $this->noDefaultRoute && $route !== $this->noDefaultAction) {
return false;
}
unset($item['url']['#']);
if (count($item['url']) > 1) {
foreach (array_splice($item['url'], 1) as $name => $value) {
if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
return false;
}
}
}
return true;
}
return false;
}
}