31 jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) { |
31 jQuery('#lazy-%(vid)s').bind('%(event)s', function(event) { |
32 load_now('#lazy-%(vid)s', '#%(vid)s-hole', %(reloadable)s); |
32 load_now('#lazy-%(vid)s', '#%(vid)s-hole', %(reloadable)s); |
33 });""" % {'event': 'load_%s' % vid, 'vid': vid, |
33 });""" % {'event': 'load_%s' % vid, 'vid': vid, |
34 'reloadable' : str(reloadable).lower()}) |
34 'reloadable' : str(reloadable).lower()}) |
35 |
35 |
36 def lazyview(self, vid, eid=None, reloadable=False, show_spinbox=True, w=None): |
36 def lazyview(self, vid, rql=None, eid=None, rset=None, static=False, |
|
37 reloadable=False, show_spinbox=True, w=None): |
37 """a lazy version of wview |
38 """a lazy version of wview |
38 first version only support lazy viewing for an entity at a time |
39 first version only support lazy viewing for an entity at a time |
39 """ |
40 """ |
|
41 assert rql or eid or rset or static, \ |
|
42 'lazyview wants at least : rql, or an eid, or an rset -- or call it with static=True' |
40 w = w or self.w |
43 w = w or self.w |
41 self.req.add_js('cubicweb.lazy.js') |
44 self.req.add_js('cubicweb.lazy.js') |
42 urlparams = {'vid' : vid, 'mode' : 'html'} |
45 urlparams = {'vid' : vid, 'mode' : 'html'} |
43 if eid: |
46 if rql: |
|
47 urlparams['rql'] = rql |
|
48 elif eid: |
44 urlparams['rql'] = rql_for_eid(eid) |
49 urlparams['rql'] = rql_for_eid(eid) |
|
50 elif rset: |
|
51 urlparams['rql'] = rset.printable_rql() |
45 w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % ( |
52 w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % ( |
46 vid, html_escape(self.build_url('json', **urlparams)))) |
53 vid, html_escape(self.build_url('json', **urlparams)))) |
47 if show_spinbox: |
54 if show_spinbox: |
48 w(u'<img src="data/loading.gif" id="%s-hole" alt="%s"/>' |
55 w(u'<img src="data/loading.gif" id="%s-hole" alt="%s"/>' |
49 % (vid, self.req._('loading'))) |
56 % (vid, self.req._('loading'))) |
63 @property |
70 @property |
64 def cookie_name(self): |
71 def cookie_name(self): |
65 return str('%s_active_tab' % self.config.appid) |
72 return str('%s_active_tab' % self.config.appid) |
66 |
73 |
67 def active_tab(self, tabs, default): |
74 def active_tab(self, tabs, default): |
68 cookie = self.req.get_cookie() |
75 cookies = self.req.get_cookie() |
69 cookiename = self.cookie_name |
76 cookiename = self.cookie_name |
70 activetab = cookie.get(cookiename) |
77 activetab = cookies.get(cookiename) |
71 if activetab is None: |
78 if activetab is None: |
72 cookie[cookiename] = default |
79 cookies[cookiename] = default |
73 self.req.set_cookie(cookie, cookiename) |
80 self.req.set_cookie(cookies, cookiename) |
74 tab = default |
81 tab = default |
75 else: |
82 else: |
76 tab = activetab.value |
83 tab = activetab.value |
77 return tab in tabs and tab or default |
84 return tab in tabs and tab or default |
78 |
85 |
94 tabs = self.prune_tabs(tabs) |
101 tabs = self.prune_tabs(tabs) |
95 # select a tab |
102 # select a tab |
96 active_tab = self.active_tab(tabs, default) |
103 active_tab = self.active_tab(tabs, default) |
97 # build the html structure |
104 # build the html structure |
98 w = self.w |
105 w = self.w |
99 w(u'<div id="entity-tabs">') |
106 w(u'<div id="entity-tabs-%s">' % entity.eid) |
100 w(u'<ul>') |
107 w(u'<ul>') |
101 for tab in tabs: |
108 for tab in tabs: |
102 w(u'<li>') |
109 w(u'<li>') |
103 w(u'<a href="#as-%s">' % tab) |
110 w(u'<a href="#as-%s">' % tab) |
104 w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name)) |
111 w(u'<span onclick="set_tab(\'%s\', \'%s\')">' % (tab, self.cookie_name)) |
108 w(u'</li>') |
115 w(u'</li>') |
109 w(u'</ul>') |
116 w(u'</ul>') |
110 w(u'</div>') |
117 w(u'</div>') |
111 for tab in tabs: |
118 for tab in tabs: |
112 w(u'<div id="as-%s">' % tab) |
119 w(u'<div id="as-%s">' % tab) |
113 self.lazyview(tab, entity.eid) |
120 self.lazyview(tab, eid=entity.eid) |
114 w(u'</div>') |
121 w(u'</div>') |
115 # call the set_tab() JS function *after* each tab is generated |
122 # call the set_tab() JS function *after* each tab is generated |
116 # because the callback binding needs to be done before |
123 # because the callback binding needs to be done before |
117 self.req.html_headers.add_onload(u""" |
124 self.req.html_headers.add_onload(u""" |
118 jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s }); |
125 jQuery('#entity-tabs-%(eeid)s > ul').tabs( { selected: %(tabindex)s }); |
119 set_tab('%(vid)s', '%(cookiename)s'); |
126 set_tab('%(vid)s', '%(cookiename)s'); |
120 """ % {'tabindex' : tabs.index(active_tab), |
127 """ % {'tabindex' : tabs.index(active_tab), |
121 'vid' : active_tab, |
128 'vid' : active_tab, |
|
129 'eeid' : entity.eid, |
122 'cookiename' : self.cookie_name}) |
130 'cookiename' : self.cookie_name}) |
123 |
131 |
124 |
132 |
125 class EntityRelatedTab(EntityView): |
133 class EntityRelatedTab(EntityView): |
126 """A view you should inherit from leftmost, |
134 """A view you should inherit from leftmost, |
136 rtype = 'screenshot' |
144 rtype = 'screenshot' |
137 target = 'object' |
145 target = 'object' |
138 vid = 'gallery' |
146 vid = 'gallery' |
139 __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,) |
147 __selectors__ = EntityRelationView.__selectors__ + (one_line_rset,) |
140 |
148 |
141 |
|
142 This is the view we want to have in a tab, only if there is something to show. |
149 This is the view we want to have in a tab, only if there is something to show. |
143 Then, just define as below, and declare this being the tab content : |
150 Then, just define as below, and declare this being the tab content : |
144 |
151 |
145 class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView): |
152 class ProjectScreenshotTab(EntityRelatedTab, ProjectScreenshotsView): |
146 id = 'screenshots_tab' |
153 id = 'screenshots_tab' |
147 """ |
154 """ |
148 __selectors__ = EntityView.__selectors__ + (has_related_entities,) |
155 __selectors__ = EntityView.__selectors__ + (has_related_entities,) |
149 vid = 'list' |
156 vid = 'list' |
150 |
157 |