From e7a9ebec3176c37485643fcda2381e489a1df86f Mon Sep 17 00:00:00 2001 From: Mathieu Duckerts-Antoine Date: Tue, 7 Sep 2021 06:22:54 +0000 Subject: [PATCH] [IMP] web: introduce processMeasure helper Both "__count__" and "__count" can be encountered as measure notations in reporting views. Typically, "__count__" is used in legacy views and "__count" in new views. The problem is that both can now be found on legacy or new side (think of favorite contexts for example). It is thus necessary to write some compatibility code on each side. Here me introduce an helper to help in that task. Part-of: odoo/odoo#73311 Co-authored-by: Mathieu Duckerts-Antoine Co-authored-by: Julien Mougenot --- addons/web/static/src/views/helpers/utils.js | 44 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/views/helpers/utils.js b/addons/web/static/src/views/helpers/utils.js index d3b9de96fb4..32f537b3fc5 100644 --- a/addons/web/static/src/views/helpers/utils.js +++ b/addons/web/static/src/views/helpers/utils.js @@ -8,11 +8,25 @@ import { _t } from "@web/core/l10n/translation"; * @param {string} str * @returns {boolean} */ -export function archParseBoolean(str) { +export const archParseBoolean = (str) => { return str !== "False" && str !== "false" && str !== "0" && str !== ""; -} +}; -export function computeReportMeasures(fields, fieldAttrs, activeMeasures, additionalMeasures = []) { +/** + * TODO: doc + * + * @param {Object} fields + * @param {Object} fieldAttrs + * @param {string[]} activeMeasures + * @param {string[]} [additionalMeasures=[]] + * @returns {Object} + */ +export const computeReportMeasures = ( + fields, + fieldAttrs, + activeMeasures, + additionalMeasures = [] +) => { const measures = { __count: { name: "__count", string: _t("Count"), type: "integer" }, }; @@ -59,4 +73,26 @@ export function computeReportMeasures(fields, fieldAttrs, activeMeasures, additi }); return Object.fromEntries(sortedMeasures); -} +}; + +/** + * In the preview implementation of reporting views, the virtual field used to + * display the number of records was named __count__, whereas __count is + * actually the one used in xml. So basically, activating a filter specifying + * __count as measures crashed. Unfortunately, as __count__ was used in the JS, + * all filters saved as favorite at that time were saved with __count__, and + * not __count. So in order the make them still work with the new + * implementation, we handle both __count__ and __count. + * + * This function replaces occurences of '__count__' by '__count' in the given + * element(s). + * + * @param {any | any[]} [measures] + * @returns {any} + */ +export const processMeasure = (measure) => { + if (Array.isArray(measure)) { + return measure.map(processMeasure); + } + return measure === "__count__" ? "__count" : measure; +};