-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollapseMenu.php
122 lines (104 loc) · 4.15 KB
/
CollapseMenu.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
<?php
namespace pipekung\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Description of CollapseMenu
*
* For example:
*
* echo CollapseMenu::widget([
* 'items' => [
* 'Collapsible Group Item #1' => [
* 'items' => [
* ['label' => '...', 'url' => ['...'], 'options' => ['...']],
* ['label' => '...', 'url' => ['...'], 'options' => ['...']],
* ],
* 'contentOptions' => ['class' => 'in']
* ],
* 'Collapsible Group Item #2' => [
* 'items' => [
* ['label' => '...', 'url' => ['...'], 'options' => ['...']],
* ['label' => '...', 'url' => ['...'], 'options' => ['...']],
* ],
* 'contentOptions' => [...],
* 'options' => [...],
* ],
* ]
* ]);
* ```
*
* @author Pipekung Specialz <chanja@kku.ac.th>
* @since 2.0
*/
class CollapseMenu extends Widget {
public $items = [];
public function init() {
parent::init();
Html::addCssClass($this->options, 'panel-group');
}
public function run() {
echo Html::beginTag('div', $this->options) . "\n";
echo $this->renderItems() . "\n";
echo Html::endTag('div') . "\n";
//$this->registerPlugin('collapse');
$this->registerScript();
}
public function renderItems() {
$items = [];
$index = 0;
foreach ($this->items as $header => $item) {
$options = ArrayHelper::getValue($item, 'options', []);
Html::addCssClass($options, 'panel panel-default');
$items[] = Html::tag('div', $this->renderItem($header, $item, ++$index), $options);
}
return implode("\n", $items);
}
public function renderItem($header, $item, $index) {
if (isset($item['items'])) {
$id = $this->options['id'] . '-collapse' . $index;
$options = ArrayHelper::getValue($item, 'contentOptions', []);
$options['id'] = $id;
Html::addCssClass($options, 'panel-collapse collapse');
$headerToggle = Html::tag('h4', $header . '<i class="glyphicon glyphicon-collapse-down pull-right"></i>', ['class' => 'panel-title', 'style' => 'padding: 10px 15px;']);
$header = Html::a($headerToggle, '#' . $id, [
'class' => 'collapse-toggle',
'data-toggle' => 'collapse',
'data-parent' => '#' . $this->options['id']
]) . "\n";
$menuItems = '';
foreach ($item['items'] as $menuItem) {
if (isset($menuItem['options'])) {
if (isset($menuItem['options']['style'])) {
$menuItem['options']['style'] .= 'border-radius: 0;';
} else {
$menuItem['options']['style'] = 'border-radius: 0;';
}
} else {
$menuItem['options']['style'] = 'border-radius: 0;';
}
if (!isset($menuItem['visible']) || $menuItem['visible']) {
$link = Html::a($menuItem['label'] . ' <i class="glyphicon glyphicon-chevron-right pull-right"></i>', $menuItem['url'], $menuItem['options']);
$menuItems .= Html::tag('li', $link, ['class' => (isset($menuItem['active']) && $menuItem['active']) ? 'active' : '']) . "\n";
}
}
$listMenu = Html::tag('ul', $menuItems, ['class' => 'nav nav-pills nav-stacked']) . "\n";
} else {
throw new InvalidConfigException('The "content" option is required.');
}
$group = [];
$group[] = Html::tag('div', $header, ['class' => 'panel-heading', 'style' => 'padding: 0;']);
$group[] = Html::tag('div', $listMenu, $options);
return implode("\n", $group);
}
public function registerScript() {
$id = $this->options['id'];
Yii::$app->view->registerJs("
$(document).ready(function() {
$('#$id li.active').parent().parent().addClass('in');
});
");
}
}