2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-27 10:02:41 +00:00
|
|
|
openerp.web_im = function(instance) {
|
2012-11-15 17:49:02 +00:00
|
|
|
|
2012-11-27 14:47:24 +00:00
|
|
|
var USERS_LIMIT = 20;
|
|
|
|
var ERROR_DELAY = 5000;
|
|
|
|
|
2012-11-30 14:16:46 +00:00
|
|
|
var _t = instance.web._t,
|
|
|
|
_lt = instance.web._lt;
|
|
|
|
var QWeb = instance.web.qweb;
|
|
|
|
|
2012-11-26 12:38:53 +00:00
|
|
|
instance.web.UserMenu.include({
|
|
|
|
do_update: function(){
|
|
|
|
var self = this;
|
|
|
|
this.update_promise.then(function() {
|
2012-11-27 10:02:41 +00:00
|
|
|
var im = new instance.web_im.InstantMessaging(self);
|
|
|
|
im.appendTo(instance.client.$el);
|
2012-11-27 15:29:55 +00:00
|
|
|
var button = new instance.web_im.ImTopButton(this);
|
2012-11-27 10:56:35 +00:00
|
|
|
button.on("clicked", im, im.switch_display);
|
2012-11-26 12:38:53 +00:00
|
|
|
button.appendTo(instance.webclient.$el.find('.oe_systray'));
|
|
|
|
});
|
|
|
|
return this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2012-11-27 15:29:55 +00:00
|
|
|
instance.web_im.ImTopButton = instance.web.Widget.extend({
|
2012-11-27 10:30:52 +00:00
|
|
|
template:'ImTopButton',
|
2012-11-26 12:38:53 +00:00
|
|
|
events: {
|
2012-11-27 16:44:32 +00:00
|
|
|
"click": "clicked",
|
2012-11-26 12:38:53 +00:00
|
|
|
},
|
|
|
|
clicked: function() {
|
2012-11-27 10:56:35 +00:00
|
|
|
this.trigger("clicked");
|
2012-11-26 12:38:53 +00:00
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
|
|
|
|
2012-11-27 10:02:41 +00:00
|
|
|
instance.web_im.InstantMessaging = instance.web.Widget.extend({
|
|
|
|
template: "InstantMessaging",
|
2012-11-27 10:30:52 +00:00
|
|
|
events: {
|
2012-11-27 16:43:41 +00:00
|
|
|
"keydown .oe_im_searchbox": "input_change",
|
|
|
|
"keyup .oe_im_searchbox": "input_change",
|
|
|
|
"change .oe_im_searchbox": "input_change",
|
2012-11-27 10:30:52 +00:00
|
|
|
},
|
2012-11-26 12:38:53 +00:00
|
|
|
init: function(parent) {
|
|
|
|
this._super(parent);
|
|
|
|
this.shown = false;
|
2012-11-26 12:55:48 +00:00
|
|
|
this.set("right_offset", 0);
|
2012-11-27 16:43:41 +00:00
|
|
|
this.set("current_search", "");
|
2012-11-27 10:30:52 +00:00
|
|
|
this.last = null;
|
|
|
|
this.users = [];
|
2012-11-30 10:13:18 +00:00
|
|
|
this.activated = false;
|
2012-11-27 15:29:55 +00:00
|
|
|
this.c_manager = new instance.web_im.ConversationManager(this);
|
|
|
|
this.on("change:right_offset", this.c_manager, _.bind(function() {
|
|
|
|
this.c_manager.set("right_offset", this.get("right_offset"));
|
|
|
|
}, this));
|
2012-11-27 16:43:41 +00:00
|
|
|
this.user_search_dm = new instance.web.DropMisordered();
|
2012-11-30 15:34:21 +00:00
|
|
|
this.users_cache = {};
|
2012-11-26 12:38:53 +00:00
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
start: function() {
|
2012-11-26 12:38:53 +00:00
|
|
|
this.$el.css("right", -this.$el.outerWidth());
|
|
|
|
$(window).scroll(_.bind(this.calc_box, this));
|
|
|
|
$(window).resize(_.bind(this.calc_box, this));
|
2012-11-27 10:30:52 +00:00
|
|
|
this.calc_box();
|
|
|
|
|
2012-11-27 16:43:41 +00:00
|
|
|
this.on("change:current_search", this, this.search_changed);
|
|
|
|
this.search_changed();
|
2012-11-30 10:13:18 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2012-11-30 16:38:14 +00:00
|
|
|
return this.ensure_users([instance.session.uid]).then(function() {
|
2012-12-03 15:30:20 +00:00
|
|
|
var me = self.users_cache[instance.session.uid];
|
|
|
|
delete self.users_cache[instance.session.uid];
|
|
|
|
self.c_manager.set_me(me);
|
2012-11-30 16:38:14 +00:00
|
|
|
return new instance.web.Model("im.message").call("activated", [], {context: new instance.web.CompoundContext()}).then(function(activated) {
|
|
|
|
if (activated) {
|
|
|
|
self.activated = true;
|
|
|
|
self.poll();
|
|
|
|
}
|
|
|
|
});
|
2012-11-30 10:13:18 +00:00
|
|
|
});
|
2012-11-15 17:49:02 +00:00
|
|
|
},
|
2012-11-26 12:38:53 +00:00
|
|
|
calc_box: function() {
|
|
|
|
var $topbar = instance.client.$(".oe_topbar");
|
|
|
|
var top = $topbar.offset().top + $topbar.height();
|
|
|
|
top = Math.max(top - $(window).scrollTop(), 0);
|
|
|
|
this.$el.css("top", top);
|
|
|
|
this.$el.css("bottom", 0);
|
|
|
|
},
|
2012-11-27 16:43:41 +00:00
|
|
|
input_change: function() {
|
|
|
|
this.set("current_search", this.$(".oe_im_searchbox").val());
|
|
|
|
},
|
|
|
|
search_changed: function(e) {
|
2012-11-27 10:30:52 +00:00
|
|
|
var users = new instance.web.Model("res.users");
|
|
|
|
var self = this;
|
2012-11-30 16:26:39 +00:00
|
|
|
return this.user_search_dm.add(users.query(["name"])
|
2012-11-27 16:43:41 +00:00
|
|
|
.filter([["name", "ilike", this.get("current_search")]])
|
|
|
|
.limit(USERS_LIMIT).all()).then(function(result) {
|
2012-11-30 16:15:59 +00:00
|
|
|
self.add_to_user_cache(result);
|
2012-11-27 10:56:35 +00:00
|
|
|
self.$(".oe_im_input").val("");
|
2012-11-30 16:15:59 +00:00
|
|
|
var old_users = self.users;
|
2012-11-27 10:30:52 +00:00
|
|
|
self.users = [];
|
|
|
|
_.each(result, function(user) {
|
2012-11-30 16:15:59 +00:00
|
|
|
var widget = new instance.web_im.UserWidget(self, self.get_user(user.id));
|
2012-11-27 10:30:52 +00:00
|
|
|
widget.appendTo(self.$(".oe_im_users"));
|
2012-11-27 10:56:35 +00:00
|
|
|
widget.on("activate_user", self, self.activate_user);
|
2012-11-27 10:30:52 +00:00
|
|
|
self.users.push(widget);
|
|
|
|
});
|
2012-11-30 16:15:59 +00:00
|
|
|
_.each(old_users, function(user) {
|
|
|
|
user.destroy();
|
|
|
|
});
|
2012-11-27 10:30:52 +00:00
|
|
|
});
|
|
|
|
},
|
2012-11-30 15:34:21 +00:00
|
|
|
ensure_users: function(user_ids) {
|
|
|
|
var no_cache = {};
|
|
|
|
_.each(user_ids, function(el) {
|
|
|
|
if (! this.users_cache[el])
|
|
|
|
no_cache[el] = el;
|
|
|
|
}, this);
|
|
|
|
var self = this;
|
|
|
|
if (_.size(no_cache) === 0)
|
|
|
|
return $.when();
|
|
|
|
else
|
2012-11-30 16:26:39 +00:00
|
|
|
return new instance.web.Model("res.users").call("read", [_.values(no_cache), ["name"]],
|
2012-11-30 15:34:21 +00:00
|
|
|
{context: new instance.web.CompoundContext()}).then(function(users) {
|
2012-11-30 16:15:59 +00:00
|
|
|
self.add_to_user_cache(users);
|
2012-11-30 15:34:21 +00:00
|
|
|
});
|
|
|
|
},
|
2012-11-30 16:15:59 +00:00
|
|
|
add_to_user_cache: function(user_recs) {
|
|
|
|
_.each(user_recs, function(user_rec) {
|
|
|
|
if (! this.users_cache[user_rec.id]) {
|
|
|
|
var user = new instance.web_im.ImUser(this, user_rec);
|
|
|
|
this.users_cache[user_rec.id] = user;
|
|
|
|
user.on("destroyed", this, function() {
|
|
|
|
delete this.users_cache[user_rec.id];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
2012-11-30 15:34:21 +00:00
|
|
|
get_user: function(user_id) {
|
|
|
|
return this.users_cache[user_id];
|
|
|
|
},
|
2012-11-26 12:38:53 +00:00
|
|
|
switch_display: function() {
|
2012-11-27 15:57:25 +00:00
|
|
|
var fct = _.bind(function(place) {
|
|
|
|
this.set("right_offset", place + this.$el.outerWidth());
|
2012-11-26 12:55:48 +00:00
|
|
|
}, this);
|
|
|
|
var opt = {
|
|
|
|
step: fct,
|
|
|
|
};
|
2012-11-26 12:38:53 +00:00
|
|
|
if (this.shown) {
|
|
|
|
this.$el.animate({
|
|
|
|
right: -this.$el.outerWidth(),
|
2012-11-26 12:55:48 +00:00
|
|
|
}, opt);
|
2012-11-26 12:38:53 +00:00
|
|
|
} else {
|
2012-11-30 10:13:18 +00:00
|
|
|
if (! this.activated) {
|
|
|
|
this.do_warn("Instant Messaging is not activated on this server.", "");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-26 12:38:53 +00:00
|
|
|
this.$el.animate({
|
|
|
|
right: 0,
|
2012-11-26 12:55:48 +00:00
|
|
|
}, opt);
|
2012-11-26 12:38:53 +00:00
|
|
|
}
|
|
|
|
this.shown = ! this.shown;
|
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
poll: function() {
|
|
|
|
var self = this;
|
2012-12-03 15:30:20 +00:00
|
|
|
var user_ids = _.map(this.users_cache, function(el) {
|
|
|
|
return el.get("id");
|
|
|
|
});
|
2012-11-27 10:02:41 +00:00
|
|
|
this.rpc("/im/poll", {
|
2012-11-22 15:23:02 +00:00
|
|
|
last: this.last,
|
2012-12-03 15:30:20 +00:00
|
|
|
users_watch: user_ids,
|
2012-11-30 10:13:18 +00:00
|
|
|
context: instance.web.pyeval.eval('context', {}),
|
2012-11-22 15:23:02 +00:00
|
|
|
}, {shadow: true}).then(function(result) {
|
2012-12-03 15:30:20 +00:00
|
|
|
_.each(result.users_status, function(el) {
|
|
|
|
self.get_user(el.id).set(el);
|
|
|
|
});
|
2012-11-15 17:49:02 +00:00
|
|
|
self.last = result.last;
|
2012-11-30 15:34:21 +00:00
|
|
|
var user_ids = _.pluck(_.pluck(result.res, "from"), 0);
|
|
|
|
self.ensure_users(user_ids).then(function() {
|
|
|
|
_.each(result.res, function(mes) {
|
2012-11-30 16:15:59 +00:00
|
|
|
var user = self.get_user(mes.from[0]);
|
|
|
|
self.c_manager.received_message(mes, user);
|
2012-11-30 15:34:21 +00:00
|
|
|
});
|
|
|
|
self.poll();
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
|
|
|
}, function(unused, e) {
|
|
|
|
e.preventDefault();
|
2012-11-27 14:47:24 +00:00
|
|
|
setTimeout(_.bind(self.poll, self), ERROR_DELAY);
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
2012-11-30 16:15:59 +00:00
|
|
|
activate_user: function(user) {
|
|
|
|
this.c_manager.activate_user(user);
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
|
|
|
|
2012-11-30 16:15:59 +00:00
|
|
|
instance.web_im.UserWidget = instance.web.Widget.extend({
|
|
|
|
"template": "UserWidget",
|
2012-11-27 10:56:35 +00:00
|
|
|
events: {
|
|
|
|
"click": "activate_user",
|
|
|
|
},
|
2012-11-30 16:15:59 +00:00
|
|
|
init: function(parent, user) {
|
2012-11-27 10:30:52 +00:00
|
|
|
this._super(parent);
|
2012-11-30 16:15:59 +00:00
|
|
|
this.user = user;
|
|
|
|
this.user.add_watcher();
|
2012-11-27 10:30:52 +00:00
|
|
|
},
|
2012-11-27 10:56:35 +00:00
|
|
|
activate_user: function() {
|
2012-11-30 16:15:59 +00:00
|
|
|
this.trigger("activate_user", this.user);
|
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
this.user.remove_watcher();
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.web_im.ImUser = instance.web.Class.extend(instance.web.PropertiesMixin, {
|
|
|
|
init: function(parent, user_rec) {
|
|
|
|
instance.web.PropertiesMixin.init.call(this, parent);
|
2012-11-30 16:26:39 +00:00
|
|
|
user_rec.image_url = instance.session.url('/web/binary/image', {model:'res.users', field: 'image_small', id: user_rec.id});
|
2012-11-30 16:15:59 +00:00
|
|
|
this.set(user_rec);
|
|
|
|
this.set("watcher_count", 0);
|
|
|
|
this.on("change:watcher_count", this, function() {
|
|
|
|
if (this.get("watcher_count") === 0)
|
|
|
|
this.destroy();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
this.trigger("destroyed");
|
|
|
|
instance.web.PropertiesMixin.destroy.call(this);
|
|
|
|
},
|
|
|
|
add_watcher: function() {
|
|
|
|
this.set("watcher_count", this.get("watcher_count") + 1);
|
|
|
|
},
|
|
|
|
remove_watcher: function() {
|
|
|
|
this.set("watcher_count", this.get("watcher_count") - 1);
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2012-11-27 15:29:55 +00:00
|
|
|
instance.web_im.ConversationManager = instance.web.Controller.extend({
|
|
|
|
init: function(parent) {
|
|
|
|
this._super(parent);
|
|
|
|
this.set("right_offset", 0);
|
|
|
|
this.conversations = [];
|
|
|
|
this.users = {};
|
|
|
|
this.on("change:right_offset", this, this.calc_positions);
|
|
|
|
},
|
2012-11-30 16:38:14 +00:00
|
|
|
set_me: function(me) {
|
|
|
|
this.me = me;
|
|
|
|
},
|
2012-11-30 16:15:59 +00:00
|
|
|
activate_user: function(user) {
|
|
|
|
if (this.users[user.get('id')]) {
|
|
|
|
return this.users[user.get('id')];
|
2012-11-27 15:29:55 +00:00
|
|
|
}
|
2012-11-30 16:38:14 +00:00
|
|
|
var conv = new instance.web_im.Conversation(this, user, this.me);
|
2012-11-27 15:29:55 +00:00
|
|
|
conv.appendTo(instance.client.$el);
|
|
|
|
conv.on("destroyed", this, function() {
|
|
|
|
this.conversations = _.without(this.conversations, conv);
|
2012-11-30 16:15:59 +00:00
|
|
|
delete this.users[conv.user.get('id')];
|
2012-11-30 15:09:12 +00:00
|
|
|
this.calc_positions();
|
2012-11-27 15:29:55 +00:00
|
|
|
});
|
|
|
|
this.conversations.push(conv);
|
2012-11-30 16:15:59 +00:00
|
|
|
this.users[user.get('id')] = conv;
|
2012-11-27 15:29:55 +00:00
|
|
|
this.calc_positions();
|
|
|
|
return conv;
|
|
|
|
},
|
2012-11-30 16:15:59 +00:00
|
|
|
received_message: function(message, user) {
|
|
|
|
var conv = this.activate_user(user);
|
2012-11-27 15:29:55 +00:00
|
|
|
conv.received_message(message);
|
|
|
|
},
|
|
|
|
calc_positions: function() {
|
|
|
|
var current = this.get("right_offset");
|
|
|
|
_.each(_.range(this.conversations.length), function(i) {
|
|
|
|
this.conversations[i].set("right_position", current);
|
2012-11-30 15:07:48 +00:00
|
|
|
current += this.conversations[i].$el.outerWidth(true);
|
2012-11-27 15:29:55 +00:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2012-11-27 10:56:35 +00:00
|
|
|
instance.web_im.Conversation = instance.web.Widget.extend({
|
|
|
|
"template": "Conversation",
|
|
|
|
events: {
|
|
|
|
"keydown input": "send_message",
|
2012-11-30 14:33:11 +00:00
|
|
|
"click .oe_im_chatview_close": "destroy",
|
2012-12-03 14:50:09 +00:00
|
|
|
"click .oe_im_chatview_header": "show_hide",
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
2012-11-30 16:38:14 +00:00
|
|
|
init: function(parent, user, me) {
|
2012-11-27 10:56:35 +00:00
|
|
|
this._super(parent);
|
2012-11-30 16:38:14 +00:00
|
|
|
this.me = me;
|
2012-11-30 16:15:59 +00:00
|
|
|
this.user = user;
|
|
|
|
this.user.add_watcher();
|
2012-11-27 15:29:55 +00:00
|
|
|
this.set("right_position", 0);
|
2012-12-03 14:50:09 +00:00
|
|
|
this.shown = true;
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
|
|
|
start: function() {
|
2012-11-27 15:29:55 +00:00
|
|
|
this.on("change:right_position", this, this.calc_pos);
|
2012-12-03 14:50:09 +00:00
|
|
|
this.full_height = this.$el.height();
|
2012-11-27 10:56:35 +00:00
|
|
|
this.calc_pos();
|
|
|
|
},
|
2012-12-03 14:50:09 +00:00
|
|
|
show_hide: function() {
|
|
|
|
if (this.shown) {
|
|
|
|
this.$el.animate({
|
|
|
|
height: this.$(".oe_im_chatview_header").outerHeight(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.$el.animate({
|
|
|
|
height: this.full_height,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.shown = ! this.shown;
|
|
|
|
},
|
2012-11-27 10:56:35 +00:00
|
|
|
calc_pos: function() {
|
2012-11-27 15:29:55 +00:00
|
|
|
this.$el.css("right", this.get("right_position"));
|
|
|
|
},
|
|
|
|
received_message: function(message) {
|
2012-11-30 17:02:43 +00:00
|
|
|
this._add_bubble(this.user, message.message, message.date);
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
|
|
|
send_message: function(e) {
|
|
|
|
if(e && e.which !== 13) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var mes = this.$("input").val();
|
|
|
|
this.$("input").val("");
|
2012-11-30 17:02:43 +00:00
|
|
|
this._add_bubble(this.me, mes, instance.web.datetime_to_str(new Date()));
|
2012-11-27 13:51:55 +00:00
|
|
|
var model = new instance.web.Model("im.message");
|
2012-11-30 16:15:59 +00:00
|
|
|
model.call("post", [mes, this.user.get('id')], {context: new instance.web.CompoundContext()});
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
2012-11-30 17:02:43 +00:00
|
|
|
_add_bubble: function(user, item, date) {
|
|
|
|
var items = [item];
|
|
|
|
if (user === this.last_user) {
|
|
|
|
this.last_bubble.remove();
|
|
|
|
items = this.last_items.concat(items);
|
|
|
|
}
|
|
|
|
this.last_user = user;
|
|
|
|
this.last_items = items;
|
2012-11-30 14:26:01 +00:00
|
|
|
date = instance.web.str_to_datetime(date);
|
|
|
|
date = date.toString(Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.shortTime);
|
2012-11-30 17:02:43 +00:00
|
|
|
this.last_bubble = $(QWeb.render("Conversation.bubble", {"items": items, "user": user, "time": date}));
|
|
|
|
$(this.$(".oe_im_chatview_content").children()[0]).append(this.last_bubble);
|
2012-11-30 15:03:13 +00:00
|
|
|
this.$(".oe_im_chatview_content").scrollTop($(this.$(".oe_im_chatview_content").children()[0]).height());
|
2012-11-30 14:16:46 +00:00
|
|
|
},
|
2012-11-27 15:29:55 +00:00
|
|
|
destroy: function() {
|
2012-11-30 16:15:59 +00:00
|
|
|
this.user.remove_watcher();
|
2012-11-27 15:29:55 +00:00
|
|
|
this.trigger("destroyed");
|
|
|
|
return this._super();
|
|
|
|
},
|
2012-11-27 10:30:52 +00:00
|
|
|
});
|
|
|
|
|
2012-11-15 17:49:02 +00:00
|
|
|
}
|