[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
This commit is contained in:
Thibault Delavallée
2019-08-22 13:54:45 +00:00
parent 857a11ca64
commit 796882b546
3 changed files with 127 additions and 118 deletions
@@ -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
*
@@ -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;
},
});
});
+1
View File
@@ -6,6 +6,7 @@
</xpath>
<xpath expr="//script[last()]" position="after">
<script type="text/javascript" src="/website_rating/static/src/js/portal_chatter.js"></script>
<script type="text/javascript" src="/website_rating/static/src/js/portal_composer.js"></script>
<script type="text/javascript" src="/website_rating/static/src/js/portal_rating_composer.js"></script>
</xpath>
</template>