cad3c6dff0bb6c5f67cb59c739f50652bcf04f59
[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();
74         },
75         calc_box: function() {
76             var $topbar = instance.client.$(".oe_topbar");
77             var top = $topbar.offset().top + $topbar.height();
78             top = Math.max(top - $(window).scrollTop(), 0);
79             this.$el.css("top", top);
80             this.$el.css("bottom", 0);
81         },
82         input_change: function() {
83             this.set("current_search", this.$(".oe_im_searchbox").val());
84         },
85         search_changed: function(e) {
86             var users = new instance.web.Model("im.user");
87             var self = this;
88             return this.user_search_dm.add(users.call("search_users", [this.get("current_search"), ["name", "user_id", "uuid", "im_status"],
89                     USERS_LIMIT], {context:new instance.web.CompoundContext()})).then(function(users) {
90                 var logged_users = _.filter(users, function(u) { return !!u.im_status; });
91                 var non_logged_users = _.filter(users, function(u) { return !u.im_status; });
92                 users = logged_users.concat(non_logged_users);
93                 self.c_manager.add_to_user_cache(users);
94                 self.$(".oe_im_input").val("");
95                 var old_users = self.users;
96                 self.users = [];
97                 _.each(users, function(user) {
98                     var widget = new instance.im.UserWidget(self, self.c_manager.get_user(user.id));
99                     widget.appendTo(self.$(".oe_im_users"));
100                     widget.on("activate_user", self, self.activate_user);
101                     self.users.push(widget);
102                 });
103                 _.each(old_users, function(user) {
104                     user.destroy();
105                 });
106             });
107         },
108         switch_display: function() {
109             var fct =  _.bind(function(place) {
110                 this.set("right_offset", place + this.$el.outerWidth());
111             }, this);
112             var opt = {
113                 step: fct,
114             };
115             if (this.shown) {
116                 this.$el.animate({
117                     right: -this.$el.outerWidth(),
118                 }, opt);
119             } else {
120                 if (! this.c_manager.get_activated()) {
121                     this.do_warn("Instant Messaging is not activated on this server.", "");
122                     return;
123                 }
124                 this.$el.animate({
125                     right: 0,
126                 }, opt);
127             }
128             this.shown = ! this.shown;
129         },
130         activate_user: function(user) {
131             var self = this;
132             im_common.connection.model("im.session").call("session_get", [user.get("id"), self.c_manager.me.get("uuid")]).then(function(session) {
133                 self.c_manager.activate_session(session.id, true);
134             });
135         },
136     });
137
138     instance.im.UserWidget = instance.web.Widget.extend({
139         "template": "UserWidget",
140         events: {
141             "click": "activate_user",
142         },
143         init: function(parent, user) {
144             this._super(parent);
145             this.user = user;
146             this.user.add_watcher();
147         },
148         start: function() {
149             var change_status = function() {
150                 this.$(".oe_im_user_online").toggle(this.user.get("im_status") === true);
151             };
152             this.user.on("change:im_status", this, change_status);
153             change_status.call(this);
154         },
155         activate_user: function() {
156             this.trigger("activate_user", this.user);
157         },
158         destroy: function() {
159             this.user.remove_watcher();
160             this._super();
161         },
162     });
163
164 })();