23 |
23 |
24 import random |
24 import random |
25 from math import floor |
25 from math import floor |
26 |
26 |
27 from logilab.mtconverter import xml_escape |
27 from logilab.mtconverter import xml_escape |
|
28 from logilab.common.deprecation import class_deprecated |
28 |
29 |
29 from cubicweb.utils import UStringIO |
30 from cubicweb.utils import UStringIO |
30 from cubicweb.uilib import toggle_action, htmlescape |
31 from cubicweb.uilib import toggle_action, htmlescape |
31 from cubicweb.web import jsonize |
32 from cubicweb.web import jsonize |
|
33 from cubicweb.web.component import _bwcompatible_render_item |
32 |
34 |
33 # XXX HTMLWidgets should have access to req (for datadir / static urls, |
35 # XXX HTMLWidgets should have access to req (for datadir / static urls, |
34 # i18n strings, etc.) |
36 # i18n strings, etc.) |
35 class HTMLWidget(object): |
37 class HTMLWidget(object): |
36 |
38 |
52 |
54 |
53 def is_empty(self): |
55 def is_empty(self): |
54 return False |
56 return False |
55 |
57 |
56 |
58 |
57 class BoxWidget(HTMLWidget): |
59 class BoxWidget(HTMLWidget): # XXX Deprecated |
|
60 |
58 def __init__(self, title, id, items=None, _class="boxFrame", |
61 def __init__(self, title, id, items=None, _class="boxFrame", |
59 islist=True, shadow=True, escape=True): |
62 islist=True, shadow=True, escape=True): |
60 self.title = title |
63 self.title = title |
61 self.id = id |
64 self.id = id |
62 self.items = items or [] |
65 self.items = items or [] |
105 title = '<span>%s</span>' % self.title |
108 title = '<span>%s</span>' % self.title |
106 self.w(u'<div class="%s">%s</div>' % (self.title_class, title)) |
109 self.w(u'<div class="%s">%s</div>' % (self.title_class, title)) |
107 if self.items: |
110 if self.items: |
108 self.box_begin_content() |
111 self.box_begin_content() |
109 for item in self.items: |
112 for item in self.items: |
110 if hasattr(item, 'render'): |
113 _bwcompatible_render_item(self.w, item) |
111 item.render(self.w) |
|
112 else: |
|
113 self.w(u'<li>%s</li>' % item) |
|
114 self.box_end_content() |
114 self.box_end_content() |
115 self.w(u'</div>') |
115 self.w(u'</div>') |
116 |
116 |
117 |
117 |
118 class SideBoxWidget(BoxWidget): |
118 class SideBoxWidget(BoxWidget): |
119 """default CubicWeb's sidebox widget""" |
119 """default CubicWeb's sidebox widget""" |
|
120 __metaclass__ = class_deprecated |
|
121 __deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
|
122 |
120 title_class = u'sideBoxTitle' |
123 title_class = u'sideBoxTitle' |
121 main_div_class = u'sideBoxBody' |
124 main_div_class = u'sideBoxBody' |
122 listing_class = '' |
125 listing_class = '' |
123 |
126 |
124 def __init__(self, title, id=None): |
127 def __init__(self, title, id=None): |
125 super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
128 super(SideBoxWidget, self).__init__(title, id=id, _class='sideBox', |
126 shadow=False) |
129 shadow=False) |
127 |
130 |
128 |
131 |
129 class MenuWidget(BoxWidget): |
132 class MenuWidget(BoxWidget): |
|
133 |
130 main_div_class = 'menuContent' |
134 main_div_class = 'menuContent' |
131 listing_class = 'menuListing' |
135 listing_class = 'menuListing' |
132 |
136 |
133 def box_end_content(self): |
137 def box_end_content(self): |
134 if self.islist: |
138 if self.islist: |
135 self.w(u'</ul>\n') |
139 self.w(u'</ul>\n') |
136 self.w(u'</div>\n') |
140 self.w(u'</div>\n') |
137 |
141 |
138 |
142 |
139 class RawBoxItem(HTMLWidget): |
143 class RawBoxItem(HTMLWidget): # XXX deprecated |
140 """a simpe box item displaying raw data""" |
144 """a simpe box item displaying raw data""" |
|
145 |
141 def __init__(self, label, liclass=None): |
146 def __init__(self, label, liclass=None): |
142 self.label = label |
147 self.label = label |
143 self.liclass = liclass |
148 self.liclass = liclass |
144 |
149 |
145 def _start_li(self): |
150 def _start_li(self): |
154 self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
159 self.w(u'%s%s</li>' % (self._start_li(), self.label)) |
155 |
160 |
156 |
161 |
157 class BoxMenu(RawBoxItem): |
162 class BoxMenu(RawBoxItem): |
158 """a menu in a box""" |
163 """a menu in a box""" |
|
164 |
159 link_class = 'boxMenu' |
165 link_class = 'boxMenu' |
160 |
166 |
161 def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
167 def __init__(self, label, items=None, isitem=True, liclass=None, ident=None, |
162 link_class=None): |
168 link_class=None): |
163 super(BoxMenu, self).__init__(label, liclass) |
169 super(BoxMenu, self).__init__(label, liclass) |
182 ident = self.ident |
188 ident = self.ident |
183 self.w(u'<a href="%s" class="%s">%s</a>' % ( |
189 self.w(u'<a href="%s" class="%s">%s</a>' % ( |
184 toggle_action(ident), self.link_class, self.label)) |
190 toggle_action(ident), self.link_class, self.label)) |
185 self._begin_menu(ident) |
191 self._begin_menu(ident) |
186 for item in self.items: |
192 for item in self.items: |
187 if hasattr(item, 'render'): |
193 _bwcompatible_render_item(self.w, item) |
188 item.render(self.w) |
|
189 else: |
|
190 self.w(u'<li>%s</li>' % item) |
|
191 self._end_menu() |
194 self._end_menu() |
192 if self.isitem: |
195 if self.isitem: |
193 self.w(u'</li>') |
196 self.w(u'</li>') |
194 |
197 |
195 |
198 |
206 self.w(u'</ul></div></div>') |
209 self.w(u'</ul></div></div>') |
207 |
210 |
208 |
211 |
209 class BoxField(HTMLWidget): |
212 class BoxField(HTMLWidget): |
210 """couples label / value meant to be displayed in a box""" |
213 """couples label / value meant to be displayed in a box""" |
|
214 __metaclass__ = class_deprecated |
|
215 __deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
211 def __init__(self, label, value): |
216 def __init__(self, label, value): |
212 self.label = label |
217 self.label = label |
213 self.value = value |
218 self.value = value |
214 |
219 |
215 def _render(self): |
220 def _render(self): |
217 u'<span class="value">%s</span></div></li>' |
222 u'<span class="value">%s</span></div></li>' |
218 % (self.label, self.value)) |
223 % (self.label, self.value)) |
219 |
224 |
220 class BoxSeparator(HTMLWidget): |
225 class BoxSeparator(HTMLWidget): |
221 """a menu separator""" |
226 """a menu separator""" |
|
227 __metaclass__ = class_deprecated |
|
228 __deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
222 |
229 |
223 def _render(self): |
230 def _render(self): |
224 self.w(u'</ul><hr class="boxSeparator"/><ul>') |
231 self.w(u'</ul><hr class="boxSeparator"/><ul>') |
225 |
232 |
226 |
233 |
227 class BoxLink(HTMLWidget): |
234 class BoxLink(HTMLWidget): |
228 """a link in a box""" |
235 """a link in a box""" |
|
236 __metaclass__ = class_deprecated |
|
237 __deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
229 def __init__(self, href, label, _class='', title='', ident='', escape=False): |
238 def __init__(self, href, label, _class='', title='', ident='', escape=False): |
230 self.href = href |
239 self.href = href |
231 if escape: |
240 if escape: |
232 self.label = xml_escape(label) |
241 self.label = xml_escape(label) |
233 else: |
242 else: |
245 self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
254 self.w(u'<li class="%s">%s</li>\n' % (self._class, link)) |
246 |
255 |
247 |
256 |
248 class BoxHtml(HTMLWidget): |
257 class BoxHtml(HTMLWidget): |
249 """a form in a box""" |
258 """a form in a box""" |
|
259 __metaclass__ = class_deprecated |
|
260 __deprecation_warning__ = '[3.10] class %(cls)s is deprecated' |
250 def __init__(self, rawhtml): |
261 def __init__(self, rawhtml): |
251 self.rawhtml = rawhtml |
262 self.rawhtml = rawhtml |
252 |
263 |
253 def _render(self): |
264 def _render(self): |
254 self.w(self.rawhtml) |
265 self.w(self.rawhtml) |
270 self.cellrenderers.append( (cellvid, colindex) ) |
281 self.cellrenderers.append( (cellvid, colindex) ) |
271 |
282 |
272 def add_attr(self, attr, value): |
283 def add_attr(self, attr, value): |
273 self.cell_attrs[attr] = value |
284 self.cell_attrs[attr] = value |
274 |
285 |
|
286 |
275 class SimpleTableModel(object): |
287 class SimpleTableModel(object): |
276 """ |
288 """ |
277 uses a list of lists as a storage backend |
289 uses a list of lists as a storage backend |
278 |
290 |
279 NB: the model expectes the cellvid passed to |
291 NB: the model expectes the cellvid passed to |
280 TableColumn.append_renderer to be a callable accepting a single |
292 TableColumn.append_renderer to be a callable accepting a single |
281 argument and returning a unicode object |
293 argument and returning a unicode object |
282 """ |
294 """ |
283 def __init__(self, rows): |
295 def __init__(self, rows): |
284 self._rows = rows |
296 self._rows = rows |
285 |
|
286 |
297 |
287 def get_rows(self): |
298 def get_rows(self): |
288 return self._rows |
299 return self._rows |
289 |
300 |
290 def render_cell(self, cellvid, rowindex, colindex, w): |
301 def render_cell(self, cellvid, rowindex, colindex, w): |