changeset 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 66baa687c682
children 3ff9d7a7fe71
files frontends/src/tools/xmlui.py src/tools/common/template_xmlui.py src/tools/xml_tools.py
diffstat 3 files changed, 33 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/tools/xmlui.py	Fri Oct 27 17:58:05 2017 +0200
+++ b/frontends/src/tools/xmlui.py	Fri Oct 27 18:03:35 2017 +0200
@@ -493,6 +493,9 @@
                     self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
                 elif type_ == "list":
                     style = [] if node.getAttribute("multi") == 'yes' else ['single']
+                    for attr in (u'noselect', u'extensible', u'reducible', u'inline'):
+                        if node.getAttribute(attr) == 'yes':
+                            style.append(attr)
                     _options = [(option.getAttribute("value"), option.getAttribute("label")) for option in node.getElementsByTagName("option")]
                     _selected = [option.getAttribute("value") for option in node.getElementsByTagName("option") if option.getAttribute('selected') == C.BOOL_TRUE]
                     ctrl = self.widget_factory.createListWidget(_xmlui_parent, _options, _selected, style)
--- a/src/tools/common/template_xmlui.py	Fri Oct 27 17:58:05 2017 +0200
+++ b/src/tools/common/template_xmlui.py	Fri Oct 27 18:03:35 2017 +0200
@@ -81,6 +81,14 @@
                 ret.append(label)
         return ret
 
+    @property
+    def inline(self):
+        return u'inline' in self.style
+
+    @property
+    def no_select(self):
+        return u'noselect' in self.style
+
 
 class EmptyWidget(xmlui.EmptyWidget, Widget):
 
--- 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'