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