summaryrefslogtreecommitdiff
path: root/addons/im_livechat
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2017-12-21 12:29:50 +0100
committerAlexandre Kühn <alex.e.kuhn@gmail.com>2018-02-01 14:51:58 +0100
commit02ec09cb1c3e2d7bc7968f40c18f2208d7f3f498 (patch)
tree10d91136786b13666e78cfb94b12f1609d6f883d /addons/im_livechat
parent0cd656b5af38051df3f7d6319e99705150bb1cd8 (diff)
[REF][IMP] *: mail js refactoring
This commit improves the JS code of the mail module, which comes maily from the new coding guidelines and the addition of "JS services". JS services are important objects that do not fit well in the component tree, such as chat_manager or ajax. The benefits of JS services are improved readability of the code, reduced coupling, and more testable modules. In particular, discuss was hard to Summary of the changes: - ClientAction has been renamed into Discuss - Clear instantiation of chatManager - New coding guidelines in most mail modules - JS Services can interact with each other - Chat Manager and Window Manager are services - Window Manager renamed to Chat Window Manager - bus.bus is now encapsulated in Bus Service (a service) - The test infrastructure has been tweaked with JS Services - Chat Mixin has been removed A future improvement would be to translate some 'trigger' into 'trigger_up'.
Diffstat (limited to 'addons/im_livechat')
-rw-r--r--addons/im_livechat/static/src/js/im_livechat.js29
-rw-r--r--addons/im_livechat/static/src/js/im_livechat_backend.js17
-rw-r--r--addons/im_livechat/static/src/xml/im_livechat_backend.xml2
-rw-r--r--addons/im_livechat/views/im_livechat_channel_templates.xml2
4 files changed, 22 insertions, 28 deletions
diff --git a/addons/im_livechat/static/src/js/im_livechat.js b/addons/im_livechat/static/src/js/im_livechat.js
index 1cda8ee0cdc..ba2433abafc 100644
--- a/addons/im_livechat/static/src/js/im_livechat.js
+++ b/addons/im_livechat/static/src/js/im_livechat.js
@@ -1,10 +1,9 @@
odoo.define('im_livechat.im_livechat', function (require) {
"use strict";
-var local_storage = require('web.local_storage');
-var bus = require('bus.bus').bus;
var config = require('web.config');
var core = require('web.core');
+var ServiceProviderMixin = require('web.ServiceProviderMixin');
var session = require('web.session');
var time = require('web.time');
var utils = require('web.utils');
@@ -20,10 +19,10 @@ var LIVECHAT_COOKIE_HISTORY = 'im_livechat_history';
var HISTORY_LIMIT = 15;
// History tracking
-var page = window.location.href.replace(/^.*\/\/[^\/]+/, '');
+var page = window.location.href.replace(/^.*\/\/[^/]+/, '');
var page_history = utils.get_cookie(LIVECHAT_COOKIE_HISTORY);
var url_history = [];
-if(page_history){
+if (page_history) {
url_history = JSON.parse(page_history) || [];
}
if (!_.contains(url_history, page)) {
@@ -34,8 +33,11 @@ if (!_.contains(url_history, page)) {
utils.set_cookie(LIVECHAT_COOKIE_HISTORY, JSON.stringify(url_history), 60*60*24); // 1 day cookie
}
-
-var LivechatButton = Widget.extend({
+/**
+ * In order to handle services like ajax in the external lib,
+ * The parent of Livechat, LivechatButton, is set as a service provider
+ */
+var LivechatButton = Widget.extend(ServiceProviderMixin, {
className:"openerp o_livechat_button hidden-print",
events: {
@@ -54,6 +56,7 @@ var LivechatButton = Widget.extend({
this.chat_window = null;
this.messages = [];
this.server_url = server_url;
+ this.busBus = this.call('bus_service', 'getBus');
},
willStart: function () {
@@ -87,7 +90,7 @@ var LivechatButton = Widget.extend({
this.auto_popup_timeout = setTimeout(this.open_chat.bind(this), this.rule.auto_popup_timer*1000);
}
}
- bus.on('notification', this, function (notifications) {
+ this.busBus.on('notification', this, function (notifications) {
var self = this;
_.each(notifications, function (notification) {
self._on_notification(notification);
@@ -95,9 +98,9 @@ var LivechatButton = Widget.extend({
});
return this._super();
},
- _on_notification: function(notification){
+ _on_notification: function (notification){
if (this.channel && (notification[0] === this.channel.uuid)) {
- if(notification[1]._type === "history_command") { // history request
+ if (notification[1]._type === "history_command") { // history request
var cookie = utils.get_cookie(LIVECHAT_COOKIE_HISTORY);
var history = cookie ? JSON.parse(cookie) : [];
session.rpc("/im_livechat/history", {
@@ -105,7 +108,7 @@ var LivechatButton = Widget.extend({
channel_uuid: this.channel.uuid,
page_history: history,
});
- }else{ // normal message
+ } else { // normal message
this.add_message(notification[1]);
this.render_messages();
if (this.chat_window.folded || !this.chat_window.thread.is_at_bottom()) {
@@ -114,7 +117,7 @@ var LivechatButton = Widget.extend({
}
}
},
- load_qweb_template: function(){
+ load_qweb_template: function (){
var xml_files = ['/mail/static/src/xml/chat_window.xml',
'/mail/static/src/xml/thread.xml',
'/im_livechat/static/src/xml/im_livechat.xml'];
@@ -153,8 +156,8 @@ var LivechatButton = Widget.extend({
self.send_welcome_message();
self.render_messages();
- bus.add_channel(channel.uuid);
- bus.start_polling();
+ this.busBus.add_channel(channel.uuid);
+ this.busBus.start_polling();
utils.set_cookie('im_livechat_session', JSON.stringify(channel), 60*60);
utils.set_cookie('im_livechat_auto_popup', JSON.stringify(false), 60*60);
diff --git a/addons/im_livechat/static/src/js/im_livechat_backend.js b/addons/im_livechat/static/src/js/im_livechat_backend.js
index 4d1e514c01b..877ef57edaa 100644
--- a/addons/im_livechat/static/src/js/im_livechat_backend.js
+++ b/addons/im_livechat/static/src/js/im_livechat_backend.js
@@ -1,11 +1,9 @@
-odoo.define('im_livechat.chat_client_action', function (require) {
+odoo.define('im_livechat.chat_discuss', function (require) {
"use strict";
-require('mail.chat_client_action');
-var chat_manager = require('mail.chat_manager');
-var core = require('web.core');
+var Discuss = require('mail.chat_discuss');
-core.action_registry.get('mail.chat.instant_messaging').include({
+Discuss.include({
_renderSidebar: function (options) {
// Override to sort livechat channels by last message's date
var channel_partition = _.partition(options.channels, function (channel) {
@@ -19,13 +17,4 @@ core.action_registry.get('mail.chat.instant_messaging').include({
},
});
-chat_manager.bus.on('new_message', null, function (msg) {
- _.each(msg.channel_ids, function (channel_id) {
- var channel = chat_manager.get_channel(channel_id);
- if (channel) {
- channel.last_message_date = msg.date; // update the last message's date of the channel
- }
- });
});
-
-}); \ No newline at end of file
diff --git a/addons/im_livechat/static/src/xml/im_livechat_backend.xml b/addons/im_livechat/static/src/xml/im_livechat_backend.xml
index 90569ba52dd..49018af65eb 100644
--- a/addons/im_livechat/static/src/xml/im_livechat_backend.xml
+++ b/addons/im_livechat/static/src/xml/im_livechat_backend.xml
@@ -14,7 +14,7 @@
</t>
<!-- Mobile templates -->
- <t t-extend="mail.client_action_mobile">
+ <t t-extend="mail.discuss_mobile">
<t t-jquery=".o_mail_mobile_tabs" t-operation="append">
<div class="o_mail_mobile_tab" data-type="livechat">
<span class="fa fa-comments"/>
diff --git a/addons/im_livechat/views/im_livechat_channel_templates.xml b/addons/im_livechat/views/im_livechat_channel_templates.xml
index 911adc39762..ec25a417dca 100644
--- a/addons/im_livechat/views/im_livechat_channel_templates.xml
+++ b/addons/im_livechat/views/im_livechat_channel_templates.xml
@@ -128,6 +128,7 @@
<script type="text/javascript" src="/web/static/src/js/core/ajax.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/time.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/mixins.js"></script>
+ <script type="text/javascript" src="/web/static/src/js/core/service_mixins.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/rpc.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/widget.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/registry.js"></script>
@@ -141,6 +142,7 @@
<script type="text/javascript" src="/web/static/src/js/services/local_storage_service.js"></script>
<!-- Bus, Mail, Livechat -->
<script type="text/javascript" src="/bus/static/src/js/bus.js"></script>
+ <script type="text/javascript" src="/bus/static/src/js/services/bus_service.js"></script>
<script type="text/javascript" src="/mail/static/src/js/chat_window.js"></script>
<script type="text/javascript" src="/mail/static/src/js/thread.js"></script>
<script type="text/javascript" src="/mail/static/src/js/document_viewer.js"></script>