diff --git a/web_search_autocomplete_prefetch/README.rst b/web_search_autocomplete_prefetch/README.rst new file mode 100644 index 00000000..0df604e0 --- /dev/null +++ b/web_search_autocomplete_prefetch/README.rst @@ -0,0 +1,91 @@ +============================ +Prefetch autocomplete offers +============================ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/10.0/web_search_autocomplete_prefetch + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-10-0/web-10-0-web_search_autocomplete_prefetch + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/162/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +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. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +* 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. +* by default, the addon triggers a search 350 milliseconds after the last key + press. If you want a different timeout, set the parameter + ``web_search_autocomplete_prefetch.keypress_timeout`` to the amount of + milliseconds you need as timeout. + +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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Therp BV + +Contributors +~~~~~~~~~~~~ + +* Holger Brunn + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/web `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_search_autocomplete_prefetch/__init__.py b/web_search_autocomplete_prefetch/__init__.py new file mode 100644 index 00000000..94673b34 --- /dev/null +++ b/web_search_autocomplete_prefetch/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). diff --git a/web_search_autocomplete_prefetch/__manifest__.py b/web_search_autocomplete_prefetch/__manifest__.py new file mode 100644 index 00000000..492421d2 --- /dev/null +++ b/web_search_autocomplete_prefetch/__manifest__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Copyright 2015-2018 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Prefetch autocomplete offers", + "version": "10.0.1.0.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/web", + "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', + 'views/base_view.xml', + ], + "installable": True, +} diff --git a/web_search_autocomplete_prefetch/i18n/es.po b/web_search_autocomplete_prefetch/i18n/es.po new file mode 100644 index 00000000..cc7ecd45 --- /dev/null +++ b/web_search_autocomplete_prefetch/i18n/es.po @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_search_autocomplete_prefetch +# +# Translators: +# Pedro M. Baeza , 2016 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-11-26 01:59+0000\n" +"PO-Revision-Date: 2016-11-26 01:59+0000\n" +"Last-Translator: Pedro M. Baeza , 2016\n" +"Language-Team: Spanish (https://www.transifex.com/oca/teams/23907/es/)\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: web_search_autocomplete_prefetch +#: view:ir.ui.view:web_search_autocomplete_prefetch.view_view_search_autocomplete +msgid "{'web_search_autocomplete_prefetch.disable': true}" +msgstr "{'web_search_autocomplete_prefetch.disable': true}" diff --git a/web_search_autocomplete_prefetch/i18n/fi.po b/web_search_autocomplete_prefetch/i18n/fi.po new file mode 100644 index 00000000..0787833a --- /dev/null +++ b/web_search_autocomplete_prefetch/i18n/fi.po @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_search_autocomplete_prefetch +# +# Translators: +# Jarmo Kortetjärvi , 2016 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-11-26 01:59+0000\n" +"PO-Revision-Date: 2016-11-26 01:59+0000\n" +"Last-Translator: Jarmo Kortetjärvi , 2016\n" +"Language-Team: Finnish (https://www.transifex.com/oca/teams/23907/fi/)\n" +"Language: fi\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: web_search_autocomplete_prefetch +#: view:ir.ui.view:web_search_autocomplete_prefetch.view_view_search_autocomplete +msgid "{'web_search_autocomplete_prefetch.disable': true}" +msgstr "{'web_search_autocomplete_prefetch.disable': true}" diff --git a/web_search_autocomplete_prefetch/i18n/sl.po b/web_search_autocomplete_prefetch/i18n/sl.po new file mode 100644 index 00000000..16196e36 --- /dev/null +++ b/web_search_autocomplete_prefetch/i18n/sl.po @@ -0,0 +1,25 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_search_autocomplete_prefetch +# +# Translators: +# Matjaž Mozetič , 2016 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-08-25 00:50+0000\n" +"PO-Revision-Date: 2016-08-25 00:50+0000\n" +"Last-Translator: Matjaž Mozetič , 2016\n" +"Language-Team: Slovenian (https://www.transifex.com/oca/teams/23907/sl/)\n" +"Language: sl\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n" +"%100==4 ? 2 : 3);\n" + +#. module: web_search_autocomplete_prefetch +#: view:ir.ui.view:web_search_autocomplete_prefetch.view_view_search_autocomplete +msgid "{'web_search_autocomplete_prefetch.disable': true}" +msgstr "{'web_search_autocomplete_prefetch.disable': true}" diff --git a/web_search_autocomplete_prefetch/i18n/tr.po b/web_search_autocomplete_prefetch/i18n/tr.po new file mode 100644 index 00000000..5ba7ed28 --- /dev/null +++ b/web_search_autocomplete_prefetch/i18n/tr.po @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_search_autocomplete_prefetch +# +# Translators: +# Ahmet Altinisik , 2016 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-12-29 03:45+0000\n" +"PO-Revision-Date: 2016-12-29 03:45+0000\n" +"Last-Translator: Ahmet Altinisik , 2016\n" +"Language-Team: Turkish (https://www.transifex.com/oca/teams/23907/tr/)\n" +"Language: tr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +#. module: web_search_autocomplete_prefetch +#: view:ir.ui.view:web_search_autocomplete_prefetch.view_view_search_autocomplete +msgid "{'web_search_autocomplete_prefetch.disable': true}" +msgstr "{'web_search_autocomplete_prefetch.disable': true}" diff --git a/web_search_autocomplete_prefetch/i18n/web_search_autocomplete_prefetch.pot b/web_search_autocomplete_prefetch/i18n/web_search_autocomplete_prefetch.pot new file mode 100644 index 00000000..6e0dfc2e --- /dev/null +++ b/web_search_autocomplete_prefetch/i18n/web_search_autocomplete_prefetch.pot @@ -0,0 +1,20 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_search_autocomplete_prefetch +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: web_search_autocomplete_prefetch +#: view:ir.ui.view:web_search_autocomplete_prefetch.view_view_search_autocomplete +msgid "{'web_search_autocomplete_prefetch.disable': true}" +msgstr "" + 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/readme/CONFIGURE.rst b/web_search_autocomplete_prefetch/readme/CONFIGURE.rst new file mode 100644 index 00000000..c56e2e8d --- /dev/null +++ b/web_search_autocomplete_prefetch/readme/CONFIGURE.rst @@ -0,0 +1,12 @@ +* 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. +* by default, the addon triggers a search 350 milliseconds after the last key + press. If you want a different timeout, set the parameter + ``web_search_autocomplete_prefetch.keypress_timeout`` to the amount of + milliseconds you need as timeout. diff --git a/web_search_autocomplete_prefetch/readme/CONTRIBUTORS.rst b/web_search_autocomplete_prefetch/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..b120a956 --- /dev/null +++ b/web_search_autocomplete_prefetch/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Holger Brunn diff --git a/web_search_autocomplete_prefetch/readme/DESCRIPTION.rst b/web_search_autocomplete_prefetch/readme/DESCRIPTION.rst new file mode 100644 index 00000000..c6509e38 --- /dev/null +++ b/web_search_autocomplete_prefetch/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +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. 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/description/index.html b/web_search_autocomplete_prefetch/static/description/index.html new file mode 100644 index 00000000..0d1a5320 --- /dev/null +++ b/web_search_autocomplete_prefetch/static/description/index.html @@ -0,0 +1,419 @@ + + + + + + +Prefetch autocomplete offers + + + +
+

Prefetch autocomplete offers

+ + +

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runbot

+

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.

+

Table of contents

+ +
+

Configuration

+
    +
  • 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.

    +
  • +
  • by default, the addon triggers a search 350 milliseconds after the last key +press. If you want a different timeout, set the parameter +web_search_autocomplete_prefetch.keypress_timeout to the amount of +milliseconds you need as timeout.

    +
  • +
+
+
+

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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Therp BV
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/web project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + 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..a4131e38 --- /dev/null +++ b/web_search_autocomplete_prefetch/static/src/js/web_search_autocomplete_prefetch.js @@ -0,0 +1,144 @@ +//-*- coding: utf-8 -*- +//Copyright 2015-2018 Therp BV +//License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +odoo.define('web_search_autocomplete_prefetch', function(require) { + var CompoundDomain = require('web.data').CompoundDomain, + Mutex = require('web.utils').Mutex; + + var complete = function(self, value, data) { + if(self.options['web_search_autocomplete_prefetch.disable']) { + return data; + } + if(!self.autocomplete_mutex) { + self.autocomplete_mutex = new Mutex(); + } + var facet = { + get: function(name) { + switch(name) { + case 'label': return value; + case 'value': return value; + case 'operator': return 'ilike'; + } + }, + attributes: { + value: value, + }, + }, + domain = new CompoundDomain( + self.get_domain({values: [facet]}), + self.searchview.dataset.domain + ); + domain.set_eval_context(self.searchview.dataset.get_context()); + return self.autocomplete_mutex.exec(function() { + return self.searchview.dataset._model.call( + 'search_count', [domain.eval()], + {context: self.searchview.dataset.get_context()} + ) + .then(function(count) { + if(count) { + _.each(data, function(obj) { + obj.label += _.str.sprintf(' (%s)', count); + }); + return data; + } + return null; + }); + }); + }; + + return { + // overwrite this or use it to recycle the functionality + // for your own field + complete: complete, + }; +}); + +odoo.define('web_search_autocomplete_prefetch.inputs', function(require) { + var AutoComplete = require('web.AutoComplete'), + Field = require('web.search_inputs').Field, + Model = require('web.Model'), + pyeval = require('web.pyeval'), + search_widgets_registry = require('web.core').search_widgets_registry, + web_search_autocomplete_prefetch = require( + 'web_search_autocomplete_prefetch' + ); + + Field.include({ + init: function() { + var result = this._super.apply(this, arguments); + this.options = pyeval.py_eval(this.attrs.options || '{}'); + return result; + }, + }); + + search_widgets_registry.get('char').include({ + complete: function(value) { + var self = this; + return this._super.apply(this, arguments).then(function(data) { + return web_search_autocomplete_prefetch.complete( + self, value, data + ); + }); + } + }); + + search_widgets_registry.get('many2one').include({ + complete: function(value) { + var self = this; + return this._super.apply(this, arguments).then(function(data) { + return web_search_autocomplete_prefetch.complete( + self, value, data + ); + }); + } + }); + + search_widgets_registry.get('selection').include({ + complete: function(value) { + var self = this; + return this._super.apply(this, arguments).then(function(data) { + return web_search_autocomplete_prefetch.complete( + self, value, data + ); + }); + } + }); + + AutoComplete.include({ + keypress_timeout: 350, + start: function() { + var self = this; + return jQuery.when( + this._super.apply(this, arguments), + new Model('ir.config_parameter').call( + 'get_param', + [ + 'web_search_autocomplete_prefetch.keypress_timeout', + this.keypress_timeout + ] + ).then(function(keypress_timeout) { + self.keypress_timeout = parseInt(keypress_timeout, 10); + }) + ); + }, + select_item: function() { + if(!this.current_result) { + return; + } + return this._super.apply(this, arguments); + }, + initiate_search: function(query) { + var self = this, + _super = this._super, + last_timeout = null; + this.last_timeout = last_timeout = window.setTimeout( + function() { + if(self.last_timeout === last_timeout) { + _super.apply(self, [query]); + } + }, + this.keypress_timeout + ); + }, + }); +}); diff --git a/web_search_autocomplete_prefetch/views/base_view.xml b/web_search_autocomplete_prefetch/views/base_view.xml new file mode 100644 index 00000000..f5fd09bc --- /dev/null +++ b/web_search_autocomplete_prefetch/views/base_view.xml @@ -0,0 +1,12 @@ + + + + ir.ui.view + + + + {'web_search_autocomplete_prefetch.disable': true} + + + + diff --git a/web_search_autocomplete_prefetch/views/templates.xml b/web_search_autocomplete_prefetch/views/templates.xml new file mode 100644 index 00000000..8d4e89e5 --- /dev/null +++ b/web_search_autocomplete_prefetch/views/templates.xml @@ -0,0 +1,8 @@ + + + +