Browse Source

Fixes #157: Active status of multilevel submenu items of the `yii\bootstrap\Nav` class is determined correctly now

tags/2.0.7
Alexander Makarov 8 years ago committed by GitHub
parent
commit
b0f2953200
  1. 1
      CHANGELOG.md
  2. 14
      Nav.php
  3. 31
      tests/NavTest.php

1
CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 bootstrap extension Change Log
2.0.7 under development 2.0.7 under development
----------------------- -----------------------
- Bug #157: Active status of multilevel submenu items of the `yii\bootstrap\Nav` class is determined correctly now (PowerGamer1)
- Enh #64: Added simplified syntax for specifying `Collapse` widget `$items` (Faryshta, cebe) - Enh #64: Added simplified syntax for specifying `Collapse` widget `$items` (Faryshta, cebe)
- Enh #131 Added `tabContentOptions` to set HTML attributes for 'tab-content' container in `Tabs` widget (AndrewKorpusov) - Enh #131 Added `tabContentOptions` to set HTML attributes for 'tab-content' container in `Tabs` widget (AndrewKorpusov)
- Enh #145: Added the ability to customize the class used to draw dropdowns in `yii\bootstrap\Nav`, `yii\bootstrapButtonDropdown` and `yii\bootstrap\Tab` widgets (PowerGamer1) - Enh #145: Added the ability to customize the class used to draw dropdowns in `yii\bootstrap\Nav`, `yii\bootstrapButtonDropdown` and `yii\bootstrap\Tab` widgets (PowerGamer1)

14
Nav.php

@ -106,7 +106,7 @@ class Nav extends Widget
*/ */
public $dropDownCaret; public $dropDownCaret;
/** /**
* @var string name of a class to use for rendering dropdowns withing this widget. Defaults to [[Dropdown]]. * @var string name of a class to use for rendering dropdowns within this widget. Defaults to [[Dropdown]].
* @since 2.0.7 * @since 2.0.7
*/ */
public $dropdownClass = 'yii\bootstrap\Dropdown'; public $dropdownClass = 'yii\bootstrap\Dropdown';
@ -234,12 +234,24 @@ class Nav extends Widget
protected function isChildActive($items, &$active) protected function isChildActive($items, &$active)
{ {
foreach ($items as $i => $child) { foreach ($items as $i => $child) {
if (is_array($child) && !ArrayHelper::getValue($child, 'visible', true)) {
continue;
}
if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) { if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) {
Html::addCssClass($items[$i]['options'], 'active'); Html::addCssClass($items[$i]['options'], 'active');
if ($this->activateParents) { if ($this->activateParents) {
$active = true; $active = true;
} }
} }
$childItems = ArrayHelper::getValue($child, 'items');
if (is_array($childItems)) {
$activeParent = false;
$items[$i]['items'] = $this->isChildActive($childItems, $activeParent);
if ($activeParent) {
Html::addCssClass($items[$i]['options'], 'active');
$active = true;
}
}
} }
return $items; return $items;
} }

31
tests/NavTest.php

@ -272,6 +272,37 @@ EXPECTED;
} }
/** /**
* @see https://github.com/yiisoft/yii2-bootstrap/issues/96
* @see https://github.com/yiisoft/yii2-bootstrap/issues/157
*/
public function testDeepActivateParents()
{
Nav::$counter = 0;
$out = Nav::widget([
'activateParents' => true,
'items' => [
[
'label' => 'Dropdown',
'items' => [
[
'label' => 'Sub-dropdown',
'items' => [
['label' => 'Page', 'content' => 'Page', 'active' => true],
],
],
],
],
],
]);
$expected = <<<EXPECTED
<ul id="w0" class="nav"><li class="dropdown active"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Dropdown <span class="caret"></span></a><ul id="w1" class="dropdown-menu"><li class="active dropdown-submenu"><a href="#" tabindex="-1">Sub-dropdown</a><ul><li class="active dropdown-header">Page</li></ul></li></ul></li></ul>
EXPECTED;
$this->assertEqualsWithoutLE($expected, $out);
}
/**
* Mocks controller action with parameters * Mocks controller action with parameters
* *
* @param string $controllerId * @param string $controllerId

Loading…
Cancel
Save