5 var instance = openerp;
11 var _t = instance.web._t;
12 var QWeb = instance.web.qweb;
14 instance.web.UserMenu.include({
15 do_update: function(){
17 this.update_promise.then(function() {
18 im_common.notification = function(message) {
19 instance.client.do_warn(message);
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});
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'));
30 return this._super.apply(this, arguments);
34 instance.im.ImTopButton = instance.web.Widget.extend({
35 template:'ImTopButton',
40 this.trigger("clicked");
44 instance.im.InstantMessaging = instance.web.Widget.extend({
45 template: "InstantMessaging",
47 "keydown .oe_im_searchbox": "input_change",
48 "keyup .oe_im_searchbox": "input_change",
49 "change .oe_im_searchbox": "input_change",
51 init: function(parent) {
54 this.set("right_offset", 0);
55 this.set("current_search", "");
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"));
61 this.user_search_dm = new instance.web.DropMisordered();
64 this.$el.css("right", -this.$el.outerWidth());
65 $(window).scroll(_.bind(this.calc_box, this));
66 $(window).resize(_.bind(this.calc_box, this));
69 this.on("change:current_search", this, this.search_changed);
70 this.search_changed();
74 return this.c_manager.start_polling().then(function() {
75 self.c_manager.on("new_conversation", self, function(conv) {
77 drop: function(event, ui) {
78 self.add_user(conv, ui.draggable.data("user"));
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);
91 input_change: function() {
92 this.set("current_search", this.$(".oe_im_searchbox").val());
94 search_changed: function(e) {
95 var users = new instance.web.Model("im.user");
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;
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);
112 _.each(old_users, function(user) {
117 switch_display: function() {
118 var fct = _.bind(function(place) {
119 this.set("right_offset", place + this.$el.outerWidth());
126 right: -this.$el.outerWidth(),
129 if (! this.c_manager.get_activated()) {
130 this.do_warn("Instant Messaging is not activated on this server.", "");
137 this.shown = ! this.shown;
139 activate_user: function(user) {
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);
145 add_user: function(conversation, user) {
146 conversation.add_user(user);
150 instance.im.UserWidget = instance.web.Widget.extend({
151 "template": "UserWidget",
153 "click": "activate_user",
155 init: function(parent, user) {
158 this.user.add_watcher();
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);
166 this.user.on("change:im_status", this, change_status);
167 change_status.call(this);
169 activate_user: function() {
170 this.trigger("activate_user", this.user);
172 destroy: function() {
173 this.user.remove_watcher();