12 |
12 |
13 from cubicweb import NoSelectableObject, role |
13 from cubicweb import NoSelectableObject, role |
14 from cubicweb.selectors import partial_has_related_entities |
14 from cubicweb.selectors import partial_has_related_entities |
15 from cubicweb.utils import HTMLHead |
15 from cubicweb.utils import HTMLHead |
16 from cubicweb.view import EntityView |
16 from cubicweb.view import EntityView |
17 from cubicweb.common.uilib import rql_for_eid |
17 from cubicweb.common import uilib, tags |
18 from cubicweb.web.views.basecontrollers import JSonController |
18 from cubicweb.web.views.basecontrollers import JSonController |
19 |
19 |
20 |
20 |
21 class LazyViewMixin(object): |
21 class LazyViewMixin(object): |
22 """provides two convenience methods for the tab machinery |
22 """provides two convenience methods for the tab machinery |
43 self.req.add_js('cubicweb.lazy.js') |
43 self.req.add_js('cubicweb.lazy.js') |
44 urlparams = {'vid' : vid, 'mode' : 'html'} |
44 urlparams = {'vid' : vid, 'mode' : 'html'} |
45 if rql: |
45 if rql: |
46 urlparams['rql'] = rql |
46 urlparams['rql'] = rql |
47 elif eid: |
47 elif eid: |
48 urlparams['rql'] = rql_for_eid(eid) |
48 urlparams['rql'] = uilib.rql_for_eid(eid) |
49 elif rset: |
49 elif rset: |
50 urlparams['rql'] = rset.printable_rql() |
50 urlparams['rql'] = rset.printable_rql() |
51 w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % ( |
51 w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % ( |
52 vid, html_escape(self.build_url('json', **urlparams)))) |
52 vid, html_escape(self.build_url('json', **urlparams)))) |
53 if show_spinbox: |
53 if show_spinbox: |
127 'vid' : active_tab, |
127 'vid' : active_tab, |
128 'eeid' : entity.eid, |
128 'eeid' : entity.eid, |
129 'cookiename' : self.cookie_name}) |
129 'cookiename' : self.cookie_name}) |
130 |
130 |
131 |
131 |
132 class EntityRelatedTab(EntityView): |
132 class EntityRelationView(EntityView): |
133 """A view you should inherit from leftmost, |
133 """view displaying entity related stuff. |
134 to wrap another actual view displaying entity related stuff. |
|
135 Such a view _must_ provide the rtype, target and vid attributes : |
134 Such a view _must_ provide the rtype, target and vid attributes : |
136 |
135 |
137 Example : |
136 Example : |
138 |
137 |
139 class ProjectScreenshotsView(EntityRelationView): |
138 class ProjectScreenshotsView(EntityRelationView): |
140 '''display project's screenshots''' |
139 '''display project's screenshots''' |
141 id = title = _('projectscreenshots') |
140 id = title = _('projectscreenshots') |
142 accepts = ('Project',) |
141 __select__ = EntityRelationView.__select__ & implements('Project') |
143 rtype = 'screenshot' |
142 rtype = 'screenshot' |
144 target = 'object' |
143 role = 'subject' |
145 vid = 'gallery' |
144 vid = 'gallery' |
146 __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,) |
|
147 |
145 |
148 |
146 in this example, entities related to project entity by the'screenshot' |
149 This is the view we want to have in a tab, only if there is something to show. |
147 relation (where the project is subject of the relation) will be displayed |
150 Then, just define as below, and declare this being the tab content : |
148 using the 'gallery' view. |
151 |
|
152 class ProjectScreenshotTab(EntityRelatedTab, ProjectScreenshotsView): |
|
153 id = 'screenshots_tab' |
|
154 """ |
149 """ |
155 __select__ = EntityView.__select__ & partial_has_related_entities() |
150 __select__ = EntityView.__select__ & partial_has_related_entities() |
156 vid = 'list' |
151 vid = 'list' |
157 |
152 |
158 def cell_call(self, row, col): |
153 def cell_call(self, row, col): |
159 rset = self.entity(row, col).related(self.rtype, role(self)) |
154 rset = self.entity(row, col).related(self.rtype, role(self)) |
160 self.w(u'<div class="mainInfo">') |
155 self.w(u'<div class="mainInfo">') |
|
156 if self.title: |
|
157 self.w(tags.h1(self.req._(title))) |
161 self.wview(self.vid, rset, 'noresult') |
158 self.wview(self.vid, rset, 'noresult') |
162 self.w(u'</div>') |
159 self.w(u'</div>') |