Skip to content

Commit 38cdac4

Browse files
committed
[MIG] website_field_autocomplete: Migration to 17.0
1 parent 8f96ff1 commit 38cdac4

File tree

13 files changed

+108
-115
lines changed

13 files changed

+108
-115
lines changed

website_field_autocomplete/__manifest__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Website Field - AutoComplete",
66
"summary": "Provides an autocomplete field for Website on any model",
7-
"version": "14.0.1.0.0",
7+
"version": "17.0.1.0.0",
88
"category": "Website",
99
"website": "https://github.com/OCA/website",
1010
"author": "LasLabs, Odoo Community Association (OCA)",
@@ -14,9 +14,13 @@
1414
"depends": [
1515
"website",
1616
],
17-
"data": [
18-
"views/assets.xml",
19-
],
17+
"assets": {
18+
"web.assets_frontend": [
19+
"website_field_autocomplete/static/src/lib/jquery.ui/jquery-ui.css",
20+
"website_field_autocomplete/static/src/lib/jquery.ui/jquery-ui.js",
21+
"website_field_autocomplete/static/src/js/field_autocomplete.esm.js",
22+
],
23+
},
2024
"demo": [
2125
"demo/field_autocomplete_demo.xml",
2226
],

website_field_autocomplete/controllers/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def _get_field_autocomplete(self, model, **kwargs):
2121
domain = kwargs.get("domain", [])
2222
fields = kwargs.get("fields", [])
2323
limit = kwargs.get("limit", None)
24-
res = self._get_autocomplete_data(model, domain, fields, limit)
24+
res = self._fetch_autocomplete_data(model, domain, fields, limit)
2525
return list(res.values())
2626

27-
def _get_autocomplete_data(self, model, domain, fields, limit=None):
27+
def _fetch_autocomplete_data(self, model, domain, fields, limit=None):
2828
"""Gets and returns raw record data
2929
Params:
3030
model: Model name to query on
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/** @odoo-module **/
2+
import publicWidget from "@web/legacy/js/public/public_widget";
3+
/* Copyright 2016 LasLabs Inc.
4+
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
5+
*/
6+
7+
publicWidget.registry.field_autocomplete = publicWidget.Widget.extend({
8+
selector: ".js_website_autocomplete",
9+
10+
/* Query remote server for autocomplete suggestions
11+
* @param request object Request from jQueryUI Autocomplete
12+
* @param response function Callback for response, accepts array of str
13+
*/
14+
autocomplete: function (request, response) {
15+
let domain = [[this.queryField, "ilike", request.term]];
16+
if (this.add_domain) {
17+
domain = domain.concat(this.add_domain);
18+
}
19+
return $.ajax({
20+
dataType: "json",
21+
url: "/website/field_autocomplete/" + this.model,
22+
contentType: "application/json; charset=utf-8",
23+
type: "POST",
24+
data: JSON.stringify({
25+
jsonrpc: "2.0",
26+
method: "call",
27+
params: {domain: domain, fields: this.fields, limit: this.limit},
28+
}),
29+
}).then((records) => {
30+
const data = records.result.reduce((a, b) => {
31+
a.push({label: b[this.displayField], value: b[this.valueField]});
32+
return a;
33+
}, []);
34+
response(data);
35+
return records;
36+
});
37+
},
38+
39+
/* Return arguments that are used to initialize autocomplete */
40+
autocompleteArgs: function () {
41+
const self = this;
42+
return {
43+
source: (request, response) => {
44+
this.autocomplete(request, response);
45+
},
46+
focus: function (event, ui) {
47+
self.$target.val(ui.item.label);
48+
self.many2oneCompatibility(ui.item);
49+
return false;
50+
},
51+
select: function (event, ui) {
52+
self.$target.val(ui.item.label);
53+
self.many2oneCompatibility(ui.item);
54+
return false;
55+
},
56+
};
57+
},
58+
59+
start: function () {
60+
this.model = this.$target.data("model");
61+
this.queryField = this.$target.data("query-field") || "name";
62+
this.displayField = this.$target.data("display-field") || this.queryField;
63+
this.field = this.$target.data("field") || this.field;
64+
this.valueField = this.$target.data("value-field") || this.displayField;
65+
this.limit = this.$target.data("limit") || 10;
66+
this.add_domain = this.$target.data("domain");
67+
this.fields = [this.displayField];
68+
if (this.valueField !== this.displayField) {
69+
this.fields.push(this.valueField);
70+
}
71+
this.$target.autocomplete(this.autocompleteArgs());
72+
return this._super(...arguments);
73+
},
74+
75+
many2oneCompatibility: function (item) {
76+
if (!this.field) return;
77+
let formField = $(this.el.closest("form")).find(`input[name='${this.field}']`);
78+
if (!formField.length) {
79+
formField = $(this.el.closest("form")).append(
80+
`<input class="d-none" name='${this.field}' />`
81+
);
82+
}
83+
formField.val(item.value);
84+
},
85+
});

website_field_autocomplete/static/src/js/field_autocomplete.js

Lines changed: 0 additions & 91 deletions
This file was deleted.
6.83 KB
Loading
6.82 KB
Loading
4.44 KB
Loading
6.83 KB
Loading
4.44 KB
Loading
6.15 KB
Loading

0 commit comments

Comments
 (0)