-
Notifications
You must be signed in to change notification settings - Fork 0
/
SidebarMenu.php
160 lines (140 loc) · 4.71 KB
/
SidebarMenu.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
<?php
namespace memclutter\AdminLTE;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\Menu;
class SidebarMenu extends Menu
{
/**
* @inheritdoc
*/
public $activateParents = true;
/**
* @inheritdoc
*/
public $encodeLabels = false;
/**
* @inheritdoc
*/
public $submenuTemplate = "\n<ul class=\"treeview-menu\">\n{items}\n</ul>\n";
/**
* @inheritdoc
*/
public $linkTemplate = '<a href="{url}">{icon} {label} {angle}</a>';
/**
* @inheritdoc
*/
public $labelTemplate = '{icon} {label}';
/**
* @var string Menu header
*/
public $header;
/**
* @var string icon template
*/
public $iconTemplate = '<i class="fa fa-{name}"></i>';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
// add css class 'sidebar-menu'
if (isset($this->options['class'])) {
if (is_array($this->options['class']) && !in_array('sidebar-menu', $this->options['class'])) {
$this->options['class'][] = 'sidebar-menu';
} elseif (false === strpos($this->options['class'], 'sidebar-menu')) {
$this->options['class'] .= ' sidebar-menu';
}
} else {
$this->options['class'] = 'sidebar-menu';
}
// add first header item
if (!empty($this->header)) {
$this->items = ArrayHelper::merge([
['label' => $this->header, 'options' => ['class' => 'header']],
], $this->items);
}
}
/**
* @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'];
$hasActiveChild = false;
if (isset($item['items'])) {
// add css class for treeview
if (isset($item['options']['class'])) {
if (is_array($item['options']['class']) && !in_array('treeview', $item['options']['class'])) {
$items[$i]['options']['class'][] = 'treeview';
} elseif (false !== strpos($item['options']['class'], 'treeview')) {
$items[$i]['options']['class'] .= ' treeview';
}
} else {
$items[$i]['options']['class'] = 'treeview';
}
$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);
}
/**
* @inheritdoc
*/
protected function renderItem($item)
{
if (isset($item['url'])) {
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
return strtr($template, [
'{icon}' => $this->renderIcon($item),
'{url}' => Html::encode(Url::to($item['url'])),
'{label}' => Html::tag('span', $item['label']),
'{angle}' => isset($item['items']) && !empty($item['items'])
? '<i class="fa fa-angle-left pull-right"></i>' : '',
]);
} else {
$template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
return strtr($template, [
'{icon}' => $this->renderIcon($item),
'{label}' => Html::tag('span', $item['label']),
]);
}
}
protected function renderIcon($item)
{
if (isset($item['icon'])) {
return strtr($this->iconTemplate, [
'{name}' => $item['icon'],
]);
} else {
return '';
}
}
}