author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 27 Jan 2009 20:51:55 +0100 | |
changeset 489 | 21183fc36b39 |
parent 487 | 355356b781d7 |
child 490 | b84d64db2350 |
permissions | -rw-r--r-- |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
1 |
"""base classes to handle tabbed views |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
2 |
|
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
3 |
:organization: Logilab |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
4 |
:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
6 |
""" |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
7 |
|
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
9 |
|
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
10 |
from logilab.mtconverter import html_escape |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
11 |
|
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
12 |
from cubicweb import NoSelectableObject, role |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
13 |
from cubicweb.common.view import EntityView |
489 | 14 |
from cubicweb.common.selectors import has_related_entities |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
15 |
|
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
16 |
from cubicweb.common.utils import HTMLHead |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
17 |
|
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
18 |
# the prepend hack only work for 1-level lazy views |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
19 |
# a whole lot different thing must be done otherwise |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
20 |
def prepend_post_inline_script(self, content): |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
21 |
self.post_inlined_scripts.insert(0, content) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
22 |
HTMLHead.prepend_post_inline_script = prepend_post_inline_script |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
23 |
|
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
24 |
class LazyViewMixin(object): |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
25 |
|
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
26 |
def lazyview(self, vid, eid=None, show_spinbox=True, w=None): |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
27 |
"""a lazy version of wview |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
28 |
first version only support lazy viewing for an entity at a time |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
29 |
""" |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
30 |
w = w or self.w |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
31 |
self.req.add_js('cubicweb.lazy.js') |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
32 |
eid = eid if eid else '' |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
33 |
w(u'<div id="lazy-%s" cubicweb__loadurl="%s-%s">' % (vid, vid, eid)) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
34 |
if show_spinbox: |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
35 |
w(u'<img src="data/loading.gif" id="%s-hole"/>' % vid) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
36 |
w(u'</div>') |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
37 |
self.req.html_headers.prepend_post_inline_script(u""" |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
38 |
jQuery(document).ready(function () { |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
39 |
$('#lazy-%(vid)s').bind('%(event)s', function(event) { |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
40 |
load_now('#lazy-%(vid)s', '#%(vid)s-hole'); |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
41 |
});});""" % {'event' : 'load_%s' % vid, |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
42 |
'vid' : vid}) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
43 |
|
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
44 |
def forceview(self, vid): |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
45 |
"""trigger an event that will force immediate loading of the view |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
46 |
on dom readyness |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
47 |
""" |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
48 |
self.req.add_js('.lazy.js') |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
49 |
self.req.html_headers.add_post_inline_script(u""" |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
50 |
jQuery(document).ready(function() { |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
51 |
trigger_load('%(vid)s');}) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
52 |
""" % {'vid' : vid}) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
53 |
|
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
54 |
class TabsMixin(LazyViewMixin): |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
55 |
|
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
56 |
def active_tab(self, tabs, default): |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
57 |
cookie = self.req.get_cookie() |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
58 |
activetab = cookie.get('active_tab') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
59 |
if activetab is None: |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
60 |
cookie['active_tab'] = default |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
61 |
self.req.set_cookie(cookie, 'active_tab') |
487
355356b781d7
[tabs] avoid nasty surprises when tab name change & the cookie remembers the past
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
485
diff
changeset
|
62 |
tab = default |
355356b781d7
[tabs] avoid nasty surprises when tab name change & the cookie remembers the past
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
485
diff
changeset
|
63 |
else: |
355356b781d7
[tabs] avoid nasty surprises when tab name change & the cookie remembers the past
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
485
diff
changeset
|
64 |
tab = activetab.value |
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
65 |
return tab if tab in tabs else default |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
66 |
|
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
67 |
def render_tabs(self, tabs, default, entity): |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
68 |
self.req.add_css('ui.tabs.css') |
489 | 69 |
self.req.add_js(('ui.core.js', 'ui.tabs.js', 'cubicweb.tabs.js', 'cubicweb.lazy.js')) |
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
70 |
active_tab = self.active_tab(tabs, default) |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
71 |
self.req.html_headers.add_post_inline_script(u""" |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
72 |
jQuery(document).ready(function() { |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
73 |
jQuery('#entity-tabs > ul').tabs( { selected: %(tabindex)s }); |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
74 |
set_tab('%(vid)s'); |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
75 |
}); |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
76 |
""" % {'tabindex' : tabs.index(active_tab), |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
77 |
'vid' : active_tab}) |
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
78 |
# build the html structure |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
79 |
w = self.w |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
80 |
w(u'<div id="entity-tabs">') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
81 |
w(u'<ul>') |
489 | 82 |
selected_tabs = [] |
214
aaf2957bf69e
tabview is used to call each view for each tab
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
diff
changeset
|
83 |
for tab in tabs: |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
84 |
try: |
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
85 |
tabview = self.vreg.select_view(tab, self.req, self.rset) |
489 | 86 |
selected_tabs.append(tab) |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
87 |
except NoSelectableObject: |
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
88 |
self.info('no selectable view for id %s', tab) |
489 | 89 |
continue |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
90 |
w(u'<li>') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
91 |
w(u'<a href="#as-%s">' % tab) |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
92 |
w(u'<span onclick="set_tab(\'%s\')">' % tab) |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
93 |
w(self.req._(tab)) |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
94 |
w(u'</span>') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
95 |
w(u'</a>') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
96 |
w(u'</li>') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
97 |
w(u'</ul>') |
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
98 |
w(u'</div>') |
489 | 99 |
for tab in selected_tabs: |
485
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
100 |
w(u'<div id="as-%s">' % tab) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
101 |
self.lazyview(tab, entity.eid) |
0f830732be19
[tabs] provide a working tabs implementation, that lazy-loads tab content & should remember the current position
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
235
diff
changeset
|
102 |
w(u'</div>') |
235
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
103 |
|
b43362d92a1d
rename to tabs.py
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
220
diff
changeset
|
104 |
|
489 | 105 |
from cubicweb.web.views.basecontrollers import JSonController |
106 |
class TabsController(JSonController): |
|
107 |
||
108 |
def js_remember_active_tab(self, tabname): |
|
109 |
cookie = self.req.get_cookie() |
|
110 |
cookie['active_tab'] = tabname |
|
111 |
self.req.set_cookie(cookie, 'active_tab') |
|
112 |
||
113 |
def js_lazily(self, vid_eid): |
|
114 |
vid, eid = vid_eid.split('-') |
|
115 |
rset = self.req.eid_rset(eid) if eid else None |
|
116 |
view = self.vreg.select_view(vid, self.req, rset) |
|
117 |
return self._set_content_type(view, view.dispatch()) |
|
118 |
||
119 |
class DataDependantTab(EntityView): |
|
120 |
"""A view you should inherit from leftmost, |
|
121 |
to wrap another actual view displaying entity related stuff. |
|
122 |
Such a view _must_ provide the rtype, target and vid attributes : |
|
123 |
||
124 |
Example : |
|
125 |
||
126 |
class ProjectScreenshotsView(EntityRelationView): |
|
127 |
"display project's screenshots" |
|
128 |
id = title = _('projectscreenshots') |
|
129 |
accepts = ('Project',) |
|
130 |
rtype = 'screenshot' |
|
131 |
target = 'object' |
|
132 |
vid = 'gallery' |
|
133 |
__selectors__ = EntityRelationView.__selectors__ + (one_line_rset,) |
|
134 |
||
135 |
||
136 |
This is the view we want to have in a tab, only if there is something to show. |
|
137 |
Then, just define as below, and declare this being the tab content : |
|
138 |
||
139 |
class ProjectScreenshotTab(DataDependantTab, ProjectScreenshotsView): |
|
140 |
id = 'screenshots_tab' |
|
141 |
""" |
|
142 |
__selectors__ = EntityView.__selectors__ + (has_related_entities,) |
|
143 |
vid = 'list' |
|
144 |
||
145 |
def cell_call(self, row, col): |
|
146 |
rset = self.rset.get_entity(row, col).related(self.rtype, role(self)) |
|
147 |
self.w(u'<div class="mainInfo">') |
|
148 |
self.wview(self.vid, rset, 'noresult') |
|
149 |
self.w(u'</div>') |