33import { Component , onWillUpdateProps } from "@odoo/owl" ;
44import { evaluateBooleanExpr , evaluateExpr } from "@web/core/py_js/py" ;
55import { Domain } from "@web/core/domain" ;
6+ import { Record } from "@web/model/relational_model/record" ;
67import { getFieldContext } from "@web/model/relational_model/utils" ;
7- import { registry } from "@web/core/registry" ;
8- const fieldRegistry = registry . category ( "fields" ) ;
98
109export class X2Many2DMatrixRenderer extends Component {
1110 setup ( ) {
12- this . ValueFieldComponent = this . _getValueFieldComponent ( ) ;
1311 this . columns = this . _getColumns ( ) ;
1412 this . rows = this . _getRows ( ) ;
1513 this . matrix = this . _getMatrix ( ) ;
16- this . ValueFieldType = this . _getValueFieldType ( ) ;
1714
1815 onWillUpdateProps ( ( newProps ) => {
1916 this . columns = this . _getColumns ( newProps . list . records ) ;
@@ -28,6 +25,7 @@ export class X2Many2DMatrixRenderer extends Component {
2825 const column = {
2926 value : record . data [ this . matrixFields . x ] ,
3027 text : record . data [ this . matrixFields . x ] ,
28+ rawValue : record . data [ this . matrixFields . x ] ,
3129 } ;
3230 if ( record . fields [ this . matrixFields . x ] . type === "many2one" ) {
3331 column . text = column . value [ 1 ] ;
@@ -45,6 +43,7 @@ export class X2Many2DMatrixRenderer extends Component {
4543 const row = {
4644 value : record . data [ this . matrixFields . y ] ,
4745 text : record . data [ this . matrixFields . y ] ,
46+ rawValue : record . data [ this . matrixFields . y ] ,
4847 } ;
4948 if ( record . fields [ this . matrixFields . y ] . type === "many2one" ) {
5049 row . text = row . value [ 1 ] ;
@@ -94,62 +93,41 @@ export class X2Many2DMatrixRenderer extends Component {
9493 return this . props . matrixFields ;
9594 }
9695
97- _getValueField ( ) {
98- const field = this . list . fields [ this . matrixFields . value ] ;
99- if ( ! field . widget ) {
100- return fieldRegistry . get ( field . type ) ;
101- }
102- return fieldRegistry . get ( field . widget ) ;
103- }
104-
105- _getValueFieldComponent ( ) {
106- return this . _getValueField ( ) . component ;
96+ get valueFieldComponent ( ) {
97+ return this . props . list_view . fieldNodes [ this . matrixFields . value + "_0" ] . field
98+ . component ;
10799 }
108100
109- _getValueFieldType ( ) {
110- const field = this . list . fields [ this . matrixFields . value ] ;
111- return field . type ;
101+ get xFieldComponent ( ) {
102+ return this . props . list_view . fieldNodes [ this . matrixFields . x + "_0" ] . field
103+ . component ;
112104 }
113105
114- _getXAxisField ( ) {
115- return this . list . fields [ this . matrixFields . x ] ;
116- }
117-
118- _getYAxisField ( ) {
119- return this . list . fields [ this . matrixFields . y ] ;
106+ get yFieldComponent ( ) {
107+ return this . props . list_view . fieldNodes [ this . matrixFields . y + "_0" ] . field
108+ . component ;
120109 }
121110
122111 _aggregateRow ( row ) {
123112 const y = this . rows . findIndex ( ( r ) => r . value === row ) ;
124113 const total = this . matrix [ y ] . map ( ( r ) => r . value ) . reduce ( ( aggr , x ) => aggr + x ) ;
125- if ( this . ValueFieldType === "integer" ) {
126- return total ;
127- }
128- return Number ( total ) . toFixed ( 2 ) ;
114+ return total ;
129115 }
130116
131117 _aggregateColumn ( column ) {
132118 const x = this . columns . findIndex ( ( c ) => c . value === column ) ;
133-
134119 const total = this . matrix
135-
136120 . map ( ( r ) => r [ x ] )
137121 . map ( ( r ) => r . value )
138122 . reduce ( ( aggr , y ) => aggr + y ) ;
139- if ( this . ValueFieldType === "integer" ) {
140- return total ;
141- }
142- return Number ( total ) . toFixed ( 2 ) ;
123+ return total ;
143124 }
144125
145126 _aggregateAll ( ) {
146127 const total = this . matrix
147128 . map ( ( r ) => r . map ( ( x ) => x . value ) . reduce ( ( aggr , x ) => aggr + x ) )
148129 . reduce ( ( aggr , y ) => aggr + y ) ;
149- if ( this . ValueFieldType === "integer" ) {
150- return total ;
151- }
152- return Number ( total ) . toFixed ( 2 ) ;
130+ return total ;
153131 }
154132
155133 _canAggregate ( ) {
@@ -158,26 +136,43 @@ export class X2Many2DMatrixRenderer extends Component {
158136 ) ;
159137 }
160138
161- getValueFieldProps ( column , row ) {
139+ _getValueFieldProps ( column , row ) {
162140 const x = this . columns . findIndex ( ( c ) => c . value === column ) ;
163141 const y = this . rows . findIndex ( ( r ) => r . value === row ) ;
164- let record = null ;
165- let value = null ;
166- if (
167- this . matrix [ y ] &&
168- this . matrix [ y ] [ x ] &&
169- ( record = this . matrix [ y ] [ x ] . records [ 0 ] )
170- ) {
171- record = this . matrix [ y ] [ x ] . records [ 0 ] ;
172- value = this . matrix [ y ] [ x ] . value ;
173- }
174- value = record ? record . data [ this . matrixFields . value ] : value ;
175- this . matrix [ y ] [ x ] . value = value ;
142+ const record = this . matrix [ y ] [ x ] . records [ 0 ] ;
143+
176144 if ( ! record ) {
177145 return null ;
178146 }
147+ return this . _getMatrixFieldProps ( record , "value" ) ;
148+ }
149+
150+ _getAxisFieldProps ( value , axis ) {
151+ const fieldName = this . matrixFields [ axis ] ;
152+ const record = new Record ( this . list . model , this . list . _config , {
153+ [ fieldName ] : value ,
154+ } ) ;
155+ const props = this . _getMatrixFieldProps ( record , axis ) ;
156+ if ( this . list . fields [ fieldName ] . type === "many2one" ) {
157+ props . canOpen =
158+ axis === "x" ? this . props . isXClickable : this . props . isYClickable ;
159+ }
160+ props . readonly = true ;
161+ return props ;
162+ }
163+
164+ _getAggregateProps ( value ) {
165+ const record = new Record ( this . list . model , this . list . _config , {
166+ [ this . matrixFields . value ] : value ,
167+ } ) ;
168+ const props = this . _getMatrixFieldProps ( record , "value" ) ;
169+ props . readonly = true ;
170+ return props ;
171+ }
172+
173+ _getMatrixFieldProps ( record , fieldName ) {
179174 const fieldInfo =
180- this . props . list_view . fieldNodes [ this . matrixFields . value + "_0" ] ;
175+ this . props . list_view . fieldNodes [ this . matrixFields [ fieldName ] + "_0" ] ;
181176 const dynamicInfo = {
182177 get context ( ) {
183178 return getFieldContext ( record , fieldInfo . name , fieldInfo . context ) ;
@@ -201,19 +196,15 @@ export class X2Many2DMatrixRenderer extends Component {
201196 record . evalContextWithVirtualIds
202197 ) ,
203198 } ;
204- const valueField = this . _getValueField ( ) ;
205199 const result = {
206200 readonly : dynamicInfo . readonly ,
207201 record : record ,
208- name : this . matrixFields . value ,
209- ...( valueField . extractProps || ( ( ) => ( { } ) ) ) . apply ( valueField , [
202+ name : this . matrixFields [ fieldName ] ,
203+ ...( fieldInfo . field . extractProps || ( ( ) => ( { } ) ) ) . apply ( fieldInfo . field , [
210204 fieldInfo ,
211205 dynamicInfo ,
212206 ] ) ,
213207 } ;
214- if ( value === null ) {
215- result . readonly = true ;
216- }
217208 return result ;
218209 }
219210}
0 commit comments