author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 20 Oct 2010 16:40:02 +0200 | |
changeset 6567 | 303b150ebb7a |
parent 6154 | b520763b6ace |
child 6569 | 806b53dc5474 |
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: |
|
110 |
item.render(self.w) |
|
111 |
self.box_end_content() |
|
112 |
self.w(u'</div>') |
|
113 |
||
114 |
||
115 |
class SideBoxWidget(BoxWidget): |
|
116 |
"""default CubicWeb's sidebox widget""" |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
117 |
title_class = u'sideBoxTitle' |
0 | 118 |
main_div_class = u'sideBoxBody' |
119 |
listing_class = '' |
|
120 |
||
121 |
def __init__(self, title, id=None): |
|
122 |
super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
|
123 |
shadow=False) |
|
124 |
||
1698 | 125 |
|
0 | 126 |
class MenuWidget(BoxWidget): |
127 |
main_div_class = 'menuContent' |
|
128 |
listing_class = 'menuListing' |
|
129 |
||
130 |
def box_end_content(self): |
|
131 |
if self.islist: |
|
132 |
self.w(u'</ul>\n') |
|
133 |
self.w(u'</div>\n') |
|
1698 | 134 |
|
0 | 135 |
|
136 |
class RawBoxItem(HTMLWidget): |
|
137 |
"""a simpe box item displaying raw data""" |
|
138 |
def __init__(self, label, liclass=None): |
|
139 |
self.label = label |
|
140 |
self.liclass = liclass |
|
141 |
||
142 |
def _start_li(self): |
|
143 |
if self.liclass is None: |
|
144 |
return u'<li>' |
|
145 |
else: |
|
146 |
return u'<li class="%s">' % self.liclass |
|
1698 | 147 |
|
0 | 148 |
return self.label |
1698 | 149 |
|
0 | 150 |
def _render(self): |
151 |
self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
|
152 |
||
153 |
||
154 |
class BoxMenu(RawBoxItem): |
|
155 |
"""a menu in a box""" |
|
156 |
link_class = 'boxMenu' |
|
1698 | 157 |
|
0 | 158 |
def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
159 |
link_class=None): |
|
160 |
super(BoxMenu, self).__init__(label, liclass) |
|
161 |
self.items = items or [] |
|
162 |
self.isitem = isitem |
|
163 |
self.ident = ident or u'boxmenu_%s' % label.replace(' ', '_').replace("'", '') |
|
164 |
if link_class: |
|
165 |
self.link_class = link_class |
|
1698 | 166 |
|
0 | 167 |
def append(self, item): |
168 |
self.items.append(item) |
|
169 |
||
170 |
def _begin_menu(self, ident): |
|
171 |
self.w(u'<ul id="%s" class="hidden">' % ident) |
|
172 |
||
173 |
def _end_menu(self): |
|
174 |
self.w(u'</ul>') |
|
1698 | 175 |
|
0 | 176 |
def _render(self): |
177 |
if self.isitem: |
|
178 |
self.w(self._start_li()) |
|
179 |
ident = self.ident |
|
180 |
self.w(u'<a href="%s" class="%s">%s</a>' % ( |
|
181 |
toggle_action(ident), self.link_class, self.label)) |
|
182 |
self._begin_menu(ident) |
|
183 |
for item in self.items: |
|
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
184 |
if hasattr(item, 'render'): |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
185 |
item.render(self.w) |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
186 |
else: |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
187 |
self.w(u'<li>%s</li>' % item) |
0 | 188 |
self._end_menu() |
189 |
if self.isitem: |
|
190 |
self.w(u'</li>') |
|
191 |
||
192 |
||
193 |
class PopupBoxMenu(BoxMenu): |
|
194 |
"""like BoxMenu but uses div and specific css class |
|
195 |
in order to behave like a popup menu |
|
196 |
""" |
|
197 |
link_class = 'popupMenu' |
|
198 |
||
199 |
def _begin_menu(self, ident): |
|
200 |
self.w(u'<div class="popupWrapper"><div id="%s" class="hidden popup"><ul>' % ident) |
|
201 |
||
202 |
def _end_menu(self): |
|
203 |
self.w(u'</ul></div></div>') |
|
204 |
||
205 |
||
206 |
class BoxField(HTMLWidget): |
|
207 |
"""couples label / value meant to be displayed in a box""" |
|
208 |
def __init__(self, label, value): |
|
209 |
self.label = label |
|
210 |
self.value = value |
|
1698 | 211 |
|
0 | 212 |
def _render(self): |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
213 |
self.w(u'<li><div><span class="label">%s</span> ' |
1698 | 214 |
u'<span class="value">%s</span></div></li>' |
0 | 215 |
% (self.label, self.value)) |
216 |
||
217 |
class BoxSeparator(HTMLWidget): |
|
218 |
"""a menu separator""" |
|
1698 | 219 |
|
0 | 220 |
def _render(self): |
221 |
self.w(u'</ul><hr class="boxSeparator"/><ul>') |
|
222 |
||
223 |
||
224 |
class BoxLink(HTMLWidget): |
|
225 |
"""a link in a box""" |
|
226 |
def __init__(self, href, label, _class='', title='', ident='', escape=False): |
|
227 |
self.href = href |
|
228 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
229 |
self.label = xml_escape(label) |
0 | 230 |
else: |
231 |
self.label = label |
|
232 |
self._class = _class or '' |
|
233 |
self.title = title |
|
234 |
self.ident = ident |
|
235 |
||
236 |
def _render(self): |
|
237 |
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
|
238 |
xml_escape(self.href), xml_escape(self.title), self.label) |
0 | 239 |
if self.ident: |
240 |
self.w(u'<li id="%s" class="%s">%s</li>\n' % (self.ident, self._class, link)) |
|
241 |
else: |
|
242 |
self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
|
243 |
||
244 |
||
245 |
class BoxHtml(HTMLWidget): |
|
246 |
"""a form in a box""" |
|
247 |
def __init__(self, rawhtml): |
|
248 |
self.rawhtml = rawhtml |
|
249 |
||
250 |
def _render(self): |
|
251 |
self.w(self.rawhtml) |
|
252 |
||
253 |
||
254 |
class TableColumn(object): |
|
255 |
def __init__(self, name, rset_sortcol): |
|
256 |
""" |
|
257 |
:param name: the column's name |
|
258 |
:param rset_sortcol: the model's column used to sort this column view |
|
259 |
""" |
|
260 |
self.name = name |
|
261 |
self.cellrenderers = [] |
|
262 |
self.rset_sortcol = rset_sortcol |
|
263 |
self.cell_attrs = {} |
|
264 |
||
265 |
def append_renderer(self, cellvid, colindex): |
|
266 |
# XXX (adim) : why do we need colindex here ? |
|
267 |
self.cellrenderers.append( (cellvid, colindex) ) |
|
268 |
||
269 |
def add_attr(self, attr, value): |
|
270 |
self.cell_attrs[attr] = value |
|
271 |
||
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
272 |
class SimpleTableModel(object): |
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 |
uses a list of lists as a storage backend |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
275 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
276 |
NB: the model expectes the cellvid passed to |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
277 |
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
|
278 |
argument and returning a unicode object |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
279 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
280 |
def __init__(self, rows): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
281 |
self._rows = rows |
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 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
284 |
def get_rows(self): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
285 |
return self._rows |
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 render_cell(self, cellvid, rowindex, colindex, w): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
288 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
289 |
w(cellvid(value)) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
290 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
291 |
@htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
292 |
@jsonize |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
293 |
def sortvalue(self, rowindex, colindex): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
294 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
295 |
if value is None: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
296 |
return u'' |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
297 |
elif isinstance(value, int): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
298 |
return u'%09d' % value |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
299 |
else: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
300 |
return unicode(value) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
301 |
|
0 | 302 |
|
303 |
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
|
304 |
""" |
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
305 |
Display data in a Table with sortable column. |
0 | 306 |
|
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
307 |
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
|
308 |
|
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
309 |
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
|
310 |
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
|
311 |
""" |
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
|
312 |
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
|
313 |
"onmouseout=\"$(this).removeClass('highlighted');\"" |
1698 | 314 |
|
0 | 315 |
def __init__(self, model): |
316 |
self.model = model |
|
317 |
self.columns = [] |
|
318 |
||
319 |
def append_column(self, column): |
|
320 |
""" |
|
321 |
:type column: TableColumn |
|
322 |
""" |
|
323 |
self.columns.append(column) |
|
324 |
||
325 |
def _render(self): |
|
326 |
self.w(u'<table class="listing">') |
|
327 |
self.w(u'<thead>') |
|
328 |
self.w(u'<tr class="header">') |
|
329 |
for column in self.columns: |
|
330 |
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
|
331 |
self.w(u'<th %s>%s</th>' % (' '.join(attrs), column.name or u'')) |
0 | 332 |
self.w(u'</tr>') |
333 |
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
|
334 |
for rowindex in xrange(len(self.model.get_rows())): |
0 | 335 |
klass = (rowindex%2==1) and 'odd' or 'even' |
336 |
self.w(u'<tr class="%s" %s>' % (klass, self.highlight)) |
|
337 |
for column, sortvalue in self.itercols(rowindex): |
|
338 |
attrs = dict(column.cell_attrs) |
|
339 |
attrs["cubicweb:sortvalue"] = 'json:' + sortvalue |
|
340 |
attrs = ('%s="%s"' % (name, value) for name, value in attrs.iteritems()) |
|
341 |
self.w(u'<td %s>' % (' '.join(attrs))) |
|
342 |
for cellvid, colindex in column.cellrenderers: |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1698
diff
changeset
|
343 |
self.model.render_cell(cellvid, rowindex, colindex, w=self.w) |
0 | 344 |
self.w(u'</td>') |
345 |
self.w(u'</tr>') |
|
346 |
self.w(u'</tbody>') |
|
347 |
self.w(u'</table>') |
|
348 |
||
349 |
def itercols(self, rowindex): |
|
350 |
for column in self.columns: |
|
351 |
yield column, self.model.sortvalue(rowindex, column.rset_sortcol) |
|
352 |
||
353 |