backport stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 12 Mar 2010 16:23:21 +0100
changeset 4902 4e67a538e476
parent 4899 c666d265fb95 (current diff)
parent 4901 19ecbbc4f633 (diff)
child 4903 627fcd90e08f
backport stable
server/sources/native.py
--- a/server/sources/native.py	Fri Mar 12 16:11:56 2010 +0100
+++ b/server/sources/native.py	Fri Mar 12 16:23:21 2010 +0100
@@ -13,7 +13,7 @@
 """
 __docformat__ = "restructuredtext en"
 
-from o_threading import Lock
+from threading import Lock
 from datetime import datetime
 from base64 import b64decode, b64encode
 
--- a/web/data/cubicweb.widgets.js	Fri Mar 12 16:11:56 2010 +0100
+++ b/web/data/cubicweb.widgets.js	Fri Mar 12 16:23:21 2010 +0100
@@ -113,8 +113,63 @@
     }
 
 });
+//remote version of RestrictedSuggestField
+Widgets.LazySuggestField = defclass('LazySuggestField', [Widgets.SuggestField], {
+    __init__: function(node, options) {
+	var self = this;
+	var multi = "no";
+	options = options || {};
+	options.max = 50;
+	options.delay = 50;
+	options.cacheLength=0;
+	options.mustMatch = true;
+        // multiple selection not supported yet (still need to formalize correctly
+        // initial values / display values)
+        var initialvalue = evalJSON(node.getAttribute('cubicweb:initialvalue') || 'null');
+        if (!initialvalue) {
+            initialvalue = node.value;
+        }
+        options = jQuery.extend({dataType: 'json',
+                                 multiple: (multi == "yes") ? true : false,
+                                 parse: this.parseResult
+                                }, options);
+        var dataurl = node.getAttribute('cubicweb:dataurl');
+        // remove 'name' from original input and add the hidden one that will
+        // store the actual value
+        var hidden = INPUT({'type': "hidden", 'name': node.name, 'value': initialvalue});
+        node.parentNode.appendChild(hidden);
+        jQuery(node).bind('result', {hinput: hidden, input:node}, self.hideRealValue)
+            .removeAttr('name').autocomplete(dataurl, options);
+    },
 
 
+    hideRealValue: function(evt, data, value) {
+	if (!value){
+	    value="";
+	}
+        evt.data.hinput.value = value;
+    },
+
+    /*
+     * @param data: a list of couple (value, label) to fill the suggestion list,
+     *              (returned by CW through AJAX)
+     */
+    parseResult: function(data) {
+        var parsed = [];
+        for (var i=0; i < data.length; i++) {
+                var value = ''+data[i][0]; // a string is required later by jquery.autocomplete.js
+                var label = data[i][1];
+                parsed[parsed.length] = {
+                    data: [label],
+                    value: value,
+                    result: label
+                };
+        };
+        return parsed;
+    }
+
+});
+
 /*
  * suggestform displays a suggest field and associated validate / cancel buttons
  * constructor's argumemts are the same that BaseSuggestField widget
--- a/web/formwidgets.py	Fri Mar 12 16:11:56 2010 +0100
+++ b/web/formwidgets.py	Fri Mar 12 16:23:21 2010 +0100
@@ -604,6 +604,34 @@
     wdgtype = 'RestrictedSuggestField'
 
 
+class LazyRestrictedAutoCompletionWidget(RestrictedAutoCompletionWidget):
+    """remote autocomplete """
+    wdgtype = 'LazySuggestField'
+
+    def values_and_attributes(self, form, field):
+        self.add_media(form)
+
+        """override values_and_attributes to handle initial displayed values"""
+        values, attrs = super(LazyRestrictedAutoCompletionWidget, self).values_and_attributes(form, field)
+        assert len(values) == 1, "multiple selection is not supported yet by LazyWidget"
+        if not values[0]:
+            values = form.cw_extra_kwargs.get(field.name,'')
+            if not isinstance(values, (tuple, list)):
+                values = (values,)
+        try:
+            values = list(values)
+            values[0] = int(values[0])
+            attrs['cubicweb:initialvalue'] = values[0]
+            values = (self.display_value_for(form, values[0]),)
+        except (TypeError, ValueError):
+            pass
+        return values, attrs
+
+    def display_value_for(self, form, value):
+        entity =form._cw.entity_from_eid(value)
+        return entity.view('combobox')
+
+
 class AddComboBoxWidget(Select):
     def values_and_attributes(self, form, field):
         values, attrs = super(AddComboBoxWidget, self).values_and_attributes(form, field)