Debian Codegraph @

addons/portal_rating/static/src/interactions/portal_composer.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { patch } from "@web/core/utils/patch";
3 import { patchDynamicContent } from "@web/public/utils";
4 import { PortalComposer } from "@portal/interactions/portal_composer";
5
6 /**
7 * PortalComposer
8 *
9 * Extends Portal Composer to handle rating submission
10 */
11 patch(PortalComposer, {
12 /**
13 * @override static
14 */
15 prepareOptions(options) {
16 options = super.prepareOptions(options);
17 // apply ratio to default rating value
18 if (options.default_rating_value) {
19 options.default_rating_value = parseFloat(options.default_rating_value);
20 }
21 if (options.rating_count) {
22 options.rating_count = parseInt(options.rating_count);
23 }
24
25 // default options
26 return Object.assign({
27 "rate_with_void_content": false,
28 "default_message": false,
29 "default_message_id": false,
30 "default_rating_value": 4.0,
31 "force_submit_url": false,
32 "reloadRatingPopupComposer": (data) => { },
33 }, options);
34 },
35 });
36
37 patch(PortalComposer.prototype, {
38 /**
39 * @override
40 */
41 setup() {
42 super.setup();
43 patchDynamicContent(this.dynamicContent, {
44 ".o-mail-Composer-stars i": {
45 "t-on-click.withTarget": this.onClickStar.bind(this),
46 "t-on-mousemove.withTarget": this.onMoveStar.bind(this),
47 "t-on-mouseleave": this.onMoveLeaveStar.bind(this),
48 "t-att-class": (el) => {
49 const index = Math.floor(this.starValue);
50 const decimal = this.starValue - index;
51 const starIndex = [...el.parentElement.children].indexOf(el) + 1; // index counts from 1 to 5
52 return {
53 "fa-star-o": starIndex > index,
54 "fa-star-half-o": decimal && starIndex === index,
55 "fa-star": decimal ? starIndex < index : starIndex <= index,
56 };
57 },
58 },
59 });
60
61 this.userClick = false; // user has click or not
62 this.starValue = this.options.default_rating_value;
63 // rating stars
64 this.starListEls = this.el.querySelectorAll(".o-mail-Composer-stars i");
65 },
66
67 /**
68 * @override
69 */
70 start() {
71 super.start();
72 // if this is the first review, we do not use grey color contrast, even with default rating value.
73 if (!this.options.default_message_id) {
74 for (const starEl of this.starListEls) {
75 starEl.classList.remove("text-black-25");
76 }
77 }
78
79 // set the default value to trigger the display of star widget and update the hidden input value.
80 this.starValue = this.options.default_rating_value;
81 this.ratingInputEl.value = this.options.default_rating_value;
82 },
83
84 get ratingInputEl() {
85 return this.el.querySelector('input[name="rating_value"]');
86 },
87
88 /**
89 * @override
90 */
91 prepareMessageData() {
92 if (this.options.force_submit_url === "/mail/message/update_content") {
93 return {
94 hash: this.options.hash,
95 message_id: parseInt(this.options.default_message_id),
96 update_data: {
97 attachment_ids: this.attachments.map((a) => a.id),
98 attachment_tokens: this.attachments.map((a) => a.ownership_token),
99 body: this.el.querySelector('textarea[name="message"]').value,
100 rating_value: this.ratingInputEl.value,
101 },
102 pid: this.options.pid,
103 token: this.options.token,
104 };
105 }
106 const res = super.prepareMessageData(...arguments)
107 res.message_id = this.options.default_message_id;
108 res.post_data.rating_value = this.ratingInputEl.value;
109 return res;
110 },
111
112 onClickStar(ev, oldFn, currentTargetEl) {
113 const index = [...currentTargetEl.parentElement.children].indexOf(currentTargetEl);
114 this.starValue = index + 1;
115 this.userClick = true;
116 this.ratingInputEl.value = this.starValue;
117 },
118
119 onMoveStar(ev, oldFn, currentTargetEl) {
120 const index = [...currentTargetEl.parentElement.children].indexOf(currentTargetEl);
121 this.starValue = index + 1;
122 },
123
124 onMoveLeaveStar() {
125 if (!this.userClick) {
126 this.starValue = parseInt(this.ratingInputEl.value);
127 }
128 this.userClick = false;
129 },
130
131 /**
132 * @override
133 */
134 async onSubmitButtonClick(ev) {
135 const result = await super.onSubmitButtonClick(...arguments);
136 const modalEl = this.el.closest("#ratingpopupcomposer");
137 this.addListener(modalEl, "hidden.bs.modal.noUpdate", () => {
138 this.options.reloadRatingPopupComposer(result);
139 });
140 window.Modal.getOrCreateInstance(modalEl).hide();
141 },
142
143 /**
144 * @override
145 */
146 onSubmitCheckContent(ev) {
147 if (this.options.rate_with_void_content) {
148 // TODO verify comparison
149 if (this.ratingInputEl.value === "0") {
150 return _t("The rating is required. Please make sure to select one before sending your review.")
151 }
152 return false;
153 }
154 return super.onSubmitCheckContent(...arguments);
155 },
156 });