Do not display all contacts in contacts <select/>, make autocomplete work

This commit is contained in:
Frédéric Péters 2011-07-29 21:42:38 +02:00
parent a56d578fef
commit c467232fb4
3 changed files with 38 additions and 11 deletions

View File

@ -151,6 +151,21 @@ function newElem(name)
function selectionError()
{alert("Must select something!")}
function formwidgetadd_autocomplete_ready(event, data, formatted) {
(function($) {
var input_box = $(event.target);
name = input_box.attr('id');
name = name.substr(0, name.lastIndexOf('-'));
var tgt = document.getElementById(name+'-to');
temp = new Option(data[1], data[0]);
tgt.options[tgt.length] = temp;
temp.selected = true;
copyDataForSubmit(name);
}(jQuery));
}
/* ]]> */
</script>
@ -267,9 +282,7 @@ function selectionError()
</td>
</tr>
</table>
<!--
<script type="text/javascript" tal:content="view/js">
</script>
-->
</html>

View File

@ -139,7 +139,7 @@ class ContactsVocabulary(SimpleVocabulary):
def search(self, qs):
q = qs.lower()
t = [self.getTermByToken(kw) for kw in self.contact_ids if q in kw.lower()]
t = [self.by_value.get('contact:'+kw) for kw in self.contact_ids if q in kw.lower()]
return t

View File

@ -131,8 +131,11 @@ def RadioChoiceFieldWidget(field, request):
class OrderedSelectAndAddWidget(OrderedSelectWidget):
implementsOnly(IOrderedSelectAndAddWidget)
formatItem = 'function(row, idx, count, value) { return row[1]; }'
formatResult = 'function(row, idx, count) { return ""; }'
formatItem = '''function(row, idx, count, value) { console.log(row, idx,
count, value); return row[1]; }'''
formatResult = '''function(row, idx, count) { console.log("t2", row, idx,
count); return ""; }'''
js_template = """\
(function($) {
@ -160,12 +163,7 @@ class OrderedSelectAndAddWidget(OrderedSelectWidget):
def js(self):
# Use a template if it exists, in case anything overrode this interface
js_callback = 'formwidget_autocomplete_ready'
if hasattr(self,'js_callback_template'):
js_callback = self.js_callback_template % dict(id=self.id,
name=self.name, klass=self.klass, title=self.title,
termCount=len(self.terms))
js_callback = 'formwidgetadd_autocomplete_ready'
return self.js_template % dict(id=self.id, url=self.autocomplete_url(),
formatItem=self.formatItem, formatResult=self.formatResult,
klass=self.klass, title=self.title,
@ -189,6 +187,22 @@ class OrderedSelectAndAddWidget(OrderedSelectWidget):
self._bound_source = source
return self._bound_source
def deselect(self):
selecteditems = []
notselecteditems = []
for selecteditem in self.selectedItems:
selecteditems.append(selecteditem['content'])
for item in self.items:
if not item['content'] in selecteditems:
if item['value'].startswith('contact:'):
# skip simple contacts, to avoid the list growing too large
# ideally we would have something like "favourite" contacts
# populated automatically on usage frequence...
continue
notselecteditems.append(item)
return notselecteditems
@adapter(IRelatedDoc, IFormLayer)
@implementer(IFieldWidget)