web/formwidgets.py
changeset 8029 805d4e121b65
parent 7990 a673d1d9a738
child 8193 0c64d6b75303
equal deleted inserted replaced
8028:58e9bc8a1f2c 8029:805d4e121b65
    73 Other widgets
    73 Other widgets
    74 '''''''''''''
    74 '''''''''''''
    75 
    75 
    76 .. autoclass:: cubicweb.web.formwidgets.PasswordInput
    76 .. autoclass:: cubicweb.web.formwidgets.PasswordInput
    77 .. autoclass:: cubicweb.web.formwidgets.IntervalWidget
    77 .. autoclass:: cubicweb.web.formwidgets.IntervalWidget
       
    78 .. autoclass:: cubicweb.web.formwidgets.BitSelect
    78 .. autoclass:: cubicweb.web.formwidgets.HorizontalLayoutWidget
    79 .. autoclass:: cubicweb.web.formwidgets.HorizontalLayoutWidget
    79 .. autoclass:: cubicweb.web.formwidgets.EditableURLWidget
    80 .. autoclass:: cubicweb.web.formwidgets.EditableURLWidget
    80 
    81 
    81 
    82 
    82 Form controls
    83 Form controls
   450                 if optgroup_opened:
   451                 if optgroup_opened:
   451                     options.append(u'</optgroup>')
   452                     options.append(u'</optgroup>')
   452                 oattrs.setdefault('label', label or '')
   453                 oattrs.setdefault('label', label or '')
   453                 options.append(u'<optgroup %s>' % uilib.sgml_attributes(oattrs))
   454                 options.append(u'<optgroup %s>' % uilib.sgml_attributes(oattrs))
   454                 optgroup_opened = True
   455                 optgroup_opened = True
   455             elif value in curvalues:
   456             elif self.value_selected(value, curvalues):
   456                 options.append(tags.option(label, value=value,
   457                 options.append(tags.option(label, value=value,
   457                                            selected='selected', **oattrs))
   458                                            selected='selected', **oattrs))
   458             else:
   459             else:
   459                 options.append(tags.option(label, value=value, **oattrs))
   460                 options.append(tags.option(label, value=value, **oattrs))
   460         if optgroup_opened:
   461         if optgroup_opened:
   465             else:
   466             else:
   466                 size = u'1'
   467                 size = u'1'
   467             attrs['size'] = size
   468             attrs['size'] = size
   468         return tags.select(name=field.input_name(form, self.suffix),
   469         return tags.select(name=field.input_name(form, self.suffix),
   469                            multiple=self._multiple, options=options, **attrs)
   470                            multiple=self._multiple, options=options, **attrs)
       
   471 
       
   472     def value_selected(self, value, curvalues):
       
   473         return value in curvalues
       
   474 
       
   475 
       
   476 class BitSelect(Select):
       
   477     """Select widget for IntField using a vocabulary with bit masks as values.
       
   478 
       
   479     See also :class:`~cubicweb.web.facet.BitFieldFacet`.
       
   480     """
       
   481     def __init__(self, attrs=None, multiple=True, **kwargs):
       
   482         super(BitSelect, self).__init__(attrs, multiple=multiple, **kwargs)
       
   483 
       
   484     def value_selected(self, value, curvalues):
       
   485         mask = reduce(lambda x, y: int(x) | int(y), curvalues, 0)
       
   486         return int(value) & mask
       
   487 
       
   488     def process_field_data(self, form, field):
       
   489         """Return process posted value(s) for widget and return something
       
   490         understandable by the associated `field`. That value may be correctly
       
   491         typed or a string that the field may parse.
       
   492         """
       
   493         val = super(BitSelect, self).process_field_data(form, field)
       
   494         if isinstance(val, list):
       
   495             val = reduce(lambda x, y: int(x) | int(y), val, 0)
       
   496         elif val:
       
   497             val = int(val)
       
   498         else:
       
   499             val = 0
       
   500         return val
   470 
   501 
   471 
   502 
   472 class CheckBox(Input):
   503 class CheckBox(Input):
   473     """Simple <input type='checkbox'>, for field having a specific
   504     """Simple <input type='checkbox'>, for field having a specific
   474     vocabulary. One input will be generated for each possible value.
   505     vocabulary. One input will be generated for each possible value.