From 6169d183fa3619a0422fea6ec94f3d9de0e8e5e4 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Tue, 21 Jun 2016 15:42:45 -0700 Subject: [PATCH 01/28] [ADD] Create new module website_field_autocomplete --- website_field_autocomplete/README.rst | 86 +++++++++++++++++++ website_field_autocomplete/__init__.py | 3 + website_field_autocomplete/__openerp__.py | 21 +++++ .../static/src/js/field_autocomplete.js | 57 ++++++++++++ website_field_autocomplete/views/assets.xml | 14 +++ 5 files changed, 181 insertions(+) create mode 100755 website_field_autocomplete/README.rst create mode 100755 website_field_autocomplete/__init__.py create mode 100755 website_field_autocomplete/__openerp__.py create mode 100644 website_field_autocomplete/static/src/js/field_autocomplete.js create mode 100644 website_field_autocomplete/views/assets.xml diff --git a/website_field_autocomplete/README.rst b/website_field_autocomplete/README.rst new file mode 100755 index 0000000000..e6067ae838 --- /dev/null +++ b/website_field_autocomplete/README.rst @@ -0,0 +1,86 @@ +.. 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 with the +``s_website_autocomplete`` class, such as in the below format: + + + + ++-------------------+--------------------------------------------+------------+----------+ +| 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 +`_. 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 + +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/website_field_autocomplete/__init__.py b/website_field_autocomplete/__init__.py new file mode 100755 index 0000000000..e50cfb2d65 --- /dev/null +++ b/website_field_autocomplete/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 LasLabs Inc. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). diff --git a/website_field_autocomplete/__openerp__.py b/website_field_autocomplete/__openerp__.py new file mode 100755 index 0000000000..a03e970bc5 --- /dev/null +++ b/website_field_autocomplete/__openerp__.py @@ -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, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "website", + ], + "data": [ + 'views/assets.xml', + ], +} diff --git a/website_field_autocomplete/static/src/js/field_autocomplete.js b/website_field_autocomplete/static/src/js/field_autocomplete.js new file mode 100644 index 0000000000..7c74292b22 --- /dev/null +++ b/website_field_autocomplete/static/src/js/field_autocomplete.js @@ -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}; + +}); diff --git a/website_field_autocomplete/views/assets.xml b/website_field_autocomplete/views/assets.xml new file mode 100644 index 0000000000..329407b0b3 --- /dev/null +++ b/website_field_autocomplete/views/assets.xml @@ -0,0 +1,14 @@ + + + + + +