Now correctly send technical message.
[odoo/odoo.git] / addons / im / static / src / js / im.js
1
2 (function() {
3     "use strict";
4
5     var instance = openerp;
6
7     openerp.im = {};
8
9     var USERS_LIMIT = 20;
10
11     var _t = instance.web._t;
12     var QWeb = instance.web.qweb;
13
14     instance.web.UserMenu.include({
15         do_update: function(){
16             var self = this;
17             this.update_promise.then(function() {
18                 im_common.notification = function(message) {
19                     instance.client.do_warn(message);
20                 };
21                 // TODO: allow to use a different host for the chat
22                 im_common.connection = new openerp.Session(self, null, {session_id: openerp.session.session_id});
23
24                 var im = new instance.im.InstantMessaging(self);
25                 im.appendTo(instance.client.$el);
26                 var button = new instance.im.ImTopButton(this);
27                 button.on("clicked", im, im.switch_display);
28                 button.appendTo(instance.webclient.$el.find('.oe_systray'));
29             });
30             return this._super.apply(this, arguments);
31         },
32     });
33
34     instance.im.ImTopButton = instance.web.Widget.extend({
35         template:'ImTopButton',
36         events: {
37             "click": "clicked",
38         },
39         clicked: function() {
40             this.trigger("clicked");
41         },
42     });
43
44     instance.im.InstantMessaging = instance.web.Widget.extend({
45         template: "InstantMessaging",
46         events: {
47             "keydown .oe_im_searchbox": "input_change",
48             "keyup .oe_im_searchbox": "input_change",
49             "change .oe_im_searchbox": "input_change",
50         },
51         init: function(parent) {
52             this._super(parent);
53             this.shown = false;
54             this.set("right_offset", 0);
55             this.set("current_search", "");
56             this.users = [];
57             this.c_manager = new im_common.ConversationManager(this);
58             this.on("change:right_offset", this.c_manager, _.bind(function() {
59                 this.c_manager.set("right_offset", this.get("right_offset"));
60             }, this));
61             this.user_search_dm = new instance.web.DropMisordered();
62         },
63         start: function() {
64             this.$el.css("right", -this.$el.outerWidth());
65             $(window).scroll(_.bind(this.calc_box, this));
66             $(window).resize(_.bind(this.calc_box, this));
67             this.calc_box();
68
69             this.on("change:current_search", this, this.search_changed);
70             this.search_changed();
71
72             var self = this;
73
74             return this.c_manager.start_polling().then(function() {
75                 self.c_manager.on("new_conversation", self, function(conv) {
76                     conv.$el.droppable({
77                         drop: function(event, ui) {
78                             self.add_user(conv, ui.draggable.data("user"));
79                         }
80                     });
81                 });
82             });
83         },
84         calc_box: function() {
85             var $topbar = instance.client.$(".oe_topbar");
86             var top = $topbar.offset().top + $topbar.height();
87             top = Math.max(top - $(window).scrollTop(), 0);
88             this.$el.css("top", top);
89             this.$el.css("bottom", 0);
90         },
91         input_change: function() {
92             this.set("current_search", this.$(".oe_im_searchbox").val());
93         },
94         search_changed: function(e) {
95             var users = new instance.web.Model("im.user");
96             var self = this;
97             return this.user_search_dm.add(users.call("search_users", [this.get("current_search"), ["name", "user_id", "uuid", "im_status"],
98                     USERS_LIMIT], {context:new instance.web.CompoundContext()})).then(function(users) {
99                 var logged_users = _.filter(users, function(u) { return !!u.im_status; });
100                 var non_logged_users = _.filter(users, function(u) { return !u.im_status; });
101                 users = logged_users.concat(non_logged_users);
102                 self.c_manager.add_to_user_cache(users);
103                 self.$(".oe_im_input").val("");
104                 var old_users = self.users;
105                 self.users = [];
106                 _.each(users, function(user) {
107                     var widget = new instance.im.UserWidget(self, self.c_manager.get_user(user.id));
108                     widget.appendTo(self.$(".oe_im_users"));
109                     widget.on("activate_user", self, self.activate_user);
110                     self.users.push(widget);
111                 });
112                 _.each(old_users, function(user) {
113                     user.destroy();
114                 });
115             });
116         },
117         switch_display: function() {
118             var fct =  _.bind(function(place) {
119                 this.set("right_offset", place + this.$el.outerWidth());
120             }, this);
121             var opt = {
122                 step: fct,
123             };
124             if (this.shown) {
125                 this.$el.animate({
126                     right: -this.$el.outerWidth(),
127                 }, opt);
128             } else {
129                 if (! this.c_manager.get_activated()) {
130                     this.do_warn("Instant Messaging is not activated on this server.", "");
131                     return;
132                 }
133                 this.$el.animate({
134                     right: 0,
135                 }, opt);
136             }
137             this.shown = ! this.shown;
138         },
139         activate_user: function(user) {
140             var self = this;
141             im_common.connection.model("im.session").call("session_get", [user.get("id"), self.c_manager.me.get("uuid")]).then(function(session) {
142                 self.c_manager.activate_session(session.id, true);
143             });
144         },
145         add_user: function(conversation, user) {
146             conversation.add_user(user);
147         },
148     });
149
150     instance.im.UserWidget = instance.web.Widget.extend({
151         "template": "UserWidget",
152         events: {
153             "click": "activate_user",
154         },
155         init: function(parent, user) {
156             this._super(parent);
157             this.user = user;
158             this.user.add_watcher();
159         },
160         start: function() {
161             this.$el.data("user", this.user);
162             this.$el.draggable({helper: "clone"});
163             var change_status = function() {
164                 this.$(".oe_im_user_online").toggle(this.user.get("im_status") === true);
165             };
166             this.user.on("change:im_status", this, change_status);
167             change_status.call(this);
168         },
169         activate_user: function() {
170             this.trigger("activate_user", this.user);
171         },
172         destroy: function() {
173             this.user.remove_watcher();
174             this._super();
175         },
176     });
177
178 })();