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-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: {
|
|
|
|
"click button": "clicked",
|
|
|
|
},
|
|
|
|
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: {
|
|
|
|
"keydown .oe_im_input": "search_users",
|
|
|
|
},
|
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 10:30:52 +00:00
|
|
|
this.last = null;
|
|
|
|
this.users = [];
|
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-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();
|
|
|
|
|
|
|
|
this.poll();
|
|
|
|
this.search_users();
|
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 10:30:52 +00:00
|
|
|
search_users: function(e) {
|
|
|
|
if(e && e.which !== 13) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var users = new instance.web.Model("res.users");
|
|
|
|
var self = this;
|
2012-11-27 14:47:24 +00:00
|
|
|
return users.query(["name"]).filter([["name", "ilike", this.$(".oe_im_input").val()]]).limit(USERS_LIMIT).all().then(function(result) {
|
2012-11-27 10:56:35 +00:00
|
|
|
self.$(".oe_im_input").val("");
|
2012-11-27 10:30:52 +00:00
|
|
|
_.each(self.users, function(user) {
|
|
|
|
user.destroy();
|
|
|
|
});
|
|
|
|
self.users = [];
|
|
|
|
_.each(result, function(user) {
|
|
|
|
var widget = new instance.web_im.ImUser(self, user);
|
|
|
|
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-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 {
|
|
|
|
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-11-27 10:02:41 +00:00
|
|
|
this.rpc("/im/poll", {
|
2012-11-22 15:23:02 +00:00
|
|
|
last: this.last,
|
|
|
|
context: new instance.web.CompoundContext()
|
|
|
|
}, {shadow: true}).then(function(result) {
|
2012-11-15 17:49:02 +00:00
|
|
|
self.last = result.last;
|
|
|
|
_.each(result.res, function(mes) {
|
2012-11-27 15:29:55 +00:00
|
|
|
self.c_manager.received_message(mes, {"id": mes.from[0]});
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
2012-11-16 09:43:10 +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
|
|
|
},
|
|
|
|
activate_user: function(user_rec) {
|
2012-11-27 15:29:55 +00:00
|
|
|
this.c_manager.activate_user(user_rec);
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
|
|
|
|
2012-11-27 10:30:52 +00:00
|
|
|
instance.web_im.ImUser = instance.web.Widget.extend({
|
|
|
|
"template": "ImUser",
|
2012-11-27 10:56:35 +00:00
|
|
|
events: {
|
|
|
|
"click": "activate_user",
|
|
|
|
},
|
2012-11-27 10:30:52 +00:00
|
|
|
init: function(parent, user_rec) {
|
|
|
|
this._super(parent);
|
|
|
|
this.user_rec = user_rec;
|
|
|
|
},
|
2012-11-27 10:56:35 +00:00
|
|
|
activate_user: function() {
|
|
|
|
this.trigger("activate_user", this.user_rec);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
activate_user: function(user_rec) {
|
|
|
|
if (this.users[user_rec.id]) {
|
|
|
|
return this.users[user_rec.id];
|
|
|
|
}
|
|
|
|
var conv = new instance.web_im.Conversation(this, user_rec);
|
|
|
|
conv.appendTo(instance.client.$el);
|
|
|
|
conv.on("destroyed", this, function() {
|
|
|
|
this.conversations = _.without(this.conversations, conv);
|
|
|
|
delete this.users[conv.user_rec.id];
|
|
|
|
});
|
|
|
|
this.conversations.push(conv);
|
|
|
|
this.users[user_rec.id] = conv;
|
|
|
|
this.calc_positions();
|
|
|
|
return conv;
|
|
|
|
},
|
|
|
|
received_message: function(message, user_rec) {
|
|
|
|
var conv = this.activate_user(user_rec);
|
|
|
|
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);
|
|
|
|
current += this.conversations[i].$el.outerWidth();
|
|
|
|
}, 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-27 15:29:55 +00:00
|
|
|
init: function(parent, user_rec) {
|
2012-11-27 10:56:35 +00:00
|
|
|
this._super(parent);
|
|
|
|
this.user_rec = user_rec;
|
2012-11-27 15:29:55 +00:00
|
|
|
this.set("right_position", 0);
|
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-11-27 10:56:35 +00:00
|
|
|
this.calc_pos();
|
|
|
|
},
|
|
|
|
calc_pos: function() {
|
2012-11-27 15:29:55 +00:00
|
|
|
this.$el.css("right", this.get("right_position"));
|
|
|
|
},
|
|
|
|
received_message: function(message) {
|
|
|
|
this.$(".oe_conversation_text").append($("<div>").text("Him: " + message.message));
|
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-27 15:29:55 +00:00
|
|
|
this.$(".oe_conversation_text").append($("<div>").text("Me: " + mes));
|
2012-11-27 13:51:55 +00:00
|
|
|
var model = new instance.web.Model("im.message");
|
|
|
|
model.call("post", [mes, this.user_rec.id], {context: new instance.web.CompoundContext()});
|
2012-11-27 10:56:35 +00:00
|
|
|
},
|
2012-11-27 15:29:55 +00:00
|
|
|
destroy: function() {
|
|
|
|
this.trigger("destroyed");
|
|
|
|
return this._super();
|
|
|
|
},
|
2012-11-27 10:30:52 +00:00
|
|
|
});
|
|
|
|
|
2012-11-15 17:49:02 +00:00
|
|
|
}
|