diff --git a/addons/portal_rating/static/src/js/portal_rating_composer.js b/addons/portal_rating/static/src/js/portal_rating_composer.js index ec39284de81..8c7210f5640 100644 --- a/addons/portal_rating/static/src/js/portal_rating_composer.js +++ b/addons/portal_rating/static/src/js/portal_rating_composer.js @@ -1,11 +1,12 @@ odoo.define('portal.rating.composer', function (require) { 'use strict'; -var publicWidget = require('web.public.widget'); -var session = require('web.session'); -var portalComposer = require('portal.composer'); +const publicWidget = require('web.public.widget'); +const session = require('web.session'); +const portalComposer = require('portal.composer'); +const {_t, qweb} = require('web.core'); -var PortalComposer = portalComposer.PortalComposer; +const PortalComposer = portalComposer.PortalComposer; /** * RatingPopupComposer @@ -13,8 +14,11 @@ var PortalComposer = portalComposer.PortalComposer; * Display the rating average with a static star widget, and open * a popup with the portal composer when clicking on it. **/ -var RatingPopupComposer = publicWidget.Widget.extend({ - template: 'portal_rating.PopupComposer', +const RatingPopupComposer = publicWidget.Widget.extend({ + selector: '.o_rating_popup_composer', + custom_events: { + reload_rating_popup_composer: '_onReloadRatingPopupComposer', + }, xmlDependencies: [ '/portal/static/src/xml/portal_chatter.xml', '/portal_rating/static/src/xml/portal_chatter.xml', @@ -22,8 +26,10 @@ var RatingPopupComposer = publicWidget.Widget.extend({ '/portal_rating/static/src/xml/portal_rating_composer.xml', ], - init: function (parent, options) { - this._super.apply(this, arguments); + willStart: function (parent) { + const def = this._super.apply(this, arguments); + + const options = this.$el.data(); this.rating_avg = Math.round(options['rating_avg'] * 100) / 100 || 0.0; this.rating_count = options['rating_count'] || 0.0; @@ -32,64 +38,65 @@ var RatingPopupComposer = publicWidget.Widget.extend({ 'res_model': false, 'res_id': false, 'pid': 0, - 'display_composer': options['disable_composer'] ? false : !session.is_website_user, 'display_rating': true, 'csrf_token': odoo.csrf_token, 'user_id': session.user_id, }); - }, - /** - * @override - */ - start: function () { - var defs = []; - defs.push(this._super.apply(this, arguments)); - // instanciate and insert composer widget - this._composer = new PortalComposer(this, this.options); - defs.push(this._composer.replace(this.$('.o_portal_chatter_composer'))); - - return Promise.all(defs); - }, -}); - -publicWidget.registry.RatingPopupComposer = publicWidget.Widget.extend({ - selector: '.o_rating_popup_composer', - custom_events: { - reload_rating_popup_composer: '_onReloadRatingPopupComposer', + return def; }, /** * @override */ start: function () { - this.ratingPopupData = this.$el.data(); - this.ratingPopupData.display_composer = !this.ratingPopupData.disable_composer && !session.is_website_user; - this.ratingPopup = new RatingPopupComposer(this, this.ratingPopupData); return Promise.all([ this._super.apply(this, arguments), - this.ratingPopup.appendTo(this.$el) + this._reloadRatingPopupComposer(), ]); }, - //-------------------------------------------------------------------------- - // Private - //-------------------------------------------------------------------------- - /** * Destroy existing ratingPopup and insert new ratingPopup widget * * @private * @param {Object} data */ - _reloadRatingPopupComposer: function (data) { - if (this.ratingPopup) { - this.ratingPopup.destroy(); + _reloadRatingPopupComposer: function () { + if (this.options.hide_rating_avg) { + this.$('.o_rating_popup_composer_stars').empty(); + } else { + const ratingAverage = qweb.render( + 'portal_rating.rating_stars_static', { + inline_mode: true, + widget: this, + val: this.rating_avg, + }); + this.$('.o_rating_popup_composer_stars').empty().html(ratingAverage); } - if (this.ratingPopupData.display_composer) { - this.ratingPopup = new RatingPopupComposer(this, Object.assign(this.ratingPopupData, data)); - this.ratingPopup.appendTo(this.$el); + + // Append the modal + const modal = qweb.render( + 'portal_rating.PopupComposer', { + inline_mode: true, + widget: this, + val: this.rating_avg, + }); + this.$('.o_rating_popup_composer_modal').html(modal); + + if (this._composer) { + this._composer.destroy(); } + + // Instantiate the "Portal Composer" widget and insert it into the modal + this._composer = new PortalComposer(this, this.options); + return this._composer.appendTo(this.$('.o_rating_popup_composer_modal .o_portal_chatter_composer')).then(() => { + // Change the text of the button + this.$('.o_rating_popup_composer_text').text( + this.options.default_message_id ? + _t('Modify your review') : _t('Add a review') + ); + }); }, //-------------------------------------------------------------------------- @@ -98,13 +105,29 @@ publicWidget.registry.RatingPopupComposer = publicWidget.Widget.extend({ /** * @private - * @param {OdooEvent} ev + * @param {OdooEvent} event */ - _onReloadRatingPopupComposer: function (ev) { - this._reloadRatingPopupComposer(ev.data); + _onReloadRatingPopupComposer: function (event) { + const data = event.data; + + // Refresh the internal state of the widget + this.rating_avg = data.rating_avg; + this.rating_count = data.rating_count; + this.rating_value = data.rating_value; + + // Clean the dictionary + delete data.rating_avg; + delete data.rating_count; + delete data.rating_value; + + this.options = _.extend(this.options, data); + + this._reloadRatingPopupComposer(); } }); +publicWidget.registry.RatingPopupComposer = RatingPopupComposer; + return RatingPopupComposer; }); diff --git a/addons/portal_rating/static/src/xml/portal_rating_composer.xml b/addons/portal_rating/static/src/xml/portal_rating_composer.xml index b437314d80b..6878c4399c2 100644 --- a/addons/portal_rating/static/src/xml/portal_rating_composer.xml +++ b/addons/portal_rating/static/src/xml/portal_rating_composer.xml @@ -5,30 +5,6 @@ It can also be used to modify a message --> -
-
- - - - -
- -
-