From 021f9aeec7c5626942fd4c00b43563592aba9532 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 3 Feb 2016 14:19:48 +0100 Subject: [PATCH] [FIX] mail: systray items must wait for chat_manager Those items might have been rendered and inserted into the DOM before the chat manager initialization was completed. As those items rely on chat manager's data, they should wait for it before executing their action (inbox redirection and channels preview display). --- addons/mail/static/src/js/systray.js | 74 +++++++++++++++------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/addons/mail/static/src/js/systray.js b/addons/mail/static/src/js/systray.js index 3929947acbb..d81aadcf8eb 100644 --- a/addons/mail/static/src/js/systray.js +++ b/addons/mail/static/src/js/systray.js @@ -21,7 +21,7 @@ var InboxItem = Widget.extend({ start: function () { this.$needaction_counter = this.$('.o_notification_counter'); chat_manager.bus.on("update_needaction", this, this.update_counter); - this.update_counter(); + chat_manager.is_ready.then(this.update_counter.bind(this)); return this._super(); }, update_counter: function () { @@ -31,7 +31,7 @@ var InboxItem = Widget.extend({ }, on_click: function (event) { event.preventDefault(); - this.discuss_redirect(); + chat_manager.is_ready.then(this.discuss_redirect.bind(this)); }, discuss_redirect: _.debounce(function () { var self = this; @@ -66,7 +66,7 @@ var MessagingMenu = Widget.extend({ this.$channels_preview = this.$('.o_mail_navbar_dropdown_channels'); this.filter = false; chat_manager.bus.on("update_channel_unread_counter", this, this.update_counter); - this.update_counter(); + chat_manager.is_ready.then(this.update_counter.bind(this)); return this._super(); }, is_open: function () { @@ -78,52 +78,56 @@ var MessagingMenu = Widget.extend({ this.$el.toggleClass('o_no_notification', !counter); this.$el.toggleClass('o_unread_chat', !!chat_manager.get_chat_unread_counter()); if (this.is_open()) { - this.render_channels_preview(); + this.update_channels_preview(); } }, - render_channels_preview: function () { + update_channels_preview: function () { var self = this; - var channels = _.filter(chat_manager.get_channels(), function (channel) { - if (self.filter === 'chat') { - return channel.is_chat; - } else if (self.filter === 'channels') { - return !channel.is_chat && channel.type !== 'static'; - } else { - return channel.type !== 'static'; - } - }); // Display spinner while waiting for channels preview this.$channels_preview.html(QWeb.render('mail.chat.Spinner')); - chat_manager.get_channels_preview(channels).then(function (channels_preview) { - // Sort channels: 1. channels with unread messages, 2. chat, 3. by date of last msg - channels_preview.sort(function (c1, c2) { - return Math.min(1, c2.unread_counter) - Math.min(1, c1.unread_counter) || - c2.is_chat - c1.is_chat || - c2.last_message.date.diff(c1.last_message.date); - }); - - // Generate last message preview (inline message body and compute date to display) - _.each(channels_preview, function (channel) { - channel.last_message_preview = - channel.last_message.displayed_author + ': ' + - chat_manager.get_message_body_preview(channel.last_message.body); - if (channel.last_message.date.isSame(new Date(), 'd')) { // today - channel.last_message_date = channel.last_message.date.format('LT'); + chat_manager.is_ready.then(function () { + var channels = _.filter(chat_manager.get_channels(), function (channel) { + if (self.filter === 'chat') { + return channel.is_chat; + } else if (self.filter === 'channels') { + return !channel.is_chat && channel.type !== 'static'; } else { - channel.last_message_date = channel.last_message.date.format('lll'); + return channel.type !== 'static'; } }); - self.$channels_preview.html(QWeb.render('mail.chat.ChannelsPreview', { - channels: channels_preview, - })); + chat_manager.get_channels_preview(channels).then(self._render_channels_preview.bind(self)); }); }, + _render_channels_preview: function (channels_preview) { + // Sort channels: 1. channels with unread messages, 2. chat, 3. by date of last msg + channels_preview.sort(function (c1, c2) { + return Math.min(1, c2.unread_counter) - Math.min(1, c1.unread_counter) || + c2.is_chat - c1.is_chat || + c2.last_message.date.diff(c1.last_message.date); + }); + + // Generate last message preview (inline message body and compute date to display) + _.each(channels_preview, function (channel) { + channel.last_message_preview = + channel.last_message.displayed_author + ': ' + + chat_manager.get_message_body_preview(channel.last_message.body); + if (channel.last_message.date.isSame(new Date(), 'd')) { // today + channel.last_message_date = channel.last_message.date.format('LT'); + } else { + channel.last_message_date = channel.last_message.date.format('lll'); + } + }); + + this.$channels_preview.html(QWeb.render('mail.chat.ChannelsPreview', { + channels: channels_preview, + })); + }, on_click: function () { if (!this.is_open()) { - this.render_channels_preview(); // we are opening the dropdown so render its content + this.update_channels_preview(); // we are opening the dropdown so update its content } }, on_click_filter_button: function (event) { @@ -132,7 +136,7 @@ var MessagingMenu = Widget.extend({ var $target = $(event.currentTarget); $target.addClass('o_selected'); this.filter = $target.data('filter'); - this.render_channels_preview(); + this.update_channels_preview(); }, on_click_new_message: function () { chat_manager.bus.trigger('open_chat');