[ADD] autocompletion correctly using input values

bzr revid: xmo@openerp.com-20120502131226-w4agkq7qq5ni8nwl
This commit is contained in:
Xavier Morel 2012-05-02 15:12:26 +02:00
parent d28498ffb1
commit 25a30ad0ce
1 changed files with 25 additions and 8 deletions

View File

@ -286,13 +286,23 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
* Sets up search view's view-wide auto-completion widget * Sets up search view's view-wide auto-completion widget
*/ */
setup_global_completion: function () { setup_global_completion: function () {
// Prevent keydown from within a facet's input from reaching the var self = this;
// auto-completion widget and opening the completion list
this.$element.on('keydown', '.search_facet input', function (e) { // autocomplete only correctly handles being initialized on the actual
e.stopImmediatePropagation(); // editable element (and only an element with a @value in 1.8 e.g.
// input or textarea), cheat by setting val() on $element
this.$element.on('keydown', function () {
// keydown is triggered *before* the element's value is set, so
// delay this. Pray that setTimeout are executed in FIFO (if they
// have the same delay) as autocomplete uses the exact same trick.
// FIXME: brittle as fuck
setTimeout(function () {
self.$element.val(self.currentInputValue());
}, 0);
}); });
this.$element.autocomplete({ var completer = this.$element.autocomplete({
source: this.proxy('complete_global_search'), source: this.proxy('complete_global_search'),
select: this.proxy('select_completion'), select: this.proxy('select_completion'),
focus: function (e) { e.preventDefault(); }, focus: function (e) { e.preventDefault(); },
@ -305,14 +315,14 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
.data( "item.autocomplete", item ) .data( "item.autocomplete", item )
.appendTo( ul ); .appendTo( ul );
if (item.value !== undefined) { if (item.facet !== undefined) {
// regular completion item // regular completion item
return $item.append( return $item.append(
(item.label) (item.label)
? $('<a>').html(item.label) ? $('<a>').html(item.label)
: $('<a>').text(item.value)); : $('<a>').text(item.value));
} }
return $item.text(item.category) return $item.text(item.label)
.css({ .css({
borderTop: '1px solid #cccccc', borderTop: '1px solid #cccccc',
margin: 0, margin: 0,
@ -322,7 +332,14 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
clear: 'left', clear: 'left',
width: '100%' width: '100%'
}); });
} };
},
/**
* Gets value out of the currently focused "input" (a
* div[contenteditable].oe_searchview_input)
*/
currentInputValue: function () {
return this.$element.find('div.oe_searchview_input:focus').text();
}, },
/** /**
* Provide auto-completion result for req.term (an array to `resp`) * Provide auto-completion result for req.term (an array to `resp`)