author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Mon, 15 Nov 2010 19:38:31 +0100 | |
branch | stable |
changeset 6702 | dfc19a6fb8aa |
parent 6569 | 806b53dc5474 |
child 6800 | 3f3d576b87d9 |
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 |
||
6154 | 24 |
import random |
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 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
27 |
from logilab.mtconverter import xml_escape |
0 | 28 |
|
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
843
diff
changeset
|
29 |
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
|
30 |
from cubicweb.uilib import toggle_action, htmlescape |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
31 |
from cubicweb.web import jsonize |
0 | 32 |
|
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
|
33 |
# 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
|
34 |
# i18n strings, etc.) |
0 | 35 |
class HTMLWidget(object): |
36 |
||
37 |
def _initialize_stream(self, w=None): |
|
38 |
if w: |
|
39 |
self.w = w |
|
40 |
else: |
|
41 |
self._stream = UStringIO() |
|
42 |
self.w = self._stream.write |
|
43 |
||
44 |
def _render(self): |
|
45 |
raise NotImplementedError |
|
46 |
||
47 |
def render(self, w=None): |
|
48 |
self._initialize_stream(w) |
|
49 |
self._render() |
|
50 |
if w is None: |
|
51 |
return self._stream.getvalue() |
|
52 |
||
53 |
def is_empty(self): |
|
54 |
return False |
|
55 |
||
56 |
||
57 |
class BoxWidget(HTMLWidget): |
|
58 |
def __init__(self, title, id, items=None, _class="boxFrame", |
|
59 |
islist=True, shadow=True, escape=True): |
|
60 |
self.title = title |
|
61 |
self.id = id |
|
62 |
self.items = items or [] |
|
63 |
self._class = _class |
|
64 |
self.islist = islist |
|
65 |
self.shadow = shadow |
|
66 |
self.escape = escape |
|
67 |
||
68 |
def __len__(self): |
|
69 |
return len(self.items) |
|
70 |
||
71 |
def is_empty(self): |
|
72 |
return len(self) == 0 |
|
73 |
||
74 |
def append(self, item): |
|
75 |
self.items.append(item) |
|
76 |
||
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
77 |
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
|
78 |
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
|
79 |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
80 |
title_class = 'boxTitle' |
0 | 81 |
main_div_class = 'boxContent' |
82 |
listing_class = 'boxListing' |
|
1698 | 83 |
|
0 | 84 |
def box_begin_content(self): |
85 |
self.w(u'<div class="%s">\n' % self.main_div_class) |
|
86 |
if self.islist: |
|
87 |
self.w(u'<ul class="%s">' % self.listing_class) |
|
88 |
||
89 |
def box_end_content(self): |
|
90 |
if self.islist: |
|
91 |
self.w(u'</ul>\n') |
|
92 |
self.w(u'</div>\n') |
|
93 |
if self.shadow: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
94 |
self.w(u'<div class="shadow"> </div>') |
1698 | 95 |
|
0 | 96 |
def _render(self): |
97 |
if self.id: |
|
98 |
self.w(u'<div class="%s" id="%s">' % (self._class, self.id)) |
|
99 |
else: |
|
1698 | 100 |
self.w(u'<div class="%s">' % self._class) |
0 | 101 |
if self.title: |
102 |
if self.escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
103 |
title = '<span>%s</span>' % xml_escape(self.title) |
0 | 104 |
else: |
105 |
title = '<span>%s</span>' % self.title |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
106 |
self.w(u'<div class="%s">%s</div>' % (self.title_class, title)) |
0 | 107 |
if self.items: |
108 |
self.box_begin_content() |
|
109 |
for item in self.items: |
|
6569
806b53dc5474
[boxes] add bw compat on BoxWidget, we mix string and box items until htmlwidgets are gone
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6154
diff
changeset
|
110 |
if hasattr(item, 'render'): |
806b53dc5474
[boxes] add bw compat on BoxWidget, we mix string and box items until htmlwidgets are gone
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6154
diff
changeset
|
111 |
item.render(self.w) |
806b53dc5474
[boxes] add bw compat on BoxWidget, we mix string and box items until htmlwidgets are gone
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6154
diff
changeset
|
112 |
else: |
806b53dc5474
[boxes] add bw compat on BoxWidget, we mix string and box items until htmlwidgets are gone
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6154
diff
changeset
|
113 |
self.w(u'<li>%s</li>' % item) |
0 | 114 |
self.box_end_content() |
115 |
self.w(u'</div>') |
|
116 |
||
117 |
||
118 |
class SideBoxWidget(BoxWidget): |
|
119 |
"""default CubicWeb's sidebox widget""" |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
120 |
title_class = u'sideBoxTitle' |
0 | 121 |
main_div_class = u'sideBoxBody' |
122 |
listing_class = '' |
|
123 |
||
124 |
def __init__(self, title, id=None): |
|
125 |
super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
|
126 |
shadow=False) |
|
127 |
||
1698 | 128 |
|
0 | 129 |
class MenuWidget(BoxWidget): |
130 |
main_div_class = 'menuContent' |
|
131 |
listing_class = 'menuListing' |
|
132 |
||
133 |
def box_end_content(self): |
|
134 |
if self.islist: |
|
135 |
self.w(u'</ul>\n') |
|
136 |
self.w(u'</div>\n') |
|
1698 | 137 |
|
0 | 138 |
|
139 |
class RawBoxItem(HTMLWidget): |
|
140 |
"""a simpe box item displaying raw data""" |
|
141 |
def __init__(self, label, liclass=None): |
|
142 |
self.label = label |
|
143 |
self.liclass = liclass |
|
144 |
||
145 |
def _start_li(self): |
|
146 |
if self.liclass is None: |
|
147 |
return u'<li>' |
|
148 |
else: |
|
149 |
return u'<li class="%s">' % self.liclass |
|
1698 | 150 |
|
0 | 151 |
return self.label |
1698 | 152 |
|
0 | 153 |
def _render(self): |
154 |
self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
|
155 |
||
156 |
||
157 |
class BoxMenu(RawBoxItem): |
|
158 |
"""a menu in a box""" |
|
159 |
link_class = 'boxMenu' |
|
1698 | 160 |
|
0 | 161 |
def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
162 |
link_class=None): |
|
163 |
super(BoxMenu, self).__init__(label, liclass) |
|
164 |
self.items = items or [] |
|
165 |
self.isitem = isitem |
|
166 |
self.ident = ident or u'boxmenu_%s' % label.replace(' ', '_').replace("'", '') |
|
167 |
if link_class: |
|
168 |
self.link_class = link_class |
|
1698 | 169 |
|
0 | 170 |
def append(self, item): |
171 |
self.items.append(item) |
|
172 |
||
173 |
def _begin_menu(self, ident): |
|
174 |
self.w(u'<ul id="%s" class="hidden">' % ident) |
|
175 |
||
176 |
def _end_menu(self): |
|
177 |
self.w(u'</ul>') |
|
1698 | 178 |
|
0 | 179 |
def _render(self): |
180 |
if self.isitem: |
|
181 |
self.w(self._start_li()) |
|
182 |
ident = self.ident |
|
183 |
self.w(u'<a href="%s" class="%s">%s</a>' % ( |
|
184 |
toggle_action(ident), self.link_class, self.label)) |
|
185 |
self._begin_menu(ident) |
|
186 |
for item in self.items: |
|
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
187 |
if hasattr(item, 'render'): |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
188 |
item.render(self.w) |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
189 |
else: |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
190 |
self.w(u'<li>%s</li>' % item) |
0 | 191 |
self._end_menu() |
192 |
if self.isitem: |
|
193 |
self.w(u'</li>') |
|
194 |
||
195 |
||
196 |
class PopupBoxMenu(BoxMenu): |
|
197 |
"""like BoxMenu but uses div and specific css class |
|
198 |
in order to behave like a popup menu |
|
199 |
""" |
|
200 |
link_class = 'popupMenu' |
|
201 |
||
202 |
def _begin_menu(self, ident): |
|
203 |
self.w(u'<div class="popupWrapper"><div id="%s" class="hidden popup"><ul>' % ident) |
|
204 |
||
205 |
def _end_menu(self): |
|
206 |
self.w(u'</ul></div></div>') |
|
207 |
||
208 |
||
209 |
class BoxField(HTMLWidget): |
|
210 |
"""couples label / value meant to be displayed in a box""" |
|
211 |
def __init__(self, label, value): |
|
212 |
self.label = label |
|
213 |
self.value = value |
|
1698 | 214 |
|
0 | 215 |
def _render(self): |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
216 |
self.w(u'<li><div><span class="label">%s</span> ' |
1698 | 217 |
u'<span class="value">%s</span></div></li>' |
0 | 218 |
% (self.label, self.value)) |
219 |
||
220 |
class BoxSeparator(HTMLWidget): |
|
221 |
"""a menu separator""" |
|
1698 | 222 |
|
0 | 223 |
def _render(self): |
224 |
self.w(u'</ul><hr class="boxSeparator"/><ul>') |
|
225 |
||
226 |
||
227 |
class BoxLink(HTMLWidget): |
|
228 |
"""a link in a box""" |
|
229 |
def __init__(self, href, label, _class='', title='', ident='', escape=False): |
|
230 |
self.href = href |
|
231 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
232 |
self.label = xml_escape(label) |
0 | 233 |
else: |
234 |
self.label = label |
|
235 |
self._class = _class or '' |
|
236 |
self.title = title |
|
237 |
self.ident = ident |
|
238 |
||
239 |
def _render(self): |
|
240 |
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
|
241 |
xml_escape(self.href), xml_escape(self.title), self.label) |
0 | 242 |
if self.ident: |
243 |
self.w(u'<li id="%s" class="%s">%s</li>\n' % (self.ident, self._class, link)) |
|
244 |
else: |
|
245 |
self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
|
246 |
||
247 |
||
248 |
class BoxHtml(HTMLWidget): |
|
249 |
"""a form in a box""" |
|
250 |
def __init__(self, rawhtml): |
|
251 |
self.rawhtml = rawhtml |
|
252 |
||
253 |
def _render(self): |
|
254 |
self.w(self.rawhtml) |
|
255 |
||
256 |
||
257 |
class TableColumn(object): |
|
258 |
def __init__(self, name, rset_sortcol): |
|
259 |
""" |
|
260 |
:param name: the column's name |
|
261 |
:param rset_sortcol: the model's column used to sort this column view |
|
262 |
""" |
|
263 |
self.name = name |
|
264 |
self.cellrenderers = [] |
|
265 |
self.rset_sortcol = rset_sortcol |
|
266 |
self.cell_attrs = {} |
|
267 |
||
268 |
def append_renderer(self, cellvid, colindex): |
|
269 |
# XXX (adim) : why do we need colindex here ? |
|
270 |
self.cellrenderers.append( (cellvid, colindex) ) |
|
271 |
||
272 |
def add_attr(self, attr, value): |
|
273 |
self.cell_attrs[attr] = value |
|
274 |
||
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
275 |
class SimpleTableModel(object): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
276 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
277 |
uses a list of lists as a storage backend |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
278 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
279 |
NB: the model expectes the cellvid passed to |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
280 |
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
|
281 |
argument and returning a unicode object |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
282 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
283 |
def __init__(self, rows): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
284 |
self._rows = rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
285 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
286 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
287 |
def get_rows(self): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
288 |
return self._rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
289 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
290 |
def render_cell(self, cellvid, rowindex, colindex, w): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
291 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
292 |
w(cellvid(value)) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
293 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
294 |
@htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
295 |
@jsonize |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
296 |
def sortvalue(self, rowindex, colindex): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
297 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
298 |
if value is None: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
299 |
return u'' |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
300 |
elif isinstance(value, int): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
301 |
return u'%09d' % value |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
302 |
else: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
303 |
return unicode(value) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
304 |
|
0 | 305 |
|
306 |
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
|
307 |
""" |
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
308 |
Display data in a Table with sortable column. |
0 | 309 |
|
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
310 |
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
|
311 |
|
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
312 |
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
|
313 |
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
|
314 |
""" |
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
|
315 |
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
|
316 |
"onmouseout=\"$(this).removeClass('highlighted');\"" |
1698 | 317 |
|
0 | 318 |
def __init__(self, model): |
319 |
self.model = model |
|
320 |
self.columns = [] |
|
321 |
||
322 |
def append_column(self, column): |
|
323 |
""" |
|
324 |
:type column: TableColumn |
|
325 |
""" |
|
326 |
self.columns.append(column) |
|
327 |
||
328 |
def _render(self): |
|
329 |
self.w(u'<table class="listing">') |
|
330 |
self.w(u'<thead>') |
|
331 |
self.w(u'<tr class="header">') |
|
332 |
for column in self.columns: |
|
333 |
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
|
334 |
self.w(u'<th %s>%s</th>' % (' '.join(attrs), column.name or u'')) |
0 | 335 |
self.w(u'</tr>') |
336 |
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
|
337 |
for rowindex in xrange(len(self.model.get_rows())): |
0 | 338 |
klass = (rowindex%2==1) and 'odd' or 'even' |
339 |
self.w(u'<tr class="%s" %s>' % (klass, self.highlight)) |
|
340 |
for column, sortvalue in self.itercols(rowindex): |
|
341 |
attrs = dict(column.cell_attrs) |
|
342 |
attrs["cubicweb:sortvalue"] = 'json:' + sortvalue |
|
343 |
attrs = ('%s="%s"' % (name, value) for name, value in attrs.iteritems()) |
|
344 |
self.w(u'<td %s>' % (' '.join(attrs))) |
|
345 |
for cellvid, colindex in column.cellrenderers: |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1698
diff
changeset
|
346 |
self.model.render_cell(cellvid, rowindex, colindex, w=self.w) |
0 | 347 |
self.w(u'</td>') |
348 |
self.w(u'</tr>') |
|
349 |
self.w(u'</tbody>') |
|
350 |
self.w(u'</table>') |
|
351 |
||
352 |
def itercols(self, rowindex): |
|
353 |
for column in self.columns: |
|
354 |
yield column, self.model.sortvalue(rowindex, column.rset_sortcol) |
|
355 |
||
356 |