Skip to content

Commit

Permalink
[MIG] web_widget_x2many_2d_matrix: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SodexisTeam committed Mar 25, 2024
1 parent d2cb8c3 commit af630f9
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 143 deletions.
2 changes: 2 additions & 0 deletions web_widget_x2many_2d_matrix/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Authors
* Camptocamp
* CorporateHub
* Onestein
* SodexisTeam

Contributors
------------
Expand All @@ -219,6 +220,7 @@ Contributors

- Adrià Gil Sorribes <adria.gil@forgeflow.com>
- Christopher Ormaza <chris.ormaza@forgeflow.com>
- SodexisTeam <dev@sodexis.com>

Maintainers
-----------
Expand Down
4 changes: 2 additions & 2 deletions web_widget_x2many_2d_matrix/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "2D matrix for x2many fields",
"version": "16.0.1.1.3",
"version": "17.0.1.0.0",
"maintainers": ["ChrisOForgeFlow"],
"development_status": "Production/Stable",
"author": (
Expand All @@ -14,6 +14,7 @@
"Camptocamp, "
"CorporateHub, "
"Onestein, "
"SodexisTeam, "
"Odoo Community Association (OCA)"
),
"website": "https://github.com/OCA/web",
Expand All @@ -35,7 +36,6 @@
"x2many_2d_matrix_field.xml",
"web_widget_x2many_2d_matrix/static/src/components/x2many_2d_matrix_field/"
"x2many_2d_matrix_field.scss",
"web_widget_x2many_2d_matrix/static/src/views/fields/boolean/boolean_field.esm.js",
],
},
}
1 change: 1 addition & 0 deletions web_widget_x2many_2d_matrix/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- Alexey Pelykh \<<alexey.pelykh@corphub.eu>\>
- Adrià Gil Sorribes \<<adria.gil@forgeflow.com>\>
- Christopher Ormaza \<<chris.ormaza@forgeflow.com>\>
- SodexisTeam \<<dev@sodexis.com>\>
2 changes: 2 additions & 0 deletions web_widget_x2many_2d_matrix/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ <h2><a class="toc-backref" href="#toc-entry-9">Authors</a></h2>
<li>Camptocamp</li>
<li>CorporateHub</li>
<li>Onestein</li>
<li>SodexisTeam</li>
</ul>
</div>
<div class="section" id="contributors">
Expand All @@ -574,6 +575,7 @@ <h2><a class="toc-backref" href="#toc-entry-10">Contributors</a></h2>
</li>
<li>Adrià Gil Sorribes &lt;<a class="reference external" href="mailto:adria.gil&#64;forgeflow.com">adria.gil&#64;forgeflow.com</a>&gt;</li>
<li>Christopher Ormaza &lt;<a class="reference external" href="mailto:chris.ormaza&#64;forgeflow.com">chris.ormaza&#64;forgeflow.com</a>&gt;</li>
<li>SodexisTeam &lt;<a class="reference external" href="mailto:dev&#64;sodexis.com">dev&#64;sodexis.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,89 +12,46 @@ export class X2Many2DMatrixField extends Component {
}

getList() {
return this.props.value;
return this.props.record.data[this.props.name];
}

get list() {
return this.getList();
}

_getDefaultRecordValues() {
return {};
}

async commitChange(x, y, value) {
const fields = this.props.matrixFields;
const values = this._getDefaultRecordValues();

const matchingRecords = this.list.records.filter((record) => {
let recordX = record.data[fields.x];
let recordY = record.data[fields.y];
if (record.fields[fields.x].type === "many2one") {
recordX = recordX[0];
}
if (record.fields[fields.y].type === "many2one") {
recordY = recordY[0];
}
return recordX === x && recordY === y;
});
if (matchingRecords.length === 1) {
values[fields.value] = value;
await matchingRecords[0].update(values);
} else {
values[fields.x] = x;
values[fields.y] = y;

if (this.list.fields[this.props.matrixFields.x].type === "many2one") {
values[fields.x] = [x, "/"];
}
if (this.list.fields[this.props.matrixFields.y].type === "many2one") {
values[fields.y] = [y, "/"];
}

let total = 0;
if (matchingRecords.length) {
total = matchingRecords
.map((r) => r.data[fields.value])
.reduce((aggr, v) => aggr + v);
}
const diff = value - total;
values[fields.value] = diff;
const record = await this.list.addNew({
mode: "edit",
});
await record.update(values);
}
this.props.setDirty(false);
}
}

X2Many2DMatrixField.template = "web_widget_x2many_2d_matrix.X2Many2DMatrixField";
X2Many2DMatrixField.props = {
...standardFieldProps,
matrixFields: Object,
isXClickable: Boolean,
isYClickable: Boolean,
showRowTotals: Boolean,
showColumnTotals: Boolean,
list: {type: Object, optional: true},
matrixFields: {type: Object, optional: true},
isXClickable: {type: Boolean, optional: true},
isYClickable: {type: Boolean, optional: true},
showRowTotals: {type: Boolean, optional: true},
showColumnTotals: {type: Boolean, optional: true},
};

X2Many2DMatrixField.components = {X2Many2DMatrixRenderer};
X2Many2DMatrixField.extractProps = ({attrs}) => {
return {
matrixFields: {
value: attrs.field_value,
x: attrs.field_x_axis,
y: attrs.field_y_axis,
},
isXClickable: archParseBoolean(attrs.x_axis_clickable),
isYClickable: archParseBoolean(attrs.y_axis_clickable),
showRowTotals:
"show_row_totals" in attrs ? archParseBoolean(attrs.show_row_totals) : true,
showColumnTotals:
"show_column_totals" in attrs
? archParseBoolean(attrs.show_column_totals)
: true,
};
export const x2Many2DMatrixField = {
component: X2Many2DMatrixField,
extractProps({attrs}) {
return {
matrixFields: {
value: attrs.field_value,
x: attrs.field_x_axis,
y: attrs.field_y_axis,
},
isXClickable: archParseBoolean(attrs.x_axis_clickable),
isYClickable: archParseBoolean(attrs.y_axis_clickable),
showRowTotals:
"show_row_totals" in attrs
? archParseBoolean(attrs.show_row_totals)
: true,
showColumnTotals:
"show_column_totals" in attrs
? archParseBoolean(attrs.show_column_totals)
: true,
};
},
};

registry.category("fields").add("x2many_2d_matrix", X2Many2DMatrixField);
registry.category("fields").add("x2many_2d_matrix", x2Many2DMatrixField);
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ $x2many_2d_matrix_max_height: 450px;
}

> td {
text-align: left;
text-align: right;

&:first-child {
position: sticky;
left: 0;
text-align: left;
border-right-width: 1px;
border-right-color: $gray-300;
border-right-style: solid;
Expand All @@ -56,8 +57,7 @@ $x2many_2d_matrix_max_height: 450px;
}

> tfoot > tr > th {
padding: 0.75rem;
text-align: left;
text-align: right;
background-color: $o-list-footer-bg-color;
position: sticky;
bottom: 0;
Expand All @@ -67,6 +67,7 @@ $x2many_2d_matrix_max_height: 450px;
border-left-width: 1px;
border-left-color: $gray-300;
border-left-style: solid;
background-color: white;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates>
<t t-name="web_widget_x2many_2d_matrix.X2Many2DMatrixField" owl="1">
<t t-name="web_widget_x2many_2d_matrix.X2Many2DMatrixField">
<div class="table-responsive">
<X2Many2DMatrixRenderer
list="list"
matrixFields="props.matrixFields"
showRowTotals="props.showRowTotals"
showColumnTotals="props.showColumnTotals"
setDirty="(isDirty) => this.setDirty(isDirty)"
onUpdate="(x, y, value) => this.commitChange(x, y, value)"
readonly="props.readonly"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import {Component, onWillUpdateProps} from "@odoo/owl";
import {registry} from "@web/core/registry";
const fieldRegistry = registry.category("fields");

export class X2Many2DMatrixRenderer extends Component {
setup() {
this.ValueFieldComponent = this._getValueFieldComponent();
this.columns = this._getColumns();
this.rows = this._getRows();
this.matrix = this._getMatrix();
this.ValueFieldType = this._getValueFieldType();

onWillUpdateProps((newProps) => {
this.columns = this._getColumns(newProps.list.records);
Expand Down Expand Up @@ -90,30 +92,48 @@ export class X2Many2DMatrixRenderer extends Component {
}

_getValueFieldComponent() {
const field = this.list.activeFields[this.matrixFields.value];
const field = this.list.fields[this.matrixFields.value];
if (!field.widget) {
return this.list.activeFields[this.matrixFields.value].FieldComponent;
return fieldRegistry.get(field.type).component;
}
return registry.category("fields").get(field.widget);
return fieldRegistry.get(field.widget).component;
}

_getValueFieldType() {
const field = this.list.fields[this.matrixFields.value];
return field.type;
}

_aggregateRow(row) {
const y = this.rows.findIndex((r) => r.value === row);
return this.matrix[y].map((r) => r.value).reduce((aggr, x) => aggr + x);
const total = this.matrix[y].map((r) => r.value).reduce((aggr, x) => aggr + x);
if (this.ValueFieldType === "integer") {
return total;
}
return Number(total).toFixed(2);
}

_aggregateColumn(column) {
const x = this.columns.findIndex((c) => c.value === column);
return this.matrix

const total = this.matrix
.map((r) => r[x])
.map((r) => r.value)
.reduce((aggr, y) => aggr + y);
if (this.ValueFieldType === "integer") {
return total;
}
return Number(total).toFixed(2);
}

_aggregateAll() {
return this.matrix
const total = this.matrix
.map((r) => r.map((x) => x.value).reduce((aggr, x) => aggr + x))
.reduce((aggr, y) => aggr + y);
if (this.ValueFieldType === "integer") {
return total;
}
return Number(total).toFixed(2);
}

_canAggregate() {
Expand All @@ -122,18 +142,9 @@ export class X2Many2DMatrixRenderer extends Component {
);
}

update(x, y, value) {
this.matrix[y][x].value = value;
const xFieldValue = this.columns[x].value;
const yFieldValue = this.rows[y].value;

this.props.onUpdate(xFieldValue, yFieldValue, value);
}

getValueFieldProps(column, row) {
const x = this.columns.findIndex((c) => c.value === column);
const y = this.rows.findIndex((r) => r.value === row);
const {props, propsFromAttrs} = this.list.activeFields[this.matrixFields.value];
let record = null;
let value = null;
if (
Expand All @@ -144,18 +155,9 @@ export class X2Many2DMatrixRenderer extends Component {
record = this.matrix[y][x].records[0];
value = this.matrix[y][x].value;
}
if (this.list.fields[this.matrixFields.value].type === "boolean") {
record.bypass_readonly = true;
}
value =
!this._canAggregate() && record
? record.data[this.matrixFields.value]
: value;
value = record ? record.data[this.matrixFields.value] : value;
this.matrix[y][x].value = value;
const result = {
...props,
...propsFromAttrs,
value: value,
update: (val) => this.update(x, y, val),
readonly: this.props.readonly,
record: record,
name: this.matrixFields.value,
Expand All @@ -169,11 +171,9 @@ export class X2Many2DMatrixRenderer extends Component {

X2Many2DMatrixRenderer.template = "web_widget_x2many_2d_matrix.X2Many2DMatrixRenderer";
X2Many2DMatrixRenderer.props = {
list: Object,
matrixFields: Object,
setDirty: Function,
onUpdate: Function,
readonly: Boolean,
showRowTotals: Boolean,
showColumnTotals: Boolean,
list: {type: Object, optional: true},
matrixFields: {type: Object, optional: true},
readonly: {type: Boolean, optional: true},
showRowTotals: {type: Boolean, optional: true},
showColumnTotals: {type: Boolean, optional: true},
};
Loading

0 comments on commit af630f9

Please sign in to comment.