Selaa lähdekoodia

ZF-6941: Add "expand sibling branches" option to Zend_View_Helper_Navigation_Menu

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24839 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 vuotta sitten
vanhempi
commit
75ac3afe19

+ 68 - 1
documentation/manual/en/module_specs/Zend_View-Helpers-Navigation.xml

@@ -1254,6 +1254,14 @@ Output:
                     should be rendered.
                 </para>
             </listitem>
+            
+            <listitem>
+                <para>
+                    <code>{get|set}ExpandSiblingNodesOfActiveBranch()</code> 
+                    gets/sets a flag specifying whether the sibling nodes of all
+                    nodes in the active branch should also be expanded and rendered.
+                </para>
+            </listitem>
 
             <listitem>
                 <para>
@@ -1357,6 +1365,15 @@ Output:
 
                     <listitem>
                         <para>
+                            <code>expandSiblingNodesOfActiveBranch</code>; 
+                            whether the sibling nodes of nodes in the active 
+                            branch should be expanded and rendered. Expects
+                            a <type>Boolean</type> value.
+                        </para>
+                    </listitem>
+
+                    <listitem>
+                        <para>
                             <code>renderParents</code>; whether parents
                             should be rendered if only rendering active
                             branch. Expects a <type>Boolean</type> value.
@@ -1791,7 +1808,7 @@ Output:
 </ul>
 ]]></programlisting>
         </example>
-
+        
         <example id="zend.view.helpers.initial.navigation.menu.example10">
             <title>Rendering a custom menu using a partial view script</title>
 
@@ -1830,6 +1847,56 @@ foreach ($this->container as $page) {
 <a href="/community">Community</a>
 ]]></programlisting>
         </example>
+        
+        <example id="zend.view.helpers.initial.navigation.menu.example11">
+            <title>
+                Rendering only the active branch and all siblings of the active branch 
+            </title>
+
+            <programlisting language="php"><![CDATA[
+echo $this->navigation()
+          ->menu()
+          ->setExpandSiblingNodesOfActiveBranch(true);
+]]></programlisting>
+
+            <para>Output:</para>
+
+            <programlisting language="php"><![CDATA[
+<ul class="navigation">
+    <li>
+        <a title="Go Home" href="/">Home</a>
+    </li>
+    <li class="active">
+        <a href="/products">Products</a>
+        <ul>
+            <li class="active">
+                <a href="/products/server">Foo Server</a>
+                <ul>
+                    <li class="active">
+                        <a href="/products/server/faq">FAQ</a>
+                    </li>
+                    <li>
+                        <a href="/products/server/editions">Editions</a>
+                    </li>
+                    <li>
+                        <a href="/products/server/requirements">System Requirements</a>
+                    </li>
+                </ul>
+            </li>
+            <li>
+                <a href="/products/studio">Foo Studio</a>
+            </li>
+        </ul>
+    </li>
+    <li>
+        <a title="About us" href="/company/about">Company</a>
+    </li>
+    <li>
+        <a href="/community">Community</a>
+    </li>
+</ul>
+]]></programlisting>
+        </example>        
     </sect4>
 
     <sect4 id="zend.view.helpers.initial.navigation.sitemap">

+ 56 - 3
library/Zend/View/Helper/Navigation/Menu.php

@@ -66,6 +66,11 @@ class Zend_View_Helper_Navigation_Menu
     protected $_partial = null;
 
     /**
+     * Expand all sibling nodes of active branch nodes
+     */
+    protected $_expandSiblingNodesOfActiveBranch = false;
+    
+    /**
      * View helper entry point:
      * Retrieves helper and optionally sets container to operate on
      *
@@ -135,8 +140,34 @@ class Zend_View_Helper_Navigation_Menu
     {
         return $this->_onlyActiveBranch;
     }
+    
+    /**
+     * Sets a flag indicating whether to expand all sibling nodes of the active branch
+     * 
+     * @param  bool $flag                        [optional] expand all siblings of
+     *                                           nodes in the active branch. Default is true.
+     * @return Zend_View_Helper_Navigation_Menu  fluent interface, returns self
+     */
+    public function setExpandSiblingNodesOfActiveBranch($flag = true)
+    {
+        $this->_expandSiblingNodesOfActiveBranch = (bool) $flag;
+        return $this;
+    }
 
     /**
+     * Returns a flag indicating whether to expand all sibling nodes of the active branch
+     *
+     * By default, this value is false, meaning the entire menu will be
+     * be rendered.
+     *
+     * @return bool  whether siblings of nodes in the active branch should be expanded
+     */
+    public function getExpandSiblingNodesOfActiveBranch()
+    {
+        return $this->_expandSiblingNodesOfActiveBranch;
+    }
+    
+    /**
      * Enables/disables rendering of parents when only rendering active branch
      *
      * See {@link setOnlyActiveBranch()} for more information.
@@ -257,7 +288,7 @@ class Zend_View_Helper_Navigation_Menu
         } else {
             $options['indent'] = $this->getIndent();
         }
-
+        
         if (isset($options['ulClass']) && $options['ulClass'] !== null) {
             $options['ulClass'] = (string) $options['ulClass'];
         } else {
@@ -287,6 +318,10 @@ class Zend_View_Helper_Navigation_Menu
         if (!isset($options['onlyActiveBranch'])) {
             $options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
         }
+        
+        if (!isset($options['expandSiblingNodesOfActiveBranch'])) {
+            $options['expandSiblingNodesOfActiveBranch'] = $this->getExpandSiblingNodesOfActiveBranch();
+        }
 
         if (!isset($options['renderParents'])) {
             $options['renderParents'] = $this->getRenderParents();
@@ -359,6 +394,7 @@ class Zend_View_Helper_Navigation_Menu
      * @param  int|null                  $minDepth    minimum depth
      * @param  int|null                  $maxDepth    maximum depth
      * @param  bool                      $onlyActive  render only active branch?
+     * @param  bool                      $expandSibs  render siblings of active branch nodes?
      * @return string
      */
     protected function _renderMenu(Zend_Navigation_Container $container,
@@ -366,7 +402,8 @@ class Zend_View_Helper_Navigation_Menu
                                    $indent,
                                    $minDepth,
                                    $maxDepth,
-                                   $onlyActive)
+                                   $onlyActive,
+                                   $expandSibs)
     {
         $html = '';
 
@@ -393,6 +430,21 @@ class Zend_View_Helper_Navigation_Menu
             if ($depth < $minDepth || !$this->accept($page)) {
                 // page is below minDepth or not accepted by acl/visibilty
                 continue;
+            } else if ($expandSibs && $depth > $minDepth) {
+            	// page is not active itself, but might be in the active branch
+                $accept = false;
+                if ($foundPage) {
+                    if ($foundPage->hasPage($page)) {
+                        // accept if page is a direct child of the active page
+                        $accept = true;
+                    } else if ($page->getParent()->isActive(true)) {
+                        // page is a sibling of the active branch...
+                        $accept = true;
+                    }
+                }
+            	if (!$isActive && !$accept) {
+                    continue;
+                }
             } else if ($onlyActive && !$isActive) {
                 // page is not active itself, but might be in the active branch
                 $accept = false;
@@ -503,7 +555,8 @@ class Zend_View_Helper_Navigation_Menu
                                        $options['indent'],
                                        $options['minDepth'],
                                        $options['maxDepth'],
-                                       $options['onlyActiveBranch']);
+                                       $options['onlyActiveBranch'],
+                                       $options['expandSiblingNodesOfActiveBranch']);
         }
 
         return $html;

+ 26 - 0
tests/Zend/View/Helper/Navigation/MenuTest.php

@@ -540,4 +540,30 @@ class Zend_View_Helper_Navigation_MenuTest
         
         $this->assertEquals($expected, $this->_helper->render($this->_nav3));
     }
+
+    /**
+     * @group ZF-6941
+     */
+    public function testExpandSiblingNodesOfActiveBranch()
+    {
+        $this->_helper->setExpandSiblingNodesOfActiveBranch(true);
+ 
+        $expected = $this->_getExpected('menu/expandbranch.html');
+        $actual = $this->_helper->renderMenu();
+ 
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-6941
+     */
+    public function testExpandSiblingNodesOfActiveBranchWhenShowingOnlyActiveBranch()
+    {
+        $this->_helper->setExpandSiblingNodesOfActiveBranch(true)->setOnlyActiveBranch(true);
+ 
+        $expected = $this->_getExpected('menu/expandbranch_onlyactivebranch.html');
+        $actual = $this->_helper->renderMenu();
+ 
+        $this->assertEquals($expected, $actual);
+    }
 }

+ 52 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/expandbranch.html

@@ -0,0 +1,52 @@
+<ul class="navigation">
+    <li>
+        <a title="Go home" href="index">Home</a>
+    </li>
+    <li>
+        <a href="page1">Page 1</a>
+    </li>
+    <li class="active">
+        <a href="page2">Page 2</a>
+        <ul>
+            <li>
+                <a href="page2/page2_1">Page 2.1</a>
+            </li>
+            <li class="active">
+                <a href="page2/page2_2">Page 2.2</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_2/page2_2_1">Page 2.2.1</a>
+                    </li>
+                    <li class="active">
+                        <a href="page2/page2_2/page2_2_2">Page 2.2.2</a>
+                    </li>
+                </ul>
+            </li>
+            <li class="active">
+                <a href="page2/page2_3">Page 2.3</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_3/page2_3_1">Page 2.3.1</a>
+                    </li>
+                    <li class="active">
+                        <a href="page2/page2_3/page2_3_3">Page 2.3.3</a>
+                        <ul>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/1">Page 2.3.3.1</a>
+                            </li>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/2">Page 2.3.3.2</a>
+                            </li>
+                        </ul>
+                    </li>
+                </ul>
+            </li>
+        </ul>
+    </li>
+    <li>
+        <a href="page3">Page 3</a>
+    </li>
+    <li>
+        <a href="http://www.zym-project.com/">Zym</a>
+    </li>
+</ul>

+ 40 - 0
tests/Zend/View/Helper/Navigation/_files/expected/menu/expandbranch_onlyactivebranch.html

@@ -0,0 +1,40 @@
+<ul class="navigation">
+    <li class="active">
+        <a href="page2">Page 2</a>
+        <ul>
+            <li>
+                <a href="page2/page2_1">Page 2.1</a>
+            </li>
+            <li class="active">
+                <a href="page2/page2_2">Page 2.2</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_2/page2_2_1">Page 2.2.1</a>
+                    </li>
+                    <li class="active">
+                        <a href="page2/page2_2/page2_2_2">Page 2.2.2</a>
+                    </li>
+                </ul>
+            </li>
+            <li class="active">
+                <a href="page2/page2_3">Page 2.3</a>
+                <ul>
+                    <li>
+                        <a href="page2/page2_3/page2_3_1">Page 2.3.1</a>
+                    </li>
+                    <li class="active">
+                        <a href="page2/page2_3/page2_3_3">Page 2.3.3</a>
+                        <ul>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/1">Page 2.3.3.1</a>
+                            </li>
+                            <li class="active">
+                                <a href="page2/page2_3/page2_3_3/2">Page 2.3.3.2</a>
+                            </li>
+                        </ul>
+                    </li>
+                </ul>
+            </li>
+        </ul>
+    </li>
+</ul>