[opencms-dev] About multi-level left menu

Nigel Kersten nigel at cofa.unsw.edu.au
Mon Jul 10 00:11:43 CEST 2006


On 07/07/2006, at 5:36 PM, Vardan Gevorgyan wrote:

> And how can I use it?
> Example please.

It really is going to depend on your site, and how you want the menu  
to appear... and I'm not sure there is a simple answer. I know it  
took me quite a while to get my side-secondary navigation working...

Mine looks like this, and I'm sure it's not as efficient as it could be.

http://www.cofa.unsw.edu.au

is our site, and here is the secondary_navigation.jsp that gets  
included in our template. Some of it is going to be very specific to  
my setup...

-------------------------------
<%@ page session="false" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="java.util.*" %>
<%@ page import="org.opencms.jsp.*" %>
<%@ page import="org.opencms.file.*" %>
<%@ page import="org.opencms.file.collectors.*" %>
<%@ page import="org.opencms.main.*" %>
<%@ page import="org.opencms.xml.page.*" %>
<%@ page import="org.opencms.xml.content.*" %>

<%
CmsJspActionElement cms = new CmsJspActionElement(pageContext,  
request, response);
CmsRequestContext requestContext = cms.getRequestContext();
CmsObject cmsObject = cms.getCmsObject();
java.util.Locale locale = java.util.Locale.ENGLISH;
%>

<%
String myNavText = new String();
CmsProperty myNavTextProperty = new CmsProperty();
CmsResource thisResource = cmsObject.readResource(request.getPathInfo 
());
String pathToResource = request.getPathInfo();

if (request.getParameter("uri") != null )
{
   thisResource = cmsObject.readResource(request.getParameter("uri"));
   pathToResource = cmsObject.readResource(request.getParameter 
("uri")).getRootPath().replace("/sites/cofa", "");
}

if ( request.getPathInfo().indexOf("/errors/") >= 0 )
{
   thisResource = cmsObject.readResource("/about/");
   pathToResource = "/about/";
}


if (thisResource.getName().equals("index.html"))
{
   myNavTextProperty = cmsObject.readPropertyObject 
(CmsResource.getParentFolder(pathToResource), "NavText", false);
}
else
{
   myNavTextProperty = cmsObject.readPropertyObject(thisResource,  
"NavText", false);
}

if (myNavTextProperty.isNullProperty())
{
   String parentResource = pathToResource;
   while (myNavTextProperty.isNullProperty())
   {
     parentResource = CmsResource.getParentFolder(parentResource);
     myNavTextProperty = cmsObject.readPropertyObject(parentResource,  
"NavText", false);
   }
}

myNavText = myNavTextProperty.getValue();
int cofaRootNavLevel = -1;


out.println("<ul>");


CmsJspNavBuilder navBuilder = new CmsJspNavBuilder(cmsObject);
java.util.List breadCrumbs = navBuilder.getNavigationBreadCrumb 
(pathToResource, (cofaRootNavLevel+2), -1, true);


if (breadCrumbs.size() == 0 )
{
   breadCrumbs.clear();
   pathToResource = pathToResource + "/";
   breadCrumbs = navBuilder.getNavigationBreadCrumb(pathToResource,  
(cofaRootNavLevel+2), -1, true);
}

java.util.Iterator bcI = breadCrumbs.iterator();
CmsJspNavElement bc = (CmsJspNavElement)bcI.next();


if (myNavText.equals(bc.getNavText()))
{
   out.println("<li class=\"openandcurrent\"><a class=\"current\"  
href=\"" + cms.link(bc.getResourceName()) + "\">" + bc.getNavText() +  
"</a>");
}
else
{
   out.println("<li class=\"open\"><a href=\"" + cms.link 
(bc.getResourceName()) + "\">" + bc.getNavText() + "</a>");
}

java.util.List subElements = navBuilder.getNavigationForFolder 
(cmsObject, pathToResource, bc.getNavTreeLevel() + 1);
java.util.Iterator subEi = subElements.iterator();

out.println("<ul>");

while (subEi.hasNext())
   {
   CmsJspNavElement subElement = (CmsJspNavElement)subEi.next();
   java.util.List subSubElements = navBuilder.getNavigationForFolder 
(cmsObject, pathToResource, subElement.getNavTreeLevel() + 1);
   java.util.Iterator subSubEi = subSubElements.iterator();

   if (breadCrumbs.contains(subElement))                 // If in the  
current bread crumb path. (first iteration)
   {
     if (subElement.isFolderLink() && (!  
navBuilder.getNavigationForFolder(subElement.getResourceName 
()).isEmpty()))
     {
       if (myNavText.equals(subElement.getNavText()))
       {
         out.println("<li class=\"openandcurrent\"><a class=\"current 
\" href=\"" + cms.link(subElement.getResourceName()) + "\">" +  
subElement.getNavText() + "</a>");
       }
       else
       {
         out.println("<li class=\"open\"><a href=\"" + cms.link 
(subElement.getResourceName()) + "\">" + subElement.getNavText() + "</ 
a>");
       }
     }
     else
     {
       out.println("<li><a class=\"current\" href=\"" + cms.link 
(subElement.getResourceName()) + "\">" + subElement.getNavText() + "</ 
a>");
     }

     if (! subSubElements.isEmpty())
     {
       out.println("<ul>");

       while (subSubEi.hasNext())
       {
         CmsJspNavElement subSubElement = (CmsJspNavElement) 
subSubEi.next();
         java.util.List subSubSubElements =  
navBuilder.getNavigationForFolder(cmsObject, pathToResource,  
subSubElement.getNavTreeLevel() + 1);
         java.util.Iterator subSubSubEi = subSubSubElements.iterator();

         if (breadCrumbs.contains(subSubElement))      // If in the  
current bread crumb path. (second iteration)
         {
           if (subSubElement.isFolderLink() && (!  
navBuilder.getNavigationForFolder(subSubElement.getResourceName 
()).isEmpty()))
           {
             if (myNavText.equals(subSubElement.getProperty("NavText")))
             {
               out.println("<li class=\"openandcurrent\"><a class= 
\"current\" href=\"" + cms.link(subSubElement.getResourceName()) +  
"\">" + subSubElement.getNavText() + "</a>");
             }
             else
             {
               out.println("<li class=\"open\"><a href=\"" + cms.link 
(subSubElement.getResourceName()) + "\">" + subSubElement.getNavText 
() + "</a>");
             }
           }
           else
           {
             out.println("<li><a class=\"current\" href=\"" + cms.link 
(subSubElement.getResourceName()) + "\">" + subSubElement.getNavText 
() + "</a>");
           }

           if (! subSubSubElements.isEmpty())
           {
             out.println("<ul>");
             while (subSubSubEi.hasNext())
             {
               CmsJspNavElement subSubSubElement = (CmsJspNavElement) 
subSubSubEi.next();
               java.util.List subSubSubSubElements =  
navBuilder.getNavigationForFolder(cmsObject, pathToResource,  
subSubSubElement.getNavTreeLevel() + 1);
               java.util.Iterator subSubSubSubEi =  
subSubSubSubElements.iterator();

               if (breadCrumbs.contains(subSubSubElement))     // If  
in the current bread crumb path. (third iteration)
               {
                 out.println("<li><a class=\"current\" href=\"" +  
cms.link(subSubSubElement.getResourceName()) + "\">" +  
subSubSubElement.getNavText() + "</a></li>");
               }
               else
                
{                                                                        
// If not in the current bread crumb path. (third iteration)
                 if ( myNavText.equals(subSubSubElement.getProperty 
("NavText")) )      // Bread Crumbs don't contain the actual page, so  
we compare the navText to the current page.
                 {
                   out.println("<li><a class=\"current\" href=\"" +  
cms.link(subSubSubElement.getResourceName()) + "\">" +  
subSubSubElement.getNavText() + "</a></li>");
                 }
                 else
                 {
                   out.println("<li><a href=\"" + cms.link 
(subSubSubElement.getResourceName()) + "\">" +  
subSubSubElement.getNavText() + "</a></li>");
                 }
               }
             }
             out.println("</ul>");
           }
           out.println("</li>");

         }
          
else                                                          // If  
not in the current bread crumb path. (second iteration)
         {
           if (subSubElement.isFolderLink() && (!  
navBuilder.getNavigationForFolder(subSubElement.getResourceName 
()).isEmpty()))
           {
             out.println("<li class=\"closed\"><a href=\"" + cms.link 
(subSubElement.getResourceName()) + "\">" + subSubElement.getNavText 
() + "</a></li>");
           }
           else
           {
             if ( myNavText.equals(subSubElement.getNavText 
()) )       // Bread Crumbs don't contain the actual page, so we  
compare the navText to the current page.
             {
               out.println("<li class=\"listpage\" ><a class=\"current 
\" href=\"" + cms.link(subSubElement.getResourceName()) + "\">" +  
subSubElement.getNavText() + "</a></li>");
             }
             else
             {
               out.println("<li><a href=\"" + cms.link 
(subSubElement.getResourceName()) + "\">" + subSubElement.getNavText 
() + "</a></li>");
             }
           }
         }
       }
       out.println("</ul>");
     }
         out.println("</li>");
   }
    
else                                                                // I 
f not in the current bread crumb path. (first iteration)
   {
     if (subElement.isFolderLink() && (!  
navBuilder.getNavigationForFolder(subElement.getResourceName 
()).isEmpty()))
     {
       out.println("<li class=\"closed\"><a href=\"" + cms.link 
(subElement.getResourceName()) + "\">" + subElement.getNavText() + "</ 
a></li>");
     }
     else
     {
       if ( myNavText.equals(subElement.getNavText()) )
       {
         out.println("<li class=\"listpage\"><a class=\"current\"  
href=\"" + cms.link(subElement.getResourceName()) + "\">" +  
subElement.getNavText() + "</a></li>");
       }
       else
       {
         out.println("<li><a href=\"" + cms.link 
(subElement.getResourceName()) + "\">" + subElement.getNavText() + "</ 
a></li>");
       }
     }
   }
}



out.println("</ul>");
out.println("</li>");
out.println("</ul>");

%>
------------------------------------------


-- 
Nigel Kersten [Senior Technical Officer]
College of Fine Arts, University of NSW, Australia.
CRICOS Provider Code: 00098G






More information about the opencms-dev mailing list