"zope.phdru.name - navigation"

__version__ = "0.1.0";

# Author: Oleg BroytMann <phd@phdru.name>
# Copyright: Russin Python and Zope User Group <info@zope.phdru.name>

# License: GPL


import string


def default_render(self, _, alist=None, indent=0):
   result = []
   SCRIPT_NAME = self.REQUEST.SCRIPT_NAME

   for url, name, xplist in alist:
      result.append('%s<a class=nbar href="%s%s">%s</a><br>' % ("&nbsp;" * 4 * indent, SCRIPT_NAME, url, name))
      if xplist:
         result.append(default_render(self, _, alist=xplist, indent=indent+1))
   return string.join(result, '')


def navigation_leftColumn(self, _, render=None):
   PARENTS = self.REQUEST.PARENTS[:-1]
   PARENTS.reverse()

   result = []
   parent_list = result # start
   FullID = ""

   for parent in PARENTS: # from the root down to the current folder
      try:
         id = parent.id()
      except TypeError:
         id = parent.id

      FullID = "%s/%s" % (FullID, id)
      CurID = FullID[len("/zope.phdru.name"):] + '/'

      base = getattr(parent, 'aq_base', parent)
      if hasattr(base, "left-col-list"):
      # is there the list in the folder (turning acquisition off)?
         leftcol_list = getattr(parent, "left-col-list")
         abs_url = leftcol_list.absolute_url()
         leftcol_list = leftcol_list(self, _) # render it to the real object (list)

         # This is the heart of the algorithm - find current folder
         # in parent list and append current leftcol_list to parent list
         # in correct position!
         for url, name, xplist in parent_list:
            if url == CurID:
               parent_list = xplist
               break

         for url, name in leftcol_list:
            parent_list.append([url, name, []])

   if render is None:
      render = default_render

   return render(self, _, alist=result, indent=0)
