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