# HG changeset patch # User Sylvain Thénault # Date 1268407401 -3600 # Node ID 4e67a538e476fe60e39eb67746b4a6acc99f4ce2 # Parent c666d265fb95d44ba7c67c2acfb5cc2329ea2b7f# Parent 19ecbbc4f6335c57c071b6413696763356535b32 backport stable diff -r c666d265fb95 -r 4e67a538e476 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 diff -r c666d265fb95 -r 4e67a538e476 web/data/cubicweb.widgets.js --- 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 diff -r c666d265fb95 -r 4e67a538e476 web/formwidgets.py --- 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)