From 8b2c0913133a6a0f0fcf2d30cde13bc8bd0121e1 Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Thu, 23 Mar 2017 17:06:28 +0100 Subject: [ADD] web: add priority widget tests and documentation --- addons/web/static/src/js/fields/basic_fields.js | 126 +++++++++++++++++------- 1 file changed, 92 insertions(+), 34 deletions(-) (limited to 'addons/web/static/src/js/fields/basic_fields.js') diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 8bee0daffac..81ca4323871 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -1048,44 +1048,37 @@ var PriorityWidget = AbstractField.extend({ // only usable for fields of type selection className: "o_priority", events: { - 'mouseover > a': function (e) { - clearTimeout(this.hover_timer); - this.$('.o_priority_star').removeClass('fa-star-o').addClass('fa-star'); - $(e.currentTarget).nextAll().removeClass('fa-star').addClass('fa-star-o'); - }, - 'mouseout > a': function () { - clearTimeout(this.hover_timer); - - var self = this; - this.hover_timer = setTimeout(function () { - self._render(); - }, 200); - }, - 'click > a': function (e) { - e.preventDefault(); - e.stopPropagation(); - - var index = $(e.currentTarget).data('index'); - var new_value = this.field.selection[index][0]; - if(new_value === this.value) { - new_value = this.empty_value; - } - this._setValue(new_value); - }, + 'mouseover > a': '_onMouseOver', + 'mouseout > a': '_onMouseOut', + 'click > a': '_onClick', }, + readonly: true, supportedFieldTypes: ['selection'], - is_set: function () { + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * Like boolean fields, this widget always has a value, since the default + * value is already a valid value. + * + * @override + */ + isSet: function () { return true; }, - render_star: function (tag, is_full, index, tip) { - return $(tag) - .attr('title', tip) - .attr('data-index', index) - .addClass('o_priority_star fa') - .toggleClass('fa-star', is_full) - .toggleClass('fa-star-o', !is_full); - }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Renders a star for each possible value, readonly or edit mode doesn't matter. + * + * @override + * @private + */ _render: function () { var self = this; var index_value = this.value ? _.findIndex(this.field.selection, function (v) { @@ -1094,9 +1087,74 @@ var PriorityWidget = AbstractField.extend({ this.$el.empty(); this.empty_value = this.field.selection[0][0]; _.each(this.field.selection.slice(1), function (choice, index) { - self.$el.append(self.render_star('', index_value >= index+1, index+1, choice[1])); + self.$el.append(self._renderStar('', index_value >= index+1, index+1, choice[1])); }); }, + + /** + * Renders a star representing a particular value for this field. + * + * @param {string} tag html tag to be passed to jquery to hold the star + * @param {boolean} isFull whether the star is a full star or not + * @param {integer} index the index of the star in the series + * @param {string} tip tooltip for this star's meaning + * @private + */ + _renderStar: function (tag, isFull, index, tip) { + return $(tag) + .attr('title', tip) + .attr('data-index', index) + .addClass('o_priority_star fa') + .toggleClass('fa-star', isFull) + .toggleClass('fa-star-o', !isFull); + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * Update the value of the field based on which star the user clicked on. + * + * @param {MouseEvent} event + * @private + */ + _onClick: function (event) { + event.preventDefault(); + event.stopPropagation(); + var index = $(event.currentTarget).data('index'); + var newValue = this.field.selection[index][0]; + if(newValue === this.value) { + newValue = this.empty_value; + } + this._setValue(newValue); + }, + + /** + * Reset the star display status. + * + * @private + */ + _onMouseOut: function () { + clearTimeout(this.hoverTimer); + var self = this; + this.hoverTimer = setTimeout(function () { + self._render(); + }, 200); + }, + + /** + * Colors the stars to show the user the result when clicking on it. + * + * @param {MouseEvent} event + * @private + */ + _onMouseOver: function (event) { + clearTimeout(this.hoverTimer); + this.$('.o_priority_star').removeClass('fa-star-o').addClass('fa-star'); + $(event.currentTarget).nextAll().removeClass('fa-star').addClass('fa-star-o'); + }, + }); var AttachmentImage = AbstractField.extend({ -- cgit v1.2.3