author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Thu, 05 Nov 2009 12:41:58 +0100 | |
branch | stable |
changeset 3796 | f4e924106fdc |
parent 2996 | 866a2c135c33 |
child 3797 | af9b0e113108 |
permissions | -rw-r--r-- |
0 | 1 |
"""html widgets |
2 |
||
3 |
those are in cubicweb.common since we need to know available widgets at schema |
|
4 |
serialization time |
|
5 |
||
6 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
7 |
:copyright: 2001-2009 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 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
12 |
from logilab.mtconverter import xml_escape |
0 | 13 |
|
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
843
diff
changeset
|
14 |
from cubicweb.utils import UStringIO |
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
15 |
from cubicweb.common.uilib import toggle_action, limitsize, htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
16 |
from cubicweb.web import jsonize |
0 | 17 |
|
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
|
18 |
# 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
|
19 |
# i18n strings, etc.) |
0 | 20 |
class HTMLWidget(object): |
21 |
||
22 |
def _initialize_stream(self, w=None): |
|
23 |
if w: |
|
24 |
self.w = w |
|
25 |
else: |
|
26 |
self._stream = UStringIO() |
|
27 |
self.w = self._stream.write |
|
28 |
||
29 |
def _render(self): |
|
30 |
raise NotImplementedError |
|
31 |
||
32 |
def render(self, w=None): |
|
33 |
self._initialize_stream(w) |
|
34 |
self._render() |
|
35 |
if w is None: |
|
36 |
return self._stream.getvalue() |
|
37 |
||
38 |
def is_empty(self): |
|
39 |
return False |
|
40 |
||
41 |
||
42 |
class BoxWidget(HTMLWidget): |
|
43 |
def __init__(self, title, id, items=None, _class="boxFrame", |
|
44 |
islist=True, shadow=True, escape=True): |
|
45 |
self.title = title |
|
46 |
self.id = id |
|
47 |
self.items = items or [] |
|
48 |
self._class = _class |
|
49 |
self.islist = islist |
|
50 |
self.shadow = shadow |
|
51 |
self.escape = escape |
|
52 |
||
53 |
def __len__(self): |
|
54 |
return len(self.items) |
|
55 |
||
56 |
def is_empty(self): |
|
57 |
return len(self) == 0 |
|
58 |
||
59 |
def append(self, item): |
|
60 |
self.items.append(item) |
|
61 |
||
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
65 |
title_class = 'boxTitle' |
0 | 66 |
main_div_class = 'boxContent' |
67 |
listing_class = 'boxListing' |
|
1698 | 68 |
|
0 | 69 |
def box_begin_content(self): |
70 |
self.w(u'<div class="%s">\n' % self.main_div_class) |
|
71 |
if self.islist: |
|
72 |
self.w(u'<ul class="%s">' % self.listing_class) |
|
73 |
||
74 |
def box_end_content(self): |
|
75 |
if self.islist: |
|
76 |
self.w(u'</ul>\n') |
|
77 |
self.w(u'</div>\n') |
|
78 |
if self.shadow: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
79 |
self.w(u'<div class="shadow"> </div>') |
1698 | 80 |
|
0 | 81 |
def _render(self): |
82 |
if self.id: |
|
83 |
self.w(u'<div class="%s" id="%s">' % (self._class, self.id)) |
|
84 |
else: |
|
1698 | 85 |
self.w(u'<div class="%s">' % self._class) |
0 | 86 |
if self.title: |
87 |
if self.escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
88 |
title = '<span>%s</span>' % xml_escape(self.title) |
0 | 89 |
else: |
90 |
title = '<span>%s</span>' % self.title |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
91 |
self.w(u'<div class="%s">%s</div>' % (self.title_class, title)) |
0 | 92 |
if self.items: |
93 |
self.box_begin_content() |
|
94 |
for item in self.items: |
|
95 |
item.render(self.w) |
|
96 |
self.box_end_content() |
|
97 |
self.w(u'</div>') |
|
98 |
||
99 |
||
100 |
class SideBoxWidget(BoxWidget): |
|
101 |
"""default CubicWeb's sidebox widget""" |
|
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1730
diff
changeset
|
102 |
title_class = u'sideBoxTitle' |
0 | 103 |
main_div_class = u'sideBoxBody' |
104 |
listing_class = '' |
|
105 |
||
106 |
def __init__(self, title, id=None): |
|
107 |
super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
|
108 |
shadow=False) |
|
109 |
||
1698 | 110 |
|
0 | 111 |
class MenuWidget(BoxWidget): |
112 |
main_div_class = 'menuContent' |
|
113 |
listing_class = 'menuListing' |
|
114 |
||
115 |
def box_end_content(self): |
|
116 |
if self.islist: |
|
117 |
self.w(u'</ul>\n') |
|
118 |
self.w(u'</div>\n') |
|
1698 | 119 |
|
0 | 120 |
|
121 |
class RawBoxItem(HTMLWidget): |
|
122 |
"""a simpe box item displaying raw data""" |
|
123 |
def __init__(self, label, liclass=None): |
|
124 |
self.label = label |
|
125 |
self.liclass = liclass |
|
126 |
||
127 |
def _start_li(self): |
|
128 |
if self.liclass is None: |
|
129 |
return u'<li>' |
|
130 |
else: |
|
131 |
return u'<li class="%s">' % self.liclass |
|
1698 | 132 |
|
0 | 133 |
return self.label |
1698 | 134 |
|
0 | 135 |
def _render(self): |
136 |
self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
|
137 |
||
138 |
||
139 |
class BoxMenu(RawBoxItem): |
|
140 |
"""a menu in a box""" |
|
141 |
link_class = 'boxMenu' |
|
1698 | 142 |
|
0 | 143 |
def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
144 |
link_class=None): |
|
145 |
super(BoxMenu, self).__init__(label, liclass) |
|
146 |
self.items = items or [] |
|
147 |
self.isitem = isitem |
|
148 |
self.ident = ident or u'boxmenu_%s' % label.replace(' ', '_').replace("'", '') |
|
149 |
if link_class: |
|
150 |
self.link_class = link_class |
|
1698 | 151 |
|
0 | 152 |
def append(self, item): |
153 |
self.items.append(item) |
|
154 |
||
155 |
def _begin_menu(self, ident): |
|
156 |
self.w(u'<ul id="%s" class="hidden">' % ident) |
|
157 |
||
158 |
def _end_menu(self): |
|
159 |
self.w(u'</ul>') |
|
1698 | 160 |
|
0 | 161 |
def _render(self): |
162 |
if self.isitem: |
|
163 |
self.w(self._start_li()) |
|
164 |
ident = self.ident |
|
165 |
self.w(u'<a href="%s" class="%s">%s</a>' % ( |
|
166 |
toggle_action(ident), self.link_class, self.label)) |
|
167 |
self._begin_menu(ident) |
|
168 |
for item in self.items: |
|
169 |
item.render(self.w) |
|
170 |
self._end_menu() |
|
171 |
if self.isitem: |
|
172 |
self.w(u'</li>') |
|
173 |
||
174 |
||
175 |
class PopupBoxMenu(BoxMenu): |
|
176 |
"""like BoxMenu but uses div and specific css class |
|
177 |
in order to behave like a popup menu |
|
178 |
""" |
|
179 |
link_class = 'popupMenu' |
|
180 |
||
181 |
def _begin_menu(self, ident): |
|
182 |
self.w(u'<div class="popupWrapper"><div id="%s" class="hidden popup"><ul>' % ident) |
|
183 |
||
184 |
def _end_menu(self): |
|
185 |
self.w(u'</ul></div></div>') |
|
186 |
||
187 |
||
188 |
class BoxField(HTMLWidget): |
|
189 |
"""couples label / value meant to be displayed in a box""" |
|
190 |
def __init__(self, label, value): |
|
191 |
self.label = label |
|
192 |
self.value = value |
|
1698 | 193 |
|
0 | 194 |
def _render(self): |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
195 |
self.w(u'<li><div><span class="label">%s</span> ' |
1698 | 196 |
u'<span class="value">%s</span></div></li>' |
0 | 197 |
% (self.label, self.value)) |
198 |
||
199 |
class BoxSeparator(HTMLWidget): |
|
200 |
"""a menu separator""" |
|
1698 | 201 |
|
0 | 202 |
def _render(self): |
203 |
self.w(u'</ul><hr class="boxSeparator"/><ul>') |
|
204 |
||
205 |
||
206 |
class BoxLink(HTMLWidget): |
|
207 |
"""a link in a box""" |
|
208 |
def __init__(self, href, label, _class='', title='', ident='', escape=False): |
|
209 |
self.href = href |
|
210 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
211 |
self.label = xml_escape(label) |
0 | 212 |
else: |
213 |
self.label = label |
|
214 |
self._class = _class or '' |
|
215 |
self.title = title |
|
216 |
self.ident = ident |
|
217 |
||
218 |
def _render(self): |
|
219 |
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
|
220 |
xml_escape(self.href), xml_escape(self.title), self.label) |
0 | 221 |
if self.ident: |
222 |
self.w(u'<li id="%s" class="%s">%s</li>\n' % (self.ident, self._class, link)) |
|
223 |
else: |
|
224 |
self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
|
225 |
||
226 |
||
227 |
class BoxHtml(HTMLWidget): |
|
228 |
"""a form in a box""" |
|
229 |
def __init__(self, rawhtml): |
|
230 |
self.rawhtml = rawhtml |
|
231 |
||
232 |
def _render(self): |
|
233 |
self.w(self.rawhtml) |
|
234 |
||
235 |
||
236 |
class TableColumn(object): |
|
237 |
def __init__(self, name, rset_sortcol): |
|
238 |
""" |
|
239 |
:param name: the column's name |
|
240 |
:param rset_sortcol: the model's column used to sort this column view |
|
241 |
""" |
|
242 |
self.name = name |
|
243 |
self.cellrenderers = [] |
|
244 |
self.rset_sortcol = rset_sortcol |
|
245 |
self.cell_attrs = {} |
|
246 |
||
247 |
def append_renderer(self, cellvid, colindex): |
|
248 |
# XXX (adim) : why do we need colindex here ? |
|
249 |
self.cellrenderers.append( (cellvid, colindex) ) |
|
250 |
||
251 |
def add_attr(self, attr, value): |
|
252 |
self.cell_attrs[attr] = value |
|
253 |
||
3796
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
254 |
class SimpleTableModel(object): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
255 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
256 |
uses a list of lists as a storage backend |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
257 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
258 |
NB: the model expectes the cellvid passed to |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
259 |
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
|
260 |
argument and returning a unicode object |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
261 |
""" |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
262 |
def __init__(self, rows): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
263 |
self._rows = rows |
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 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
266 |
def get_rows(self): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
267 |
return self._rows |
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 render_cell(self, cellvid, rowindex, colindex, w): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
270 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
271 |
w(cellvid(value)) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
272 |
|
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
273 |
@htmlescape |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
274 |
@jsonize |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
275 |
def sortvalue(self, rowindex, colindex): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
276 |
value = self._rows[rowindex][colindex] |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
277 |
if value is None: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
278 |
return u'' |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
279 |
elif isinstance(value, int): |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
280 |
return u'%09d'%value |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
281 |
else: |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
282 |
return unicode(value) |
f4e924106fdc
added SimpleModel for TableWidget
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
2996
diff
changeset
|
283 |
|
0 | 284 |
|
285 |
class TableWidget(HTMLWidget): |
|
286 |
||
287 |
highlight = "onmouseover=\"addElementClass(this, 'highlighted');\" " \ |
|
288 |
"onmouseout=\"removeElementClass(this, 'highlighted');\"" |
|
1698 | 289 |
|
0 | 290 |
def __init__(self, model): |
291 |
self.model = model |
|
292 |
self.columns = [] |
|
293 |
||
294 |
def append_column(self, column): |
|
295 |
""" |
|
296 |
:type column: TableColumn |
|
297 |
""" |
|
298 |
self.columns.append(column) |
|
299 |
||
300 |
def _render(self): |
|
301 |
self.w(u'<table class="listing">') |
|
302 |
self.w(u'<thead>') |
|
303 |
self.w(u'<tr class="header">') |
|
304 |
for column in self.columns: |
|
305 |
attrs = ('%s="%s"' % (name, value) for name, value in column.cell_attrs.iteritems()) |
|
306 |
self.w(u'<th %s>%s</th>' % (' '.join(attrs), column.name)) |
|
307 |
self.w(u'</tr>') |
|
308 |
self.w(u'</thead><tbody>') |
|
309 |
for rowindex, row in enumerate(self.model.get_rows()): |
|
310 |
klass = (rowindex%2==1) and 'odd' or 'even' |
|
311 |
self.w(u'<tr class="%s" %s>' % (klass, self.highlight)) |
|
312 |
for column, sortvalue in self.itercols(rowindex): |
|
313 |
attrs = dict(column.cell_attrs) |
|
314 |
attrs["cubicweb:sortvalue"] = 'json:' + sortvalue |
|
315 |
attrs = ('%s="%s"' % (name, value) for name, value in attrs.iteritems()) |
|
316 |
self.w(u'<td %s>' % (' '.join(attrs))) |
|
317 |
for cellvid, colindex in column.cellrenderers: |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1698
diff
changeset
|
318 |
self.model.render_cell(cellvid, rowindex, colindex, w=self.w) |
0 | 319 |
self.w(u'</td>') |
320 |
self.w(u'</tr>') |
|
321 |
self.w(u'</tbody>') |
|
322 |
self.w(u'</table>') |
|
323 |
||
324 |
def itercols(self, rowindex): |
|
325 |
for column in self.columns: |
|
326 |
yield column, self.model.sortvalue(rowindex, column.rset_sortcol) |
|
327 |
||
328 |
||
329 |
class ProgressBarWidget(HTMLWidget): |
|
330 |
"""display a progress bar widget""" |
|
331 |
def __init__(self, done, todo, total): |
|
332 |
self.done = done |
|
333 |
self.todo = todo |
|
334 |
self.total = total |
|
335 |
||
336 |
def _render(self): |
|
337 |
try: |
|
338 |
pourcent = self.done*100./self.total |
|
339 |
except ZeroDivisionError: |
|
340 |
pourcent = 0 |
|
341 |
real_pourcent = pourcent |
|
342 |
if pourcent > 100 : |
|
343 |
color = 'done' |
|
344 |
pourcent = 100 |
|
345 |
elif self.todo + self.done > self.total : |
|
346 |
color = 'overpassed' |
|
347 |
else: |
|
348 |
color = 'inprogress' |
|
349 |
if pourcent < 0: |
|
350 |
pourcent = 0 |
|
351 |
self.w(u'<div class="progressbarback" title="%i %%">' % real_pourcent) |
|
352 |
self.w(u'<div class="progressbar %s" style="width: %spx; align: left;" ></div>' % (color, pourcent)) |
|
353 |
self.w(u'</div>') |
|
213
6842c3dee34b
adding files (formely appearing in jpl) specific to cubicweb
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
203
diff
changeset
|
354 |