rename to tabs.py
authorSylvain Thenault <sylvain.thenault@logilab.fr>
Fri, 19 Dec 2008 14:54:35 +0100
changeset 235 b43362d92a1d
parent 234 3a15b00df8a2
child 236 8ab9e72dd8f1
rename to tabs.py
web/views/tabs.py
web/views/tabview.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/views/tabs.py	Fri Dec 19 14:54:35 2008 +0100
@@ -0,0 +1,73 @@
+"""base classes to handle tabbed views
+
+:organization: Logilab
+:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+"""
+
+__docformat__ = "restructuredtext en"
+
+from logilab.mtconverter import html_escape
+
+from cubicweb import NoSelectableObject, role
+from cubicweb.common.selectors import has_related_entities
+from cubicweb.common.view import EntityView
+
+class TabsMixIn(object):
+    
+    def active_tab(self, default):
+        cookie = self.req.get_cookie()
+        activetab = cookie.get('active_tab')
+        if activetab is None:
+            cookie['active_tab'] = default
+            self.req.set_cookie(cookie, 'active_tab')
+            return default
+        return activetab.value
+
+    def render_tabs(self, tabs, default, **kwargs):
+        self.req.add_css('ui.tabs.css')
+        self.req.add_js( ('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js') )
+        active_tab = self.active_tab(default)
+        self.req.html_headers.add_post_inline_script(u"""
+ jQuery(document).ready(function() {
+   jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s });
+   set_tab('%(vid)s');
+ });
+ """ % {'tabindex' : tabs.index(active_tab),
+        'vid'      : active_tab})
+        # build the html structure
+        w = self.w
+        w(u'<div id="entity-tabs">')
+        w(u'<ul>')
+        tabviews = []
+        for tab in tabs:
+            try:
+                tabview = self.vreg.select_view(tab, self.req, self.rset, **kwargs)
+            except NoSelectableObject:
+                continue
+            tabviews.append(tabview)
+            w(u'<li>')
+            w(u'<a href="#as-%s">' % tab)
+            w(u'<span onclick="set_tab(\'%s\')">' % tab)
+            w(self.req._(tab))
+            w(u'</span>')
+            w(u'</a>')
+            w(u'</li>')
+        w(u'</ul>')
+        w(u'</div>')
+        # XXX ajaxify !
+        for tabview in tabviews:
+            w(u'<div id="as-%s">' % tabview.id)
+            tabview.dispatch(w=self.w, **kwargs)
+            w(u'</div>')    
+
+  
+class EntityRelationTab(EntityView):
+    __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>')
--- a/web/views/tabview.py	Fri Dec 19 14:54:13 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-from logilab.mtconverter import html_escape
-
-from cubicweb.common.view import EntityView
-
-# XXX
-# This is premature & tied to JPL
-# It should go away & be replaced by something like
-# a TabbedPrimaryView (that would really a primary view)
-
-class TabView(EntityView):
-    id = 'tabview'
-    accepts = ('Project',)
-
-    def active_tab(self):
-        cookie = self.req.get_cookie()
-        cookietab = cookie.get('active_tab')
-        if cookietab is None:
-            cookie['active_tab'] = 'project_main'
-            self.req.set_cookie(cookie, 'active_tab')
-        return cookietab and cookietab.value or 'project_main'
-
-    def cell_call(self, row, col, tabs):
-        self.req.add_css('ui.tabs.css')
-        self.req.add_js( ('ui.core.js', 'ui.tabs.js', 'cubes.jpl.primary.js') )
-        active_tab = self.active_tab()
-        self.req.html_headers.add_post_inline_script(u"""
- jQuery(document).ready(function() {
-   jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s });
-   set_tab('%(vid)s');
- });
- """ % {'tabindex' : tabs.index(active_tab),
-        'vid'      : active_tab})
-        # build the html structure
-        self.w(u'<div id="entity-tabs">')
-        self.w(u'<ul>')
-        for tab in tabs:
-            self.w(u'<li>')
-            self.w(u'<a href="#as-%s">' % tab)
-            cookie_setter = "set_tab('%s')" % tab
-            self.w(u'<span onclick="%s">' % cookie_setter)
-            self.w('%s' % self.req._(tab))
-            self.w(u'</span>')
-            self.w(u'</a>')
-            self.w(u'</li>')
-        self.w(u'</ul>')
-        self.w(u'</div>')
-        for tab in tabs:
-            self.w(u'<div id="as-%s">' % tab)
-            self.wview(tab, self.rset)
-            self.w(u'</div>')    
-