author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 27 Jan 2010 09:56:58 +0100 | |
changeset 4389 | 14a993bc8d1e |
parent 4252 | 6c4f109c2b03 |
child 4719 | aaed3f813ef8 |
permissions | -rw-r--r-- |
0 | 1 |
"""html widgets |
2 |
||
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3798
diff
changeset
|
3 |
those are in cubicweb since we need to know available widgets at schema |
0 | 4 |
serialization time |
5 |
||
6 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3798
diff
changeset
|
7 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 8 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
9 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 10 |
""" |
11 |
||
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
12 |
from math import floor |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
13 |
import random |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
14 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
15 |
from logilab.mtconverter import xml_escape |
0 | 16 |
|
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
843
diff
changeset
|
17 |
from cubicweb.utils import UStringIO |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3798
diff
changeset
|
18 |
from cubicweb.uilib import toggle_action, limitsize, htmlescape |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
19 |
from cubicweb.web import jsonize |
0 | 20 |
|
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
|
21 |
# 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
|
22 |
# i18n strings, etc.) |
0 | 23 |
class HTMLWidget(object): |
24 |
||
25 |
def _initialize_stream(self, w=None): |
|
26 |
if w: |
|
27 |
self.w = w |
|
28 |
else: |
|
29 |
self._stream = UStringIO() |
|
30 |
self.w = self._stream.write |
|
31 |
||
32 |
def _render(self): |
|
33 |
raise NotImplementedError |
|
34 |
||
35 |
def render(self, w=None): |
|
36 |
self._initialize_stream(w) |
|
37 |
self._render() |
|
38 |
if w is None: |
|
39 |
return self._stream.getvalue() |
|
40 |
||
41 |
def is_empty(self): |
|
42 |
return False |
|
43 |
||
44 |
||
45 |
class BoxWidget(HTMLWidget): |
|
46 |
def __init__(self, title, id, items=None, _class="boxFrame", |
|
47 |
islist=True, shadow=True, escape=True): |
|
48 |
self.title = title |
|
49 |
self.id = id |
|
50 |
self.items = items or [] |
|
51 |
self._class = _class |
|
52 |
self.islist = islist |
|
53 |
self.shadow = shadow |
|
54 |
self.escape = escape |
|
55 |
||
56 |
def __len__(self): |
|
57 |
return len(self.items) |
|
58 |
||
59 |
def is_empty(self): |
|
60 |
return len(self) == 0 |
|
61 |
||
62 |
def append(self, item): |
|
63 |
self.items.append(item) |
|
64 |
||
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
68 |
title_class = 'boxTitle' |
0 | 69 |
main_div_class = 'boxContent' |
70 |
listing_class = 'boxListing' |
|
1698 | 71 |
|
0 | 72 |
def box_begin_content(self): |
73 |
self.w(u'<div class="%s">\n' % self.main_div_class) |
|
74 |
if self.islist: |
|
75 |
self.w(u'<ul class="%s">' % self.listing_class) |
|
76 |
||
77 |
def box_end_content(self): |
|
78 |
if self.islist: |
|
79 |
self.w(u'</ul>\n') |
|
80 |
self.w(u'</div>\n') |
|
81 |
if self.shadow: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
82 |
self.w(u'<div class="shadow"> </div>') |
1698 | 83 |
|
0 | 84 |
def _render(self): |
85 |
if self.id: |
|
86 |
self.w(u'<div class="%s" id="%s">' % (self._class, self.id)) |
|
87 |
else: |
|
1698 | 88 |
self.w(u'<div class="%s">' % self._class) |
0 | 89 |
if self.title: |
90 |
if self.escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
91 |
title = '<span>%s</span>' % xml_escape(self.title) |
0 | 92 |
else: |
93 |
title = '<span>%s</span>' % self.title |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
94 |
self.w(u'<div class="%s">%s</div>' % (self.title_class, title)) |
0 | 95 |
if self.items: |
96 |
self.box_begin_content() |
|
97 |
for item in self.items: |
|
98 |
item.render(self.w) |
|
99 |
self.box_end_content() |
|
100 |
self.w(u'</div>') |
|
101 |
||
102 |
||
103 |
class SideBoxWidget(BoxWidget): |
|
104 |
"""default CubicWeb's sidebox widget""" |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
105 |
title_class = u'sideBoxTitle' |
0 | 106 |
main_div_class = u'sideBoxBody' |
107 |
listing_class = '' |
|
108 |
||
109 |
def __init__(self, title, id=None): |
|
110 |
super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
|
111 |
shadow=False) |
|
112 |
||
1698 | 113 |
|
0 | 114 |
class MenuWidget(BoxWidget): |
115 |
main_div_class = 'menuContent' |
|
116 |
listing_class = 'menuListing' |
|
117 |
||
118 |
def box_end_content(self): |
|
119 |
if self.islist: |
|
120 |
self.w(u'</ul>\n') |
|
121 |
self.w(u'</div>\n') |
|
1698 | 122 |
|
0 | 123 |
|
124 |
class RawBoxItem(HTMLWidget): |
|
125 |
"""a simpe box item displaying raw data""" |
|
126 |
def __init__(self, label, liclass=None): |
|
127 |
self.label = label |
|
128 |
self.liclass = liclass |
|
129 |
||
130 |
def _start_li(self): |
|
131 |
if self.liclass is None: |
|
132 |
return u'<li>' |
|
133 |
else: |
|
134 |
return u'<li class="%s">' % self.liclass |
|
1698 | 135 |
|
0 | 136 |
return self.label |
1698 | 137 |
|
0 | 138 |
def _render(self): |
139 |
self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
|
140 |
||
141 |
||
142 |
class BoxMenu(RawBoxItem): |
|
143 |
"""a menu in a box""" |
|
144 |
link_class = 'boxMenu' |
|
1698 | 145 |
|
0 | 146 |
def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
147 |
link_class=None): |
|
148 |
super(BoxMenu, self).__init__(label, liclass) |
|
149 |
self.items = items or [] |
|
150 |
self.isitem = isitem |
|
151 |
self.ident = ident or u'boxmenu_%s' % label.replace(' ', '_').replace("'", '') |
|
152 |
if link_class: |
|
153 |
self.link_class = link_class |
|
1698 | 154 |
|
0 | 155 |
def append(self, item): |
156 |
self.items.append(item) |
|
157 |
||
158 |
def _begin_menu(self, ident): |
|
159 |
self.w(u'<ul id="%s" class="hidden">' % ident) |
|
160 |
||
161 |
def _end_menu(self): |
|
162 |
self.w(u'</ul>') |
|
1698 | 163 |
|
0 | 164 |
def _render(self): |
165 |
if self.isitem: |
|
166 |
self.w(self._start_li()) |
|
167 |
ident = self.ident |
|
168 |
self.w(u'<a href="%s" class="%s">%s</a>' % ( |
|
169 |
toggle_action(ident), self.link_class, self.label)) |
|
170 |
self._begin_menu(ident) |
|
171 |
for item in self.items: |
|
172 |
item.render(self.w) |
|
173 |
self._end_menu() |
|
174 |
if self.isitem: |
|
175 |
self.w(u'</li>') |
|
176 |
||
177 |
||
178 |
class PopupBoxMenu(BoxMenu): |
|
179 |
"""like BoxMenu but uses div and specific css class |
|
180 |
in order to behave like a popup menu |
|
181 |
""" |
|
182 |
link_class = 'popupMenu' |
|
183 |
||
184 |
def _begin_menu(self, ident): |
|
185 |
self.w(u'<div class="popupWrapper"><div id="%s" class="hidden popup"><ul>' % ident) |
|
186 |
||
187 |
def _end_menu(self): |
|
188 |
self.w(u'</ul></div></div>') |
|
189 |
||
190 |
||
191 |
class BoxField(HTMLWidget): |
|
192 |
"""couples label / value meant to be displayed in a box""" |
|
193 |
def __init__(self, label, value): |
|
194 |
self.label = label |
|
195 |
self.value = value |
|
1698 | 196 |
|
0 | 197 |
def _render(self): |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
198 |
self.w(u'<li><div><span class="label">%s</span> ' |
1698 | 199 |
u'<span class="value">%s</span></div></li>' |
0 | 200 |
% (self.label, self.value)) |
201 |
||
202 |
class BoxSeparator(HTMLWidget): |
|
203 |
"""a menu separator""" |
|
1698 | 204 |
|
0 | 205 |
def _render(self): |
206 |
self.w(u'</ul><hr class="boxSeparator"/><ul>') |
|
207 |
||
208 |
||
209 |
class BoxLink(HTMLWidget): |
|
210 |
"""a link in a box""" |
|
211 |
def __init__(self, href, label, _class='', title='', ident='', escape=False): |
|
212 |
self.href = href |
|
213 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
214 |
self.label = xml_escape(label) |
0 | 215 |
else: |
216 |
self.label = label |
|
217 |
self._class = _class or '' |
|
218 |
self.title = title |
|
219 |
self.ident = ident |
|
220 |
||
221 |
def _render(self): |
|
222 |
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
|
223 |
xml_escape(self.href), xml_escape(self.title), self.label) |
0 | 224 |
if self.ident: |
225 |
self.w(u'<li id="%s" class="%s">%s</li>\n' % (self.ident, self._class, link)) |
|
226 |
else: |
|
227 |
self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
|
228 |
||
229 |
||
230 |
class BoxHtml(HTMLWidget): |
|
231 |
"""a form in a box""" |
|
232 |
def __init__(self, rawhtml): |
|
233 |
self.rawhtml = rawhtml |
|
234 |
||
235 |
def _render(self): |
|
236 |
self.w(self.rawhtml) |
|
237 |
||
238 |
||
239 |
class TableColumn(object): |
|
240 |
def __init__(self, name, rset_sortcol): |
|
241 |
""" |
|
242 |
:param name: the column's name |
|
243 |
:param rset_sortcol: the model's column used to sort this column view |
|
244 |
""" |
|
245 |
self.name = name |
|
246 |
self.cellrenderers = [] |
|
247 |
self.rset_sortcol = rset_sortcol |
|
248 |
self.cell_attrs = {} |
|
249 |
||
250 |
def append_renderer(self, cellvid, colindex): |
|
251 |
# XXX (adim) : why do we need colindex here ? |
|
252 |
self.cellrenderers.append( (cellvid, colindex) ) |
|
253 |
||
254 |
def add_attr(self, attr, value): |
|
255 |
self.cell_attrs[attr] = value |
|
256 |
||
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
257 |
class SimpleTableModel(object): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
258 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
259 |
uses a list of lists as a storage backend |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
260 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
261 |
NB: the model expectes the cellvid passed to |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
262 |
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
|
263 |
argument and returning a unicode object |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
264 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
265 |
def __init__(self, rows): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
266 |
self._rows = rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
267 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
268 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
269 |
def get_rows(self): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
270 |
return self._rows |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
271 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
272 |
def render_cell(self, cellvid, rowindex, colindex, w): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
273 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
274 |
w(cellvid(value)) |
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 |
@htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
277 |
@jsonize |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
278 |
def sortvalue(self, rowindex, colindex): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
279 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
280 |
if value is None: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
281 |
return u'' |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
282 |
elif isinstance(value, int): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
283 |
return u'%09d'%value |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
284 |
else: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
285 |
return unicode(value) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
286 |
|
0 | 287 |
|
288 |
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
|
289 |
""" |
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
290 |
Display data in a Table with sortable column. |
0 | 291 |
|
3798
d2c27621cc81
[doc] added note about the required js and css in TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3797
diff
changeset
|
292 |
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
|
293 |
|
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
294 |
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
|
295 |
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
|
296 |
""" |
0 | 297 |
highlight = "onmouseover=\"addElementClass(this, 'highlighted');\" " \ |
298 |
"onmouseout=\"removeElementClass(this, 'highlighted');\"" |
|
1698 | 299 |
|
0 | 300 |
def __init__(self, model): |
301 |
self.model = model |
|
302 |
self.columns = [] |
|
303 |
||
304 |
def append_column(self, column): |
|
305 |
""" |
|
306 |
:type column: TableColumn |
|
307 |
""" |
|
308 |
self.columns.append(column) |
|
309 |
||
310 |
def _render(self): |
|
311 |
self.w(u'<table class="listing">') |
|
312 |
self.w(u'<thead>') |
|
313 |
self.w(u'<tr class="header">') |
|
314 |
for column in self.columns: |
|
315 |
attrs = ('%s="%s"' % (name, value) for name, value in column.cell_attrs.iteritems()) |
|
316 |
self.w(u'<th %s>%s</th>' % (' '.join(attrs), column.name)) |
|
317 |
self.w(u'</tr>') |
|
318 |
self.w(u'</thead><tbody>') |
|
319 |
for rowindex, row in enumerate(self.model.get_rows()): |
|
320 |
klass = (rowindex%2==1) and 'odd' or 'even' |
|
321 |
self.w(u'<tr class="%s" %s>' % (klass, self.highlight)) |
|
322 |
for column, sortvalue in self.itercols(rowindex): |
|
323 |
attrs = dict(column.cell_attrs) |
|
324 |
attrs["cubicweb:sortvalue"] = 'json:' + sortvalue |
|
325 |
attrs = ('%s="%s"' % (name, value) for name, value in attrs.iteritems()) |
|
326 |
self.w(u'<td %s>' % (' '.join(attrs))) |
|
327 |
for cellvid, colindex in column.cellrenderers: |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1698
diff
changeset
|
328 |
self.model.render_cell(cellvid, rowindex, colindex, w=self.w) |
0 | 329 |
self.w(u'</td>') |
330 |
self.w(u'</tr>') |
|
331 |
self.w(u'</tbody>') |
|
332 |
self.w(u'</table>') |
|
333 |
||
334 |
def itercols(self, rowindex): |
|
335 |
for column in self.columns: |
|
336 |
yield column, self.model.sortvalue(rowindex, column.rset_sortcol) |
|
337 |
||
338 |
||
339 |
class ProgressBarWidget(HTMLWidget): |
|
340 |
"""display a progress bar widget""" |
|
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
341 |
precision = 0.1 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
342 |
red_threshold = 1.1 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
343 |
orange_threshold = 1.05 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
344 |
yellow_threshold = 1 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
345 |
|
0 | 346 |
def __init__(self, done, todo, total): |
347 |
self.done = done |
|
348 |
self.todo = todo |
|
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
349 |
self.budget = total |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
350 |
|
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
351 |
@property |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
352 |
def overrun(self): |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
353 |
"""overrun = done + todo - """ |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
354 |
if self.done + self.todo > self.budget: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
355 |
overrun = self.done + self.todo - self.budget |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
356 |
else: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
357 |
overrun = 0 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
358 |
if overrun < self.precision: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
359 |
overrun = 0 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
360 |
return overrun |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
361 |
|
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
362 |
@property |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
363 |
def overrun_percentage(self): |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
364 |
"""pourcentage overrun = overrun / budget""" |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
365 |
if self.budget == 0: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
366 |
return 0 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
367 |
else: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
368 |
return self.overrun * 100. / self.budget |
0 | 369 |
|
370 |
def _render(self): |
|
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
371 |
done = self.done |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
372 |
todo = self.todo |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
373 |
budget = self.budget |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
374 |
if budget == 0: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
375 |
pourcent = 100 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
376 |
todo_pourcent = 0 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
377 |
else: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
378 |
pourcent = done*100./budget |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
379 |
todo_pourcent = min(todo*100./budget, 100-pourcent) |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
380 |
bar_pourcent = pourcent |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
381 |
if pourcent > 100.1: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
382 |
color = 'red' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
383 |
bar_pourcent = 100 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
384 |
elif todo+done > self.red_threshold*budget: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
385 |
color = 'red' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
386 |
elif todo+done > self.orange_threshold*budget: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
387 |
color = 'orange' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
388 |
elif todo+done > self.yellow_threshold*budget: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
389 |
color = 'yellow' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
390 |
else: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
391 |
color = 'green' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
392 |
if pourcent < 0: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
393 |
pourcent = 0 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
394 |
|
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
395 |
if floor(done) == done or done>100: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
396 |
done_str = '%i' % done |
0 | 397 |
else: |
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
398 |
done_str = '%.1f' % done |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
399 |
if floor(budget) == budget or budget>100: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
400 |
budget_str = '%i' % budget |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
401 |
else: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
402 |
budget_str = '%.1f' % budget |
213
6842c3dee34b
adding files (formely appearing in jpl) specific to cubicweb
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
203
diff
changeset
|
403 |
|
4211
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
404 |
title = u'%s/%s = %i%%' % (done_str, budget_str, pourcent) |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
405 |
short_title = title |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
406 |
if self.overrun_percentage: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
407 |
title += u' overrun +%sj (+%i%%)' % (self.overrun, |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
408 |
self.overrun_percentage) |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
409 |
overrun = self.overrun |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
410 |
if floor(overrun) == overrun or overrun>100: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
411 |
overrun_str = '%i' % overrun |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
412 |
else: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
413 |
overrun_str = '%.1f' % overrun |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
414 |
short_title += u' +%s' % overrun_str |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
415 |
# write bars |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
416 |
maxi = max(done+todo, budget) |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
417 |
if maxi == 0: |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
418 |
maxi = 1 |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
419 |
|
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
420 |
cid = random.randint(0, 100000) |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
421 |
self.w(u'%s<br/>' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
422 |
u'<canvas class="progressbar" id="canvas%i" width="100" height="10"></canvas>' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
423 |
u'<script type="application/x-javascript">' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
424 |
u'draw_progressbar("canvas%i", %i, %i, %i, "%s");</script>' |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
425 |
% (short_title.replace(' ',' '), cid, cid, |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
426 |
int(100.*done/maxi), int(100.*(done+todo)/maxi), |
eac4b23a42ff
[views] backport progressbar template from shingouz
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4183
diff
changeset
|
427 |
int(100.*budget/maxi), color)) |