Просмотр исходного кода

Fixes ZF-7341: Zend_View_Helper_Menu::_renderDeepestMenu() now has a special case for when the active page is at depth one below minDepth.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16969 44c647ce-9c0f-0410-b52a-842ac1e357ba
robinsk 16 лет назад
Родитель
Сommit
9e8869c63d

+ 14 - 13
library/Zend/View/Helper/Navigation/Menu.php

@@ -314,32 +314,33 @@ class Zend_View_Helper_Navigation_Menu
                                           $minDepth,
                                           $maxDepth)
     {
-        if (!$found = $this->findActive($container, $minDepth, $maxDepth)) {
+        if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
             return '';
         }
 
-        $foundPage  = $found['page'];
-        $foundDepth = $found['depth'];
-
-        // render children or siblings?
-        if (!$foundPage->hasPages()) {
+        // special case if active page is one below minDepth
+        if ($active['depth'] < $minDepth) {
+            if (!$active['page']->hasPages()) {
+                return '';
+            }
+        } else if (!$active['page']->hasPages()) {
             // found pages has no children; render siblings
-            $foundPage = $foundPage->getParent();
-        } else if (is_int($maxDepth) && $foundDepth +1 > $maxDepth) {
+            $active['page'] = $active['page']->getParent();
+        } else if (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) {
             // children are below max depth; render siblings
-            $foundPage = $foundPage->getParent();
+            $active['page'] = $active['page']->getParent();
         }
 
         $ulClass = $ulClass ? ' class="' . $ulClass . '"' : '';
         $html = $indent . '<ul' . $ulClass . '>' . self::EOL;
 
-        foreach ($foundPage as $page) {
-            if (!$this->accept($page)) {
+        foreach ($active['page'] as $subPage) {
+            if (!$this->accept($subPage)) {
                 continue;
             }
-            $liClass = $page->isActive(true) ? ' class="active"' : '';
+            $liClass = $subPage->isActive(true) ? ' class="active"' : '';
             $html .= $indent . '    <li' . $liClass . '>' . self::EOL;
-            $html .= $indent . '        ' . $this->htmlify($page) . self::EOL;
+            $html .= $indent . '        ' . $this->htmlify($subPage) . self::EOL;
             $html .= $indent . '    </li>' . self::EOL;
         }
 

+ 28 - 17
tests/Zend/View/Helper/Navigation/MenuTest.php

@@ -341,6 +341,34 @@ class Zend_View_Helper_Navigation_MenuTest
         $this->assertEquals($expected, $actual);
     }
 
+    private function _setActive($label)
+    {
+        $container = $this->_helper->getContainer();
+
+        foreach ($container->findAllByActive(true) as $page) {
+            $page->setActive(false);
+        }
+
+        if ($p = $container->findOneByLabel($label)) {
+            $p->setActive(true);
+        }
+    }
+
+    public function testOnlyActiveBranchNoParentsActiveOneBelowMinDepth()
+    {
+        $this->_setActive('Page 2');
+
+        $this->_helper->setOnlyActiveBranch()
+                      ->setMinDepth(1)
+                      ->setMaxDepth(1)
+                      ->setRenderParents(false);
+
+        $expected = $this->_getExpected('menu/onlyactivebranch_np_bd2.html');
+        $actual = $this->_helper->renderMenu();
+
+        $this->assertEquals($expected, $actual);
+    }
+
     public function testRenderSubMenuShouldOverrideOptions()
     {
         $this->_helper->setOnlyActiveBranch(false)
@@ -354,23 +382,6 @@ class Zend_View_Helper_Navigation_MenuTest
         $this->assertEquals($expected, $actual);
     }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
     public function testOptionMaxDepth()
     {
         $options = array(

+ 11 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/onlyactivebranch_np_bd2.html

@@ -0,0 +1,11 @@
+<ul class="navigation">
+    <li>
+        <a href="page2/page2_1">Page 2.1</a>
+    </li>
+    <li>
+        <a href="page2/page2_2">Page 2.2</a>
+    </li>
+    <li>
+        <a href="page2/page2_3">Page 2.3</a>
+    </li>
+</ul>