diff src/tools/xml_tools.py @ 977:d2e612a45e76

tools, frontends (xmlui): add Widget.setInternalCallback: - to add UI callback not communication with the bridge - a list of predefined actions can be used to copy, move, select values - extra data can be passed within the 'internal_data' element TODO: frontend side operation to retrieve the data from node to data structure should be generic
author souliane <souliane@mailoo.org>
date Thu, 03 Apr 2014 14:56:16 +0200
parents b37b1d183ac3
children 58a57ce5932a
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Thu Apr 03 14:49:05 2014 +0200
+++ b/src/tools/xml_tools.py	Thu Apr 03 14:56:16 2014 +0200
@@ -24,6 +24,7 @@
 from twisted.words.xish import domish
 from sat.core import exceptions
 
+
 """This library help manage XML used in SàT (parameters, registration, etc) """
 
 SAT_FORM_PREFIX = "SAT_FORM_"
@@ -299,6 +300,26 @@
         self.elem.setAttribute('name', name)
 
 
+class InternalFieldElement(Element):
+    """ Used by internal callbacks to indicate which fields are manipulated """
+    type = 'internal_field'
+
+    def __init__(self, parent, name):
+        super(InternalFieldElement, self).__init__(parent.xmlui, parent)
+        self.elem.setAttribute('name', name)
+
+
+class InternalDataElement(Element):
+    """ Used by internal callbacks to retrieve extra data """
+    type = 'internal_data'
+
+    def __init__(self, parent, children):
+        super(InternalDataElement, self).__init__(parent.xmlui, parent)
+        assert(isinstance(children, list))
+        for child in children:
+            self.elem.childNodes.append(child)
+
+
 class OptionElement(Element):
     """" Used by ListWidget to specify options """
     type = 'option'
@@ -510,6 +531,32 @@
             self.elem.setAttribute('name', name)
         self.elem.setAttribute('type', self.type)
 
+    def setInternalCallback(self, callback, fields, data_elts=None):
+        """Set an internal UI callback when the widget value is changed.
+
+        The internal callbacks are NO callback ids, they are strings from
+        a predefined set of actions that are running in the scope of XMLUI.
+
+        @param callback (string): a value from:
+            - 'copy': process the widgets given in 'fields' two by two, by
+                copying the values of one widget to the other. Target widgets
+                of type List do not accept the empty value.
+            - 'move': same than copy but moves the values if the source widget
+                is not a List.
+            - 'groups_of_contact': process the widgets two by two, assume A is
+                is a list of JID and B a list of groups, select in B the groups
+                to which the JID selected in A belongs.
+            - more operation to be added when necessary...
+        @param fields (list): a list of widget names (string)
+        @param data_elts (list[Element]): extra data elements
+        """
+        self.elem.setAttribute('internal_callback', callback)
+        if fields:
+            for field in fields:
+                InternalFieldElement(self, field)
+        if data_elts:
+            InternalDataElement(self, data_elts)
+
 
 class InputWidget(Widget):
     pass