[IMP] web_widget_x2many_2d_matrix: black, isort

pull/2756/head
Adrià Gil Sorribes 2020-02-14 10:02:14 +01:00 committed by SodexisTeam
parent 8f203482fa
commit 42545a3225
7 changed files with 288 additions and 264 deletions

View File

@ -3,24 +3,20 @@
# Copyright 2018 Simone Orsi <simone.orsi@camptocamp.com> # Copyright 2018 Simone Orsi <simone.orsi@camptocamp.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': '2D matrix for x2many fields', "name": "2D matrix for x2many fields",
'version': '12.0.2.2.0', "version": "12.0.2.2.0",
'author': ( "author": (
'Therp BV, ' "Therp BV, "
'Tecnativa, ' "Tecnativa, "
'Camptocamp, ' "Camptocamp, "
'Brainbean Apps, ' "Brainbean Apps, "
'Odoo Community Association (OCA)' "Odoo Community Association (OCA)"
), ),
'website': 'https://github.com/OCA/web', "website": "https://github.com/OCA/web",
'license': 'AGPL-3', "license": "AGPL-3",
'category': 'Hidden/Dependency', "category": "Hidden/Dependency",
'summary': 'Show list fields as a matrix', "summary": "Show list fields as a matrix",
'depends': [ "depends": ["web",],
'web', "data": ["views/assets.xml",],
], "installable": True,
'data': [
'views/assets.xml',
],
'installable': True,
} }

View File

@ -2,26 +2,25 @@
* Copyright 2018 Brainbean Apps * Copyright 2018 Brainbean Apps
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (require) { odoo.define("web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer", function(require) {
"use strict"; "use strict";
var BasicRenderer = require('web.BasicRenderer'); var BasicRenderer = require("web.BasicRenderer");
var config = require('web.config'); var config = require("web.config");
var core = require('web.core'); var core = require("web.core");
var field_utils = require('web.field_utils'); var field_utils = require("web.field_utils");
var _t = core._t; var _t = core._t;
var FIELD_CLASSES = { var FIELD_CLASSES = {
float: 'o_list_number', float: "o_list_number",
integer: 'o_list_number', integer: "o_list_number",
monetary: 'o_list_number', monetary: "o_list_number",
text: 'o_list_text', text: "o_list_text",
}; };
// X2Many2dMatrixRenderer is heavily inspired by Odoo's ListRenderer // X2Many2dMatrixRenderer is heavily inspired by Odoo's ListRenderer
// and is reusing portions of code from list_renderer.js // and is reusing portions of code from list_renderer.js
var X2Many2dMatrixRenderer = BasicRenderer.extend({ var X2Many2dMatrixRenderer = BasicRenderer.extend({
/** /**
* @override * @override
*/ */
@ -55,32 +54,26 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
_renderView: function() { _renderView: function() {
var self = this; var self = this;
this.$el this.$el.removeClass("table-responsive").empty();
.removeClass('table-responsive')
.empty();
// Display a nice message if there's no data to display // Display a nice message if there's no data to display
if (!self.rows.length) { if (!self.rows.length) {
var $alert = $('<div>', {'class': 'alert alert-info'}); var $alert = $("<div>", {class: "alert alert-info"});
$alert.text(_t('Sorry no matrix data to display.')); $alert.text(_t("Sorry no matrix data to display."));
this.$el.append($alert); this.$el.append($alert);
return this._super(); return this._super();
} }
var $table = $('<table>').addClass( var $table = $("<table>").addClass(
'o_list_view table table-condensed table-striped ' + "o_list_view table table-condensed table-striped " +
'o_x2many_2d_matrix ' "o_x2many_2d_matrix "
); );
this.$el this.$el.addClass("table-responsive").append($table);
.addClass('table-responsive')
.append($table);
this._computeColumnAggregates(); this._computeColumnAggregates();
this._computeRowAggregates(); this._computeRowAggregates();
$table $table.append(this._renderHeader()).append(this._renderBody());
.append(this._renderHeader())
.append(this._renderBody());
if (self.matrix_data.show_column_totals) { if (self.matrix_data.show_column_totals) {
$table.append(this._renderFooter()); $table.append(this._renderFooter());
} }
@ -97,9 +90,9 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} The table body element just filled. * @returns {jQueryElement} The table body element just filled.
*/ */
_renderBody: function() { _renderBody: function() {
var $body = $('<tbody>').append(this._renderRows()); var $body = $("<tbody>").append(this._renderRows());
_.each($body.find('input'), function (td, i) { _.each($body.find("input"), function(td, i) {
$(td).attr('tabindex', i); $(td).attr("tabindex", i);
}); });
return $body; return $body;
}, },
@ -112,15 +105,12 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} The thead element that was inserted into. * @returns {jQueryElement} The thead element that was inserted into.
*/ */
_renderHeader: function() { _renderHeader: function() {
var $tr = $('<tr>').append('<th/>'); var $tr = $("<tr>").append("<th/>");
$tr = $tr.append(_.map( $tr = $tr.append(_.map(this.columns, this._renderHeaderCell.bind(this)));
this.columns,
this._renderHeaderCell.bind(this)
));
if (this.matrix_data.show_row_totals) { if (this.matrix_data.show_row_totals) {
$tr.append($('<th/>', {class: 'total'})); $tr.append($("<th/>", {class: "total"}));
} }
return $('<thead>').append($tr); return $("<thead>").append($tr);
}, },
/** /**
@ -135,27 +125,28 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
_renderHeaderCell: function(node) { _renderHeaderCell: function(node) {
var name = node.attrs.name; var name = node.attrs.name;
var field = this.state.fields[name]; var field = this.state.fields[name];
var $th = $('<th>'); var $th = $("<th>");
if (!field) { if (!field) {
return $th; return $th;
} }
var description = null; var description = null;
if (node.attrs.widget) { if (node.attrs.widget) {
description = this.state.fieldsInfo.list[name] description = this.state.fieldsInfo.list[name].Widget.prototype
.Widget.prototype.description; .description;
} }
if (_.isNull(description)) { if (_.isNull(description)) {
description = node.attrs.string || field.string; description = node.attrs.string || field.string;
} }
$th.text(description).data('name', name); $th.text(description).data("name", name);
if ( if (
field.type === 'float' || field.type === 'integer' || field.type === "float" ||
field.type === 'monetary' field.type === "integer" ||
field.type === "monetary"
) { ) {
$th.addClass('text-right'); $th.addClass("text-right");
} else { } else {
$th.addClass('text-center'); $th.addClass("text-center");
} }
if (config.debug) { if (config.debug) {
@ -178,10 +169,13 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {String} a string with the generated html. * @returns {String} a string with the generated html.
*/ */
_renderRows: function() { _renderRows: function() {
return _.map(this.rows, function (row) { return _.map(
this.rows,
function(row) {
row.attrs.name = this.matrix_data.field_value; row.attrs.name = this.matrix_data.field_value;
return this._renderRow(row); return this._renderRow(row);
}.bind(this)); }.bind(this)
);
}, },
/** /**
@ -195,15 +189,18 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} the <tr> element that has been rendered. * @returns {jQueryElement} the <tr> element that has been rendered.
*/ */
_renderRow: function(row) { _renderRow: function(row) {
var $tr = $('<tr/>', {class: 'o_data_row'}), var $tr = $("<tr/>", {class: "o_data_row"}),
_data = _.without(row.data, undefined); _data = _.without(row.data, undefined);
$tr = $tr.append(this._renderLabelCell(_data[0])); $tr = $tr.append(this._renderLabelCell(_data[0]));
var $cells = _.map(this.columns, function (column, index) { var $cells = _.map(
this.columns,
function(column, index) {
var record = row.data[index]; var record = row.data[index];
// Make the widget use our field value for each cell // Make the widget use our field value for each cell
column.attrs.name = this.matrix_data.field_value; column.attrs.name = this.matrix_data.field_value;
return this._renderBodyCell(record, column, index, {mode:''}); return this._renderBodyCell(record, column, index, {mode: ""});
}.bind(this)); }.bind(this)
);
$tr = $tr.append($cells); $tr = $tr.append($cells);
if (row.aggregate) { if (row.aggregate) {
$tr.append(this._renderAggregateRowCell(row)); $tr.append(this._renderAggregateRowCell(row));
@ -219,9 +216,9 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} the cell that was rendered. * @returns {jQueryElement} the cell that was rendered.
*/ */
_renderLabelCell: function(record) { _renderLabelCell: function(record) {
var $td = $('<td>'); var $td = $("<td>");
var value = record.data[this.matrix_data.field_y_axis]; var value = record.data[this.matrix_data.field_y_axis];
if (value.type === 'record') { if (value.type === "record") {
// We have a related record // We have a related record
value = value.data.display_name; value = value.data.display_name;
} }
@ -238,7 +235,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} The rendered cell. * @returns {jQueryElement} The rendered cell.
*/ */
_renderAggregateRowCell: function(row) { _renderAggregateRowCell: function(row) {
var $cell = $('<td/>', {class: 'row-total'}); var $cell = $("<td/>", {class: "row-total"});
this.applyAggregateValue($cell, row); this.applyAggregateValue($cell, row);
return $cell; return $cell;
}, },
@ -256,24 +253,22 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} the rendered cell. * @returns {jQueryElement} the rendered cell.
*/ */
_renderBodyCell: function(record, node, colIndex, options) { _renderBodyCell: function(record, node, colIndex, options) {
var tdClassName = 'o_data_cell'; var tdClassName = "o_data_cell";
if (node.tag === 'field') { if (node.tag === "field") {
var typeClass = FIELD_CLASSES[ var typeClass = FIELD_CLASSES[this.state.fields[node.attrs.name].type];
this.state.fields[node.attrs.name].type
];
if (typeClass) { if (typeClass) {
tdClassName += ' ' + typeClass; tdClassName += " " + typeClass;
} }
if (node.attrs.widget) { if (node.attrs.widget) {
tdClassName += ' o_' + node.attrs.widget + '_cell'; tdClassName += " o_" + node.attrs.widget + "_cell";
} }
} }
// TODO roadmap: here we should collect possible extra params // TODO roadmap: here we should collect possible extra params
// the user might want to attach to each single cell. // the user might want to attach to each single cell.
var $td = $('<td>', { var $td = $("<td>", {
'class': tdClassName, class: tdClassName,
}); });
if (_.isUndefined(record)) { if (_.isUndefined(record)) {
@ -281,8 +276,8 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
return $td; return $td;
} }
$td.attr({ $td.attr({
'data-form-id': record.id, "data-form-id": record.id,
'data-id': record.data.id, "data-id": record.data.id,
}); });
// We register modifiers on the <td> element so that it gets // We register modifiers on the <td> element so that it gets
@ -291,7 +286,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
node, node,
record, record,
$td, $td,
_.pick(options, 'mode') _.pick(options, "mode")
); );
// If the invisible modifiers is true, the <td> element is // If the invisible modifiers is true, the <td> element is
// left empty. Indeed, if the modifiers was to change the // left empty. Indeed, if the modifiers was to change the
@ -303,10 +298,10 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
// Enforce mode of the parent // Enforce mode of the parent
options.mode = this.getParent().mode; options.mode = this.getParent().mode;
if (node.tag === 'widget') { if (node.tag === "widget") {
return $td.append(this._renderWidget(record, node)); return $td.append(this._renderWidget(record, node));
} }
var $el = this._renderFieldWidget(node, record, _.pick(options, 'mode')); var $el = this._renderFieldWidget(node, record, _.pick(options, "mode"));
this._handleAttributes($el, node); this._handleAttributes($el, node);
return $td.append($el); return $td.append($el);
}, },
@ -320,12 +315,14 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
_renderFooter: function() { _renderFooter: function() {
var $cells = this._renderAggregateColCells(); var $cells = this._renderAggregateColCells();
if ($cells) { if ($cells) {
var $tr = $('<tr>').append('<td/>').append($cells); var $tr = $("<tr>")
.append("<td/>")
.append($cells);
var $total_cell = this._renderTotalCell(); var $total_cell = this._renderTotalCell();
if ($total_cell) { if ($total_cell) {
$tr.append($total_cell); $tr.append($total_cell);
} }
return $('<tfoot>').append($tr); return $("<tfoot>").append($tr);
} }
}, },
@ -336,12 +333,14 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
* @returns {jQueryElement} The td element with the total in it. * @returns {jQueryElement} The td element with the total in it.
*/ */
_renderTotalCell: function() { _renderTotalCell: function() {
if (!this.matrix_data.show_column_totals || if (
!this.matrix_data.show_row_totals) { !this.matrix_data.show_column_totals ||
!this.matrix_data.show_row_totals
) {
return; return;
} }
var $cell = $('<td>', {class: 'col-total'}); var $cell = $("<td>", {class: "col-total"});
this.applyAggregateValue($cell, this.total); this.applyAggregateValue($cell, this.total);
return $cell; return $cell;
}, },
@ -356,12 +355,12 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
var self = this; var self = this;
return _.map(this.columns, function(column) { return _.map(this.columns, function(column) {
var $cell = $('<td>'); var $cell = $("<td>");
if (config.debug) { if (config.debug) {
$cell.addClass(column.attrs.name); $cell.addClass(column.attrs.name);
} }
if (column.aggregate) { if (column.aggregate) {
self.applyAggregateValue($cell, column) self.applyAggregateValue($cell, column);
} }
return $cell; return $cell;
}); });
@ -383,7 +382,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
return; return;
} }
var type = field.type; var type = field.type;
if (!~['integer', 'float', 'monetary'].indexOf(type)) { if (!~["integer", "float", "monetary"].indexOf(type)) {
return; return;
} }
this.total = { this.total = {
@ -391,13 +390,15 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
name: fname, name: fname,
}, },
aggregate: { aggregate: {
help: _t('Sum Total'), help: _t("Sum Total"),
value: 0, value: 0,
}, },
}; };
_.each(this.columns, function (column, index) { _.each(
this.columns,
function(column, index) {
column.aggregate = { column.aggregate = {
help: _t('Sum'), help: _t("Sum"),
value: 0, value: 0,
}; };
_.each(this.rows, function(row) { _.each(this.rows, function(row) {
@ -409,7 +410,8 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
} }
}); });
this.total.aggregate.value += column.aggregate.value; this.total.aggregate.value += column.aggregate.value;
}.bind(this)); }.bind(this)
);
}, },
/** /**
@ -467,12 +469,12 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
return; return;
} }
var type = field.type; var type = field.type;
if (!~['integer', 'float', 'monetary'].indexOf(type)) { if (!~["integer", "float", "monetary"].indexOf(type)) {
return; return;
} }
_.each(this.rows, function(row) { _.each(this.rows, function(row) {
row.aggregate = { row.aggregate = {
help: _t('Sum'), help: _t("Sum"),
value: 0, value: 0,
}; };
_.each(row.data, function(col) { _.each(row.data, function(col) {
@ -502,11 +504,13 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
var value = axis.aggregate.value; var value = axis.aggregate.value;
var help = axis.aggregate.help; var help = axis.aggregate.help;
var fieldInfo = this.state.fieldsInfo.list[axis.attrs.name]; var fieldInfo = this.state.fieldsInfo.list[axis.attrs.name];
var formatFunc = field_utils.format[ var formatFunc =
fieldInfo.widget ? fieldInfo.widget : field.type field_utils.format[fieldInfo.widget ? fieldInfo.widget : field.type];
];
var formattedValue = formatFunc(value, field, {escape: true}); var formattedValue = formatFunc(value, field, {escape: true});
$cell.addClass('o_list_number').attr('title', help).html(formattedValue); $cell
.addClass("o_list_number")
.attr("title", help)
.html(formattedValue);
}, },
/** /**
@ -570,7 +574,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
*/ */
_refreshColTotals: function() { _refreshColTotals: function() {
this._computeColumnAggregates(); this._computeColumnAggregates();
this.$('tfoot').replaceWith(this._renderFooter()); this.$("tfoot").replaceWith(this._renderFooter());
}, },
/** /**
@ -579,10 +583,11 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
_refreshRowTotals: function() { _refreshRowTotals: function() {
var self = this; var self = this;
this._computeRowAggregates(); this._computeRowAggregates();
var $rows = self.$el.find('tr.o_data_row'); var $rows = self.$el.find("tr.o_data_row");
_.each(self.rows, function(row, i) { _.each(self.rows, function(row, i) {
if (row.aggregate) { if (row.aggregate) {
$($rows[i]).find('.row-total') $($rows[i])
.find(".row-total")
.replaceWith(self._renderAggregateRowCell(row)); .replaceWith(self._renderAggregateRowCell(row));
} }
}); });
@ -596,7 +601,6 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
getEditableRecordID: function() { getEditableRecordID: function() {
return null; return null;
}, },
}); });
return X2Many2dMatrixRenderer; return X2Many2dMatrixRenderer;

View File

@ -1,10 +1,10 @@
/* Copyright 2019 Alexandre Díaz /* Copyright 2019 Alexandre Díaz
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixView', function (require) { odoo.define("web_widget_x2many_2d_matrix.X2Many2dMatrixView", function(require) {
"use strict"; "use strict";
var BasicView = require('web.BasicView'); var BasicView = require("web.BasicView");
BasicView.include({ BasicView.include({
_processField: function(viewType, field, attrs) { _processField: function(viewType, field, attrs) {
@ -13,11 +13,10 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixView', function (require)
// See https://github.com/OCA/web/pull/1404#pullrequestreview-305813206 . // See https://github.com/OCA/web/pull/1404#pullrequestreview-305813206 .
// In the long term we should a way to handle kanban mode // In the long term we should a way to handle kanban mode
// better (eg: a specific renderer). // better (eg: a specific renderer).
if (attrs.widget === 'x2many_2d_matrix') { if (attrs.widget === "x2many_2d_matrix") {
attrs.mode = 'tree'; attrs.mode = "tree";
} }
return this._super(viewType, field, attrs); return this._super(viewType, field, attrs);
}, },
}); });
}); });

View File

@ -3,17 +3,15 @@
* Copyright 2018 Simone Orsi <simone.orsi@camptocamp.com> * Copyright 2018 Simone Orsi <simone.orsi@camptocamp.com>
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define('web_widget_x2many_2d_matrix.widget', function (require) { odoo.define("web_widget_x2many_2d_matrix.widget", function(require) {
"use strict"; "use strict";
var field_registry = require('web.field_registry'); var field_registry = require("web.field_registry");
var relational_fields = require('web.relational_fields'); var relational_fields = require("web.relational_fields");
var X2Many2dMatrixRenderer = require( var X2Many2dMatrixRenderer = require("web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer");
'web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer'
);
var WidgetX2Many2dMatrix = relational_fields.FieldOne2Many.extend({ var WidgetX2Many2dMatrix = relational_fields.FieldOne2Many.extend({
widget_class: 'o_form_field_x2many_2d_matrix', widget_class: "o_form_field_x2many_2d_matrix",
/** /**
* Initialize the widget & parameters. * Initialize the widget & parameters.
@ -39,40 +37,39 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
this.y_axis = []; this.y_axis = [];
this.field_x_axis = node.field_x_axis || this.field_x_axis; this.field_x_axis = node.field_x_axis || this.field_x_axis;
this.field_y_axis = node.field_y_axis || this.field_y_axis; this.field_y_axis = node.field_y_axis || this.field_y_axis;
this.field_label_x_axis = this.field_label_x_axis = node.field_label_x_axis || this.field_x_axis;
node.field_label_x_axis || this.field_x_axis; this.field_label_y_axis = node.field_label_y_axis || this.field_y_axis;
this.field_label_y_axis = this.x_axis_clickable = this.parse_boolean(node.x_axis_clickable || "1");
node.field_label_y_axis || this.field_y_axis; this.y_axis_clickable = this.parse_boolean(node.y_axis_clickable || "1");
this.x_axis_clickable = this.parse_boolean(
node.x_axis_clickable || '1'
);
this.y_axis_clickable = this.parse_boolean(
node.y_axis_clickable || '1'
);
this.field_value = node.field_value || this.field_value; this.field_value = node.field_value || this.field_value;
// TODO: is this really needed? Holger? // TODO: is this really needed? Holger?
for (var property in node) { for (var property in node) {
if (property.startsWith("field_att_")) { if (property.startsWith("field_att_")) {
this.fields_att[property.substring(10)] = this.fields_att[property.substring(10)] = node[property];
node[property];
} }
} }
var field_defs = this.recordData[this.name].fields; var field_defs = this.recordData[this.name].fields;
// TODO: raise when any of the fields above don't exist with a // TODO: raise when any of the fields above don't exist with a
// helpful error message // helpful error message
if (!field_defs[this.field_value]) { if (!field_defs[this.field_value]) {
throw new Error(_.str.sprintf( throw new Error(
'You need to include %s in your view definition', _.str.sprintf(
"You need to include %s in your view definition",
this.field_value this.field_value
)); )
);
} }
this.show_row_totals = this.parse_boolean( this.show_row_totals = this.parse_boolean(
node.show_row_totals || node.show_row_totals ||
this.is_aggregatable(field_defs[this.field_value]) ? '1' : '' this.is_aggregatable(field_defs[this.field_value])
? "1"
: ""
); );
this.show_column_totals = this.parse_boolean( this.show_column_totals = this.parse_boolean(
node.show_column_totals || node.show_column_totals ||
this.is_aggregatable(field_defs[this.field_value]) ? '1' : '' this.is_aggregatable(field_defs[this.field_value])
? "1"
: ""
); );
}, },
@ -88,14 +85,16 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
this.by_y_axis = {}; this.by_y_axis = {};
this.x_axis = []; this.x_axis = [];
this.y_axis = []; this.y_axis = [];
_.each(records, function (record) { _.each(
records,
function(record) {
var x = record.data[this.field_x_axis], var x = record.data[this.field_x_axis],
y = record.data[this.field_y_axis]; y = record.data[this.field_y_axis];
if (x.type === 'record') { if (x.type === "record") {
// We have a related record // We have a related record
x = x.data.display_name; x = x.data.display_name;
} }
if (y.type === 'record') { if (y.type === "record") {
// We have a related record // We have a related record
y = y.data.display_name; y = y.data.display_name;
} }
@ -107,24 +106,31 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
if (this.x_axis.indexOf(x) === -1) { if (this.x_axis.indexOf(x) === -1) {
this.x_axis.push(x); this.x_axis.push(x);
} }
}.bind(this)); }.bind(this)
);
// Init columns // Init columns
this.columns = []; this.columns = [];
_.each(this.x_axis, function (x) { _.each(
this.x_axis,
function(x) {
this.columns.push(this._make_column(x)); this.columns.push(this._make_column(x));
}.bind(this)); }.bind(this)
);
this.rows = []; this.rows = [];
_.each(this.y_axis, function (y) { _.each(
this.y_axis,
function(y) {
this.rows.push(this._make_row(y)); this.rows.push(this._make_row(y));
}.bind(this)); }.bind(this)
);
this.matrix_data = { this.matrix_data = {
'field_value': this.field_value, field_value: this.field_value,
'field_x_axis': this.field_x_axis, field_x_axis: this.field_x_axis,
'field_y_axis': this.field_y_axis, field_y_axis: this.field_y_axis,
'columns': this.columns, columns: this.columns,
'rows': this.rows, rows: this.rows,
'show_row_totals': this.show_row_totals, show_row_totals: this.show_row_totals,
'show_column_totals': this.show_column_totals, show_column_totals: this.show_column_totals,
}; };
}, },
@ -137,10 +143,10 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
_make_column: function(x) { _make_column: function(x) {
return { return {
// Simulate node parsed on xml arch // Simulate node parsed on xml arch
'tag': 'field', tag: "field",
'attrs': { attrs: {
'name': this.field_x_axis, name: this.field_x_axis,
'string': x, string: x,
}, },
}; };
}, },
@ -155,12 +161,12 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
var self = this; var self = this;
// Use object so that we can attach more data if needed // Use object so that we can attach more data if needed
var row = { var row = {
'tag': 'field', tag: "field",
'attrs': { attrs: {
'name': this.field_y_axis, name: this.field_y_axis,
'string': y, string: y,
}, },
'data': [], data: [],
}; };
_.each(self.x_axis, function(x) { _.each(self.x_axis, function(x) {
row.data.push(self.by_y_axis[y][x]); row.data.push(self.by_y_axis[y][x]);
@ -182,7 +188,7 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
* @returns {Boolean} The parsed boolean. * @returns {Boolean} The parsed boolean.
*/ */
parse_boolean: function(val) { parse_boolean: function(val) {
if (val.toLowerCase() === 'true' || val === '1') { if (val.toLowerCase() === "true" || val === "1") {
return true; return true;
} }
return false; return false;
@ -210,11 +216,11 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
// Create a new matrix renderer // Create a new matrix renderer
this.renderer = new X2Many2dMatrixRenderer(this, this.value, { this.renderer = new X2Many2dMatrixRenderer(this, this.value, {
arch: arch, arch: arch,
editable: this.mode === 'edit' && arch.attrs.editable, editable: this.mode === "edit" && arch.attrs.editable,
viewType: "list", viewType: "list",
matrix_data: this.matrix_data, matrix_data: this.matrix_data,
}); });
this.$el.addClass('o_field_x2many o_field_x2many_2d_matrix'); this.$el.addClass("o_field_x2many o_field_x2many_2d_matrix");
return this.renderer.appendTo(this.$el); return this.renderer.appendTo(this.$el);
}, },
@ -246,7 +252,7 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
}, },
}); });
field_registry.add('x2many_2d_matrix', WidgetX2Many2dMatrix); field_registry.add("x2many_2d_matrix", WidgetX2Many2dMatrix);
return { return {
WidgetX2Many2dMatrix: WidgetX2Many2dMatrix, WidgetX2Many2dMatrix: WidgetX2Many2dMatrix,

View File

@ -21,10 +21,12 @@ $x2many_2d_matrix_max_height: 450px;
> tbody { > tbody {
> tr { > tr {
&:nth-of-type(2n+1) td.row-total, &:nth-of-type(2n+1) td:first-child { &:nth-of-type(2n + 1) td.row-total,
&:nth-of-type(2n + 1) td:first-child {
background-color: mix(#000, #fff, 1%); background-color: mix(#000, #fff, 1%);
} }
&:nth-of-type(2n) td.row-total, &:nth-of-type(2n) td:first-child { &:nth-of-type(2n) td.row-total,
&:nth-of-type(2n) td:first-child {
background-color: white; background-color: white;
} }

View File

@ -3,15 +3,32 @@
<!-- <!--
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
--> -->
<template
<template id="assets_backend" name="web_widget_x2many_2d_matrix assets" inherit_id="web.assets_backend"> id="assets_backend"
name="web_widget_x2many_2d_matrix assets"
inherit_id="web.assets_backend"
>
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<script type="text/javascript" src="/web_widget_x2many_2d_matrix/static/src/js/2d_matrix_view.js" /> <script
<script type="text/javascript" src="/web_widget_x2many_2d_matrix/static/src/js/2d_matrix_renderer.js" /> type="text/javascript"
<script type="text/javascript" src="/web_widget_x2many_2d_matrix/static/src/js/widget_x2many_2d_matrix.js" /> src="/web_widget_x2many_2d_matrix/static/src/js/2d_matrix_view.js"
<script type="text/javascript" src="/web_widget_x2many_2d_matrix/static/src/js/abstract_view_matrix_limit_extend.js" /> />
<link rel="stylesheet" href="/web_widget_x2many_2d_matrix/static/src/scss/web_widget_x2many_2d_matrix.scss"/> <script
type="text/javascript"
src="/web_widget_x2many_2d_matrix/static/src/js/2d_matrix_renderer.js"
/>
<script
type="text/javascript"
src="/web_widget_x2many_2d_matrix/static/src/js/widget_x2many_2d_matrix.js"
/>
<script
type="text/javascript"
src="/web_widget_x2many_2d_matrix/static/src/js/abstract_view_matrix_limit_extend.js"
/>
<link
rel="stylesheet"
href="/web_widget_x2many_2d_matrix/static/src/scss/web_widget_x2many_2d_matrix.scss"
/>
</xpath> </xpath>
</template> </template>
</odoo> </odoo>