-
Notifications
You must be signed in to change notification settings - Fork 29
/
SideNav.php
250 lines (221 loc) · 8.39 KB
/
SideNav.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* @link https://github.com/MacGyer/yii2-materializecss
* @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding
* @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE
*/
namespace macgyer\yii2materializecss\widgets\navigation;
use macgyer\yii2materializecss\lib\Html;
use macgyer\yii2materializecss\widgets\Button;
use macgyer\yii2materializecss\widgets\Collapsible;
use macgyer\yii2materializecss\widgets\navigation\Nav;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* SideNav renders a side navigation, which is especially useful for mobile devices or small screen sizes.
*
* See [[Nav::$items]] for details on item structure.
*
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @see https://materializecss.github.io/materialize/sidenav.html
* @package widgets
* @subpackage navigation
*/
class SideNav extends Nav
{
/**
* @var array list of items in the nav widget. Each array element represents a single
* menu item which can be either a string or an array with the following structure:
*
* - label: string, required, the nav item label.
* - url: optional, the item's URL. Defaults to "#".
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
* - linkOptions: array, optional, the HTML attributes of the item's link.
* - options: array, optional, the HTML attributes of the item container (LI).
* - active: boolean, optional, whether the item should be on active state or not.
* - dropDownOptions: array, optional, the HTML options that will passed to the [[Dropdown]] widget.
* - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
* or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
* - encode: boolean, optional, whether the label will be HTML-encoded. If set, supersedes the $encodeLabels option for only this item.
*
* If a menu item is a string, it will be rendered directly without HTML encoding.
*/
public $items = [];
/**
* @var array the options for the underlying JS sideNav() plugin.
* The following options are supported:
* - menuWidth: 300, // Default is 240
* - edge: 'right', // Choose the horizontal origin
* - closeOnClick: true, // Closes side-nav on <a> clicks, useful for Angular/Meteor
* - draggable: true // Choose whether you can drag to open on touch screens
*
* @see https://materializecss.github.io/materialize/sidenav.html#options
*/
public $clientOptions = [];
/**
* @var bool whether the toggle button shall be rendered.
*/
public $renderToggleButton = true;
/**
* @var array the configuration options for the toggle button.
* The toggle button is rendered by the [[Button]] widget. See the docs for all available options.
*
* @see Button|Button
*/
public $toggleButtonOptions = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->activateParents = true;
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getUniqueId('sidenav_');
}
Html::addCssClass($this->options, ['widget' => 'sidenav']);
if ($this->renderToggleButton) {
$this->toggleButtonOptions = ArrayHelper::merge([
'label' => false,
'icon' => [
'name' => 'menu'
],
'type' => Button::TYPE_FLAT,
], $this->toggleButtonOptions);
Html::addCssClass($this->toggleButtonOptions['options'], ['toggleButton' => 'sidenav-trigger']);
$this->toggleButtonOptions['options']['data-target'] = $this->options['id'];
}
$this->registerPlugin('Sidenav', '.sidenav');
}
/**
* Executes the widget.
* @return string
* @throws \Exception
*/
public function run()
{
if ($this->renderToggleButton) {
$html[] = $this->renderToggleButton();
}
$html[] = $this->renderItems();
return implode("\n", $html);
}
/**
* Renders widget items.
*/
protected function renderItems()
{
$items = [];
foreach ($this->items as $i => $item) {
if (isset($item['visible']) && !$item['visible']) {
continue;
}
$items[] = $this->renderItem($item);
}
return Html::tag('ul', implode("\n", $items), $this->options);
}
/**
* Renders a widget's item.
* @param string|array $item the item to render.
* @return string the rendering result.
* @throws InvalidConfigException
*/
protected function renderItem($item)
{
if (is_string($item)) {
return $item;
}
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$listItemOptions = 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 (empty($items)) {
$content = Html::a($label, $url, $linkOptions);
} else {
Html::addCssClass($listItemOptions, ['widget' => 'submenu']);
Html::addCssClass($linkOptions, ['widget' => 'submenu-opener']);
if ($this->dropDownCaret !== '') {
$label .= ' ' . $this->dropDownCaret;
}
if (is_array($items)) {
if ($this->activateItems) {
$items = $this->isChildActive($items, $active);
}
$items = $this->renderCollapsible(Html::a($label, $url, $linkOptions), $items, $active);
}
$content = $items;
}
if ($this->activateItems && $active) {
Html::addCssClass($listItemOptions, 'active');
}
return Html::tag('li', $content, $listItemOptions);
}
/**
* Renders a submenu as Collapsible in side navigation element.
*
* @param string $link the trigger link.
* @param array $items the submenu items.
* @param bool $isParentActive whether the submenu's parent list element shall get an 'active' state.
* @return string the Collapsible markup.
*
* @throws \Exception
*/
protected function renderCollapsible($link, $items = [], $isParentActive = false)
{
$itemOptions = [];
if ($isParentActive) {
Html::addCssClass($itemOptions, ['item-activation' => 'active']);
}
$collapsibleItems = [
[
'header' => ['content' => $link],
'body' => ['content' => $this->buildCollapsibleBody($items)],
'options' => $itemOptions
],
];
return Collapsible::widget([
'items' => $collapsibleItems,
'type' => Collapsible::TYPE_ACCORDION,
]);
}
/**
* Build the needed markup for the collapsible body, i. e. the `<ul>` containing the submenu links.
*
* @param array $items the submenu items.
* @return string the Collapsible body markup.
*/
protected function buildCollapsibleBody($items = [])
{
$html[] = Html::beginTag('ul');
foreach ($items as $item) {
$url = ArrayHelper::getValue($item, 'url', null);
$label = ArrayHelper::getValue($item, 'label', '');
$options = ArrayHelper::getValue($item, 'options', []);
$link = Html::a($label, $url, $options);
$html[] = Html::tag('li', $link);
}
$html[] = Html::endTag('ul');
return implode("\n", $html);
}
/**
* Renders the side navigation toggle button.
*
* @see Button|Button
* @return string
* @throws \Exception
*/
protected function renderToggleButton()
{
return Button::widget($this->toggleButtonOptions);
}
}