From ed1abc43d15d8f5b1a4ffc9b767fcf22637a3639 Mon Sep 17 00:00:00 2001 From: Adrien Dieudonne Date: Thu, 23 Feb 2017 14:03:31 +0100 Subject: [PATCH] [FIX] web: string values in priority widget (1) The priority widget is based on a selection field. So, this priority widget must support string values. Before this commit, the widget worked correctly with numeric values. For example: '0', '1', '2', '3' was converted to 0, 1, 2, 3. But sometimes, we have non-numerical values. For example: 'Normal', 'Low', 'High', 'Very High'. In this case, we can not compare the elements to each other according to their values. If you select 'High', we don't know if the star of the value 'Low' must be filled. To fix this, we base the comparison on the indexes of the table of possible values. For example: [('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')] Now, if you select D, we know that A (with index 0) is smaller than D (with index 3). This could be a behavioral change if you had [('1', '1'), ('0', '0'), ('2', '2')] (2) When the selection field had no value, the 'o_form_field_empty' class was added. In this case, the stars must be displayed without being filled. --- addons/web/static/src/js/views/form_widgets.js | 3 +++ addons/web/static/src/js/widgets/priority.js | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/js/views/form_widgets.js b/addons/web/static/src/js/views/form_widgets.js index e76da1eec30..09459f655fc 100644 --- a/addons/web/static/src/js/views/form_widgets.js +++ b/addons/web/static/src/js/views/form_widgets.js @@ -323,6 +323,9 @@ var FieldPriority = common.AbstractField.extend({ self.replaceElement(self.priority.$el); })); }, + is_false: function() { + return false; + }, render_value: function() { this.priority.set_value(this.get('value')); }, diff --git a/addons/web/static/src/js/widgets/priority.js b/addons/web/static/src/js/widgets/priority.js index fbe7b61bb43..44ff3b50c1e 100644 --- a/addons/web/static/src/js/widgets/priority.js +++ b/addons/web/static/src/js/widgets/priority.js @@ -66,13 +66,15 @@ var Priority = Widget.extend({ this._render_value(); }, _render_value: function() { - var current_value = this.value; + var self = this; + var index_value = this.value ? _.findIndex(this.values, function (v) { + return v[0] === self.value; + }) : 0; this.$stars = this.$('.o_priority_star'); this.$stars.each(function(i, el) { var $star = $(el); - var star_value = $star.data('value'); - $star.toggleClass('fa-star', (star_value <= current_value)) - .toggleClass('fa-star-o', (star_value > current_value)); + $star.toggleClass('fa-star', (i+1 <= index_value)) + .toggleClass('fa-star-o', (i+1 > index_value)); }); }, });