[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 <dam@odoo.com>
Co-authored-by: Julien Mougenot <jum@odoo.com>
This commit is contained in:
Mathieu Duckerts-Antoine
2021-09-07 10:04:37 +00:00
committed by Aaron Bohy
co-authored by Julien Mougenot
parent 05143f9e50
commit e7a9ebec31
+40 -4
View File
@@ -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;
};