web/views/ibreadcrumbs.py
changeset 0 b97547f5f1fa
child 237 3df2e0ae2eba
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """navigation components definition for CubicWeb web client
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8 
       
     9 from logilab.mtconverter import html_escape
       
    10 
       
    11 from cubicweb.interfaces import IBreadCrumbs
       
    12 from cubicweb.common.selectors import (contextprop_selector, onelinerset_selector, 
       
    13                                     interface_selector)
       
    14 from cubicweb.common.view import EntityView
       
    15 from cubicweb.common.uilib import cut
       
    16 # don't use AnyEntity since this may cause bug with isinstance() due to reloading
       
    17 from cubicweb.common.entity import Entity
       
    18 from cubicweb.web.component import EntityVComponent
       
    19 
       
    20 _ = unicode
       
    21 
       
    22 def bc_title(entity):
       
    23     textsize = entity.req.property_value('navigation.short-line-size')
       
    24     return html_escape(cut(entity.dc_title(), textsize))
       
    25     
       
    26 
       
    27 class BreadCrumbEntityVComponent(EntityVComponent):
       
    28     id = 'breadcrumbs'
       
    29     # register msg not generated since no entity implements IPrevNext in cubicweb itself
       
    30     title = _('contentnavigation_breadcrumbs')
       
    31     help = _('contentnavigation_breadcrumbs_description')
       
    32     __selectors__ = (onelinerset_selector, contextprop_selector, interface_selector)
       
    33     accepts_interfaces = (IBreadCrumbs,)
       
    34     context = 'navtop'
       
    35     order = 5
       
    36     visible = False
       
    37     separator = u' > '
       
    38 
       
    39     def call(self, view=None, first_separator=True):
       
    40         entity = self.entity(0)
       
    41         path = entity.breadcrumbs(view)
       
    42         if path:
       
    43             self.w(u'<span class="pathbar">')
       
    44             if first_separator:
       
    45                 self.w(self.separator)
       
    46             root = path.pop(0)
       
    47             if isinstance(root, Entity):
       
    48                 self.w(u'<a href="%s">%s</a>' % (self.req.build_url(root.id),
       
    49                                                  root.dc_type('plural')))
       
    50                 self.w(self.separator)
       
    51             self.wpath_part(root, entity, not path)
       
    52             for i, parent in enumerate(path):
       
    53                 self.w(self.separator)
       
    54                 self.w(u"\n")
       
    55                 self.wpath_part(parent, entity, i == len(path) - 1)
       
    56             self.w(u'</span>')
       
    57             
       
    58     def wpath_part(self, part, contextentity, last=False):
       
    59         if isinstance(part, Entity):
       
    60             if last and part.eid == contextentity.eid:
       
    61                 self.w(bc_title(part))
       
    62             else:
       
    63                 part.view('breadcrumbs', w=self.w)
       
    64         elif isinstance(part, tuple):
       
    65             url, title = part
       
    66             textsize = self.req.property_value('navigation.short-line-size')
       
    67             self.w(u'<a href="%s">%s</a>' % (
       
    68                 html_escape(url), html_escape(cut(title, textsize))))
       
    69         else:
       
    70             textsize = self.req.property_value('navigation.short-line-size')
       
    71             self.w(cut(unicode(part), textsize))
       
    72         
       
    73 
       
    74 class BreadCrumbComponent(BreadCrumbEntityVComponent):
       
    75     __registry__ = 'components'
       
    76     __selectors__ = (onelinerset_selector, interface_selector)
       
    77     visible = True
       
    78 
       
    79 
       
    80 class BreadCrumbView(EntityView):
       
    81     id = 'breadcrumbs'
       
    82 
       
    83     def cell_call(self, row, col):
       
    84         entity = self.entity(row, col)
       
    85         desc = cut(entity.dc_description(), 50)
       
    86         self.w(u'<a href="%s" title="%s">%s</a>' % (html_escape(entity.absolute_url()),
       
    87                                                     html_escape(desc),
       
    88                                                     bc_title(entity)))