From fbfc5b2d5da11832a7878314d20677e093376512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Tue, 17 Oct 2023 17:51:45 +0200 Subject: [PATCH] [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 --- .../static/src/pivot/pivot_data_source.js | 21 +++++++++---- .../static/src/pivot/pivot_functions.js | 6 ++-- .../static/src/pivot/pivot_model.js | 31 ++++++++----------- .../src/pivot/plugins/pivot_ui_plugin.js | 17 +++++----- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/addons/spreadsheet/static/src/pivot/pivot_data_source.js b/addons/spreadsheet/static/src/pivot/pivot_data_source.js index fef4ffcb235..2e7c09b7933 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_data_source.js +++ b/addons/spreadsheet/static/src/pivot/pivot_data_source.js @@ -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); } /** diff --git a/addons/spreadsheet/static/src/pivot/pivot_functions.js b/addons/spreadsheet/static/src/pivot/pivot_functions.js index b9138533a7d..9f63fd2e797 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_functions.js +++ b/addons/spreadsheet/static/src/pivot/pivot_functions.js @@ -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); diff --git a/addons/spreadsheet/static/src/pivot/pivot_model.js b/addons/spreadsheet/static/src/pivot/pivot_model.js index 379373d43f3..2f24d148394 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_model.js +++ b/addons/spreadsheet/static/src/pivot/pivot_model.js @@ -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 //-------------------------------------------------------------------------- diff --git a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js index ab544175702..757fe0e1339 100644 --- a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js +++ b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js @@ -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} 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",