2012-11-15 17:49:02 +00:00
|
|
|
|
|
|
|
openerp.web_chat = function(instance) {
|
|
|
|
|
2012-11-26 12:38:53 +00:00
|
|
|
instance.web.UserMenu.include({
|
|
|
|
do_update: function(){
|
|
|
|
var self = this;
|
|
|
|
this.update_promise.then(function() {
|
|
|
|
var chat = new instance.web_chat.Chat(self);
|
|
|
|
chat.appendTo(instance.client.$el);
|
|
|
|
var button = new instance.web.ChatTopButton(self);
|
|
|
|
button.chat = chat;
|
|
|
|
button.appendTo(instance.webclient.$el.find('.oe_systray'));
|
|
|
|
});
|
|
|
|
return this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.web.ChatTopButton = instance.web.Widget.extend({
|
|
|
|
template:'ChatTopButton',
|
|
|
|
events: {
|
|
|
|
"click button": "clicked",
|
|
|
|
},
|
|
|
|
clicked: function() {
|
|
|
|
this.chat.switch_display();
|
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
instance.web_chat.Chat = instance.web.Widget.extend({
|
|
|
|
template: "Chat",
|
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-26 12:38:53 +00:00
|
|
|
},
|
2012-11-15 17:49:02 +00:00
|
|
|
start: function() {
|
|
|
|
var self = this;
|
2012-11-26 12:38:53 +00:00
|
|
|
this.$el.css("right", -this.$el.outerWidth());
|
2012-11-15 17:49:02 +00:00
|
|
|
self.poll();
|
|
|
|
self.last = null;
|
2012-11-26 12:38:53 +00:00
|
|
|
$(window).scroll(_.bind(this.calc_box, this));
|
|
|
|
$(window).resize(_.bind(this.calc_box, this));
|
|
|
|
self.calc_box();
|
2012-11-15 17:49:02 +00:00
|
|
|
self.$(".oe_chat_input").keypress(function(e) {
|
|
|
|
if(e.which != 13) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var mes = self.$(".oe_chat_input").val();
|
|
|
|
self.$(".oe_chat_input").val("");
|
|
|
|
var model = new instance.web.Model("chat.message");
|
2012-11-16 09:43:10 +00:00
|
|
|
model.call("post", [mes], {context: new instance.web.CompoundContext()});
|
2012-11-15 17:49:02 +00:00
|
|
|
}).focus();
|
|
|
|
},
|
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);
|
|
|
|
},
|
|
|
|
switch_display: function() {
|
2012-11-26 12:55:48 +00:00
|
|
|
var fct = _.bind(function() {
|
|
|
|
this.set("right_offset", $(window).width() - this.$el.offset().left);
|
|
|
|
}, this);
|
|
|
|
var opt = {
|
|
|
|
step: fct,
|
|
|
|
complete: 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-22 15:23:02 +00:00
|
|
|
this.rpc("/chat/poll", {
|
|
|
|
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) {
|
|
|
|
$("<div>").text(mes).appendTo(self.$(".oe_chat_content"));
|
|
|
|
});
|
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-16 09:43:10 +00:00
|
|
|
setTimeout(_.bind(self.poll, self), 5000);
|
2012-11-15 17:49:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|