diff --git a/web_search_autocomplete_prefetch/README.rst b/web_search_autocomplete_prefetch/README.rst new file mode 100644 index 00000000..e8f8af57 --- /dev/null +++ b/web_search_autocomplete_prefetch/README.rst @@ -0,0 +1,64 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +============================ +Prefetch autocomplete offers +============================ + +When searching, the autocomplete options can be a bit frustrating because you +will be offered choices that won't yield a result. This addon searches for the +term in the background and only offers an option if this search has a result. + +Usage +===== + +* go to ... +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/162/8.0 + +For further information, please visit: + +* https://www.odoo.com/forum/help-1 + +Known issues / Roadmap +====================== + +* some searches (especially via function fields) can be very heavy on the + server. + + To disable prefetching on a per field basis, set the option + `web_search_autocomplete_prefetch.disable`:: + options="{'web_search_autocomplete_prefetch.disable': true}" + on your field in the search view. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed feedback +`here `_. + +Credits +======= + +Contributors +------------ + +* Holger Brunn + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/web_search_autocomplete_prefetch/__init__.py b/web_search_autocomplete_prefetch/__init__.py new file mode 100644 index 00000000..6d9656f1 --- /dev/null +++ b/web_search_autocomplete_prefetch/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +# © 2015 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). diff --git a/web_search_autocomplete_prefetch/__openerp__.py b/web_search_autocomplete_prefetch/__openerp__.py new file mode 100644 index 00000000..e12afde7 --- /dev/null +++ b/web_search_autocomplete_prefetch/__openerp__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# © 2015 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Prefetch autocomplete offers", + "version": "8.0.1.0.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Usability", + "summary": "Offer only items on autocompletion that will yield results", + "depends": [ + 'web', + ], + "images": [ + 'images/web_search_autocomplete_prefetch.png', + ], + "data": [ + 'views/templates.xml', + ], + "installable": True, +} diff --git a/web_search_autocomplete_prefetch/images/web_search_autocomplete_prefetch.png b/web_search_autocomplete_prefetch/images/web_search_autocomplete_prefetch.png new file mode 100644 index 00000000..a1ab7db8 Binary files /dev/null and b/web_search_autocomplete_prefetch/images/web_search_autocomplete_prefetch.png differ diff --git a/web_search_autocomplete_prefetch/static/description/icon.png b/web_search_autocomplete_prefetch/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/web_search_autocomplete_prefetch/static/description/icon.png differ diff --git a/web_search_autocomplete_prefetch/static/src/js/web_search_autocomplete_prefetch.js b/web_search_autocomplete_prefetch/static/src/js/web_search_autocomplete_prefetch.js new file mode 100644 index 00000000..b9f82588 --- /dev/null +++ b/web_search_autocomplete_prefetch/static/src/js/web_search_autocomplete_prefetch.js @@ -0,0 +1,95 @@ +//-*- coding: utf-8 -*- +//© 2015 Therp BV +//License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +openerp.web_search_autocomplete_prefetch = function(instance) +{ + // overwrite this or use it to recycle the functionality for your own field + openerp.web_search_autocomplete_prefetch.complete = function(self, value, data) + { + if(self.options['web_search_autocomplete_prefetch.disable']) + { + return data; + } + if(!self.autocomplete_mutex) + { + self.autocomplete_mutex = new instance.Mutex() + } + var facet = { + get: function(name) + { + switch(name) + { + case 'label': return value; + case 'value': return value; + case 'operator': return 'ilike'; + } + }, + }, + domain = new instance.web.CompoundDomain( + self.get_domain({values: [facet]}), + self.view.dataset.domain); + domain.set_eval_context(self.view.dataset.get_context()); + return self.autocomplete_mutex.exec(function() + { + return self.view.dataset._model.call( + 'search_count', [domain.eval()]) + .then(function(count) + { + if(count) + { + _.each(data, function(obj) + { + obj.label += _.str.sprintf(' (%s)', count); + }); + return data; + } + else + { + return null; + } + }); + }); + } + + instance.web.search.CharField.include({ + init: function() + { + var result = this._super.apply(this, arguments); + this.options = instance.web.py_eval(this.attrs.options || '{}'); + return result; + }, + complete: function(value) + { + var self = this; + return this._super.apply(this, arguments).then(function(data) + { + return openerp.web_search_autocomplete_prefetch.complete( + self, value, data); + }); + } + }); + + instance.web.search.ManyToOneField.include({ + complete: function(value) + { + var self = this; + return this._super.apply(this, arguments).then(function(data) + { + return openerp.web_search_autocomplete_prefetch.complete( + self, value, data); + }); + } + }); + + instance.web.search.AutoComplete.include({ + select_item: function() + { + if(!this.current_result) + { + return; + } + return this._super.apply(this, arguments); + }, + }); +} diff --git a/web_search_autocomplete_prefetch/views/templates.xml b/web_search_autocomplete_prefetch/views/templates.xml new file mode 100644 index 00000000..624c099d --- /dev/null +++ b/web_search_autocomplete_prefetch/views/templates.xml @@ -0,0 +1,10 @@ + + + + + +