author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 21 Jul 2011 14:20:45 +0200 | |
branch | stable |
changeset 7682 | 07b592a62be3 |
parent 6800 | 3f3d576b87d9 |
child 8126 | a4d8064bf393 |
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 |
return self.label |
1698 | 157 |
|
0 | 158 |
def _render(self): |
159 |
self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
|
160 |
||
161 |
||
162 |
class BoxMenu(RawBoxItem): |
|
163 |
"""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
|
164 |
|
0 | 165 |
link_class = 'boxMenu' |
1698 | 166 |
|
0 | 167 |
def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
168 |
link_class=None): |
|
169 |
super(BoxMenu, self).__init__(label, liclass) |
|
170 |
self.items = items or [] |
|
171 |
self.isitem = isitem |
|
172 |
self.ident = ident or u'boxmenu_%s' % label.replace(' ', '_').replace("'", '') |
|
173 |
if link_class: |
|
174 |
self.link_class = link_class |
|
1698 | 175 |
|
0 | 176 |
def append(self, item): |
177 |
self.items.append(item) |
|
178 |
||
179 |
def _begin_menu(self, ident): |
|
180 |
self.w(u'<ul id="%s" class="hidden">' % ident) |
|
181 |
||
182 |
def _end_menu(self): |
|
183 |
self.w(u'</ul>') |
|
1698 | 184 |
|
0 | 185 |
def _render(self): |
186 |
if self.isitem: |
|
187 |
self.w(self._start_li()) |
|
188 |
ident = self.ident |
|
189 |
self.w(u'<a href="%s" class="%s">%s</a>' % ( |
|
190 |
toggle_action(ident), self.link_class, self.label)) |
|
191 |
self._begin_menu(ident) |
|
192 |
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
|
193 |
_bwcompatible_render_item(self.w, item) |
0 | 194 |
self._end_menu() |
195 |
if self.isitem: |
|
196 |
self.w(u'</li>') |
|
197 |
||
198 |
||
199 |
class PopupBoxMenu(BoxMenu): |
|
200 |
"""like BoxMenu but uses div and specific css class |
|
201 |
in order to behave like a popup menu |
|
202 |
""" |
|
203 |
link_class = 'popupMenu' |
|
204 |
||
205 |
def _begin_menu(self, ident): |
|
206 |
self.w(u'<div class="popupWrapper"><div id="%s" class="hidden popup"><ul>' % ident) |
|
207 |
||
208 |
def _end_menu(self): |
|
209 |
self.w(u'</ul></div></div>') |
|
210 |
||
211 |
||
212 |
class BoxField(HTMLWidget): |
|
213 |
"""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
|
214 |
__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
|
215 |
__deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
0 | 216 |
def __init__(self, label, value): |
217 |
self.label = label |
|
218 |
self.value = value |
|
1698 | 219 |
|
0 | 220 |
def _render(self): |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
221 |
self.w(u'<li><div><span class="label">%s</span> ' |
1698 | 222 |
u'<span class="value">%s</span></div></li>' |
0 | 223 |
% (self.label, self.value)) |
224 |
||
225 |
class BoxSeparator(HTMLWidget): |
|
226 |
"""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
|
227 |
__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
|
228 |
__deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
1698 | 229 |
|
0 | 230 |
def _render(self): |
231 |
self.w(u'</ul><hr class="boxSeparator"/><ul>') |
|
232 |
||
233 |
||
234 |
class BoxLink(HTMLWidget): |
|
235 |
"""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
|
236 |
__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
|
237 |
__deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
0 | 238 |
def __init__(self, href, label, _class='', title='', ident='', escape=False): |
239 |
self.href = href |
|
240 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
241 |
self.label = xml_escape(label) |
0 | 242 |
else: |
243 |
self.label = label |
|
244 |
self._class = _class or '' |
|
245 |
self.title = title |
|
246 |
self.ident = ident |
|
247 |
||
248 |
def _render(self): |
|
249 |
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
|
250 |
xml_escape(self.href), xml_escape(self.title), self.label) |
0 | 251 |
if self.ident: |
252 |
self.w(u'<li id="%s" class="%s">%s</li>\n' % (self.ident, self._class, link)) |
|
253 |
else: |
|
254 |
self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
|
255 |
||
256 |
||
257 |
class BoxHtml(HTMLWidget): |
|
258 |
"""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
|
259 |
__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
|
260 |
__deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
0 | 261 |
def __init__(self, rawhtml): |
262 |
self.rawhtml = rawhtml |
|
263 |
||
264 |
def _render(self): |
|
265 |
self.w(self.rawhtml) |
|
266 |
||
267 |
||
268 |
class TableColumn(object): |
|
269 |
def __init__(self, name, rset_sortcol): |
|
270 |
""" |
|
271 |
:param name: the column's name |
|
272 |
:param rset_sortcol: the model's column used to sort this column view |
|
273 |
""" |
|
274 |
self.name = name |
|
275 |
self.cellrenderers = [] |
|
276 |
self.rset_sortcol = rset_sortcol |
|
277 |
self.cell_attrs = {} |
|
278 |
||
279 |
def append_renderer(self, cellvid, colindex): |
|
280 |
# XXX (adim) : why do we need colindex here ? |
|
281 |
self.cellrenderers.append( (cellvid, colindex) ) |
|
282 |
||
283 |
def add_attr(self, attr, value): |
|
284 |
self.cell_attrs[attr] = value |
|
285 |
||
6800
3f3d576b87d9
[web action] refactor box menu handling, fixing #1401943 on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6569
diff
changeset
|
286 |
|
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
287 |
class SimpleTableModel(object): |
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 |
uses a list of lists as a storage backend |
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 |
NB: the model expectes the cellvid passed to |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
292 |
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
|
293 |
argument and returning a unicode object |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
294 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
295 |
def __init__(self, rows): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
296 |
self._rows = rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
297 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
298 |
def get_rows(self): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
299 |
return self._rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
300 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
301 |
def render_cell(self, cellvid, rowindex, colindex, w): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
302 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
303 |
w(cellvid(value)) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
304 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
305 |
@htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
306 |
@jsonize |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
307 |
def sortvalue(self, rowindex, colindex): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
308 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
309 |
if value is None: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
310 |
return u'' |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
311 |
elif isinstance(value, int): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
312 |
return u'%09d' % value |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
313 |
else: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
314 |
return unicode(value) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
315 |
|
0 | 316 |
|
317 |
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
|
318 |
""" |
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
319 |
Display data in a Table with sortable column. |
0 | 320 |
|
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
321 |
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
|
322 |
|
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
323 |
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
|
324 |
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
|
325 |
""" |
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
|
326 |
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
|
327 |
"onmouseout=\"$(this).removeClass('highlighted');\"" |
1698 | 328 |
|
0 | 329 |
def __init__(self, model): |
330 |
self.model = model |
|
331 |
self.columns = [] |
|
332 |
||
333 |
def append_column(self, column): |
|
334 |
""" |
|
335 |
:type column: TableColumn |
|
336 |
""" |
|
337 |
self.columns.append(column) |
|
338 |
||
339 |
def _render(self): |
|
340 |
self.w(u'<table class="listing">') |
|
341 |
self.w(u'<thead>') |
|
342 |
self.w(u'<tr class="header">') |
|
343 |
for column in self.columns: |
|
344 |
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
|
345 |
self.w(u'<th %s>%s</th>' % (' '.join(attrs), column.name or u'')) |
0 | 346 |
self.w(u'</tr>') |
347 |
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
|
348 |
for rowindex in xrange(len(self.model.get_rows())): |
0 | 349 |
klass = (rowindex%2==1) and 'odd' or 'even' |
350 |
self.w(u'<tr class="%s" %s>' % (klass, self.highlight)) |
|
351 |
for column, sortvalue in self.itercols(rowindex): |
|
352 |
attrs = dict(column.cell_attrs) |
|
353 |
attrs["cubicweb:sortvalue"] = 'json:' + sortvalue |
|
354 |
attrs = ('%s="%s"' % (name, value) for name, value in attrs.iteritems()) |
|
355 |
self.w(u'<td %s>' % (' '.join(attrs))) |
|
356 |
for cellvid, colindex in column.cellrenderers: |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1698
diff
changeset
|
357 |
self.model.render_cell(cellvid, rowindex, colindex, w=self.w) |
0 | 358 |
self.w(u'</td>') |
359 |
self.w(u'</tr>') |
|
360 |
self.w(u'</tbody>') |
|
361 |
self.w(u'</table>') |
|
362 |
||
363 |
def itercols(self, rowindex): |
|
364 |
for column in self.columns: |
|
365 |
yield column, self.model.sortvalue(rowindex, column.rset_sortcol) |
|
366 |
||
367 |