1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
odoo.define('rating.portal.composer', function (require) {
'use strict';
var core = require('web.core');
var portalComposer = require('portal.composer');
var _t = core._t;
var PortalComposer = portalComposer.PortalComposer;
/**
* PortalComposer
*
* Extends Portal Composer to handle rating submission
*/
PortalComposer.include({
events: _.extend({}, PortalComposer.prototype.events, {
'click .stars i': '_onClickStar',
'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);
}
// default options
this.options = _.defaults(this.options, {
'default_message': false,
'default_message_id': false,
'default_rating_value': 0.0,
'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);
});
},
//--------------------------------------------------------------------------
// 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
*/
_onClickStar: function (ev) {
var index = this.$('.stars i').index(ev.currentTarget);
this.set("star_value", index + 1);
this.user_click = true;
this.$input.val(this.get("star_value"));
},
/**
* @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;
},
});
});
|