mirror of https://github.com/OCA/web.git
[FIX] web_widget_x2many_2d_matrix: Fix linters
parent
cce5cebab2
commit
c83ef4a9fa
|
@ -154,7 +154,7 @@ Contributors
|
||||||
* Artem Kostyuk <a.kostyuk@mobilunity.com>
|
* Artem Kostyuk <a.kostyuk@mobilunity.com>
|
||||||
* Simone Orsi <simone.orsi@camptocamp.com>
|
* Simone Orsi <simone.orsi@camptocamp.com>
|
||||||
* Timon Tschanz <timon.tschanz@camptocamp.com>
|
* Timon Tschanz <timon.tschanz@camptocamp.com>
|
||||||
|
* Jairo Llopis <jairo.llopis@tecnativa.com>
|
||||||
|
|
||||||
Maintainer
|
Maintainer
|
||||||
----------
|
----------
|
||||||
|
|
|
@ -7,10 +7,9 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
// heavily inspired by Odoo's `ListRenderer`
|
// heavily inspired by Odoo's `ListRenderer`
|
||||||
var BasicRenderer = require('web.BasicRenderer');
|
var BasicRenderer = require('web.BasicRenderer');
|
||||||
var config = require('web.config');
|
var config = require('web.config');
|
||||||
var field_utils = require('web.field_utils');
|
|
||||||
var utils = require('web.utils');
|
|
||||||
var core = require('web.core');
|
var core = require('web.core');
|
||||||
var _t = core._t
|
var field_utils = require('web.field_utils');
|
||||||
|
var _t = core._t;
|
||||||
var FIELD_CLASSES = {
|
var FIELD_CLASSES = {
|
||||||
// copied from ListRenderer
|
// copied from ListRenderer
|
||||||
float: 'o_list_number',
|
float: 'o_list_number',
|
||||||
|
@ -21,6 +20,9 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
|
|
||||||
var X2Many2dMatrixRenderer = BasicRenderer.extend({
|
var X2Many2dMatrixRenderer = BasicRenderer.extend({
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @override
|
||||||
|
*/
|
||||||
init: function (parent, state, params) {
|
init: function (parent, state, params) {
|
||||||
this._super.apply(this, arguments);
|
this._super.apply(this, arguments);
|
||||||
this.editable = params.editable;
|
this.editable = params.editable;
|
||||||
|
@ -28,13 +30,16 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
this.rows = params.matrix_data.rows;
|
this.rows = params.matrix_data.rows;
|
||||||
this.matrix_data = params.matrix_data;
|
this.matrix_data = params.matrix_data;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main render function for the matrix widget. It is rendered as a table. For now,
|
* Main render function for the matrix widget.
|
||||||
|
*
|
||||||
|
* It is rendered as a table. For now,
|
||||||
* this method does not wait for the field widgets to be ready.
|
* this method does not wait for the field widgets to be ready.
|
||||||
*
|
*
|
||||||
* @override
|
* @override
|
||||||
* @private
|
* @private
|
||||||
* returns {Deferred} this deferred is resolved immediately
|
* @returns {Deferred} this deferred is resolved immediately
|
||||||
*/
|
*/
|
||||||
_renderView: function () {
|
_renderView: function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -48,7 +53,9 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
return this._super();
|
return this._super();
|
||||||
}
|
}
|
||||||
|
|
||||||
var $table = $('<table>').addClass('o_list_view table table-condensed table-striped');
|
var $table = $('<table>').addClass(
|
||||||
|
'o_list_view table table-condensed table-striped'
|
||||||
|
);
|
||||||
this.$el
|
this.$el
|
||||||
.addClass('table-responsive')
|
.addClass('table-responsive')
|
||||||
.append($table);
|
.append($table);
|
||||||
|
@ -64,12 +71,15 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
}
|
}
|
||||||
return this._super();
|
return this._super();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the table body. Looks for the table body and renders the rows in it.
|
* Render the table body.
|
||||||
|
*
|
||||||
|
* Looks for the table body and renders the rows in it.
|
||||||
* Also it sets the tabindex on every input element.
|
* Also it sets the tabindex on every input element.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* return {jQueryElement} The table body element that was 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());
|
||||||
|
@ -78,23 +88,30 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
});
|
});
|
||||||
return $body;
|
return $body;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the table head of our matrix. Looks for the first table head
|
* Render the table head of our matrix. Looks for the first table head
|
||||||
* and inserts the header into it.
|
* and inserts the header into it.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @return {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(this.columns, this._renderHeaderCell.bind(this)));
|
$tr = $tr.append(_.map(
|
||||||
|
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);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a single header cell. Creates a th and adds the description as text.
|
* Render a single header cell.
|
||||||
|
*
|
||||||
|
* Creates a th and adds the description as text.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {jQueryElement} node
|
* @param {jQueryElement} node
|
||||||
|
@ -107,16 +124,20 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
if (!field) {
|
if (!field) {
|
||||||
return $th;
|
return $th;
|
||||||
}
|
}
|
||||||
var description;
|
var description = null;
|
||||||
if (node.attrs.widget) {
|
if (node.attrs.widget) {
|
||||||
description = this.state.fieldsInfo.list[name].Widget.prototype.description;
|
description = this.state.fieldsInfo.list[name]
|
||||||
|
.Widget.prototype.description;
|
||||||
}
|
}
|
||||||
if (description === undefined) {
|
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 (field.type === 'float' || field.type === 'integer' || field.type === 'monetary') {
|
if (
|
||||||
|
field.type === 'float' || field.type === 'integer' ||
|
||||||
|
field.type === 'monetary'
|
||||||
|
) {
|
||||||
$th.addClass('text-right');
|
$th.addClass('text-right');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,6 +153,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
}
|
}
|
||||||
return $th;
|
return $th;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proxy call to function rendering single row.
|
* Proxy call to function rendering single row.
|
||||||
*
|
*
|
||||||
|
@ -139,13 +161,15 @@ 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, this._renderRow.bind(this));
|
return _.map(this.rows, this._renderRow.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a single row with all its columns. Renders all the cells and then wraps them with a <tr>.
|
* Render a single row with all its columns.
|
||||||
* If aggregate is set on the row it also will generate the aggregate cell.
|
* Renders all the cells and then wraps them with a <tr>.
|
||||||
|
* If aggregate is set on the row it also will generate
|
||||||
|
* the aggregate cell.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Object} row: The row that will be rendered.
|
* @param {Object} row: The row that will be rendered.
|
||||||
|
@ -167,17 +191,18 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
}
|
}
|
||||||
return $tr;
|
return $tr;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the label for a specific row.
|
* Renders the label for a specific row.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @params {Object} record: Contains the information about the record.
|
* @param {Object} record Contains the information about the record.
|
||||||
* @params {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;
|
||||||
}
|
}
|
||||||
|
@ -185,6 +210,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
$td.text(value);
|
$td.text(value);
|
||||||
return $td;
|
return $td;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a cell and fill it with the aggregate value.
|
* Create a cell and fill it with the aggregate value.
|
||||||
*
|
*
|
||||||
|
@ -197,6 +223,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
this._apply_aggregate_value($cell, row.aggregate);
|
this._apply_aggregate_value($cell, row.aggregate);
|
||||||
return $cell;
|
return $cell;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a single body Cell.
|
* Render a single body Cell.
|
||||||
* Gets the field and renders the widget. We force the edit mode, since
|
* Gets the field and renders the widget. We force the edit mode, since
|
||||||
|
@ -214,12 +241,14 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
if (node.tag === 'button') {
|
if (node.tag === 'button') {
|
||||||
tdClassName += ' o_list_button';
|
tdClassName += ' o_list_button';
|
||||||
} else if (node.tag === 'field') {
|
} else if (node.tag === 'field') {
|
||||||
var typeClass = FIELD_CLASSES[this.state.fields[node.attrs.name].type];
|
var typeClass = FIELD_CLASSES[
|
||||||
|
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
|
||||||
|
@ -229,20 +258,29 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
'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 the correct
|
// We register modifiers on the <td> element so that it gets
|
||||||
// modifiers classes (for styling)
|
// the correct modifiers classes (for styling)
|
||||||
var modifiers = this._registerModifiers(node, record, $td, _.pick(options, 'mode'));
|
var modifiers = this._registerModifiers(
|
||||||
// If the invisible modifiers is true, the <td> element is left empty.
|
node,
|
||||||
// Indeed, if the modifiers was to change the whole cell would be
|
record,
|
||||||
// rerendered anyway.
|
$td,
|
||||||
|
_.pick(options, 'mode')
|
||||||
|
);
|
||||||
|
// If the invisible modifiers is true, the <td> element is
|
||||||
|
// left empty. Indeed, if the modifiers was to change the
|
||||||
|
// whole cell would be rerendered anyway.
|
||||||
if (modifiers.invisible && !(options && options.renderInvisible)) {
|
if (modifiers.invisible && !(options && options.renderInvisible)) {
|
||||||
return $td;
|
return $td;
|
||||||
}
|
}
|
||||||
options.mode = this.getParent().mode; // enforce mode of the parent
|
// enforce mode of the parent
|
||||||
var widget = this._renderFieldWidget(node, record, _.pick(options, 'mode'));
|
options.mode = this.getParent().mode;
|
||||||
|
var widget = this._renderFieldWidget(
|
||||||
|
node, record, _.pick(options, 'mode')
|
||||||
|
);
|
||||||
this._handleAttributes(widget.$el, node);
|
this._handleAttributes(widget.$el, node);
|
||||||
return $td.append(widget.$el);
|
return $td.append(widget.$el);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps the column aggregate with a tfoot element
|
* Wraps the column aggregate with a tfoot element
|
||||||
*
|
*
|
||||||
|
@ -252,10 +290,12 @@ 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) {
|
||||||
return $('<tfoot>').append($('<tr>').append('<td/>').append($cells));
|
return $('<tfoot>').append(
|
||||||
|
$('<tr>').append('<td/>').append($cells)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the Aggregate cells for the column.
|
* Render the Aggregate cells for the column.
|
||||||
*
|
*
|
||||||
|
@ -264,7 +304,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
*/
|
*/
|
||||||
_renderAggregateColCells: function () {
|
_renderAggregateColCells: function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
return _.map(this.columns, function (column, index) {
|
return _.map(this.columns, function (column) {
|
||||||
var $cell = $('<td>', {class: 'col-total text-right'});
|
var $cell = $('<td>', {class: 'col-total text-right'});
|
||||||
if (column.aggregate) {
|
if (column.aggregate) {
|
||||||
self._apply_aggregate_value($cell, column.aggregate);
|
self._apply_aggregate_value($cell, column.aggregate);
|
||||||
|
@ -272,6 +312,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
return $cell;
|
return $cell;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the column aggregates.
|
* Compute the column aggregates.
|
||||||
* This function is called everytime the value is changed.
|
* This function is called everytime the value is changed.
|
||||||
|
@ -282,30 +323,31 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
if (!this.matrix_data.show_column_totals) {
|
if (!this.matrix_data.show_column_totals) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var self = this,
|
var fname = this.matrix_data.field_value,
|
||||||
fname = this.matrix_data.field_value,
|
|
||||||
field = this.state.fields[fname];
|
field = this.state.fields[fname];
|
||||||
if (!field) { return; }
|
if (!field) {
|
||||||
var type = field.type;
|
|
||||||
if (type !== 'integer' && type !== 'float' && type !== 'monetary') {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_.each(self.columns, function (column, index) {
|
var type = field.type;
|
||||||
|
if (!_.inArray(type, ['integer', 'float', 'monetary'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_.each(this.columns, function (column, index) {
|
||||||
column.aggregate = {
|
column.aggregate = {
|
||||||
fname: fname,
|
fname: fname,
|
||||||
ftype: type,
|
ftype: type,
|
||||||
// TODO: translate
|
help: _t('Sum'),
|
||||||
help: 'Sum',
|
value: 0,
|
||||||
value: 0
|
|
||||||
};
|
};
|
||||||
_.each(self.rows, function (row) {
|
_.each(this.rows, function (row) {
|
||||||
// var record = _.findWhere(self.state.data, {id: col.data.id});
|
|
||||||
column.aggregate.value += row.data[index].data[fname];
|
column.aggregate.value += row.data[index].data[fname];
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the row aggregates.
|
* Compute the row aggregates.
|
||||||
|
*
|
||||||
* This function is called everytime the value is changed.
|
* This function is called everytime the value is changed.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
|
@ -314,49 +356,67 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
if (!this.matrix_data.show_row_totals) {
|
if (!this.matrix_data.show_row_totals) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var self = this,
|
var fname = this.matrix_data.field_value,
|
||||||
fname = this.matrix_data.field_value,
|
|
||||||
field = this.state.fields[fname];
|
field = this.state.fields[fname];
|
||||||
if (!field) { return; }
|
if (!field) {
|
||||||
var type = field.type;
|
|
||||||
if (type !== 'integer' && type !== 'float' && type !== 'monetary') {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_.each(self.rows, function (row) {
|
var type = field.type;
|
||||||
|
if (!_.inArray(type, ['integer', 'float', 'monetary'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_.each(this.rows, function (row) {
|
||||||
row.aggregate = {
|
row.aggregate = {
|
||||||
fname: fname,
|
fname: fname,
|
||||||
ftype: type,
|
ftype: type,
|
||||||
// TODO: translate
|
help: _t('Sum'),
|
||||||
help: 'Sum',
|
value: 0,
|
||||||
value: 0
|
|
||||||
};
|
};
|
||||||
_.each(row.data, function (col) {
|
_.each(row.data, function (col) {
|
||||||
row.aggregate.value += col.data[fname];
|
row.aggregate.value += col.data[fname];
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes the given Value, formats it and adds it to the given cell.
|
* Takes the given Value, formats it and adds it to the given cell.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {jQueryElement} $cell: The Cell where the aggregate should be added.
|
*
|
||||||
* @param {Object} aggregate: The object which contains the information about the aggregate value
|
* @param {jQueryElement} $cell
|
||||||
|
* The Cell where the aggregate should be added.
|
||||||
|
*
|
||||||
|
* @param {Object} aggregate
|
||||||
|
* The object which contains the information about the aggregate value
|
||||||
*/
|
*/
|
||||||
_apply_aggregate_value: function ($cell, aggregate) {
|
_apply_aggregate_value: function ($cell, aggregate) {
|
||||||
var field = this.state.fields[aggregate.fname],
|
var field = this.state.fields[aggregate.fname],
|
||||||
formatter = field_utils.format[field.type];
|
formatter = field_utils.format[field.type];
|
||||||
var formattedValue = formatter(aggregate.value, field, {escape: true, });
|
var formattedValue = formatter(
|
||||||
$cell.addClass('total').attr('title', aggregate.help).html(formattedValue);
|
aggregate.value, field, {escape: true}
|
||||||
|
);
|
||||||
|
$cell.addClass('total').attr('title', aggregate.help)
|
||||||
|
.html(formattedValue);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the change was successful and then update the grid.
|
* Check if the change was successful and then update the grid.
|
||||||
* This function is required on relational fields.
|
* This function is required on relational fields.
|
||||||
*
|
*
|
||||||
* @params {Object} state: Contains the current state of the field & all the data
|
* @param {Object} state
|
||||||
* @params {String} id: the id of the updated object.
|
* Contains the current state of the field & all the data
|
||||||
* @params {Array} fields: The fields we have in the view.
|
*
|
||||||
* @params {Object} ev: The event object.
|
* @param {String} id
|
||||||
* @returns {Deferred} The deferred object thats gonna be resolved when the change is made.
|
* the id of the updated object.
|
||||||
|
*
|
||||||
|
* @param {Array} fields
|
||||||
|
* The fields we have in the view.
|
||||||
|
*
|
||||||
|
* @param {Object} ev
|
||||||
|
* The event object.
|
||||||
|
*
|
||||||
|
* @returns {Deferred}
|
||||||
|
* The deferred object thats gonna be resolved when the change is made.
|
||||||
*/
|
*/
|
||||||
confirmUpdate: function (state, id, fields, ev) {
|
confirmUpdate: function (state, id, fields, ev) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -365,32 +425,35 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
self._refresh(id);
|
self._refresh(id);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refresh our grid.
|
* Refresh our grid.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
|
* @param {String} id Datapoint ID
|
||||||
*/
|
*/
|
||||||
_refresh: function (id) {
|
_refresh: function (id) {
|
||||||
this._updateRow(id);
|
this._updateRow(id);
|
||||||
this._refreshColTotals();
|
this._refreshColTotals();
|
||||||
this._refreshRowTotals();
|
this._refreshRowTotals();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Update row data in our internal rows.
|
*Update row data in our internal rows.
|
||||||
*
|
*
|
||||||
* @params {String} id: The id of the row that needs to be updated.
|
* @param {String} id: The id of the row that needs to be updated.
|
||||||
*/
|
*/
|
||||||
_updateRow: function (id) {
|
_updateRow: function (id) {
|
||||||
var self = this,
|
var record = _.findWhere(this.state.data, {id: id});
|
||||||
record = _.findWhere(self.state.data, {id: id});
|
_.each(this.rows, function (row) {
|
||||||
_.each(self.rows, function(row) {
|
|
||||||
_.each(row.data, function (col, i) {
|
_.each(row.data, function (col, i) {
|
||||||
if (col.id == id) {
|
if (col.id === id) {
|
||||||
row.data[i] = record;
|
row.data[i] = record;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the row total.
|
* Update the row total.
|
||||||
*/
|
*/
|
||||||
|
@ -398,6 +461,7 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
this._computeColumnAggregates();
|
this._computeColumnAggregates();
|
||||||
this.$('tfoot').replaceWith(this._renderFooter());
|
this.$('tfoot').replaceWith(this._renderFooter());
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the column total.
|
* Update the column total.
|
||||||
*/
|
*/
|
||||||
|
@ -412,10 +476,15 @@ odoo.define('web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer', function (requ
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/*
|
|
||||||
x2m fields expect this
|
/**
|
||||||
|
* X2many fields expect this
|
||||||
|
*
|
||||||
|
* @returns {null}
|
||||||
*/
|
*/
|
||||||
getEditableRecordID: function (){ return false;}
|
getEditableRecordID: function () {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,24 +6,22 @@
|
||||||
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 core = require('web.core');
|
|
||||||
// var FieldManagerMixin = require('web.FieldManagerMixin');
|
|
||||||
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 weContext = require('web_editor.context');
|
var X2Many2dMatrixRenderer = require(
|
||||||
// var Helpers = require('web_widget_x2many_2d_matrix.helpers');
|
'web_widget_x2many_2d_matrix.X2Many2dMatrixRenderer'
|
||||||
var AbstractField = require('web.AbstractField');
|
);
|
||||||
var X2Many2dMatrixRenderer = require('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.
|
||||||
*
|
*
|
||||||
* @param {Object} parent: contains the form view.
|
* @param {Object} parent contains the form view.
|
||||||
* @param {String} name: the name of the field.
|
* @param {String} name the name of the field.
|
||||||
* @param {Object} record: Contains the information about the database records.
|
* @param {Object} record information about the database records.
|
||||||
* @param {Object} options: Contains the view options.
|
* @param {Object} options view options.
|
||||||
*/
|
*/
|
||||||
init: function (parent, name, record, options) {
|
init: function (parent, name, record, options) {
|
||||||
this._super(parent, name, record, options);
|
this._super(parent, name, record, options);
|
||||||
|
@ -40,73 +38,85 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
|
||||||
this.by_y_axis = {};
|
this.by_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 = node.field_label_x_axis || this.field_x_axis;
|
this.field_label_x_axis =
|
||||||
this.field_label_y_axis = node.field_label_y_axis || this.field_y_axis;
|
node.field_label_x_axis || this.field_x_axis;
|
||||||
this.x_axis_clickable = this.parse_boolean(node.x_axis_clickable || '1');
|
this.field_label_y_axis =
|
||||||
this.y_axis_clickable = this.parse_boolean(node.y_axis_clickable || '1');
|
node.field_label_y_axis || this.field_y_axis;
|
||||||
|
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)] = node[property];
|
this.fields_att[property.substring(10)] =
|
||||||
|
node[property];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// and this?
|
// and this?
|
||||||
this.field_editability = node.field_editability || this.field_editability;
|
this.field_editability =
|
||||||
this.show_row_totals = this.parse_boolean(node.show_row_totals || '1');
|
node.field_editability || this.field_editability;
|
||||||
this.show_column_totals = this.parse_boolean(node.show_column_totals || '1');
|
this.show_row_totals =
|
||||||
|
this.parse_boolean(node.show_row_totals || '1');
|
||||||
|
this.show_column_totals =
|
||||||
|
this.parse_boolean(node.show_column_totals || '1');
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the Value matrix.
|
* Initializes the Value matrix.
|
||||||
* Puts the values in the grid. If we have related items we use the display name.
|
*
|
||||||
|
* Puts the values in the grid.
|
||||||
|
* If we have related items we use the display name.
|
||||||
*/
|
*/
|
||||||
init_matrix: function () {
|
init_matrix: function () {
|
||||||
var self = this,
|
var records = this.recordData[this.name].data;
|
||||||
records = self.recordData[this.name].data;
|
|
||||||
// Wipe the content if something still exists
|
// Wipe the content if something still exists
|
||||||
this.by_x_axis = {};
|
this.by_x_axis = {};
|
||||||
this.by_y_axis = {};
|
this.by_y_axis = {};
|
||||||
_.each(records, function (record) {
|
_.each(records, function (record) {
|
||||||
var x = record.data[self.field_x_axis],
|
var x = record.data[this.field_x_axis],
|
||||||
y = record.data[self.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;
|
||||||
}
|
}
|
||||||
self.by_x_axis[x] = self.by_x_axis[x] || {};
|
this.by_x_axis[x] = this.by_x_axis[x] || {};
|
||||||
self.by_y_axis[y] = self.by_y_axis[y] || {};
|
this.by_y_axis[y] = this.by_y_axis[y] || {};
|
||||||
self.by_x_axis[x][y] = record;
|
this.by_x_axis[x][y] = record;
|
||||||
self.by_y_axis[y][x] = record;
|
this.by_y_axis[y][x] = record;
|
||||||
});
|
}.bind(this));
|
||||||
// init columns
|
// init columns
|
||||||
self.columns = [];
|
this.columns = [];
|
||||||
$.each(self.by_x_axis, function(x){
|
$.each(this.by_x_axis, function (x) {
|
||||||
self.columns.push(self._make_column(x));
|
this.columns.push(this._make_column(x));
|
||||||
});
|
}.bind(this));
|
||||||
self.rows = [];
|
this.rows = [];
|
||||||
$.each(self.by_y_axis, function(y){
|
$.each(this.by_y_axis, function (y) {
|
||||||
self.rows.push(self._make_row(y));
|
this.rows.push(this._make_row(y));
|
||||||
});
|
}.bind(this));
|
||||||
self.matrix_data = {
|
this.matrix_data = {
|
||||||
'field_value': self.field_value,
|
'field_value': this.field_value,
|
||||||
'field_x_axis': self.field_x_axis,
|
'field_x_axis': this.field_x_axis,
|
||||||
'field_y_axis': self.field_y_axis,
|
'field_y_axis': this.field_y_axis,
|
||||||
'columns': self.columns,
|
'columns': this.columns,
|
||||||
'rows': self.rows,
|
'rows': this.rows,
|
||||||
'show_row_totals': self.show_row_totals,
|
'show_row_totals': this.show_row_totals,
|
||||||
'show_column_totals': self.show_column_totals
|
'show_column_totals': this.show_column_totals,
|
||||||
};
|
};
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create scaffold for a column.
|
* Create scaffold for a column.
|
||||||
*
|
*
|
||||||
* @params {String} x: The string used as a column title
|
* @param {String} x The string used as a column title
|
||||||
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
_make_column: function (x) {
|
_make_column: function (x) {
|
||||||
return {
|
return {
|
||||||
|
@ -114,14 +124,16 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
|
||||||
'tag': 'field',
|
'tag': 'field',
|
||||||
'attrs': {
|
'attrs': {
|
||||||
'name': this.field_x_axis,
|
'name': this.field_x_axis,
|
||||||
'string': x
|
'string': x,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create scaffold for a row.
|
* Create scaffold for a row.
|
||||||
*
|
*
|
||||||
* @params {String} x: The string used as a row title
|
* @param {String} y The string used as a row title
|
||||||
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
_make_row: function (y) {
|
_make_row: function (y) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -132,10 +144,11 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
|
||||||
});
|
});
|
||||||
return row;
|
return row;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Parse a String containing a Python bool or 1 and convert it to a proper bool.
|
* Parse a String containing a bool and convert it to a JS bool.
|
||||||
*
|
*
|
||||||
* @params {String} val: the string to be parsed.
|
* @param {String} val: the string to be parsed.
|
||||||
* @returns {Boolean} The parsed boolean.
|
* @returns {Boolean} The parsed boolean.
|
||||||
*/
|
*/
|
||||||
parse_boolean: function (val) {
|
parse_boolean: function (val) {
|
||||||
|
@ -144,10 +157,12 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the matrix renderer and add its output to our element
|
* Create the matrix renderer and add its output to our element
|
||||||
*
|
*
|
||||||
* @returns {Deferred} A deferred object to be completed when it finished rendering.
|
* @returns {Deferred}
|
||||||
|
* A deferred object to be completed when it finished rendering.
|
||||||
*/
|
*/
|
||||||
_render: function () {
|
_render: function () {
|
||||||
if (!this.view) {
|
if (!this.view) {
|
||||||
|
@ -161,20 +176,20 @@ odoo.define('web_widget_x2many_2d_matrix.widget', function (require) {
|
||||||
arch: arch,
|
arch: arch,
|
||||||
editable: true,
|
editable: true,
|
||||||
viewType: viewType,
|
viewType: viewType,
|
||||||
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');
|
||||||
// Remove previous rendered and add the newly created one
|
// Remove previous rendered and add the newly created one
|
||||||
this.$el.find('div:not(.o_x2m_control_panel)').remove();
|
this.$el.find('div:not(.o_x2m_control_panel)').remove();
|
||||||
return this.renderer.appendTo(this.$el);
|
return this.renderer.appendTo(this.$el);
|
||||||
|
|
||||||
}
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
field_registry.add('x2many_2d_matrix', WidgetX2Many2dMatrix);
|
field_registry.add('x2many_2d_matrix', WidgetX2Many2dMatrix);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
WidgetX2Many2dMatrix: WidgetX2Many2dMatrix
|
WidgetX2Many2dMatrix: WidgetX2Many2dMatrix,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue