diff src/tools/xml_tools.py @ 2397:7fff98d64ab5

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
author Goffi <goffi@goffi.org>
date Fri, 27 Oct 2017 18:03:35 +0200
parents 2e05921df16a
children 3ff9d7a7fe71
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Fri Oct 27 17:58:05 2017 +0200
+++ b/src/tools/xml_tools.py	Fri Oct 27 18:03:35 2017 +0200
@@ -350,6 +350,7 @@
                 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 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 @@
         @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 @@
             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 @@
             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'