From 796882b546748166bf156f20a052f4d07ce3d346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 22 Aug 2019 12:05:38 +0000 Subject: [PATCH] [IMP] website_rating: lightly lint portal chatter PURPOSE Allow publishers to add a comment on frontend reviews with ratings. Purpose is to allow to give some feedback or context on published reviews. SPECIFICATIONS * rename / reorder some methods according to JS guidelines; * split portal composer and portal chatter override to better understand module organization and ease future code additions; LINKS Task ID : 2026911 --- .../static/src/js/portal_chatter.js | 119 +---------------- .../static/src/js/portal_composer.js | 125 ++++++++++++++++++ addons/website_rating/views/assets.xml | 1 + 3 files changed, 127 insertions(+), 118 deletions(-) create mode 100644 addons/website_rating/static/src/js/portal_composer.js diff --git a/addons/website_rating/static/src/js/portal_chatter.js b/addons/website_rating/static/src/js/portal_chatter.js index d3a351ad9c1..d8d4f9d6d29 100644 --- a/addons/website_rating/static/src/js/portal_chatter.js +++ b/addons/website_rating/static/src/js/portal_chatter.js @@ -1,130 +1,13 @@ -odoo.define('website_rating.thread', function (require) { +odoo.define('rating.portal.chatter', function (require) { 'use strict'; -var core = require('web.core'); var portalChatter = require('portal.chatter'); var utils = require('web.utils'); -var _t = core._t; - -var PortalComposer = portalChatter.PortalComposer; var PortalChatter = portalChatter.PortalChatter; var STAR_RATING_RATIO = 2; // conversion factor from the star (1-5) to the db rating range (1-10) -/** - * PortalComposer - * - * Extends Frontend Composer to handle rating submission - */ -PortalComposer.include({ - events: _.extend({}, PortalComposer.prototype.events, { - "mousemove .stars i": "_onMoveStar", - "mouseleave .stars i": "_onMoveOutStar", - "click .stars": "_onClickStar", - "mouseleave .stars": "_onMouseleaveStar", - }), - - /** - * @constructor - */ - init: function (parent, options) { - this._super.apply(this, arguments); - - // apply ratio to default rating value - if (options.default_rating_value) { - options.default_rating_value = parseFloat(options.default_rating_value) / STAR_RATING_RATIO; - } - - // default options - this.options = _.defaults(this.options, { - 'default_message': false, - 'default_message_id': false, - 'default_rating_value': false, - 'force_submit_url': false, - }); - // star input widget - this.labels = { - '0': "", - '1': _t("I hate it"), - '2': _t("I don't like it"), - '3': _t("It's okay"), - '4': _t("I like it"), - '5': _t("I love it"), - }; - this.user_click = false; // user has click or not - this.set("star_value", this.options.default_rating_value); - this.on("change:star_value", this, this._onChangeStarValue); - }, - /** - * @override - */ - start: function () { - var self = this; - return this._super.apply(this, arguments).then(function () { - // rating stars - self.$input = self.$('input[name="rating_value"]'); - self.$star_list = self.$('.stars').find('i'); - - // set the default value to trigger the display of star widget and update the hidden input value. - self.set("star_value", self.options.default_rating_value); - self.$input.val(self.options.default_rating_value * STAR_RATING_RATIO); - }); - }, - - //-------------------------------------------------------------------------- - // Handlers - //-------------------------------------------------------------------------- - - /** - * @private - */ - _onClickStar: function () { - this.user_click = true; - this.$input.val(this.get("star_value") * STAR_RATING_RATIO); - }, - /** - * @private - */ - _onChangeStarValue: function () { - var val = this.get("star_value"); - var index = Math.floor(val); - var decimal = val - index; - // reset the stars - this.$star_list.removeClass('fa-star fa-star-half-o').addClass('fa-star-o'); - - this.$('.stars').find("i:lt(" + index + ")").removeClass('fa-star-o fa-star-half-o').addClass('fa-star'); - if (decimal) { - this.$('.stars').find("i:eq(" + index + ")").removeClass('fa-star-o fa-star fa-star-half-o').addClass('fa-star-half-o'); - } - this.$('.rate_text .badge').text(this.labels[index]); - }, - /** - * @private - */ - _onMouseleaveStar: function () { - this.$('.rate_text').hide(); - }, - /** - * @private - * @param {MouseEvent} ev - */ - _onMoveStar: function (ev) { - var index = this.$('.stars i').index(ev.currentTarget); - this.$('.rate_text').show(); - this.set("star_value", index + 1); - }, - /** - * @private - */ - _onMoveOutStar: function () { - if (!this.user_click) { - this.set("star_value", parseInt(this.$input.val())); - } - this.user_click = false; - }, -}); - /** * PortalChatter * diff --git a/addons/website_rating/static/src/js/portal_composer.js b/addons/website_rating/static/src/js/portal_composer.js new file mode 100644 index 00000000000..5c4f9648350 --- /dev/null +++ b/addons/website_rating/static/src/js/portal_composer.js @@ -0,0 +1,125 @@ +odoo.define('rating.portal.composer', function (require) { +'use strict'; + +var core = require('web.core'); +var portalChatter = require('portal.chatter'); + +var _t = core._t; + +var PortalComposer = portalChatter.PortalComposer; + +var STAR_RATING_RATIO = 2; // conversion factor from the star (1-5) to the db rating range (1-10) + +/** + * PortalComposer + * + * Extends Portal Composer to handle rating submission + */ +PortalComposer.include({ + events: _.extend({}, PortalComposer.prototype.events, { + 'click .stars': '_onClickStarBlock', + 'mouseleave .stars': '_onMouseleaveStarBlock', + 'mousemove .stars i': '_onMoveStar', + 'mouseleave .stars i': '_onMoveLeaveStar', + }), + + /** + * @constructor + */ + init: function (parent, options) { + this._super.apply(this, arguments); + + // apply ratio to default rating value + if (options.default_rating_value) { + options.default_rating_value = parseFloat(options.default_rating_value) / STAR_RATING_RATIO; + } + + // default options + this.options = _.defaults(this.options, { + 'default_message': false, + 'default_message_id': false, + 'default_rating_value': false, + 'force_submit_url': false, + }); + // star input widget + this.labels = { + '0': "", + '1': _t("I hate it"), + '2': _t("I don't like it"), + '3': _t("It's okay"), + '4': _t("I like it"), + '5': _t("I love it"), + }; + this.user_click = false; // user has click or not + this.set("star_value", this.options.default_rating_value); + this.on("change:star_value", this, this._onChangeStarValue); + }, + /** + * @override + */ + start: function () { + var self = this; + return this._super.apply(this, arguments).then(function () { + // rating stars + self.$input = self.$('input[name="rating_value"]'); + self.$star_list = self.$('.stars').find('i'); + + // set the default value to trigger the display of star widget and update the hidden input value. + self.set("star_value", self.options.default_rating_value); + self.$input.val(self.options.default_rating_value * STAR_RATING_RATIO); + }); + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + */ + _onChangeStarValue: function () { + var val = this.get("star_value"); + var index = Math.floor(val); + var decimal = val - index; + // reset the stars + this.$star_list.removeClass('fa-star fa-star-half-o').addClass('fa-star-o'); + + this.$('.stars').find("i:lt(" + index + ")").removeClass('fa-star-o fa-star-half-o').addClass('fa-star'); + if (decimal) { + this.$('.stars').find("i:eq(" + index + ")").removeClass('fa-star-o fa-star fa-star-half-o').addClass('fa-star-half-o'); + } + this.$('.rate_text .badge').text(this.labels[index]); + }, + /** + * @private + */ + _onClickStarBlock: function () { + this.user_click = true; + this.$input.val(this.get("star_value") * STAR_RATING_RATIO); + }, + /** + * @private + */ + _onMouseleaveStarBlock: function () { + this.$('.rate_text').hide(); + }, + /** + * @private + * @param {MouseEvent} ev + */ + _onMoveStar: function (ev) { + var index = this.$('.stars i').index(ev.currentTarget); + this.$('.rate_text').show(); + this.set("star_value", index + 1); + }, + /** + * @private + */ + _onMoveLeaveStar: function () { + if (!this.user_click) { + this.set("star_value", parseInt(this.$input.val())); + } + this.user_click = false; + }, +}); +}); diff --git a/addons/website_rating/views/assets.xml b/addons/website_rating/views/assets.xml index 70ff3ed7c13..f5f2dd2e8e9 100644 --- a/addons/website_rating/views/assets.xml +++ b/addons/website_rating/views/assets.xml @@ -6,6 +6,7 @@ +