summaryrefslogtreecommitdiff
path: root/addons/im_livechat/static/src/js/im_livechat.js
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/static/src/js/im_livechat.js
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/static/src/js/im_livechat.js')
-rw-r--r--addons/im_livechat/static/src/js/im_livechat.js29
1 files changed, 16 insertions, 13 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);