Skip to content

Commit 7b413b0

Browse files
committed
[IMP] web: tree processor service
We create a new service tree_processor whose purpose is to ease the processing of condition trees (~ domain). In that way, we are able to simplify some code, notably treeFromDomain. closes odoo#216038 Related: odoo/enterprise#88759 Signed-off-by: Michaël Mattiello (mcm) <[email protected]>
1 parent ce23749 commit 7b413b0

File tree

12 files changed

+447
-481
lines changed

12 files changed

+447
-481
lines changed

addons/web/static/src/core/domain_selector/domain_selector.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { Domain } from "@web/core/domain";
44
import { getDomainDisplayedOperators } from "@web/core/domain_selector/domain_selector_operator_editor";
55
import { _t } from "@web/core/l10n/translation";
66
import { ModelFieldSelector } from "@web/core/model_field_selector/model_field_selector";
7-
import { condition, formatValue } from "@web/core/tree_editor/condition_tree";
8-
import { constructTreeFromDomain } from "@web/core/tree_editor/construct_tree_from_domain";
7+
import {
8+
areEqualTrees,
9+
condition,
10+
connector,
11+
formatValue,
12+
} from "@web/core/tree_editor/condition_tree";
913
import { domainFromTree } from "@web/core/tree_editor/domain_from_tree";
1014
import { TreeEditor } from "@web/core/tree_editor/tree_editor";
1115
import { getOperatorEditorInfo } from "@web/core/tree_editor/tree_editor_operator_editor";
12-
import { treeFromDomain } from "@web/core/tree_editor/tree_from_domain";
13-
import { useMakeGetFieldDef } from "@web/core/tree_editor/utils";
1416
import { useService } from "@web/core/utils/hooks";
15-
import { deepEqual } from "@web/core/utils/objects";
1617
import { getDefaultCondition } from "./utils";
1718

1819
const ARCHIVED_CONDITION = condition("active", "in", [true, false]);
@@ -41,7 +42,7 @@ export class DomainSelector extends Component {
4142

4243
setup() {
4344
this.fieldService = useService("field");
44-
this.makeGetFieldDef = useMakeGetFieldDef(this.fieldService);
45+
this.treeProcessor = useService("tree_processor");
4546

4647
this.tree = null;
4748
this.showArchivedCheckbox = false;
@@ -66,23 +67,19 @@ export class DomainSelector extends Component {
6667
return;
6768
}
6869

69-
const getFieldDef = await this.makeGetFieldDef(
70-
p.resModel,
71-
constructTreeFromDomain(domain),
72-
["active"]
73-
);
70+
const [tree, { fieldDef: activeFieldDef }] = await Promise.all([
71+
this.treeProcessor.treeFromDomain(p.resModel, domain, !p.isDebugMode),
72+
this.fieldService.loadFieldInfo(p.resModel, "active"),
73+
]);
7474

75-
this.tree = treeFromDomain(domain, {
76-
getFieldDef,
77-
distributeNot: !p.isDebugMode,
78-
});
75+
this.tree = tree;
76+
this.showArchivedCheckbox = this.getShowArchivedCheckBox(Boolean(activeFieldDef), p);
7977

80-
this.showArchivedCheckbox = this.getShowArchivedCheckBox(Boolean(getFieldDef("active")), p);
8178
this.includeArchived = false;
8279
if (this.showArchivedCheckbox) {
8380
if (this.tree.value === "&") {
8481
this.tree.children = this.tree.children.filter((child) => {
85-
if (deepEqual(child, ARCHIVED_CONDITION)) {
82+
if (areEqualTrees(child, ARCHIVED_CONDITION)) {
8683
this.includeArchived = true;
8784
return false;
8885
}
@@ -91,9 +88,9 @@ export class DomainSelector extends Component {
9188
if (this.tree.children.length === 1) {
9289
this.tree = this.tree.children[0];
9390
}
94-
} else if (deepEqual(this.tree, ARCHIVED_CONDITION)) {
91+
} else if (areEqualTrees(this.tree, ARCHIVED_CONDITION)) {
9592
this.includeArchived = true;
96-
this.tree = treeFromDomain(`[]`);
93+
this.tree = connector("&");
9794
}
9895
}
9996
}

addons/web/static/src/core/tree_editor/domain_contains_expressions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Expression, isTree } from "./condition_tree";
2-
import { treeFromDomain } from "./tree_from_domain";
2+
import { constructTreeFromDomain } from "./construct_tree_from_domain";
33

44
function treeContainsExpressions(tree) {
55
if (tree.type === "condition") {
@@ -24,7 +24,7 @@ function treeContainsExpressions(tree) {
2424
export function domainContainsExpressions(domain) {
2525
let tree;
2626
try {
27-
tree = treeFromDomain(domain);
27+
tree = constructTreeFromDomain(domain);
2828
} catch {
2929
return null;
3030
}

addons/web/static/src/core/tree_editor/tree_editor.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import {
66
getDefaultValue,
77
getValueEditorInfo,
88
} from "@web/core/tree_editor/tree_editor_value_editors";
9-
import {
10-
getResModel,
11-
useMakeGetConditionDescription,
12-
useMakeGetFieldDef,
13-
} from "@web/core/tree_editor/utils";
9+
import { getResModel } from "@web/core/tree_editor/utils";
1410
import { areEquivalentTrees } from "@web/core/tree_editor/virtual_operators";
1511
import { useService } from "@web/core/utils/hooks";
1612
import { shallowEqual } from "@web/core/utils/objects";
@@ -47,12 +43,7 @@ export class TreeEditor extends Component {
4743
setup() {
4844
this.isTree = isTree;
4945
this.fieldService = useService("field");
50-
this.nameService = useService("name");
51-
this.makeGetFieldDef = useMakeGetFieldDef(this.fieldService);
52-
this.makeGetConditionDescription = useMakeGetConditionDescription(
53-
this.fieldService,
54-
this.nameService
55-
);
46+
this.treeProcessor = useService("tree_processor");
5647
onWillStart(() => this.onPropsUpdated(this.props));
5748
onWillUpdateProps((nextProps) => this.onPropsUpdated(nextProps));
5849
}
@@ -79,16 +70,15 @@ export class TreeEditor extends Component {
7970
async prepareInfo(props) {
8071
const [fieldDefs, getFieldDef] = await Promise.all([
8172
this.fieldService.loadFields(props.resModel),
82-
this.makeGetFieldDef(props.resModel, this.tree),
73+
this.treeProcessor.makeGetFieldDef(props.resModel, this.tree),
8374
]);
8475
this.getFieldDef = getFieldDef;
8576
this.defaultCondition = props.getDefaultCondition(fieldDefs);
8677

8778
if (props.readonly) {
88-
this.getConditionDescription = await this.makeGetConditionDescription(
79+
this.getConditionDescription = await this.treeProcessor.makeGetConditionDescription(
8980
props.resModel,
90-
this.tree,
91-
this.getFieldDef
81+
this.tree
9282
);
9383
}
9484
}

0 commit comments

Comments
 (0)