8 __docformat__ = "restructuredtext en" |
8 __docformat__ = "restructuredtext en" |
9 |
9 |
10 from logilab.mtconverter import html_escape |
10 from logilab.mtconverter import html_escape |
11 |
11 |
12 from cubicweb import NoSelectableObject, role |
12 from cubicweb import NoSelectableObject, role |
13 from cubicweb.common.selectors import has_related_entities |
|
14 from cubicweb.common.view import EntityView |
13 from cubicweb.common.view import EntityView |
15 |
14 |
16 class TabsMixIn(object): |
15 from cubicweb.common.utils import HTMLHead |
17 |
16 |
18 def active_tab(self, default): |
17 # the prepend hack only work for 1-level lazy views |
|
18 # a whole lot different thing must be done otherwise |
|
19 def prepend_post_inline_script(self, content): |
|
20 self.post_inlined_scripts.insert(0, content) |
|
21 HTMLHead.prepend_post_inline_script = prepend_post_inline_script |
|
22 |
|
23 class LazyViewMixin(object): |
|
24 |
|
25 def lazyview(self, vid, eid=None, show_spinbox=True, w=None): |
|
26 """a lazy version of wview |
|
27 first version only support lazy viewing for an entity at a time |
|
28 """ |
|
29 w = w or self.w |
|
30 self.req.add_js('cubicweb.lazy.js') |
|
31 eid = eid if eid else '' |
|
32 w(u'<div id="lazy-%s" cubicweb__loadurl="%s-%s">' % (vid, vid, eid)) |
|
33 if show_spinbox: |
|
34 w(u'<img src="data/loading.gif" id="%s-hole"/>' % vid) |
|
35 w(u'</div>') |
|
36 self.req.html_headers.prepend_post_inline_script(u""" |
|
37 jQuery(document).ready(function () { |
|
38 $('#lazy-%(vid)s').bind('%(event)s', function(event) { |
|
39 load_now('#lazy-%(vid)s', '#%(vid)s-hole'); |
|
40 });});""" % {'event' : 'load_%s' % vid, |
|
41 'vid' : vid}) |
|
42 |
|
43 def forceview(self, vid): |
|
44 """trigger an event that will force immediate loading of the view |
|
45 on dom readyness |
|
46 """ |
|
47 self.req.add_js('.lazy.js') |
|
48 self.req.html_headers.add_post_inline_script(u""" |
|
49 jQuery(document).ready(function() { |
|
50 trigger_load('%(vid)s');}) |
|
51 """ % {'vid' : vid}) |
|
52 |
|
53 class TabsMixin(LazyViewMixin): |
|
54 |
|
55 def active_tab(self, tabs, default): |
19 cookie = self.req.get_cookie() |
56 cookie = self.req.get_cookie() |
20 activetab = cookie.get('active_tab') |
57 activetab = cookie.get('active_tab') |
21 if activetab is None: |
58 if activetab is None: |
22 cookie['active_tab'] = default |
59 cookie['active_tab'] = default |
23 self.req.set_cookie(cookie, 'active_tab') |
60 self.req.set_cookie(cookie, 'active_tab') |
24 return default |
61 return default |
25 return activetab.value |
62 tab = activetab.value |
|
63 return tab if tab in tabs else default |
26 |
64 |
27 def render_tabs(self, tabs, default, **kwargs): |
65 def render_tabs(self, tabs, default, entity): |
28 self.req.add_css('ui.tabs.css') |
66 self.req.add_css('ui.tabs.css') |
29 self.req.add_js( ('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js') ) |
67 self.req.add_js( ('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js') ) |
30 active_tab = self.active_tab(default) |
68 active_tab = self.active_tab(tabs, default) |
31 self.req.html_headers.add_post_inline_script(u""" |
69 self.req.html_headers.add_post_inline_script(u""" |
32 jQuery(document).ready(function() { |
70 jQuery(document).ready(function() { |
33 jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s }); |
71 jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s }); |
34 set_tab('%(vid)s'); |
72 set_tab('%(vid)s'); |
35 }); |
73 }); |
37 'vid' : active_tab}) |
75 'vid' : active_tab}) |
38 # build the html structure |
76 # build the html structure |
39 w = self.w |
77 w = self.w |
40 w(u'<div id="entity-tabs">') |
78 w(u'<div id="entity-tabs">') |
41 w(u'<ul>') |
79 w(u'<ul>') |
42 tabviews = [] |
|
43 for tab in tabs: |
80 for tab in tabs: |
44 try: |
81 try: |
45 tabview = self.vreg.select_view(tab, self.req, self.rset, **kwargs) |
82 tabview = self.vreg.select_view(tab, self.req, self.rset) |
46 except NoSelectableObject: |
83 except NoSelectableObject: |
47 continue |
84 self.info('no selectable view for id %s', tab) |
48 tabviews.append(tabview) |
|
49 w(u'<li>') |
85 w(u'<li>') |
50 w(u'<a href="#as-%s">' % tab) |
86 w(u'<a href="#as-%s">' % tab) |
51 w(u'<span onclick="set_tab(\'%s\')">' % tab) |
87 w(u'<span onclick="set_tab(\'%s\')">' % tab) |
52 w(self.req._(tab)) |
88 w(self.req._(tab)) |
53 w(u'</span>') |
89 w(u'</span>') |
54 w(u'</a>') |
90 w(u'</a>') |
55 w(u'</li>') |
91 w(u'</li>') |
56 w(u'</ul>') |
92 w(u'</ul>') |
57 w(u'</div>') |
93 w(u'</div>') |
58 # XXX ajaxify ! |
94 for tab in tabs: |
59 for tabview in tabviews: |
95 w(u'<div id="as-%s">' % tab) |
60 w(u'<div id="as-%s">' % tabview.id) |
96 self.lazyview(tab, entity.eid) |
61 tabview.dispatch(w=self.w, **kwargs) |
97 w(u'</div>') |
62 w(u'</div>') |
|
63 |
98 |
64 |
|
65 class EntityRelationTab(EntityView): |
|
66 __selectors__ = EntityView.__selectors__ + (has_related_entities,) |
|
67 vid = 'list' |
|
68 |
99 |
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>') |
|