-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControlSideBar.php
85 lines (71 loc) · 2.86 KB
/
ControlSideBar.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
<?php
namespace yiichina\adminlte;
use yii\helpers\ArrayHelper;
use yii\bootstrap\Html;
use yii\helpers\Url;
class ControllerSidebar extends \yii\widgets\Menu
{
public $defaultRight = false;
public $labelTemplate = '{icon}{label}';
public $linkTemplate = '<a href="{url}">{icon}{label}{right}</a>';
public $submenuTemplate = "\n<ul class=\"treeview-menu\">\n{items}\n</ul>\n";
public $options = ['class' => 'sidebar-menu tree', 'data' => ['widget' => 'tree']];
public function init()
{
echo Html::beginTag('aside', ['class' => 'main-sidebar']);
echo Html::beginTag('section', ['class' => 'sidebar']);
}
public function run()
{
parent::run();
echo Html::endTag('section');
echo Html::endTag('aside');
}
protected function renderItems($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;
}
$menu = $this->renderItem($item);
if (!empty($item['items'])) {
$class[] = 'treeview';
$submenuTemplate = ArrayHelper::getValue($item, 'submenuTemplate', $this->submenuTemplate);
$menu .= strtr($submenuTemplate, [
'{items}' => $this->renderItems($item['items']),
]);
}
Html::addCssClass($options, $class);
$lines[] = Html::tag($tag, $menu, $options);
}
return implode("\n", $lines);
}
/**
* Renders the content of a menu item.
* Note that the container and the sub-menus are not rendered here.
* @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item.
* @return string the rendering result
*/
protected function renderItem($item)
{
if (isset($item['url'])) {
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
return strtr($template, [
'{url}' => Url::to($item['url']),
'{label}' => Html::tag('span', $item['label']),
'{icon}' => $item['icon'] ?? null,
'{right}' => empty($item['items']) ? null : Html::tag('span', ArrayHelper::getValue($item, 'right', $this->defaultRight), ['class' => 'pull-right-container']),
]);
} else {
$template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
return strtr($template, [
'{label}' => $item['label'],
'{icon}' => $item['icon'] ?? null,
]);
}
}
}