[REF] spreadsheet: clean method names

Method names in the pivot data source/model are not particularly clear
and self-explanatory.

To commit renames some methods (and their argument names) with hopefully
more meaningful names.

I'm also moving `getDisplayedPivotHeaderValue` (now
`computeOdooPivotHeaderValue`) from the model to the data source. It's
a high level function, the implementation can be in the data source.

Task: 3570281
Part-of: odoo/odoo#139295
This commit is contained in:
Lucas Lefèvre (lul)
2024-01-23 22:51:48 +00:00
parent 47ce4a3623
commit fbfc5b2d5d
4 changed files with 39 additions and 36 deletions
@@ -61,19 +61,28 @@ export class PivotDataSource extends OdooViewsDataSource {
}
/**
* @param {string[]} domain
* High level method computing the result of ODOO.PIVOT.HEADER functions.
* - regular function 'ODOO.PIVOT.HEADER(1,"stage_id",2,"user_id",6)'
* - measure header 'ODOO.PIVOT.HEADER(1,"stage_id",2,"user_id",6,"measure","expected_revenue")
* - positional header 'ODOO.PIVOT.HEADER(1,"#stage_id",1,"#user_id",1)'
*
* @param {(string | number)[]} domainArgs arguments of the function (except the first one which is the pivot id)
* @returns {string | number}
*/
getDisplayedPivotHeaderValue(domain) {
computeOdooPivotHeaderValue(domainArgs) {
this._assertDataIsLoaded();
return this._model.getDisplayedPivotHeaderValue(domain);
return this._model.getGroupByDisplayLabel(
domainArgs.at(-2),
this._model.getLastPivotGroupValue(domainArgs)
);
}
/**
* @param {string[]} domain
* @param {(string | number)[]} domainArgs
*/
getPivotHeaderValue(domain) {
getLastPivotGroupValue(domainArgs) {
this._assertDataIsLoaded();
return this._model.getPivotHeaderValue(domain);
return this._model.getLastPivotGroupValue(domainArgs);
}
/**
@@ -101,10 +101,10 @@ const ODOO_PIVOT_HEADER = {
category: "Odoo",
compute: function (pivotId, ...domain) {
pivotId = toString(pivotId);
const args = domain.map(toString);
const domainArgs = domain.map(toString);
assertPivotsExists(pivotId, this.getters);
assertDomainLength(args);
return this.getters.getDisplayedPivotHeaderValue(pivotId, args, this.locale);
assertDomainLength(domainArgs);
return this.getters.computeOdooPivotHeaderValue(pivotId, domainArgs);
},
computeFormat: function (pivotId, ...domain) {
pivotId = toString(pivotId.value);
@@ -336,32 +336,27 @@ export class SpreadsheetPivotModel extends PivotModel {
}
/**
* Get the label of the last group by of the domain
* Get the value of the last group by of the function arguments
* e.g. in `ODOO.PIVOT.HEADER(1, "stage_id", "42", "status", "won")`
* the last group value is "won".
*
* @param {string[]} domain Domain of the formula
* It can also handle positional arguments.
* e.g. in `ODOO.PIVOT.HEADER(1, "#stage_id", 1, "#user_id", 1)`
* the last group value is the id of the first user of the first stage.
*
* @param {(string | number)[]} domainArgs ODOO.PIVOT.HEADER arguments
*/
getPivotHeaderValue(domain) {
const groupFieldString = domain[domain.length - 2];
getLastPivotGroupValue(domainArgs) {
const groupFieldString = domainArgs.at(-2);
if (groupFieldString.startsWith("#")) {
const { field } = this.parseGroupField(groupFieldString);
const { cols, rows } = this._getColsRowsValuesFromDomain(domain);
return this._isCol(field) ? cols[cols.length - 1] : rows[rows.length - 1];
const { cols, rows } = this._getColsRowsValuesFromDomain(domainArgs);
return this._isCol(field) ? cols.at(-1) : rows.at(-1);
}
const groupValueString = domain[domain.length - 1];
const groupValueString = domainArgs.at(-1);
return groupValueString;
}
/**
* Get the displayed label of the last group by of the domain
*
* @param {string[]} domain Domain of the formula
* @returns {string}
*/
getDisplayedPivotHeaderValue(domain) {
const groupFieldString = domain[domain.length - 2];
return this.getGroupByDisplayLabel(groupFieldString, this.getPivotHeaderValue(domain));
}
//--------------------------------------------------------------------------
// Misc
//--------------------------------------------------------------------------
@@ -306,19 +306,18 @@ export class PivotUIPlugin extends spreadsheet.UIPlugin {
}
/**
* Get the value of a pivot header
* High level method computing the result of ODOO.PIVOT.HEADER functions.
*
* @param {string} pivotId Id of a pivot
* @param {Array<string>} domain Domain
* @param {(string | number)[]} domainArgs arguments of the function (except the first one which is the pivot id)
*/
getDisplayedPivotHeaderValue(pivotId, domain) {
computeOdooPivotHeaderValue(pivotId, domainArgs) {
const dataSource = this.getters.getPivotDataSource(pivotId);
dataSource.markAsHeaderUsed(domain);
const len = domain.length;
if (len === 0) {
dataSource.markAsHeaderUsed(domainArgs);
if (domainArgs.length === 0) {
return _t("Total");
}
return dataSource.getDisplayedPivotHeaderValue(domain);
return dataSource.computeOdooPivotHeaderValue(domainArgs);
}
/**
@@ -376,7 +375,7 @@ export class PivotUIPlugin extends spreadsheet.UIPlugin {
const { field, aggregateOperator: time } = dataSource.parseGroupField(argField);
const pivotFieldMatching = this.getters.getPivotFieldMatching(pivotId, filter.id);
if (pivotFieldMatching && pivotFieldMatching.chain === field.name) {
let value = dataSource.getPivotHeaderValue(domainArgs.slice(-2));
let value = dataSource.getLastPivotGroupValue(domainArgs.slice(-2));
if (value === NO_RECORD_AT_THIS_POSITION) {
continue;
}
@@ -521,7 +520,7 @@ PivotUIPlugin.getters = [
"getFirstPivotFunction",
"getSelectedPivotId",
"getPivotComputedDomain",
"getDisplayedPivotHeaderValue",
"computeOdooPivotHeaderValue",
"getPivotIdFromPosition",
"getPivotCellValue",
"getPivotGroupByValues",