refactor to ease overriding of what's displayed in breadcrum for an entity 3.5
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 02 Sep 2009 16:39:19 +0200
branch3.5
changeset 3089 72c23fc7fe20
parent 3088 854a30d8c092
child 3090 8184bec7414d
child 3100 b0a583156d6d
refactor to ease overriding of what's displayed in breadcrum for an entity
web/views/ibreadcrumbs.py
--- a/web/views/ibreadcrumbs.py	Wed Sep 02 16:38:08 2009 +0200
+++ b/web/views/ibreadcrumbs.py	Wed Sep 02 16:39:19 2009 +0200
@@ -16,14 +16,8 @@
 from cubicweb.view import EntityView, Component
 # don't use AnyEntity since this may cause bug with isinstance() due to reloading
 from cubicweb.entity import Entity
-from cubicweb.common.uilib import cut
-
+from cubicweb.common import tags, uilib
 
-def bc_title(entity):
-    textsize = entity.req.property_value('navigation.short-line-size')
-    return xml_escape(cut(entity.dc_title(), textsize))
-
-# XXX only provides the component version
 
 class BreadCrumbEntityVComponent(Component):
     id = 'breadcrumbs'
@@ -38,7 +32,7 @@
     separator = u'&#160;&gt;&#160;'
 
     def call(self, view=None, first_separator=True):
-        entity = self.entity(0)
+        entity = self.rset.get_entity(0, 0)
         path = entity.breadcrumbs(view)
         if path:
             self.w(u'<span id="breadcrumbs" class="pathbar">')
@@ -62,17 +56,17 @@
     def wpath_part(self, part, contextentity, last=False):
         if isinstance(part, Entity):
             if last and part.eid == contextentity.eid:
-                self.w(bc_title(part))
+                part.view('breadcrumbtext', w=self.w)
             else:
                 part.view('breadcrumbs', w=self.w)
         elif isinstance(part, tuple):
             url, title = part
             textsize = self.req.property_value('navigation.short-line-size')
             self.w(u'<a href="%s">%s</a>' % (
-                xml_escape(url), xml_escape(cut(title, textsize))))
+                xml_escape(url), xml_escape(uilib.cut(title, textsize))))
         else:
             textsize = self.req.property_value('navigation.short-line-size')
-            self.w(cut(unicode(part), textsize))
+            self.w(uilib.cut(unicode(part), textsize))
 
 
 class BreadCrumbETypeVComponent(BreadCrumbEntityVComponent):
@@ -103,7 +97,16 @@
     id = 'breadcrumbs'
 
     def cell_call(self, row, col):
-        entity = self.entity(row, col)
-        desc = xml_escape(cut(entity.dc_description(), 50))
-        self.w(u'<a href="%s" title="%s">%s</a>' % (
-            xml_escape(entity.absolute_url()), desc, bc_title(entity)))
+        entity = self.rset.get_entity(row, col)
+        desc = xml_escape(uilib.cut(entity.dc_description(), 50))
+        self.w(tags.a(entity.view('breadcrumbtext'), href=entity.absolute_url(),
+                      title=desc))
+
+
+class BreadCrumbTextView(EntityView):
+    id = 'breadcrumbtext'
+
+    def cell_call(self, row, col):
+        entity = self.rset.get_entity(row, col)
+        textsize = self.req.property_value('navigation.short-line-size')
+        self.w(uilib.cut(entity.dc_title(), textsize))