From 176d3be297e2d2db55ce821ea2e9f50e5925ae87 Mon Sep 17 00:00:00 2001 From: Pierre Rousseau Date: Tue, 3 Oct 2023 10:07:58 +0200 Subject: [PATCH] [IMP] web: add support for domain and context in DomainSelectorAutocomplete This commit adds support for domain and context in DomainSelectorAutocomplete. This is a step towards the simplification of the Record component. We would like to remove the case where the Record component is used with fake resModel/resId only to display a Many2ManyTags or a Many2One. Instead, we could simply use the DomainSelectorAutocomplete or DomainSelectorSingleAutocomplete to achieve the same result. Part of task-id 3433922 Part-of: odoo/odoo#137650 --- .../domain_selector_autocomplete.js | 20 +++++++++++++++++-- .../domain_selector_autocomplete.xml | 4 ++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.js b/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.js index 2cb81cb6eb7..3db747068b4 100644 --- a/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.js +++ b/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.js @@ -3,6 +3,7 @@ import { Component, onWillStart, onWillUpdateProps } from "@odoo/owl"; import { AutoComplete } from "@web/core/autocomplete/autocomplete"; import { _t } from "@web/core/l10n/translation"; +import { Domain } from "@web/core/domain"; import { formatAST, toPyValue } from "@web/core/py_js/py_utils"; import { registry } from "@web/core/registry"; import { TagsList } from "@web/core/tags_list/tags_list"; @@ -21,6 +22,8 @@ class AutoCompleteWithSources extends Component { multiSelect: Boolean, getIds: Function, value: String, + domain: { type: Array, optional: true }, + context: { type: Object, optional: true }, className: { type: String, optional: true }, fieldString: { type: String, optional: true }, }; @@ -101,6 +104,7 @@ class AutoCompleteWithSources extends Component { resModel, noCreate: true, multiSelect, + context: this.props.context || {}, onSelected: (resId) => { const resIds = Array.isArray(resId) ? resId : [resId]; this.props.update([...resIds]); @@ -108,6 +112,14 @@ class AutoCompleteWithSources extends Component { }); } + getDomain() { + const domainIds = Domain.not([["id", "in", this.getIds()]]); + if (this.props.domain) { + return Domain.and([this.props.domain, domainIds]).toList(); + } + return domainIds.toList(); + } + onSelect({ value: resId, action }, params) { if (action) { return action(params); @@ -116,12 +128,12 @@ class AutoCompleteWithSources extends Component { } search(name, limit) { - const ids = this.getIds(); - const domain = [["id", "not in", ids]]; + const domain = this.getDomain(); return this.orm.call(this.props.resModel, "name_search", [], { name, args: domain, limit, + context: this.props.context || {}, }); } @@ -156,6 +168,8 @@ export class DomainSelectorAutocomplete extends Component { resModel: String, update: Function, value: true, + domain: { type: Array, optional: true }, + context: { type: Object, optional: true }, fieldString: { type: String, optional: true }, }; static components = { AutoCompleteWithSources, TagsList }; @@ -206,6 +220,8 @@ export class DomainSelectorSingleAutocomplete extends Component { static props = { resModel: String, update: Function, + domain: { type: Array, optional: true }, + context: { type: Object, optional: true }, value: true, fieldString: { type: String, optional: true }, }; diff --git a/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.xml b/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.xml index e5b81d04448..b6e7453c79c 100644 --- a/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.xml +++ b/addons/web/static/src/core/domain_selector/domain_selector_autocomplete.xml @@ -19,6 +19,8 @@