summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/fields/relational_fields.js
diff options
context:
space:
mode:
authorAntony Lesuisse <al@openerp.com>2018-09-18 17:16:16 +0200
committerTWA <twa@odoo.com>2018-09-21 14:02:22 +0200
commita6e1eb9f0ad285fac7d0ca0b9f89f046d78ec9c7 (patch)
tree147f86739a95d6924ebda835c62716e66edcbdb5 /addons/web/static/src/js/fields/relational_fields.js
parentce16d0d2ab20ed223ab3dc2932ac7a6f27cccf5f (diff)
[ADD] partner_autocomplete: Added autocomplete on partners fields
For Name field or M2O, gives a list of companies Data comes from Odoo IAP Service
Diffstat (limited to 'addons/web/static/src/js/fields/relational_fields.js')
-rw-r--r--addons/web/static/src/js/fields/relational_fields.js62
1 files changed, 60 insertions, 2 deletions
diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js
index a1efa85498e..c3d25f37d09 100644
--- a/addons/web/static/src/js/fields/relational_fields.js
+++ b/addons/web/static/src/js/fields/relational_fields.js
@@ -138,6 +138,11 @@ var FieldMany2One = AbstractField.extend({
this.isDirty = false;
this.lastChangeEvent = undefined;
+ // List of autocomplete sources
+ this._autocompleteSources = [];
+ // Add default search method for M20 (name_search)
+ this._addAutocompleteSource(this._search, {placeholder: _t('Loading...'), order: 1});
+
// use a DropPrevious to properly handle related record quick creations,
// and store a createDef to be able to notify the environment that there
// is pending quick create operation
@@ -213,14 +218,46 @@ var FieldMany2One = AbstractField.extend({
//--------------------------------------------------------------------------
/**
+ * Add a source to the autocomplete results
+ *
+ * @param {function} method : A function that returns a list of results. If async source, the function should return a promise
+ * @param {Object} params : Parameters containing placeholder/validation/order
+ * @private
+ */
+ _addAutocompleteSource: function (method, params) {
+ this._autocompleteSources.push({
+ method: method,
+ placeholder: (params.placeholder ? _t(params.placeholder) : _t('Loading...')) + '<i class="fa fa-spinner fa-spin pull-right"></i>' ,
+ validation: params.validation,
+ loading: false,
+ order: params.order || 999
+ });
+
+ this._autocompleteSources = _.sortBy(this._autocompleteSources, 'order');
+ },
+ /**
* @private
*/
_bindAutoComplete: function () {
var self = this;
this.$input.autocomplete({
source: function (req, resp) {
- self._search(req.term).then(function (result) {
- resp(result);
+ _.each(self._autocompleteSources, function (source) {
+ // Resets the results for this source
+ source.results = [];
+
+ // Check if this source should be used for the searched term
+ if (!source.validation || source.validation.call(self, req.term)) {
+ source.loading = true;
+
+ // Wrap the returned value of the source.method with $.when.
+ // So event if the returned value is not async, it will work
+ $.when(source.method.call(self, req.term)).then(function (results) {
+ source.results = results;
+ source.loading = false;
+ resp(self._concatenateAutocompleteResults());
+ });
+ }
});
},
select: function (event, ui) {
@@ -257,6 +294,25 @@ var FieldMany2One = AbstractField.extend({
this.autocomplete_bound = true;
},
/**
+ * Concatenate async results for autocomplete.
+ *
+ * @returns {Array}
+ * @private
+ */
+ _concatenateAutocompleteResults: function () {
+ var results = [];
+ _.each(this._autocompleteSources, function (source) {
+ if (source.results && source.results.length) {
+ results = results.concat(source.results);
+ } else if (source.loading) {
+ results.push({
+ label: source.placeholder
+ });
+ }
+ });
+ return results;
+ },
+ /**
* @private
* @param {string} [name]
* @returns {Object}
@@ -389,6 +445,8 @@ var FieldMany2One = AbstractField.extend({
this.m2o_value = this._formatValue(this.value);
},
/**
+ * Executes a name_search and process its result.
+ *
* @private
* @param {string} search_val
* @returns {Deferred}