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 im_common.connection = openerp.session;
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'));
29 return this._super.apply(this, arguments);
33 instance.im.ImTopButton = instance.web.Widget.extend({
34 template:'ImTopButton',
39 this.trigger("clicked");
43 instance.im.InstantMessaging = instance.web.Widget.extend({
44 template: "InstantMessaging",
46 "keydown .oe_im_searchbox": "input_change",
47 "keyup .oe_im_searchbox": "input_change",
48 "change .oe_im_searchbox": "input_change",
50 init: function(parent) {
53 this.set("right_offset", 0);
54 this.set("current_search", "");
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"));
60 this.user_search_dm = new instance.web.DropMisordered();
63 this.$el.css("right", -this.$el.outerWidth());
64 $(window).scroll(_.bind(this.calc_box, this));
65 $(window).resize(_.bind(this.calc_box, this));
68 this.on("change:current_search", this, this.search_changed);
69 this.search_changed();
73 return this.c_manager.start_polling().then(function() {
74 self.c_manager.on("new_conversation", self, function(conv) {
76 drop: function(event, ui) {
77 self.add_user(conv, ui.draggable.data("user"));
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);
90 input_change: function() {
91 this.set("current_search", this.$(".oe_im_searchbox").val());
93 search_changed: function(e) {
94 var users = new instance.web.Model("im.user");
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;
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);
111 _.each(old_users, function(user) {
116 switch_display: function() {
117 var fct = _.bind(function(place) {
118 this.set("right_offset", place + this.$el.outerWidth());
125 right: -this.$el.outerWidth(),
128 if (! this.c_manager.get_activated()) {
129 this.do_warn("Instant Messaging is not activated on this server.", "");
136 this.shown = ! this.shown;
138 activate_user: function(user) {
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);
144 add_user: function(conversation, user) {
145 conversation.add_user(user);
149 instance.im.UserWidget = instance.web.Widget.extend({
150 "template": "UserWidget",
152 "click": "activate_user",
154 init: function(parent, user) {
157 this.user.add_watcher();
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);
165 this.user.on("change:im_status", this, change_status);
166 change_status.call(this);
168 activate_user: function() {
169 this.trigger("activate_user", this.user);
171 destroy: function() {
172 this.user.remove_watcher();