|
1 """base classes to handle tabbed views |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 """ |
|
7 |
|
8 __docformat__ = "restructuredtext en" |
|
9 |
|
10 from logilab.mtconverter import html_escape |
|
11 |
|
12 from cubicweb import NoSelectableObject, role |
|
13 from cubicweb.common.selectors import has_related_entities |
|
14 from cubicweb.common.view import EntityView |
|
15 |
|
16 class TabsMixIn(object): |
|
17 |
|
18 def active_tab(self, default): |
|
19 cookie = self.req.get_cookie() |
|
20 activetab = cookie.get('active_tab') |
|
21 if activetab is None: |
|
22 cookie['active_tab'] = default |
|
23 self.req.set_cookie(cookie, 'active_tab') |
|
24 return default |
|
25 return activetab.value |
|
26 |
|
27 def render_tabs(self, tabs, default, **kwargs): |
|
28 self.req.add_css('ui.tabs.css') |
|
29 self.req.add_js( ('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js') ) |
|
30 active_tab = self.active_tab(default) |
|
31 self.req.html_headers.add_post_inline_script(u""" |
|
32 jQuery(document).ready(function() { |
|
33 jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s }); |
|
34 set_tab('%(vid)s'); |
|
35 }); |
|
36 """ % {'tabindex' : tabs.index(active_tab), |
|
37 'vid' : active_tab}) |
|
38 # build the html structure |
|
39 w = self.w |
|
40 w(u'<div id="entity-tabs">') |
|
41 w(u'<ul>') |
|
42 tabviews = [] |
|
43 for tab in tabs: |
|
44 try: |
|
45 tabview = self.vreg.select_view(tab, self.req, self.rset, **kwargs) |
|
46 except NoSelectableObject: |
|
47 continue |
|
48 tabviews.append(tabview) |
|
49 w(u'<li>') |
|
50 w(u'<a href="#as-%s">' % tab) |
|
51 w(u'<span onclick="set_tab(\'%s\')">' % tab) |
|
52 w(self.req._(tab)) |
|
53 w(u'</span>') |
|
54 w(u'</a>') |
|
55 w(u'</li>') |
|
56 w(u'</ul>') |
|
57 w(u'</div>') |
|
58 # XXX ajaxify ! |
|
59 for tabview in tabviews: |
|
60 w(u'<div id="as-%s">' % tabview.id) |
|
61 tabview.dispatch(w=self.w, **kwargs) |
|
62 w(u'</div>') |
|
63 |
|
64 |
|
65 class EntityRelationTab(EntityView): |
|
66 __selectors__ = EntityView.__selectors__ + (has_related_entities,) |
|
67 vid = 'list' |
|
68 |
|
69 def cell_call(self, row, col): |
|
70 rset = self.rset.get_entity(row, col).related(self.rtype, role(self)) |
|
71 self.w(u'<div class="mainInfo">') |
|
72 self.wview(self.vid, rset, 'noresult') |
|
73 self.w(u'</div>') |