From 0f2ecee3c4b1b317fa22a97e7aeff84ee3923126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 30 Aug 2018 16:05:38 +0200 Subject: [PATCH] Apply factor on domain parser issue7642 review68301002 --- CHANGELOG | 1 + src/common.js | 20 +++++++++++++------- src/screen.js | 22 ++++++++++++++-------- tests/sao.js | 46 ++++++++++++++++++++++++++++++++-------------- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 26b68daea9..2a1389517c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Apply factor on domain parser * Use tab name in CSV Export/Import * Add bus management * Load custom CSS and Javascript diff --git a/src/common.js b/src/common.js index 1a802ca620..f1954e4568 100644 --- a/src/common.js +++ b/src/common.js @@ -1779,28 +1779,31 @@ } }, 'float': function() { + var factor = Number(field.factor || 1); var result = Number(value); if (isNaN(result) || value === '' || value === null) { return null; } else { - return result; + return result / factor; } }, 'integer': function() { + var factor = Number(field.factor || 1, 10); var result = parseInt(value, 10); if (isNaN(result)) { return null; } else { - return result; + return result / factor; } }, 'numeric': function() { - var result = new Sao.Decimal(value); + var factor = Number(field.factor || 1); + var result = Number(value); if (isNaN(result.valueOf()) || value === '' || value === null) { return null; } else { - return result; + return new Sao.Decimal(result / factor); } }, 'selection': convert_selection, @@ -1855,13 +1858,15 @@ if (!value && value !== 0 && value !== new Sao.Decimal(0)) { return ''; } - var digit = String(value).split('.')[1]; + var factor = Number(field.factor || 1); + var digit = String(value * factor) + .replace(/0+$/, '').split('.')[1]; if (digit) { digit = digit.length; } else { digit = 0; } - return value.toFixed(digit); + return (value * factor).toFixed(digit); }; var format_selection = function() { for (var i = 0; i < field.selection.length; i++) { @@ -1894,8 +1899,9 @@ } }, 'integer': function() { + var factor = Number(field.factor || 1); if (value || value === 0) { - return '' + parseInt(value, 10); + return '' + parseInt(parseInt(value, 10) * factor, 10); } else { return ''; } diff --git a/src/screen.js b/src/screen.js index 91b9312683..1cb11c60d3 100644 --- a/src/screen.js +++ b/src/screen.js @@ -1493,15 +1493,21 @@ if ('arch' in view_tree) { // Filter only fields in XML view var xml_view = jQuery(jQuery.parseXML(view_tree.arch)); - var xml_fields = xml_view.find('tree').children() - .filter(function(i, node) { - return node.tagName == 'field'; - }).map(function(i, node) { - return node.getAttribute('name'); - }); var dom_fields = {}; - xml_fields.each(function(k, name) { - dom_fields[name] = fields[name]; + xml_view.find('tree').children().each(function(i, node) { + if (node.tagName == 'field') { + var name = node.getAttribute('name'); + // If a field is defined multiple times in the XML, + // take only the first definition + if (!(name in dom_fields)) { + dom_fields[name] = fields[name]; + ['string', 'factor'].forEach(function(attr) { + if (node.getAttribute(attr)) { + dom_fields[name][attr] = node.getAttribute(attr); + } + }); + } + } }); fields = dom_fields; } diff --git a/tests/sao.js b/tests/sao.js index c32e19a99a..ebc2351c49 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -1309,6 +1309,8 @@ [null, null] ].forEach(test_func, field); + test_func.call({'type': 'float', 'factor': '100'}, ['42', 0.42]); + field = { 'type': 'integer' }; @@ -1320,6 +1322,23 @@ [null, null] ].forEach(test_func, field); + test_func.call({'type': 'integer', 'factor': '2'}, ['6', 3]); + + var test_func_numeric = function(test) { + var value = test[0]; + var result = test[1]; + value = parser.convert_value(this, value); + if (value !== null) { + value = value.toString(); + } + if (result !== null) { + result = result.toString(); + } + QUnit.strictEqual(value, result, + 'convert_value(' + JSON.stringify(this) + ', ' + + JSON.stringify(test[0]) + ')'); + }; + field = { 'type': 'numeric' }; @@ -1329,20 +1348,11 @@ ['', null], ['test', null], [null, null] - ].forEach(function(test) { - var value = test[0]; - var result = test[1]; - value = parser.convert_value(field, value); - if (value !== null) { - value = value.toString(); - } - if (result !== null) { - result = result.toString(); - } - QUnit.strictEqual(value, result, - 'convert_value(' + JSON.stringify(field) + ', ' + - JSON.stringify(test[0]) + ')'); - }); + ].forEach(test_func_numeric, field); + + test_func_numeric.call( + {'type': 'numeric', 'factor': '5'}, + ['1', new Sao.Decimal('0.2')]); field = { 'type': 'selection', @@ -1523,6 +1533,8 @@ [null, ''] ].forEach(test_func, field); + test_func.call({'type': 'integer', 'factor': '2'}, [3, '6']); + field = { 'type': 'float' }; @@ -1537,6 +1549,8 @@ [null, ''] ].forEach(test_func, field); + test_func.call({'type': 'float', 'factor': '100'}, [0.42, '42']); + field = { 'type': 'numeric' }; @@ -1551,6 +1565,10 @@ [null, ''] ].forEach(test_func, field); + test_func.call( + {'type': 'numeric', 'factor': '5'}, + [new Sao.Decimal('0.2'), '1']); + field = { 'type': 'selection', 'selection': [