author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Thu, 16 Sep 2010 16:49:13 +0200 | |
branch | stable |
changeset 6269 | 2220fa621455 |
parent 5727 | 29afb9e715bb |
child 6140 | 65a619eb31c4 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4786
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""html widgets |
19 |
||
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3798
diff
changeset
|
20 |
those are in cubicweb since we need to know available widgets at schema |
0 | 21 |
serialization time |
22 |
||
23 |
""" |
|
24 |
||
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
25 |
from math import floor |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
26 |
import random |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
27 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
28 |
from logilab.mtconverter import xml_escape |
0 | 29 |
|
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
843
diff
changeset
|
30 |
from cubicweb.utils import UStringIO |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
31 |
from cubicweb.uilib import toggle_action, htmlescape |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
32 |
from cubicweb.web import jsonize |
0 | 33 |
|
203
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
34 |
# XXX HTMLWidgets should have access to req (for datadir / static urls, |
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
35 |
# i18n strings, etc.) |
0 | 36 |
class HTMLWidget(object): |
37 |
||
38 |
def _initialize_stream(self, w=None): |
|
39 |
if w: |
|
40 |
self.w = w |
|
41 |
else: |
|
42 |
self._stream = UStringIO() |
|
43 |
self.w = self._stream.write |
|
44 |
||
45 |
def _render(self): |
|
46 |
raise NotImplementedError |
|
47 |
||
48 |
def render(self, w=None): |
|
49 |
self._initialize_stream(w) |
|
50 |
self._render() |
|
51 |
if w is None: |
|
52 |
return self._stream.getvalue() |
|
53 |
||
54 |
def is_empty(self): |
|
55 |
return False |
|
56 |
||
57 |
||
58 |
class BoxWidget(HTMLWidget): |
|
59 |
def __init__(self, title, id, items=None, _class="boxFrame", |
|
60 |
islist=True, shadow=True, escape=True): |
|
61 |
self.title = title |
|
62 |
self.id = id |
|
63 |
self.items = items or [] |
|
64 |
self._class = _class |
|
65 |
self.islist = islist |
|
66 |
self.shadow = shadow |
|
67 |
self.escape = escape |
|
68 |
||
69 |
def __len__(self): |
|
70 |
return len(self.items) |
|
71 |
||
72 |
def is_empty(self): |
|
73 |
return len(self) == 0 |
|
74 |
||
75 |
def append(self, item): |
|
76 |
self.items.append(item) |
|
77 |
||
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
78 |
def extend(self, items): |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
79 |
self.items.extend(items) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
80 |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
81 |
title_class = 'boxTitle' |
0 | 82 |
main_div_class = 'boxContent' |
83 |
listing_class = 'boxListing' |
|
1698 | 84 |
|
0 | 85 |
def box_begin_content(self): |
86 |
self.w(u'<div class="%s">\n' % self.main_div_class) |
|
87 |
if self.islist: |
|
88 |
self.w(u'<ul class="%s">' % self.listing_class) |
|
89 |
||
90 |
def box_end_content(self): |
|
91 |
if self.islist: |
|
92 |
self.w(u'</ul>\n') |
|
93 |
self.w(u'</div>\n') |
|
94 |
if self.shadow: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
95 |
self.w(u'<div class="shadow"> </div>') |
1698 | 96 |
|
0 | 97 |
def _render(self): |
98 |
if self.id: |
|
99 |
self.w(u'<div class="%s" id="%s">' % (self._class, self.id)) |
|
100 |
else: |
|
1698 | 101 |
self.w(u'<div class="%s">' % self._class) |
0 | 102 |
if self.title: |
103 |
if self.escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
104 |
title = '<span>%s</span>' % xml_escape(self.title) |
0 | 105 |
else: |
106 |
title = '<span>%s</span>' % self.title |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
107 |
self.w(u'<div class="%s">%s</div>' % (self.title_class, title)) |
0 | 108 |
if self.items: |
109 |
self.box_begin_content() |
|
110 |
for item in self.items: |
|
111 |
item.render(self.w) |
|
112 |
self.box_end_content() |
|
113 |
self.w(u'</div>') |
|
114 |
||
115 |
||
116 |
class SideBoxWidget(BoxWidget): |
|
117 |
"""default CubicWeb's sidebox widget""" |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
118 |
title_class = u'sideBoxTitle' |
0 | 119 |
main_div_class = u'sideBoxBody' |
120 |
listing_class = '' |
|
121 |
||
122 |
def __init__(self, title, id=None): |
|
123 |
super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
|
124 |
shadow=False) |
|
125 |
||
1698 | 126 |
|
0 | 127 |
class MenuWidget(BoxWidget): |
128 |
main_div_class = 'menuContent' |
|
129 |
listing_class = 'menuListing' |
|
130 |
||
131 |
def box_end_content(self): |
|
132 |
if self.islist: |
|
133 |
self.w(u'</ul>\n') |
|
134 |
self.w(u'</div>\n') |
|
1698 | 135 |
|
0 | 136 |
|
137 |
class RawBoxItem(HTMLWidget): |
|
138 |
"""a simpe box item displaying raw data""" |
|
139 |
def __init__(self, label, liclass=None): |
|
140 |
self.label = label |
|
141 |
self.liclass = liclass |
|
142 |
||
143 |
def _start_li(self): |
|
144 |
if self.liclass is None: |
|
145 |
return u'<li>' |
|
146 |
else: |
|
147 |
return u'<li class="%s">' % self.liclass |
|
1698 | 148 |
|
0 | 149 |
return self.label |
1698 | 150 |
|
0 | 151 |
def _render(self): |
152 |
self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
|
153 |
||
154 |
||
155 |
class BoxMenu(RawBoxItem): |
|
156 |
"""a menu in a box""" |
|
157 |
link_class = 'boxMenu' |
|
1698 | 158 |
|
0 | 159 |
def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
160 |
link_class=None): |
|
161 |
super(BoxMenu, self).__init__(label, liclass) |
|
162 |
self.items = items or [] |
|
163 |
self.isitem = isitem |
|
164 |
self.ident = ident or u'boxmenu_%s' % label.replace(' ', '_').replace("'", '') |
|
165 |
if link_class: |
|
166 |
self.link_class = link_class |
|
1698 | 167 |
|
0 | 168 |
def append(self, item): |
169 |
self.items.append(item) |
|
170 |
||
171 |
def _begin_menu(self, ident): |
|
172 |
self.w(u'<ul id="%s" class="hidden">' % ident) |
|
173 |
||
174 |
def _end_menu(self): |
|
175 |
self.w(u'</ul>') |
|
1698 | 176 |
|
0 | 177 |
def _render(self): |
178 |
if self.isitem: |
|
179 |
self.w(self._start_li()) |
|
180 |
ident = self.ident |
|
181 |
self.w(u'<a href="%s" class="%s">%s</a>' % ( |
|
182 |
toggle_action(ident), self.link_class, self.label)) |
|
183 |
self._begin_menu(ident) |
|
184 |
for item in self.items: |
|
185 |
item.render(self.w) |
|
186 |
self._end_menu() |
|
187 |
if self.isitem: |
|
188 |
self.w(u'</li>') |
|
189 |
||
190 |
||
191 |
class PopupBoxMenu(BoxMenu): |
|
192 |
"""like BoxMenu but uses div and specific css class |
|
193 |
in order to behave like a popup menu |
|
194 |
""" |
|
195 |
link_class = 'popupMenu' |
|
196 |
||
197 |
def _begin_menu(self, ident): |
|
198 |
self.w(u'<div class="popupWrapper"><div id="%s" class="hidden popup"><ul>' % ident) |
|
199 |
||
200 |
def _end_menu(self): |
|
201 |
self.w(u'</ul></div></div>') |
|
202 |
||
203 |
||
204 |
class BoxField(HTMLWidget): |
|
205 |
"""couples label / value meant to be displayed in a box""" |
|
206 |
def __init__(self, label, value): |
|
207 |
self.label = label |
|
208 |
self.value = value |
|
1698 | 209 |
|
0 | 210 |
def _render(self): |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
211 |
self.w(u'<li><div><span class="label">%s</span> ' |
1698 | 212 |
u'<span class="value">%s</span></div></li>' |
0 | 213 |
% (self.label, self.value)) |
214 |
||
215 |
class BoxSeparator(HTMLWidget): |
|
216 |
"""a menu separator""" |
|
1698 | 217 |
|
0 | 218 |
def _render(self): |
219 |
self.w(u'</ul><hr class="boxSeparator"/><ul>') |
|
220 |
||
221 |
||
222 |
class BoxLink(HTMLWidget): |
|
223 |
"""a link in a box""" |
|
224 |
def __init__(self, href, label, _class='', title='', ident='', escape=False): |
|
225 |
self.href = href |
|
226 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
227 |
self.label = xml_escape(label) |
0 | 228 |
else: |
229 |
self.label = label |
|
230 |
self._class = _class or '' |
|
231 |
self.title = title |
|
232 |
self.ident = ident |
|
233 |
||
234 |
def _render(self): |
|
235 |
link = u'<a href="%s" title="%s">%s</a>' % ( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
236 |
xml_escape(self.href), xml_escape(self.title), self.label) |
0 | 237 |
if self.ident: |
238 |
self.w(u'<li id="%s" class="%s">%s</li>\n' % (self.ident, self._class, link)) |
|
239 |
else: |
|
240 |
self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
|
241 |
||
242 |
||
243 |
class BoxHtml(HTMLWidget): |
|
244 |
"""a form in a box""" |
|
245 |
def __init__(self, rawhtml): |
|
246 |
self.rawhtml = rawhtml |
|
247 |
||
248 |
def _render(self): |
|
249 |
self.w(self.rawhtml) |
|
250 |
||
251 |
||
252 |
class TableColumn(object): |
|
253 |
def __init__(self, name, rset_sortcol): |
|
254 |
""" |
|
255 |
:param name: the column's name |
|
256 |
:param rset_sortcol: the model's column used to sort this column view |
|
257 |
""" |
|
258 |
self.name = name |
|
259 |
self.cellrenderers = [] |
|
260 |
self.rset_sortcol = rset_sortcol |
|
261 |
self.cell_attrs = {} |
|
262 |
||
263 |
def append_renderer(self, cellvid, colindex): |
|
264 |
# XXX (adim) : why do we need colindex here ? |
|
265 |
self.cellrenderers.append( (cellvid, colindex) ) |
|
266 |
||
267 |
def add_attr(self, attr, value): |
|
268 |
self.cell_attrs[attr] = value |
|
269 |
||
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
270 |
class SimpleTableModel(object): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
271 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
272 |
uses a list of lists as a storage backend |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
273 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
274 |
NB: the model expectes the cellvid passed to |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
275 |
TableColumn.append_renderer to be a callable accepting a single |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
276 |
argument and returning a unicode object |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
277 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
278 |
def __init__(self, rows): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
279 |
self._rows = rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
280 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
281 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
282 |
def get_rows(self): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
283 |
return self._rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
284 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
285 |
def render_cell(self, cellvid, rowindex, colindex, w): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
286 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
287 |
w(cellvid(value)) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
288 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
289 |
@htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
290 |
@jsonize |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
291 |
def sortvalue(self, rowindex, colindex): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
292 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
293 |
if value is None: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
294 |
return u'' |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
295 |
elif isinstance(value, int): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
296 |
return u'%09d' % value |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
297 |
else: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
298 |
return unicode(value) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
299 |
|
0 | 300 |
|
301 |
class TableWidget(HTMLWidget): |
|
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
302 |
""" |
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
303 |
Display data in a Table with sortable column. |
0 | 304 |
|
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
305 |
When using remember to include the required css and js with: |
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
306 |
|
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
307 |
self._cw.add_js('jquery.tablesorter.js') |
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
308 |
self._cw.add_css(('cubicweb.tablesorter.css', 'cubicweb.tableview.css')) |
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
309 |
""" |
5727
29afb9e715bb
[3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5503
diff
changeset
|
310 |
highlight = "onmouseover=\"$(this).addClass('highlighted');\" " \ |
29afb9e715bb
[3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5503
diff
changeset
|
311 |
"onmouseout=\"$(this).removeClass('highlighted');\"" |
1698 | 312 |
|
0 | 313 |
def __init__(self, model): |
314 |
self.model = model |
|
315 |
self.columns = [] |
|
316 |
||
317 |
def append_column(self, column): |
|
318 |
""" |
|
319 |
:type column: TableColumn |
|
320 |
""" |
|
321 |
self.columns.append(column) |
|
322 |
||
323 |
def _render(self): |
|
324 |
self.w(u'<table class="listing">') |
|
325 |
self.w(u'<thead>') |
|
326 |
self.w(u'<tr class="header">') |
|
327 |
for column in self.columns: |
|
328 |
attrs = ('%s="%s"' % (name, value) for name, value in column.cell_attrs.iteritems()) |
|
5503
5b7eb0839da3
[table view] take care to None label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
329 |
self.w(u'<th %s>%s</th>' % (' '.join(attrs), column.name or u'')) |
0 | 330 |
self.w(u'</tr>') |
331 |
self.w(u'</thead><tbody>') |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
332 |
for rowindex in xrange(len(self.model.get_rows())): |
0 | 333 |
klass = (rowindex%2==1) and 'odd' or 'even' |
334 |
self.w(u'<tr class="%s" %s>' % (klass, self.highlight)) |
|
335 |
for column, sortvalue in self.itercols(rowindex): |
|
336 |
attrs = dict(column.cell_attrs) |
|
337 |
attrs["cubicweb:sortvalue"] = 'json:' + sortvalue |
|
338 |
attrs = ('%s="%s"' % (name, value) for name, value in attrs.iteritems()) |
|
339 |
self.w(u'<td %s>' % (' '.join(attrs))) |
|
340 |
for cellvid, colindex in column.cellrenderers: |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1698
diff
changeset
|
341 |
self.model.render_cell(cellvid, rowindex, colindex, w=self.w) |
0 | 342 |
self.w(u'</td>') |
343 |
self.w(u'</tr>') |
|
344 |
self.w(u'</tbody>') |
|
345 |
self.w(u'</table>') |
|
346 |
||
347 |
def itercols(self, rowindex): |
|
348 |
for column in self.columns: |
|
349 |
yield column, self.model.sortvalue(rowindex, column.rset_sortcol) |
|
350 |
||
351 |