core (XMLUI), template(XMLUI): added flags to ListWidget:
following flags have been added: - noselect: list selection is not allowed (options are the important data) - extensible: user can add elements to the list - reducible: user can remove elements from the list - inline: presentation hint: the list should be displayed horizontally
This commit is contained in:
parent
55a5b603fd
commit
3ccb4c1038
1 changed files with 33 additions and 5 deletions
|
|
@ -350,6 +350,7 @@ def paramsXML2XMLUI(xml):
|
|||
options, selected = _paramsGetListOptions(param)
|
||||
widget_kwargs['options'] = options
|
||||
widget_kwargs['selected'] = selected
|
||||
widget_kwargs['styles'] = ['extensible']
|
||||
elif type_ == 'jids_list':
|
||||
widget_kwargs['jids'] = _paramsGetListJids(param)
|
||||
|
||||
|
|
@ -939,6 +940,7 @@ class ButtonWidget(Widget):
|
|||
|
||||
class ListWidget(InputWidget):
|
||||
type = 'list'
|
||||
STYLES = (u'multi', u'noselect', u'extensible', u'reducible', u'inline')
|
||||
|
||||
def __init__(self, xmlui, options, selected=None, styles=None, name=None, parent=None):
|
||||
"""
|
||||
|
|
@ -950,7 +952,12 @@ class ListWidget(InputWidget):
|
|||
@param selected (list[string]): list of the selected values
|
||||
@param styles (iterable[string]): flags to set the behaviour of the list
|
||||
can be:
|
||||
- multi: mutliple selection is allowed
|
||||
- multi: multiple selection is allowed
|
||||
- noselect: no selection is allowed
|
||||
useful when only the list itself is needed
|
||||
- extensible: can be extended by user (i.e. new options can be added)
|
||||
- reducible: can be reduced by user (i.e. options can be removed)
|
||||
- inline: hint that this list should be displayed on a single line (e.g. list of labels)
|
||||
@param name (string)
|
||||
@param parent
|
||||
"""
|
||||
|
|
@ -959,17 +966,16 @@ class ListWidget(InputWidget):
|
|||
styles = set()
|
||||
else:
|
||||
styles = set(styles)
|
||||
if u'noselect' in styles and (u'multi' in styles or selected):
|
||||
raise exceptions.DataError(_(u'"multi" flag and "selected" option are not compatible with "noselect" flag'))
|
||||
if not options:
|
||||
# we can have no options if we get a submitted data form
|
||||
# but we can't use submitted values directly,
|
||||
# because we would not have the labels
|
||||
log.warning(_('empty "options" list'))
|
||||
if not styles.issubset(['multi']):
|
||||
raise exceptions.DataError(_("invalid styles"))
|
||||
super(ListWidget, self).__init__(xmlui, name, parent)
|
||||
self.addOptions(options, selected)
|
||||
for style in styles:
|
||||
self.elem.setAttribute(style, 'yes')
|
||||
self.setStyles(styles)
|
||||
|
||||
def addOptions(self, options, selected=None):
|
||||
"""Add options to a multi-values element (e.g. list) """
|
||||
|
|
@ -983,6 +989,17 @@ class ListWidget(InputWidget):
|
|||
value = option if isinstance(option, basestring) else option[0]
|
||||
OptionElement(self, option, value in selected)
|
||||
|
||||
def setStyles(self, styles):
|
||||
if not styles.issubset(self.STYLES):
|
||||
raise exceptions.DataError(_(u"invalid styles"))
|
||||
for style in styles:
|
||||
self.elem.setAttribute(style, 'yes')
|
||||
# TODO: check flags incompatibily (noselect and multi) like in __init__
|
||||
|
||||
def setStyle(self, style):
|
||||
self.setStyles([style])
|
||||
|
||||
|
||||
class JidsListWidget(InputWidget):
|
||||
"""A list of text or jids where elements can be added/removed or modified"""
|
||||
type = 'jids_list'
|
||||
|
|
|
|||
Loading…
Reference in a new issue