summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core/utils.js
diff options
context:
space:
mode:
authorjem-odoo <jem@openerp.com>2018-05-09 09:40:02 +0200
committerjem-odoo <jem@openerp.com>2018-08-13 14:33:48 +0200
commit1f72644c445826ae7263e9b0f4737ffab99a778e (patch)
tree255ca293129b536eee1675403bdbea7e57519d67 /addons/web/static/src/js/core/utils.js
parent6b4062ea63c628c81caecfca21968820b17d0573 (diff)
[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 <field name="my_float_field" widget="float_factor" option="{'factor': 2}"/> 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 <field name="my_float" widget="float_toggle" option="{'range': [0, 0.5, 1], 'factor': 2}"/> '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
Diffstat (limited to 'addons/web/static/src/js/core/utils.js')
-rw-r--r--addons/web/static/src/js/core/utils.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js
index 3068a6528c9..f4031fccec9 100644
--- a/addons/web/static/src/js/core/utils.js
+++ b/addons/web/static/src/js/core/utils.js
@@ -511,6 +511,26 @@ var utils = {
return Object.freeze(obj);
},
+ /**
+ * Find the closest value of the given one in the provided array
+ *
+ * @param {Number} num
+ * @param {Array} arr
+ * @returns {Number|undefined}
+ */
+ closestNumber: function (num, arr) {
+ var curr = arr[0];
+ var diff = Math.abs (num - curr);
+ for (var val = 0; val < arr.length; val++) {
+ var newdiff = Math.abs (num - arr[val]);
+ if (newdiff < diff) {
+ diff = newdiff;
+ curr = arr[val];
+ }
+ }
+ return curr;
+ },
+
};
return utils;