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

Merge r25194 to 1.12 release branch

git-svn-id: http://framework.zend.com/svn/framework/standard/branches/release-1.12@25195 44c647ce-9c0f-0410-b52a-842ac1e357ba
frosch 13 лет назад
Родитель
Сommit
575f7dfc55

+ 87 - 27
library/Zend/View/Helper/Navigation/Menu.php

@@ -52,6 +52,13 @@ class Zend_View_Helper_Navigation_Menu
     protected $_ulId = null;
 
     /**
+     * CSS class to use for the active elements
+     *
+     * @var string
+     */
+    protected $_activeClass = 'active';
+
+    /**
      * Whether only active branch should be rendered
      *
      * @var bool
@@ -159,6 +166,31 @@ class Zend_View_Helper_Navigation_Menu
     }
 
     /**
+     * Sets CSS class to use for the active elements when rendering
+     *
+     * @param string $activeClass               CSS class to set
+     * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
+     */
+    public function setActiveClass($activeClass)
+    {
+        if (is_string($activeClass)) {
+            $this->_activeClass = $activeClass;
+        }
+
+        return $this;
+    }
+
+    /**
+     * Returns CSS class to use for the active elements when rendering
+     *
+     * @return string  CSS class
+     */
+    public function getActiveClass()
+    {
+        return $this->_activeClass;
+    }
+
+    /**
      * Sets a flag indicating whether only active branch should be rendered
      *
      * @param  bool $flag                        [optional] render only active
@@ -372,24 +404,36 @@ class Zend_View_Helper_Navigation_Menu
      */
     protected function _normalizeOptions(array $options = array())
     {
+        // Ident
         if (isset($options['indent'])) {
             $options['indent'] = $this->_getWhitespace($options['indent']);
         } else {
             $options['indent'] = $this->getIndent();
         }
-        
+
+        // UL class
         if (isset($options['ulClass']) && $options['ulClass'] !== null) {
             $options['ulClass'] = (string) $options['ulClass'];
         } else {
             $options['ulClass'] = $this->getUlClass();
         }
 
+        // UL id
         if (isset($options['ulId']) && $options['ulId'] !== null) {
             $options['ulId'] = (string) $options['ulId'];
         } else {
             $options['ulId'] = $this->getUlId();
         }
 
+        // Active class
+        if (isset($options['activeClass']) && $options['activeClass'] !== null
+        ) {
+            $options['activeClass'] = (string) $options['activeClass'];
+        } else {
+            $options['activeClass'] = $this->getActiveClass();
+        }
+
+        // Minimum depth
         if (array_key_exists('minDepth', $options)) {
             if (null !== $options['minDepth']) {
                 $options['minDepth'] = (int) $options['minDepth'];
@@ -411,18 +455,22 @@ class Zend_View_Helper_Navigation_Menu
             $options['maxDepth'] = $this->getMaxDepth();
         }
 
+        // Only active branch
         if (!isset($options['onlyActiveBranch'])) {
             $options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
         }
-        
+
+        // Expand sibling nodes of active branch
         if (!isset($options['expandSiblingNodesOfActiveBranch'])) {
             $options['expandSiblingNodesOfActiveBranch'] = $this->getExpandSiblingNodesOfActiveBranch();
         }
 
+        // Render parents
         if (!isset($options['renderParents'])) {
             $options['renderParents'] = $this->getRenderParents();
         }
 
+        // Add page CSS class to LI element
         if (!isset($options['addPageClassToLi'])) {
             $options['addPageClassToLi'] = $this->getAddPageClassToLi();
         }
@@ -445,7 +493,9 @@ class Zend_View_Helper_Navigation_Menu
      *                                               first UL
      * @param  bool                      $addPageClassToLi  adds CSS class from
      *                                                      page to li element
-     * @return string                                rendered menu
+     * @param  string|null               $activeClass       CSS class for active
+     *                                                      element
+     * @return string                                       rendered menu (HTML)
      */
     protected function _renderDeepestMenu(Zend_Navigation_Container $container,
                                           $ulClass,
@@ -453,7 +503,8 @@ class Zend_View_Helper_Navigation_Menu
                                           $minDepth,
                                           $maxDepth,
                                           $ulId,
-                                          $addPageClassToLi)
+                                          $addPageClassToLi,
+                                          $activeClass)
     {
         if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
             return '';
@@ -497,10 +548,10 @@ class Zend_View_Helper_Navigation_Menu
             $liClass = '';
             if ($subPage->isActive(true) && $addPageClassToLi) {
                 $liClass = $this->_htmlAttribs(
-                    array('class' => 'active ' . $subPage->getClass())
+                    array('class' => $activeClass . ' ' . $subPage->getClass())
                 );
             } else if ($subPage->isActive(true)) {
-                $liClass = $this->_htmlAttribs(array('class' => 'active'));
+                $liClass = $this->_htmlAttribs(array('class' => $activeClass));
             } else if ($addPageClassToLi) {
                 $liClass = $this->_htmlAttribs(
                     array('class' => $subPage->getClass())
@@ -531,7 +582,9 @@ class Zend_View_Helper_Navigation_Menu
      *                                                first UL
      * @param  bool                      $addPageClassToLi  adds CSS class from
      *                                                      page to li element
-     * @return string
+     * @param  string|null               $activeClass       CSS class for active
+     *                                                      element
+     * @return string                                       rendered menu (HTML)
      */
     protected function _renderMenu(Zend_Navigation_Container $container,
                                    $ulClass,
@@ -541,7 +594,8 @@ class Zend_View_Helper_Navigation_Menu
                                    $onlyActive,
                                    $expandSibs,
                                    $ulId,
-                                   $addPageClassToLi)
+                                   $addPageClassToLi,
+                                   $activeClass)
     {
         $html = '';
 
@@ -569,7 +623,7 @@ class Zend_View_Helper_Navigation_Menu
                 // 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
+                // page is not active itself, but might be in the active branch
                 $accept = false;
                 if ($foundPage) {
                     if ($foundPage->hasPage($page)) {
@@ -650,10 +704,10 @@ class Zend_View_Helper_Navigation_Menu
             $liClass = '';
             if ($isActive && $addPageClassToLi) {
                 $liClass = $this->_htmlAttribs(
-                    array('class' => 'active ' . $page->getClass())
+                    array('class' => $activeClass . ' ' . $page->getClass())
                 );
             } else if ($isActive) {
-                $liClass = $this->_htmlAttribs(array('class' => 'active'));
+                $liClass = $this->_htmlAttribs(array('class' => $activeClass));
             } else if ($addPageClassToLi) {
                 $liClass = $this->_htmlAttribs(
                     array('class' => $page->getClass())
@@ -707,23 +761,29 @@ class Zend_View_Helper_Navigation_Menu
         $options = $this->_normalizeOptions($options);
 
         if ($options['onlyActiveBranch'] && !$options['renderParents']) {
-            $html = $this->_renderDeepestMenu($container,
-                                              $options['ulClass'],
-                                              $options['indent'],
-                                              $options['minDepth'],
-                                              $options['maxDepth'],
-                                              $options['ulId'],
-                                              $options['addPageClassToLi']);
+            $html = $this->_renderDeepestMenu(
+                $container,
+                $options['ulClass'],
+                $options['indent'],
+                $options['minDepth'],
+                $options['maxDepth'],
+                $options['ulId'],
+                $options['addPageClassToLi'],
+                $options['activeClass']
+            );
         } else {
-            $html = $this->_renderMenu($container,
-                                       $options['ulClass'],
-                                       $options['indent'],
-                                       $options['minDepth'],
-                                       $options['maxDepth'],
-                                       $options['onlyActiveBranch'],
-                                       $options['expandSiblingNodesOfActiveBranch'],
-                                       $options['ulId'],
-                                       $options['addPageClassToLi']);
+            $html = $this->_renderMenu(
+                $container,
+                $options['ulClass'],
+                $options['indent'],
+                $options['minDepth'],
+                $options['maxDepth'],
+                $options['onlyActiveBranch'],
+                $options['expandSiblingNodesOfActiveBranch'],
+                $options['ulId'],
+                $options['addPageClassToLi'],
+                $options['activeClass']
+            );
         }
 
         return $html;

+ 34 - 1
tests/Zend/View/Helper/Navigation/MenuTest.php

@@ -772,4 +772,37 @@ class Zend_View_Helper_Navigation_MenuTest
             $this->_helper->renderMenu(null, $options)
         );
     }
-}
+
+    /**
+     * @group ZF-9543
+     */
+    public function testSetActiveClass()
+    {
+        $this->_helper->setActiveClass('current');
+
+        // Test getter
+        $this->assertEquals('current', $this->_helper->getActiveClass());
+
+        // Test rendering
+        $expected = $this->_getExpected('menu/css_active.html');
+        $this->assertEquals($expected, $this->_helper->render($this->_nav2));
+    }
+
+    /**
+     * @group ZF-9543
+     */
+    public function testRenderDeepestMenuWithCustomActiveClass()
+    {
+        // Tests
+        $options = array(
+            'onlyActiveBranch' => true,
+            'renderParents'    => false,
+            'activeClass'      => 'current',
+        );
+
+        $html = $this->_helper->renderMenu(null, $options);
+
+        $this->assertContains('<li class="current">', $html);
+        $this->assertNotContains('<li class="active">', $html);
+    }
+}

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

@@ -0,0 +1,11 @@
+<ul class="navigation">
+    <li>
+        <a href="site1">Site 1</a>
+    </li>
+    <li class="current">
+        <a href="site2">Site 2</a>
+    </li>
+    <li>
+        <a href="site3">Site 3</a>
+    </li>
+</ul>