8 __docformat__ = "restructuredtext en" |
8 __docformat__ = "restructuredtext en" |
9 |
9 |
10 from datetime import date |
10 from datetime import date |
11 from warnings import warn |
11 from warnings import warn |
12 |
12 |
13 from cubicweb.common import tags |
13 from cubicweb.common import tags, uilib |
14 from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE |
14 from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE |
15 |
15 |
16 |
16 |
17 class FieldWidget(object): |
17 class FieldWidget(object): |
18 """abstract widget class""" |
18 """abstract widget class""" |
188 name, curvalues, attrs = self._render_attrs(form, field) |
188 name, curvalues, attrs = self._render_attrs(form, field) |
189 if not 'size' in attrs: |
189 if not 'size' in attrs: |
190 attrs['size'] = self._multiple and '5' or '1' |
190 attrs['size'] = self._multiple and '5' or '1' |
191 options = [] |
191 options = [] |
192 optgroup_opened = False |
192 optgroup_opened = False |
193 for label, value in field.vocabulary(form): |
193 for option in field.vocabulary(form): |
|
194 try: |
|
195 label, value, oattrs = option |
|
196 except ValueError: |
|
197 label, value = option |
|
198 oattrs = {} |
194 if value is None: |
199 if value is None: |
195 # handle separator |
200 # handle separator |
196 if optgroup_opened: |
201 if optgroup_opened: |
197 options.append(u'</optgroup>') |
202 options.append(u'</optgroup>') |
198 options.append(u'<optgroup label="%s">' % (label or '')) |
203 oattrs.setdefault('label', label or '') |
|
204 options.append(u'<optgroup %s>' % uilib.sgml_attributes(oattrs)) |
199 optgroup_opened = True |
205 optgroup_opened = True |
200 elif value in curvalues: |
206 elif value in curvalues: |
201 options.append(tags.option(label, value=value, selected='selected')) |
207 options.append(tags.option(label, value=value, |
|
208 selected='selected', **oattrs)) |
202 else: |
209 else: |
203 options.append(tags.option(label, value=value)) |
210 options.append(tags.option(label, value=value, **oattrs)) |
204 if optgroup_opened: |
211 if optgroup_opened: |
205 options.append(u'</optgroup>') |
212 options.append(u'</optgroup>') |
206 return tags.select(name=name, multiple=self._multiple, |
213 return tags.select(name=name, multiple=self._multiple, |
207 options=options, **attrs) |
214 options=options, **attrs) |
208 |
215 |
217 def render(self, form, field): |
224 def render(self, form, field): |
218 name, curvalues, attrs = self._render_attrs(form, field) |
225 name, curvalues, attrs = self._render_attrs(form, field) |
219 domid = attrs.pop('id', None) |
226 domid = attrs.pop('id', None) |
220 sep = attrs.pop('separator', u'<br/>') |
227 sep = attrs.pop('separator', u'<br/>') |
221 options = [] |
228 options = [] |
222 for i, (label, value) in enumerate(field.vocabulary(form)): |
229 for i, option in enumerate(field.vocabulary(form)): |
|
230 try: |
|
231 label, value, oattrs = option |
|
232 except ValueError: |
|
233 label, value = option |
|
234 oattrs = {} |
223 iattrs = attrs.copy() |
235 iattrs = attrs.copy() |
|
236 iattrs.update(oattrs) |
224 if i == 0 and domid is not None: |
237 if i == 0 and domid is not None: |
225 iattrs['id'] = domid |
238 iattrs.setdefault('id', domid) |
226 if value in curvalues: |
239 if value in curvalues: |
227 iattrs['checked'] = u'checked' |
240 iattrs['checked'] = u'checked' |
228 tag = tags.input(name=name, type=self.type, value=value, **iattrs) |
241 tag = tags.input(name=name, type=self.type, value=value, **iattrs) |
229 options.append(tag + label + sep) |
242 options.append(tag + label + sep) |
230 return '\n'.join(options) |
243 return '\n'.join(options) |