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