-
Notifications
You must be signed in to change notification settings - Fork 23.4k
/
systray.js
151 lines (139 loc) · 5.76 KB
/
systray.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
odoo.define('mail.systray', function (require) {
"use strict";
var core = require('web.core');
var SystrayMenu = require('web.SystrayMenu');
var Widget = require('web.Widget');
var chat_manager = require('mail.chat_manager');
var QWeb = core.qweb;
/**
* Menu item appended in the systray part of the navbar, redirects to the Inbox in Discuss
* Also displays the needaction counter (= Inbox counter)
*/
var InboxItem = Widget.extend({
template:'mail.chat.InboxItem',
events: {
"click": "on_click",
},
start: function () {
this.$needaction_counter = this.$('.o_notification_counter');
chat_manager.bus.on("update_needaction", this, this.update_counter);
chat_manager.is_ready.then(this.update_counter.bind(this));
return this._super();
},
update_counter: function () {
var counter = chat_manager.get_needaction_counter();
this.$needaction_counter.text(counter);
this.$el.toggleClass('o_no_notification', !counter);
},
on_click: function (event) {
event.preventDefault();
chat_manager.is_ready.then(this.discuss_redirect.bind(this));
},
discuss_redirect: _.debounce(function () {
var self = this;
this.do_action('mail.mail_channel_action_client_chat', {clear_breadcrumbs: true}).then(function () {
self.trigger_up('hide_app_switcher');
core.bus.trigger('change_menu_section', chat_manager.get_discuss_menu_id());
});
}, 1000, true),
});
/**
* Menu item appended in the systray part of the navbar
*
* The menu item indicates the counter of needactions + unread messages in chat channels. When
* clicking on it, it toggles a dropdown containing a preview of each pinned channels (except
* static and mass mailing channels) with a quick link to open them in chat windows. It also
* contains a direct link to the Inbox in Discuss.
**/
var MessagingMenu = Widget.extend({
template:'mail.chat.MessagingMenu',
events: {
"click": "on_click",
"click .o_filter_button": "on_click_filter_button",
"click .o_new_message": "on_click_new_message",
"click .o_mail_channel_preview": "on_click_channel",
},
start: function () {
this.$filter_buttons = this.$('.o_filter_button');
this.$channels_preview = this.$('.o_mail_navbar_dropdown_channels');
this.filter = false;
chat_manager.bus.on("update_channel_unread_counter", this, this.update_counter);
chat_manager.is_ready.then(this.update_counter.bind(this));
return this._super();
},
is_open: function () {
return this.$el.hasClass('open');
},
update_counter: function () {
var counter = chat_manager.get_unread_conversation_counter();
this.$('.o_notification_counter').text(counter);
this.$el.toggleClass('o_no_notification', !counter);
this.$el.toggleClass('o_unread_chat', !!chat_manager.get_chat_unread_counter());
if (this.is_open()) {
this.update_channels_preview();
}
},
update_channels_preview: function () {
var self = this;
// Display spinner while waiting for channels preview
this.$channels_preview.html(QWeb.render('Spinner'));
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 {
return channel.type !== 'static';
}
});
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 = 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.update_channels_preview(); // we are opening the dropdown so update its content
}
},
on_click_filter_button: function (event) {
event.stopPropagation();
this.$filter_buttons.removeClass('o_selected');
var $target = $(event.currentTarget);
$target.addClass('o_selected');
this.filter = $target.data('filter');
this.update_channels_preview();
},
on_click_new_message: function () {
chat_manager.bus.trigger('open_chat');
},
on_click_channel: function (event) {
var channel_id = $(event.currentTarget).data('channel_id');
var channel = chat_manager.get_channel(channel_id);
if (channel) {
chat_manager.open_channel(channel);
}
},
});
SystrayMenu.Items.push(MessagingMenu);
SystrayMenu.Items.push(InboxItem);
});