summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js
diff options
context:
space:
mode:
authorJulien (juc) Castiaux <juc@odoo.com>2018-11-06 11:45:07 +0000
committerAaron Bohy <aab@odoo.com>2018-11-08 09:51:38 +0000
commit60d519b89043f1116ff6686ec4752593c7a49e1f (patch)
treef261900de5cde424712f3d04d022233bc7e5ae4e /addons/web/static/src/js
parent566cece592b4c711b655a7445ed454afc6ced46a (diff)
[FIX] web: format function missing in percentage widget
The format function was missing for the percentage widget type, so `field_utils.parse[this.formatType]` (see [1]) resolved as `undefined` which is not a function and so raised an error when the user was trying to update the field. This PR add that parsing function. Both `xx%` and `0.xx` are understood by the parser and resolve to the correct float value. opw-1895979 [1] https://github.com/odoo/odoo/blob/e220721a8ee80c02fe5aba5ed8c9f3f4ae54ea97/addons/web/static/src/js/fields/abstract_field.js#L399 closes odoo/odoo#28431
Diffstat (limited to 'addons/web/static/src/js')
-rw-r--r--addons/web/static/src/js/fields/field_utils.js17
1 files changed, 17 insertions, 0 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js
index 9d40c88019d..bbdee83c4f9 100644
--- a/addons/web/static/src/js/fields/field_utils.js
+++ b/addons/web/static/src/js/fields/field_utils.js
@@ -587,6 +587,22 @@ function parseFloatTime(value) {
}
/**
+ * Parse a String containing a percentage and convert it to float.
+ * The percentage can be a regular xx.xx float or a xx%.
+ *
+ * @param {string} value
+ * The string to be parsed
+ * @returns {float}
+ * @throws {Error} if the value couldn't be converted to float
+ */
+function parsePercentage(value) {
+ if (value.slice(-1) === '%') {
+ return parseFloat(value.slice(0, -1)) / 100;
+ }
+ return parseFloat(value);
+}
+
+/**
* Parse a String containing integer with language formating
*
* @param {string} value
@@ -668,6 +684,7 @@ return {
many2one: parseMany2one,
monetary: parseMonetary,
one2many: _.identity,
+ percentage: parsePercentage,
reference: parseMany2one,
selection: _.identity, // todo
text: _.identity, // todo