Skip to content

Commit

Permalink
[ADD] Create new module website_field_autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
lasley committed Jun 21, 2016
1 parent 72d301e commit d557c10
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
85 changes: 85 additions & 0 deletions website_field_autocomplete/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.. image:: https://img.shields.io/badge/license-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

============================
Website Field - AutoComplete
============================

Adds an AutoComplete field for use in Odoo Website.

This module is somewhat difficult to use on its own in an effort to not require
any dependencies other than website.

Usage
=====

To use this module, you need to add an input field matching the below format:

<input type="text"
class="s_website_autocomplete"
data-queryField="name"
data-displayField="name"
data-limit="5"
data-domain='[["website_published", "=", true]]'
data-model="product.template"
/>


+-------------------+--------------------------------------------+------------+----------+
| Attribute | Description | Default | Required |
+===================+============================================+============+==========+
| data-model | Model name to query | | True |
+-------------------+--------------------------------------------+------------+----------+
| data-queryField | Field to query when searching | name | False |
+-------------------+--------------------------------------------+------------+----------+
| data-displayField | Field to display (defaults to queryField) | queryField | False |
+-------------------+--------------------------------------------+------------+----------+
| data-limit | Limit results to this many | 10 | False |
+-------------------+--------------------------------------------+------------+----------+
| data-domain | Additional domain for query | [] | False |
+-------------------+--------------------------------------------+------------+----------+


.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/186/9.0


Known Issues / Road Map
=======================

* Use of Model/jsonRPC requires a user session.


Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/website/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.


Credits
=======

Contributors
------------

* Dave Lasley <dave@laslabs.com>

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.
3 changes: 3 additions & 0 deletions website_field_autocomplete/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
21 changes: 21 additions & 0 deletions website_field_autocomplete/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Website Field - AutoComplete",
"summary": 'Provides an autocomplete field for Website on any model',
"version": "9.0.1.0.0",
"category": "Website",
"website": "https://laslabs.com/",
"author": "LasLabs",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"website",
],
"data": [
'views/assets.xml',
],
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions website_field_autocomplete/static/description/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions website_field_autocomplete/static/src/js/field_autocomplete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright 2016 LasLabs Inc.
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
*/

odoo.define('website_field_autocomplete.field_autocomplete', function(require){
"use strict";

var snippet_animation = require('web_editor.snippets.animation');
var Model = require('web.Model');

snippet_animation.registry.field_autocomplete = snippet_animation.Class.extend({

selector: '.s_website_autocomplete',

start: function() {

let self = this;

this.$target.autocomplete({
source: function(request, response) {
// Must use var, let yields undefined
var QueryModel = new Model(self.$target.data('model'));
let queryField = self.$target.data('queryField');
if (!queryField) {
queryField = 'name';
}
let displayField = self.$target.data('displayField');
if (!displayField) {
displayField = queryField;
}
let limit = self.$target.data('limit');
if (!limit) {
limit = 10;
}
let domain = [[queryField, 'ilike', request.term]];
let add_domain = self.$target.data('domain');
if (add_domain) {
domain = domain.concat(add_domain);
}
QueryModel.call('search_read', [domain, [displayField]], {limit: limit})
.then(function(records) {
let data = records.reduce(function(a, b) {
a.push(b[displayField]);
return a;
}, []);
response(data);
});
}
});

},

});

return {field_autocomplete: snippet_animation.registry.field_autocomplete};

});
14 changes: 14 additions & 0 deletions website_field_autocomplete/views/assets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Copyright 2016 LasLabs Inc.
@license AGPL-3 or later (http://www.gnu.org/licenses/agpl.html).
-->

<odoo>
<template id="assets_recaptcha" name="website_form_recaptcha Assets" inherit_id="website.assets_frontend">
<xpath expr="." position="inside">
<script src="/website_field_autocomplete/static/src/js/field_autocomplete.js" />
</xpath>
</template>
</odoo>

0 comments on commit d557c10

Please sign in to comment.