[tabs]
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 27 Jan 2009 20:51:55 +0100
changeset 489 21183fc36b39
parent 488 2984b3ec7034
child 490 b84d64db2350
[tabs] * provide the required json controller there * stuff back in the lazy tab construction mechanism, with some documentation bits
web/views/tabs.py
--- a/web/views/tabs.py	Tue Jan 27 19:13:54 2009 +0100
+++ b/web/views/tabs.py	Tue Jan 27 20:51:55 2009 +0100
@@ -11,6 +11,7 @@
 
 from cubicweb import NoSelectableObject, role
 from cubicweb.common.view import EntityView
+from cubicweb.common.selectors import has_related_entities
 
 from cubicweb.common.utils import HTMLHead
 
@@ -65,7 +66,7 @@
 
     def render_tabs(self, tabs, default, entity):
         self.req.add_css('ui.tabs.css')
-        self.req.add_js( ('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js') )
+        self.req.add_js(('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js', 'cubicweb.lazy.js'))
         active_tab = self.active_tab(tabs, default)
         self.req.html_headers.add_post_inline_script(u"""
  jQuery(document).ready(function() {
@@ -78,11 +79,14 @@
         w = self.w
         w(u'<div id="entity-tabs">')
         w(u'<ul>')
+        selected_tabs = []
         for tab in tabs:
             try:
                 tabview = self.vreg.select_view(tab, self.req, self.rset)
+                selected_tabs.append(tab)
             except NoSelectableObject:
                 self.info('no selectable view for id %s', tab)
+                continue
             w(u'<li>')
             w(u'<a href="#as-%s">' % tab)
             w(u'<span onclick="set_tab(\'%s\')">' % tab)
@@ -92,9 +96,54 @@
             w(u'</li>')
         w(u'</ul>')
         w(u'</div>')
-        for tab in tabs:
+        for tab in selected_tabs:
             w(u'<div id="as-%s">' % tab)
             self.lazyview(tab, entity.eid)
             w(u'</div>')
 
 
+from cubicweb.web.views.basecontrollers import JSonController
+class TabsController(JSonController):
+
+    def js_remember_active_tab(self, tabname):
+        cookie = self.req.get_cookie()
+        cookie['active_tab'] = tabname
+        self.req.set_cookie(cookie, 'active_tab')
+
+    def js_lazily(self, vid_eid):
+        vid, eid = vid_eid.split('-')
+        rset = self.req.eid_rset(eid) if eid else None
+        view = self.vreg.select_view(vid, self.req, rset)
+        return self._set_content_type(view, view.dispatch())
+
+class DataDependantTab(EntityView):
+    """A view you should inherit from leftmost,
+    to wrap another actual view displaying entity related stuff.
+    Such a view _must_ provide the rtype, target and vid attributes :
+
+    Example :
+
+    class ProjectScreenshotsView(EntityRelationView):
+        "display project's screenshots"
+        id = title = _('projectscreenshots')
+        accepts = ('Project',)
+        rtype = 'screenshot'
+        target = 'object'
+        vid = 'gallery'
+        __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,)
+
+
+    This is the view we want to have in a tab, only if there is something to show.
+    Then, just define as below, and declare this being the tab content :
+
+    class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView):
+        id = 'screenshots_tab'
+    """
+    __selectors__ = EntityView.__selectors__ + (has_related_entities,)
+    vid = 'list'
+
+    def cell_call(self, row, col):
+        rset = self.rset.get_entity(row, col).related(self.rtype, role(self))
+        self.w(u'<div class="mainInfo">')
+        self.wview(self.vid, rset, 'noresult')
+        self.w(u'</div>')