From 62e6e297a8ada39d02e99303f7ed7e52595fe0c7 Mon Sep 17 00:00:00 2001 From: Laurent Stukkens Date: Tue, 16 Jan 2024 19:16:41 +0100 Subject: [PATCH] [IMP] mis_builder: take field context into account Prior to this commit only the action context was taken into account when looking for the `mis_analytic_domain` key in the context. This commit adds the support of the field context, where `self` can be used in order to inject record data in the context. --- .../src/components/mis_report_widget.esm.js | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/mis_builder/static/src/components/mis_report_widget.esm.js b/mis_builder/static/src/components/mis_report_widget.esm.js index e05e5698c..16d8fd6cf 100644 --- a/mis_builder/static/src/components/mis_report_widget.esm.js +++ b/mis_builder/static/src/components/mis_report_widget.esm.js @@ -4,6 +4,7 @@ import {Component, onWillStart, useState, useSubEnv} from "@odoo/owl"; import {useBus, useService} from "@web/core/utils/hooks"; import {DatePicker} from "@web/core/datepicker/datepicker"; import {Domain} from "@web/core/domain"; +import {evaluateExpr} from "@web/core/py_js/py"; import {FilterMenu} from "@web/search/filter_menu/filter_menu"; import {SearchBar} from "@web/search/search_bar/search_bar"; import {SearchModel} from "@web/search/search_model"; @@ -121,15 +122,54 @@ export class MisReportWidget extends Component { } } + /** + * The domain built from both the context that is passed through the action + * and the one that is set on the field in the view. The last one being evaluated + * against the context populated with a self property populated with the record data. + * + * @returns Domain + */ + get misc_analytic_domain() { + let domain = Domain.TRUE; + const domain_mis_analytic_domain_key = "mis_analytic_domain"; + // Get the domain that is set on the action + const record_context = this.props.record.context; + if (domain_mis_analytic_domain_key in record_context) { + const record_domain = new Domain(record_context.mis_analytic_domain); + domain = Domain.and([domain, record_domain]); + } + // Get the domain that is set on the field and evaluate it with both the context + // and the record data mounted on a self property. + if ("context" in this.props.record.activeFields[this.props.name]) { + const context_attr = + this.props.record.activeFields[this.props.name].context; + const evaluation_context = { + ...record_context, + self: this.props.record.data, + }; + const field_context = evaluateExpr(context_attr, evaluation_context); + if (domain_mis_analytic_domain_key in field_context) { + const field_domain = new Domain(field_context.mis_analytic_domain); + domain = Domain.and([domain, field_domain]); + } + } + return domain; + } + get context() { let ctx = this.props.record.context; + const mis_analytic_domain = this.misc_analytic_domain; + if (mis_analytic_domain !== Domain.TRUE) { + ctx = { + ...ctx, + mis_analytic_domain: mis_analytic_domain.toList(), + }; + } if (this.showSearchBar && this.searchModel.searchDomain) { ctx = { ...ctx, mis_analytic_domain: Domain.and([ - new Domain( - this.props.record.context.mis_analytic_domain || Domain.TRUE - ), + new Domain(ctx.mis_analytic_domain || Domain.TRUE), new Domain(this.searchModel.searchDomain), ]).toList(), };