Skip to content

Commit cca0947

Browse files
committed
Merge PR #3246 into 17.0
Signed-off-by pedrobaeza
2 parents c385b42 + 54ae7b5 commit cca0947

File tree

3 files changed

+100
-86
lines changed

3 files changed

+100
-86
lines changed

web_widget_x2many_2d_matrix/demo/res_groups_views.xml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
field_y_axis="partner_id"
1616
field_value="country_id"
1717
>
18-
<tree>[
18+
<tree>
1919
<field name="login" />
2020
<field name="partner_id" />
2121
<field
@@ -27,22 +27,26 @@
2727
/>
2828
</tree>
2929
</field>
30-
<div class="o_form_label">Char field as value</div>
30+
<div
31+
class="o_form_label"
32+
>Char field as value, clickable X axis</div>
3133
<field
3234
name="users"
3335
widget="x2many_2d_matrix"
3436
field_x_axis="country_id"
3537
field_y_axis="partner_id"
3638
field_value="name"
39+
x_axis_clickable="True"
3740
>
38-
<tree>[
39-
<field name="name" />
41+
<tree>
42+
<field name="name" required="login == 'admin'" />
4043
<field name="partner_id" />
4144
<field
4245
name="country_id"
4346
domain="[('name', 'like', 'land')]"
4447
options="{'no_create': True}"
4548
/>
49+
<field name="login" />
4650
</tree>
4751
</field>
4852
<div class="o_form_label">Boolean field as value</div>
@@ -53,7 +57,7 @@
5357
field_y_axis="partner_id"
5458
field_value="active"
5559
>
56-
<tree>[
60+
<tree>
5761
<field name="active" />
5862
<field name="partner_id" />
5963
<field
@@ -71,7 +75,7 @@
7175
field_y_axis="partner_id"
7276
field_value="lang"
7377
>
74-
<tree>[
78+
<tree>
7579
<field name="lang" />
7680
<field name="partner_id" />
7781
<field
@@ -99,6 +103,26 @@
99103
/>
100104
</tree>
101105
</field>
106+
<div
107+
class="o_form_label"
108+
>Float field as value with float_time widget</div>
109+
<field
110+
name="users"
111+
widget="x2many_2d_matrix"
112+
field_x_axis="country_id"
113+
field_y_axis="partner_id"
114+
field_value="partner_latitude"
115+
>
116+
<tree>
117+
<field name="partner_latitude" widget="float_time" />
118+
<field name="partner_id" />
119+
<field
120+
name="country_id"
121+
domain="[('name', 'like', 'land')]"
122+
options="{'no_create': True}"
123+
/>
124+
</tree>
125+
</field>
102126
</page>
103127
</notebook>
104128
</field>

web_widget_x2many_2d_matrix/static/src/components/x2many_2d_matrix_renderer/x2many_2d_matrix_renderer.esm.js

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
import {Component, onWillUpdateProps} from "@odoo/owl";
44
import {evaluateBooleanExpr, evaluateExpr} from "@web/core/py_js/py";
55
import {Domain} from "@web/core/domain";
6+
import {Record} from "@web/model/relational_model/record";
67
import {getFieldContext} from "@web/model/relational_model/utils";
7-
import {registry} from "@web/core/registry";
8-
const fieldRegistry = registry.category("fields");
98

109
export 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
}

web_widget_x2many_2d_matrix/static/src/components/x2many_2d_matrix_renderer/x2many_2d_matrix_renderer.xml

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,58 @@
1414
t-key="column.value"
1515
class="text-center"
1616
>
17-
<t t-if="props.isXClickable and _getXAxisField().relation">
18-
<a
19-
class="o_form_uri"
20-
t-attf-href="/odoo/{{_getXAxisField().relation}}/{{column.value}}"
21-
>
22-
<t t-esc="column.text" />
23-
</a>
24-
</t>
25-
<t t-else="" t-esc="column.text" />
17+
<t
18+
t-component="xFieldComponent"
19+
t-props="_getAxisFieldProps(column.rawValue, 'x')"
20+
/>
2621
</th>
2722
<th t-if="props.showRowTotals" />
2823
</tr>
2924
</thead>
3025
<tbody>
3126
<tr t-foreach="rows" t-as="row" t-key="row.value">
3227
<td>
33-
<t t-if="props.isYClickable and _getYAxisField().relation">
34-
<a
35-
class="o_form_uri"
36-
t-attf-href="/odoo/{{_getYAxisField().relation}}/{{row.value}}"
37-
>
38-
<t t-esc="row.text" />
39-
</a>
40-
</t>
41-
<t t-else="" t-esc="row.text" />
28+
<t
29+
t-component="yFieldComponent"
30+
t-props="_getAxisFieldProps(row.rawValue, 'y')"
31+
/>
4232
</td>
4333
<td t-foreach="columns" t-as="column" t-key="column.value">
4434
<t
4535
t-set="value_field_props"
46-
t-value="getValueFieldProps(column.value, row.value)"
36+
t-value="_getValueFieldProps(column.value, row.value)"
4737
/>
4838
<t
4939
t-if="value_field_props"
50-
t-component="ValueFieldComponent"
40+
t-component="valueFieldComponent"
5141
t-props="value_field_props"
5242
/>
5343
</td>
5444
<td
5545
t-if="props.showRowTotals and _canAggregate()"
5646
class="row-total"
5747
>
58-
<span t-esc="_aggregateRow(row.value)" />
48+
<t
49+
t-component="valueFieldComponent"
50+
t-props="_getAggregateProps(_aggregateRow(row.value))"
51+
/>
5952
</td>
6053
</tr>
6154
</tbody>
6255
<tfoot>
6356
<tr t-if="props.showColumnTotals and _canAggregate()">
6457
<th />
6558
<th t-foreach="columns" t-as="column" t-key="column.value">
66-
<span t-esc="_aggregateColumn(column.value)" />
59+
<t
60+
t-component="valueFieldComponent"
61+
t-props="_getAggregateProps(_aggregateColumn(column.value))"
62+
/>
6763
</th>
6864
<th t-if="props.showRowTotals" class="col-total">
69-
<span t-esc="_aggregateAll()" />
65+
<t
66+
t-component="valueFieldComponent"
67+
t-props="_getAggregateProps(_aggregateAll())"
68+
/>
7069
</th>
7170
</tr>
7271
</tfoot>

0 commit comments

Comments
 (0)