From 1f72644c445826ae7263e9b0f4737ffab99a778e Mon Sep 17 00:00:00 2001 From: jem-odoo Date: Wed, 9 May 2018 09:40:02 +0200 Subject: [IMP] web: float_factor and float_toggle widgets This commit provides new js widgets for list, kanban and form views on float field. 1/ 'float_factor' displays the normal float field, but takes a conversion factor as option. The displays value is the normal one multiplied by the factor. You can use the widget like 2/ 'float_toggle' displays a button (in edit mode) looping on a range of given value at each click. A conversion factor is optional (default will be 1) to display a converted value, but sent to non converted one to the server. You can use the widget like 'range' and 'factor' are put in the options. In simple read mode, the widget display the value as a traditionnal text (like a normal char field). This commit provides tests and documentation for those 2 widgets. Task #39079 --- addons/web/static/src/js/fields/field_utils.js | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'addons/web/static/src/js/fields/field_utils.js') diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 989f99c5fef..0e9dc063834 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -173,6 +173,21 @@ function formatFloat(value, field, options) { return formatted.join(l10n.decimal_point); } + +/** + * Returns a string representing a float value, from a float converted with a + * factor. + * + * @param {number} value + * @param {number} [options.factor] + * Conversion factor, default value is 1.0 + * @returns {string} + */ +function formatFloatFactor(value, field, options) { + var factor = options.factor || 1; + return formatFloat(value * factor, field, options); +} + /** * Returns a string representing a time value, from a float. The idea is that * we sometimes want to display something like 1:45 instead of 1.75, or 0:15 @@ -528,6 +543,18 @@ function parseMonetary(value, field, options) { return parseFloat(values[0] === currency.symbol ? values[1] : values[0]); } +/** + * Parse a String containing float and unconvert it with a conversion factor + * + * @param {number} [options.factor] + * Conversion factor, default value is 1.0 + */ +function parseFloatFactor(value, field, options) { + var parsed = parseFloat(value); + var factor = options.factor || 1.0; + return parsed / factor; +} + function parseFloatTime(value) { var factor = 1; if (value[0] === '-') { @@ -596,6 +623,7 @@ return { date: formatDate, datetime: formatDateTime, float: formatFloat, + float_factor: formatFloatFactor, float_time: formatFloatTime, html: _.identity, // todo integer: formatInteger, @@ -615,6 +643,7 @@ return { date: parseDate, // todo datetime: parseDateTime, // todo float: parseFloat, + float_factor: parseFloatFactor, float_time: parseFloatTime, html: _.identity, // todo integer: parseInteger, -- cgit v1.2.3