[ADD] web_search_autocomplete_prefetch

pull/1030/head
Holger Brunn 2015-12-09 21:30:36 +01:00
parent fb000f2bf6
commit 71210c651b
No known key found for this signature in database
GPG Key ID: 01C9760FECA3AE18
7 changed files with 185 additions and 0 deletions

View File

@ -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 <https://github.com/OCA/web/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 <https://github.com/OCA/web/issues/new?body=module:%20web_search_autocomplete_prefetch%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Credits
=======
Contributors
------------
* Holger Brunn <hbrunn@therp.nl>
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 http://odoo-community.org.

View File

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV <http://therp.nl>
# 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,
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@ -0,0 +1,87 @@
//-*- coding: utf-8 -*-
//© 2015 Therp BV <http://therp.nl>
//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;
}
var facet = {
get: function(name)
{
switch(name)
{
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.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);
},
});
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<template id="assets_backend" name="web_search_autocomplete_prefetch assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/web_search_autocomplete_prefetch/static/src/js/web_search_autocomplete_prefetch.js"></script>
</xpath>
</template>
</data>
</openerp>