2012-02-01 16:22:13 +00:00
|
|
|
openerp.mail = function(session) {
|
2012-02-02 14:18:43 +00:00
|
|
|
|
|
|
|
var mail = session.mail = {};
|
2012-02-08 16:42:29 +00:00
|
|
|
|
|
|
|
/* Add ThreadView widget to registry */
|
2012-02-06 09:06:18 +00:00
|
|
|
session.web.form.widgets.add(
|
|
|
|
'ThreadView', 'openerp.mail.ThreadView');
|
2012-02-08 15:25:21 +00:00
|
|
|
session.web.page.readonly.add(
|
|
|
|
'ThreadView', 'openerp.mail.ThreadView');
|
2012-02-06 09:06:18 +00:00
|
|
|
|
2012-02-08 16:42:29 +00:00
|
|
|
/* ThreadView widget: thread of comments */
|
2012-02-06 09:06:18 +00:00
|
|
|
mail.ThreadView = session.web.form.Field.extend({
|
|
|
|
// QWeb template to use when rendering the object
|
2012-02-20 09:22:39 +00:00
|
|
|
form_template: 'ThreadView',
|
2012-02-06 09:06:18 +00:00
|
|
|
|
|
|
|
init: function() {
|
2012-02-22 14:48:35 +00:00
|
|
|
this.is_sub = 0;
|
|
|
|
this.see_sub = 1;
|
2012-02-22 14:58:00 +00:00
|
|
|
this.user_init = 0;
|
2012-02-06 09:06:18 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-02-06 17:19:11 +00:00
|
|
|
/* DataSets */
|
2012-02-06 09:06:18 +00:00
|
|
|
this.ds = new session.web.DataSet(this, this.view.model);
|
2012-02-22 14:48:35 +00:00
|
|
|
this.ds_users = new session.web.DataSet(this, 'res.users');
|
2012-02-02 14:18:43 +00:00
|
|
|
},
|
2012-02-06 11:26:23 +00:00
|
|
|
|
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
this._super.apply(this, arguments);
|
2012-02-16 16:03:02 +00:00
|
|
|
/* bind buttons */
|
|
|
|
self.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); });
|
|
|
|
self.$element.find('button.oe_mail_button_followers').bind('click', function () { self.do_toggle_followers(); });
|
2012-02-10 10:52:23 +00:00
|
|
|
self.$element.find('button.oe_mail_button_follow').bind('click', function () { self.do_follow(); });
|
|
|
|
self.$element.find('button.oe_mail_button_unfollow').bind('click', function () { self.do_unfollow(); });
|
2012-02-16 16:03:02 +00:00
|
|
|
/* hide follow/unfollow buttons */
|
|
|
|
self.$element.find('button.oe_mail_button_follow').hide();
|
2012-02-10 10:52:23 +00:00
|
|
|
self.$element.find('button.oe_mail_button_unfollow').hide();
|
2012-02-06 15:10:09 +00:00
|
|
|
},
|
|
|
|
|
2012-02-06 09:06:18 +00:00
|
|
|
stop: function () {
|
2012-02-16 16:03:02 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-02-02 14:18:43 +00:00
|
|
|
},
|
|
|
|
|
2012-02-06 09:06:18 +00:00
|
|
|
set_value: function() {
|
2012-02-16 16:03:02 +00:00
|
|
|
var self = this;
|
2012-02-06 09:06:18 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-02-16 16:03:02 +00:00
|
|
|
/* hide follow/unfollow buttons */
|
|
|
|
self.$element.find('button.oe_mail_button_follow').hide();
|
|
|
|
self.$element.find('button.oe_mail_button_unfollow').hide();
|
2012-02-06 09:06:18 +00:00
|
|
|
if (! this.view.datarecord.id) { return; }
|
2012-02-16 16:03:02 +00:00
|
|
|
/* find wich (un)follow buttons to show */
|
|
|
|
var call_res = this.ds.call('message_is_subscriber', [[this.view.datarecord.id]]).then(function (records) {
|
2012-02-22 14:48:35 +00:00
|
|
|
if (records == true) { self.is_sub = 1; self.$element.find('button.oe_mail_button_unfollow').show(); }
|
|
|
|
else { self.is_sub = 0; self.$element.find('button.oe_mail_button_follow').show(); }
|
2012-02-16 16:03:02 +00:00
|
|
|
});
|
2012-02-22 14:48:35 +00:00
|
|
|
console.log(this);
|
2012-02-16 16:03:02 +00:00
|
|
|
/* fetch comments and subscribers */
|
2012-02-22 14:58:00 +00:00
|
|
|
if (this.user_init == 0) {
|
|
|
|
this.fetch_current_user();
|
|
|
|
this.user_init = 1;
|
|
|
|
}
|
2012-02-16 16:03:02 +00:00
|
|
|
this.fetch_subscribers();
|
|
|
|
return this.fetch_comments();
|
2012-02-06 09:06:18 +00:00
|
|
|
},
|
2012-02-02 14:18:43 +00:00
|
|
|
|
2012-02-22 14:48:35 +00:00
|
|
|
fetch_current_user: function () {
|
|
|
|
return this.ds_users.read_ids([this.session.uid], ['id', 'name', 'avatar_mini']).then(
|
|
|
|
this.proxy('display_current_user'));
|
|
|
|
},
|
|
|
|
|
2012-02-16 16:03:02 +00:00
|
|
|
fetch_comments: function () {
|
2012-02-10 12:43:48 +00:00
|
|
|
var load_res = this.ds.call('message_load', [[this.view.datarecord.id]]).then(
|
|
|
|
this.proxy('display_comments'));
|
2012-02-16 16:03:02 +00:00
|
|
|
return load_res;
|
|
|
|
},
|
|
|
|
|
|
|
|
fetch_subscribers: function () {
|
|
|
|
var follow_res = this.ds.call('message_get_subscribers', [[this.view.datarecord.id]]).then(
|
2012-02-10 12:43:48 +00:00
|
|
|
this.proxy('display_followers'));
|
|
|
|
return follow_res;
|
2012-02-02 14:18:43 +00:00
|
|
|
},
|
2012-02-06 09:06:18 +00:00
|
|
|
|
2012-02-22 14:48:35 +00:00
|
|
|
display_current_user: function (records) {
|
|
|
|
$('<div>').html(
|
|
|
|
'<img src="' + this.thread_get_mini('res.users', 'avatar_mini', records[0].id) + '" title="' + records[0].name + '" alt="' + records[0].name + '"/>'
|
|
|
|
).appendTo(this.$element.find('div.oe_mail_msg_image'));
|
|
|
|
},
|
|
|
|
|
2012-02-10 12:43:48 +00:00
|
|
|
display_comments: function (records) {
|
2012-02-10 10:52:23 +00:00
|
|
|
this.$element.find('div.oe_mail_msg').empty();
|
2012-02-06 09:06:18 +00:00
|
|
|
var self = this;
|
|
|
|
_(records).each(function (record) {
|
2012-02-22 14:04:03 +00:00
|
|
|
record.mini_url = self.thread_get_mini('res.users', 'avatar_mini', record.user_id[0]);
|
2012-02-06 17:19:11 +00:00
|
|
|
var template = 'ThreadMsgView';
|
|
|
|
var render_res = session.web.qweb.render(template, {
|
|
|
|
'record': record,
|
|
|
|
});
|
2012-02-10 10:52:23 +00:00
|
|
|
$('<div class="oe_mail_comment">').html(render_res).appendTo(self.$element.find('div.oe_mail_msg'));
|
2012-02-06 09:06:18 +00:00
|
|
|
});
|
2012-02-06 11:26:23 +00:00
|
|
|
},
|
2012-02-22 14:04:03 +00:00
|
|
|
|
2012-02-10 12:43:48 +00:00
|
|
|
display_followers: function (records) {
|
|
|
|
this.$element.find('div.oe_mail_followers').empty();
|
|
|
|
var self = this;
|
|
|
|
_(records).each(function (record) {
|
2012-02-22 14:04:03 +00:00
|
|
|
$('<div class="oe_mail_followers_vignette">').html(
|
|
|
|
'<img src="' + self.thread_get_mini('res.users', 'avatar_mini', record.id) + '" title="' + record.name + '" alt="' + record.name + '"/>'
|
|
|
|
).appendTo(self.$element.find('div.oe_mail_followers'));
|
2012-02-10 12:43:48 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-02-06 11:26:23 +00:00
|
|
|
do_follow: function () {
|
2012-02-16 16:03:02 +00:00
|
|
|
this.do_toggle_follow();
|
|
|
|
return this.ds.call('message_subscribe', [[this.view.datarecord.id]]).then();
|
2012-02-06 11:26:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_unfollow: function () {
|
2012-02-16 16:03:02 +00:00
|
|
|
this.do_toggle_follow();
|
2012-02-08 15:25:21 +00:00
|
|
|
return this.ds.call('message_unsubscribe', [[this.view.datarecord.id]]).then();
|
2012-02-06 17:19:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_comment: function () {
|
|
|
|
var body_text = this.$element.find('textarea').val();
|
2012-02-08 15:25:21 +00:00
|
|
|
return this.ds.call('message_append_note', [[this.view.datarecord.id], 'Reply comment', body_text, type='comment']).then(
|
2012-02-16 16:03:02 +00:00
|
|
|
this.proxy('fetch_comments'));
|
|
|
|
},
|
|
|
|
|
|
|
|
do_toggle_follow: function () {
|
2012-02-22 14:48:35 +00:00
|
|
|
this.is_sub = 1 - this.is_sub;
|
2012-02-16 16:03:02 +00:00
|
|
|
this.$element.find('button.oe_mail_button_unfollow').toggle();
|
|
|
|
this.$element.find('button.oe_mail_button_follow').toggle();
|
2012-02-06 17:19:11 +00:00
|
|
|
},
|
2012-02-10 12:43:48 +00:00
|
|
|
|
|
|
|
do_toggle_followers: function () {
|
2012-02-22 14:48:35 +00:00
|
|
|
this.see_sub = 1 - this.see_sub;
|
|
|
|
if (this.see_sub == 1) { this.$element.find('button.oe_mail_button_followers').html('Hide followers'); }
|
|
|
|
else { this.$element.find('button.oe_mail_button_followers').html('Display followers'); }
|
2012-02-10 12:43:48 +00:00
|
|
|
this.$element.find('div.oe_mail_followers').toggle();
|
|
|
|
},
|
2012-02-22 14:04:03 +00:00
|
|
|
|
|
|
|
thread_get_mini: function(model, field, id) {
|
|
|
|
id = id || '';
|
|
|
|
var url = this.session.prefix + '/web/binary/image?session_id=' + this.session.session_id + '&model=' + model + '&field=' + field + '&id=' + id;
|
|
|
|
return url;
|
|
|
|
},
|
2012-02-02 14:18:43 +00:00
|
|
|
});
|
|
|
|
|
2012-02-08 16:42:29 +00:00
|
|
|
/* Add WallView widget to registry */
|
|
|
|
session.web.client_actions.add('mail.all_feeds', 'session.mail.WallView');
|
|
|
|
|
|
|
|
/* WallView widget: a wall of messages */
|
|
|
|
mail.WallView = session.web.Widget.extend({
|
|
|
|
// QWeb template to use when rendering the object
|
|
|
|
template: 'WallView',
|
|
|
|
|
2012-02-09 10:04:47 +00:00
|
|
|
init: function (parent, params) {
|
|
|
|
this._super(parent);
|
|
|
|
this.filter_search = params['filter_search'];
|
2012-02-08 17:08:08 +00:00
|
|
|
/* DataSets */
|
|
|
|
this.ds_msg = new session.web.DataSet(this, 'mail.message');
|
2012-02-22 14:04:03 +00:00
|
|
|
this.ds_users = new session.web.DataSet(this, 'res.users');
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
this._super.apply(this, arguments);
|
2012-02-21 15:39:42 +00:00
|
|
|
self.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); });
|
|
|
|
return this.fetch_comments();
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function () {
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
|
2012-02-21 15:39:42 +00:00
|
|
|
fetch_comments: function () {
|
|
|
|
var load_res = this.ds_msg.call('get_pushed_messages', [[this.session.uid]]).then(
|
|
|
|
this.proxy('display_comments'));
|
|
|
|
return load_res;
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
|
|
|
|
2012-02-21 15:39:42 +00:00
|
|
|
display_comments: function (records) {
|
|
|
|
this.$element.find('div.oe_mail_msg').empty();
|
2012-02-22 14:04:03 +00:00
|
|
|
sorted_records = this.sort_comments(records);
|
2012-02-08 17:08:08 +00:00
|
|
|
var self = this;
|
2012-02-22 14:04:03 +00:00
|
|
|
_(sorted_records).each(function (rec_models, model) { // each model
|
|
|
|
_(rec_models).each(function (record_id, id) { // each record
|
|
|
|
var template = 'WallThreadView';
|
|
|
|
var render_res = session.web.qweb.render(template, {
|
|
|
|
'record_model': model,
|
|
|
|
'record_id': id,
|
2012-02-08 17:08:08 +00:00
|
|
|
});
|
2012-02-22 14:04:03 +00:00
|
|
|
$('<div class="oe_mail_thread">').html(render_res).appendTo(self.$element.find('div.oe_mail_msg'));
|
|
|
|
_(record_id).each(function (record) { // each record
|
|
|
|
record.mini_url = self.thread_get_mini('res.users', 'avatar_mini', record.user_id[0]);
|
|
|
|
var template = 'ThreadMsgView';
|
|
|
|
var render_res = session.web.qweb.render(template, {
|
|
|
|
'record': record,
|
|
|
|
});
|
|
|
|
$('<div class="oe_mail_comment">').html(render_res).appendTo(self.$element.find('div.oe_mail_thread_content:last'));
|
|
|
|
});
|
|
|
|
});
|
2012-02-08 17:08:08 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-02-22 14:04:03 +00:00
|
|
|
sort_comments: function (records) {
|
|
|
|
sorted_comments = {};
|
|
|
|
_(records).each(function (record) {
|
|
|
|
if (! (record.model in sorted_comments)) { sorted_comments[record.model] = {}; }
|
|
|
|
if (! (record.res_id in sorted_comments[record.model])) {
|
|
|
|
sorted_comments[record.model][record.res_id] = []; }
|
|
|
|
sorted_comments[record.model][record.res_id].push(record);
|
|
|
|
});
|
|
|
|
return sorted_comments;
|
|
|
|
},
|
|
|
|
|
2012-02-08 17:08:08 +00:00
|
|
|
do_comment: function () {
|
|
|
|
var body_text = this.$element.find('textarea').val();
|
2012-02-22 14:04:03 +00:00
|
|
|
return this.ds_users.call('message_append_note', [[this.session.uid], 'Tweet', body_text, type='comment']).then(
|
|
|
|
this.proxy('fetch_comments'));
|
|
|
|
},
|
|
|
|
|
|
|
|
thread_get_mini: function(model, field, id) {
|
|
|
|
id = id || '';
|
|
|
|
var url = this.session.prefix + '/web/binary/image?session_id=' + this.session.session_id + '&model=' + model + '&field=' + field + '&id=' + id;
|
|
|
|
return url;
|
2012-02-08 16:42:29 +00:00
|
|
|
},
|
|
|
|
});
|
2012-02-01 16:22:13 +00:00
|
|
|
};
|
2012-02-02 14:18:43 +00:00
|
|
|
|
2012-02-01 16:22:13 +00:00
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|