-
Notifications
You must be signed in to change notification settings - Fork 1
/
NavX.php
executable file
·122 lines (111 loc) · 3.81 KB
/
NavX.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
/**
* @Author: pangpond
* @Date: 2015-03-12 20:43:00
* @Last Modified by: pangpond
* @Last Modified time: 2015-03-27 15:17:58
*/
namespace nextensions\material;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* An extended nav menu for Bootstrap 3
* that offers submenu drilldown
*
*/
class NavX extends \yii\bootstrap\Nav
{
/**
* @var string the class name to render the Dropdown items.
* Defaults to `\nextensions\material\DropdownX`.
*/
public $dropdownClass = 'nextensions\material\DropdownX';
/**
* @var array the dropdown widget options
*/
public $dropdownOptions = [];
/**
* @var string the caret indicator to display for dropdowns
*/
public $dropdownIndicator = '';
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init() {
if (!class_exists($this->dropdownClass)) {
throw new InvalidConfigException("The dropdownClass '{$this->dropdownClass}' does not exist or is not accessible.");
}
parent::init();
Html::removeCssClass($this->options, 'nav');
}
/**
* @inheritdoc
*/
public function renderItem($item)
{
if (is_string($item)) {
return $item;
}
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
$options = ArrayHelper::getValue($item, 'options', []);
$items = ArrayHelper::getValue($item, 'items');
$url = ArrayHelper::getValue($item, 'url', '#');
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
if (isset($item['active'])) {
$active = ArrayHelper::remove($item, 'active', false);
} else {
$active = $this->isItemActive($item);
}
if ($items !== null) {
// $linkOptions['data-toggle'] = 'dropdown';
Html::addCssClass($options, 'gui-folder');
// Html::addCssClass($linkOptions, 'dropdown-toggle');
$label .= $this->dropdownIndicator;
if (is_array($items)) {
if ($this->activateItems) {
$items = $this->isChildActive($items, $active);
}
$dropdown = $this->dropdownClass;
$dropdownOptions = ArrayHelper::merge($this->dropdownOptions, [
'items' => $items,
'encodeLabels' => $this->encodeLabels,
'clientOptions' => false,
'view' => $this->getView(),
]);
$items = $dropdown::widget($dropdownOptions);
}
}
if ($this->activateItems && $active) {
Html::addCssClass($options, 'active');
}
return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
}
/**
* @inheritdoc
*/
protected function isChildActive($items, &$active)
{
foreach ($items as $i => $child) {
if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) {
Html::addCssClass($items[$i]['options'], 'active');
if ($this->activateParents) {
$active = true;
}
}
if (isset($items[$i]['items']) && is_array($items[$i]['items'])) {
$childActive = false;
$items[$i]['items'] = $this->isChildActive($items[$i]['items'], $childActive);
if ($childActive) {
Html::addCssClass($items[$i]['options'], 'active');
$active = true;
}
}
}
return $items;
}
}