Skip to content

Commit bbb275d

Browse files
committed
overhauled sidenav
1 parent fb09405 commit bbb275d

File tree

3 files changed

+169
-87
lines changed

3 files changed

+169
-87
lines changed

src/widgets/navigation/Nav.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,6 @@ class Nav extends BaseWidget
110110
*/
111111
public $dropDownCaret;
112112

113-
/**
114-
* @var array the options for the underlying JS sideNav() plugin.
115-
* The following options are supported:
116-
* - menuWidth: 300, // Default is 240
117-
* - edge: 'right', // Choose the horizontal origin
118-
* - closeOnClick: true, // Closes side-nav on `<a>` clicks, useful for Angular/Meteor
119-
* - draggable: true // Choose whether you can drag to open on touch screens
120-
*
121-
* @see http://materializecss.com/side-nav.html#options
122-
*/
123-
public $sideNavClientOptions = [];
124-
125-
/**
126-
* @var array the configuration options for the toggle button.
127-
* The toggle button is rendered by the [[\macgyer\yii2materializecss\widgets\Button|Button]] widget. See the docs for all available options.
128-
*
129-
* @see \macgyer\yii2materializecss\widgets\Button|Button
130-
*/
131-
public $sideNavToggleButtonOptions = [];
132-
133-
/**
134-
* @var boolean whether to render a side navigation.
135-
* Set this option to `false` if you do not want the side navigation to be rendered automatically.
136-
*/
137-
public $renderSideNav = true;
138-
139113
/**
140114
* Initializes the widget.
141115
*/
@@ -161,10 +135,6 @@ public function run()
161135
MaterializeAsset::register($this->getView());
162136
$html[] = $this->renderItems();
163137

164-
if ($this->renderSideNav === true) {
165-
$html[] = $this->renderSideNav();
166-
}
167-
168138
return implode("\n", $html);
169139
}
170140

@@ -214,7 +184,7 @@ protected function renderItem($item)
214184
if (empty($items)) {
215185
$items = '';
216186
} else {
217-
$toggleTarget = 'dropdown_' . md5(uniqid());
187+
$toggleTarget = $this->getUniqueId('dropdown_');
218188
$linkOptions['data-target'] = $toggleTarget;
219189
Html::addCssClass($options, ['widget' => 'dropdown']);
220190
Html::addCssClass($linkOptions, ['widget' => 'dropdown-trigger']);
@@ -260,22 +230,6 @@ protected function renderDropdown($items, $parentItem, $targetId)
260230
'items' => $items,
261231
'encodeLabels' => $this->encodeLabels,
262232
'toggleButtonOptions' => false,
263-
// 'clientOptions' => false,
264-
// 'view' => $this->getView(),
265-
]);
266-
}
267-
268-
/**
269-
* Renders the side navigation and corresponding toggle button.
270-
* @return string the rendered side navigation markup.
271-
* @throws \Exception
272-
*/
273-
protected function renderSideNav()
274-
{
275-
return SideNav::widget([
276-
'items' => $this->items,
277-
'toggleButtonOptions' => $this->sideNavToggleButtonOptions,
278-
'clientOptions' => $this->sideNavClientOptions,
279233
]);
280234
}
281235

src/widgets/navigation/NavBar.php

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
use macgyer\yii2materializecss\assets\MaterializePluginAsset;
1111
use macgyer\yii2materializecss\lib\BaseWidget;
1212
use macgyer\yii2materializecss\lib\Html;
13+
use macgyer\yii2materializecss\widgets\Button;
14+
use macgyer\yii2materializecss\widgets\Icon;
1315
use Yii;
16+
use yii\helpers\ArrayHelper;
1417

1518
/**
1619
* NavBar renders a navbar HTML component.
@@ -99,13 +102,56 @@ class NavBar extends BaseWidget
99102
*/
100103
public $wrapperOptions = [];
101104

105+
/**
106+
* @var array the side nav configuration options.
107+
* See SideNav|SideNav for details.
108+
*/
109+
public $sidenavOptions = [];
110+
111+
/**
112+
* @var bool whether the side nav shall be rendered.
113+
*/
114+
public $renderSidenav = true;
115+
116+
/**
117+
* @var array the options for the underlying JS sideNav() plugin.
118+
* @see https://materializecss.com/sidenav.html#options
119+
*/
120+
public $sidenavClientOptions = [];
121+
122+
/**
123+
* @var array the configuration options for the toggle button.
124+
* The toggle button is rendered by the [[\macgyer\yii2materializecss\widgets\Button|Button]] widget. See the docs for all available options.
125+
*
126+
* @see \macgyer\yii2materializecss\widgets\Button|Button
127+
*/
128+
public $sidenavToggleButtonOptions = [];
129+
130+
/**
131+
* @var array list of items in the nav widget. Each array element represents a single
132+
* menu item which can be either a string or an array with the following structure:
133+
*
134+
* - label: string, required, the nav item label.
135+
* - url: optional, the item's URL. Defaults to "#".
136+
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
137+
* - linkOptions: array, optional, the HTML attributes of the item's link.
138+
* - options: array, optional, the HTML attributes of the item container (LI).
139+
* - active: boolean, optional, whether the item should be on active state or not.
140+
* - dropDownOptions: array, optional, the HTML options that will passed to the [[Dropdown]] widget.
141+
* - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
142+
* or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
143+
* - encode: boolean, optional, whether the label will be HTML-encoded. If set, supersedes the $encodeLabels option for only this item.
144+
*
145+
* If a menu item is a string, it will be rendered directly without HTML encoding.
146+
*/
147+
public $sidenavItems = [];
148+
102149
/**
103150
* Initializes the widget.
104151
*/
105152
public function init()
106153
{
107154
parent::init();
108-
$this->clientOptions = false;
109155
$html = [];
110156

111157
if (empty($this->options['role'])) {
@@ -140,9 +186,18 @@ public function init()
140186
*/
141187
public function run()
142188
{
189+
if ($this->renderSidenav) {
190+
$sidenavId = $this->getUniqueId('sidenav_');
191+
$this->initSidenav($sidenavId);
192+
}
193+
143194
$html = [];
144195
$html[] = Html::endTag('div'); // container
145196

197+
if ($this->renderSidenav) {
198+
$html[] = $this->renderSidenavToggleButton();
199+
}
200+
146201
$html[] = Html::endTag('div'); // nav-wrapper
147202

148203
$html[] = Html::endTag('nav');
@@ -151,8 +206,48 @@ public function run()
151206
$html[] = Html::endTag('div');
152207
}
153208

154-
MaterializePluginAsset::register($this->getView());
209+
if ($this->renderSidenav) {
210+
$html[] = $this->renderSidenav($sidenavId);
211+
}
155212

156213
return implode("\n", $html);
157214
}
215+
216+
protected function initSidenav($sidenavId)
217+
{
218+
$this->sidenavToggleButtonOptions = ArrayHelper::merge([
219+
'label' => false,
220+
'icon' => [
221+
'name' => 'menu'
222+
],
223+
'type' => Button::TYPE_FLAT,
224+
], $this->sidenavToggleButtonOptions);
225+
226+
if (!isset($this->sidenavToggleButtonOptions['options']['data-target'])) {
227+
$this->sidenavToggleButtonOptions['options']['data-target'] = $sidenavId;
228+
}
229+
230+
Html::addCssClass($this->sidenavToggleButtonOptions['options'], ['trigger' => 'sidenav-trigger']);
231+
}
232+
233+
/**
234+
* Renders the side navigation and corresponding toggle button.
235+
* @param $sidenavId
236+
* @return string the rendered side navigation markup.
237+
* @throws \Exception
238+
*/
239+
protected function renderSidenav($sidenavId)
240+
{
241+
return SideNav::widget([
242+
'items' => $this->sidenavItems,
243+
'renderToggleButton' => false,
244+
'clientOptions' => $this->sidenavClientOptions,
245+
'options' => ['id' => $sidenavId],
246+
]);
247+
}
248+
249+
protected function renderSidenavToggleButton()
250+
{
251+
return Button::widget($this->sidenavToggleButtonOptions);
252+
}
158253
}

0 commit comments

Comments
 (0)