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
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
/* Add ThreadDisplay widget to registry */
|
|
|
|
session.web.form.widgets.add(
|
|
|
|
'ThreadDisplay', 'openerp.mail.ThreadDisplay');
|
|
|
|
session.web.page.readonly.add(
|
|
|
|
'ThreadDisplay', 'openerp.mail.ThreadDisplay');
|
|
|
|
|
|
|
|
/* ThreadDisplay widget: display a thread of comments */
|
|
|
|
mail.ThreadDisplay = session.web.Widget.extend({
|
|
|
|
template: 'ThreadDisplay',
|
2012-02-27 16:04:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @params {Object} parent parent
|
|
|
|
* @params {Object} [params]
|
|
|
|
* @param {String} [params.res_model] res_model of mail.thread object
|
|
|
|
* @param {Number} [params.res_id] res_id of record
|
|
|
|
* @param {Number} [params.uid] user id
|
|
|
|
* @param {Number} [params.char_show_more=100] number of character to display before adding a "show more"
|
|
|
|
* @param {Number} [params.limit=10] maximum number of messages to fetch
|
|
|
|
* @param {Number} [params.offset=0] offset for fetchign messages
|
|
|
|
* @param {Number} [params.records=null] records to show instead of fetching messages
|
|
|
|
*/
|
2012-02-23 16:32:36 +00:00
|
|
|
init: function(parent, params) {
|
|
|
|
this._super(parent);
|
2012-02-27 16:04:42 +00:00
|
|
|
this.params = params;
|
|
|
|
this.params.limit = this.params.limit || 10;
|
|
|
|
this.params.offset = this.params.offset || 0;
|
|
|
|
this.params.records = this.params.records || null;
|
|
|
|
this.params.char_show_more = this.params.char_show_more || 100;
|
2012-02-27 17:13:23 +00:00
|
|
|
this.map_hash = {'res.users': {'login': [] }};
|
2012-02-29 08:59:53 +00:00
|
|
|
this.params.show_more = true;
|
2012-02-27 17:13:23 +00:00
|
|
|
/* define DataSets */
|
2012-02-27 16:04:42 +00:00
|
|
|
this.ds = new session.web.DataSet(this, this.params.res_model);
|
2012-02-23 16:32:36 +00:00
|
|
|
this.ds_users = new session.web.DataSet(this, 'res.users');
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
this._super.apply(this, arguments);
|
2012-02-29 08:59:53 +00:00
|
|
|
/* events */
|
2012-02-23 16:32:36 +00:00
|
|
|
this.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); });
|
2012-02-29 09:11:09 +00:00
|
|
|
this.$element.find('button.oe_mail_button_more').bind('click', function () { self.do_more(); });
|
2012-02-29 08:59:53 +00:00
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.intlink', 'click', function (event) {
|
|
|
|
// lazy implementation: fetch data and try to redirect
|
2012-02-23 17:33:39 +00:00
|
|
|
self.do_action({
|
|
|
|
type: 'ir.actions.act_window',
|
2012-02-27 17:13:23 +00:00
|
|
|
res_model: event.srcElement.dataset.resModel,
|
|
|
|
res_id: parseInt(event.srcElement.dataset.resId),
|
2012-02-23 17:33:39 +00:00
|
|
|
views: [[false, 'form']]
|
|
|
|
});
|
|
|
|
});
|
2012-02-29 09:11:09 +00:00
|
|
|
this.$element.find('div.oe_mail_thread_nomore').hide();
|
2012-02-23 16:32:36 +00:00
|
|
|
/* display user, fetch comments */
|
|
|
|
this.display_current_user();
|
2012-02-28 11:07:40 +00:00
|
|
|
if (this.params.records) return this.display_comments(this.params.records);
|
2012-02-27 16:04:42 +00:00
|
|
|
else return this.init_comments();
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function () {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2012-02-27 16:04:42 +00:00
|
|
|
init_comments: function() {
|
|
|
|
var self = this;
|
|
|
|
this.params.offset = 0;
|
|
|
|
this.$element.find('div.oe_mail_thread_display').empty();
|
2012-02-29 09:11:09 +00:00
|
|
|
return this.fetch_comments().then();
|
2012-02-27 16:04:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetch_comments: function (limit, offset) {
|
2012-02-29 08:59:53 +00:00
|
|
|
var self = this;
|
|
|
|
var defer = this.ds.call('message_load', [[this.params.res_id], limit=(limit||this.params.limit), offset=(offset||this.params.offset)]);
|
|
|
|
$.when(defer).then(function (records) {
|
|
|
|
if (records.length < self.params.limit) self.params.show_more = false;
|
|
|
|
self.display_comments(records);
|
|
|
|
if (self.params.show_more == true) {
|
|
|
|
self.$element.find('div.oe_mail_thread_more').show();
|
|
|
|
self.$element.find('div.oe_mail_thread_nomore').hide(); }
|
|
|
|
else {
|
|
|
|
self.$element.find('div.oe_mail_thread_more').hide();
|
|
|
|
self.$element.find('div.oe_mail_thread_nomore').show(); }
|
|
|
|
});
|
|
|
|
return defer;
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
display_comments: function (records) {
|
|
|
|
var self = this;
|
2012-02-24 16:09:20 +00:00
|
|
|
/* WIP: map matched regexp -> records to browse with name */
|
2012-02-27 17:13:23 +00:00
|
|
|
//_(records).each(function (record) {
|
|
|
|
//self.do_check_internal_links(record.body_text);
|
|
|
|
//});
|
2012-02-23 16:32:36 +00:00
|
|
|
_(records).each(function (record) {
|
2012-02-27 11:07:54 +00:00
|
|
|
if (record.type == 'email') { record.mini_url = ('/mail/static/src/img/email_icon.png'); }
|
|
|
|
else { record.mini_url = self.thread_get_avatar_mini('res.users', 'avatar_mini', record.user_id[0]); }
|
2012-02-27 17:13:23 +00:00
|
|
|
|
2012-02-27 16:04:42 +00:00
|
|
|
// body text manipulation
|
|
|
|
record.body_text = self.do_clean_text(record.body_text);
|
2012-02-27 17:13:23 +00:00
|
|
|
record.tr_body_text = self.do_truncate_string(record.body_text, self.params.char_show_more);
|
2012-02-23 16:32:36 +00:00
|
|
|
record.body_text = self.do_replace_internal_links(record.body_text);
|
2012-02-27 16:04:42 +00:00
|
|
|
if (record.tr_body_text) record.tr_body_text = self.do_replace_internal_links(record.tr_body_text);
|
2012-02-27 17:13:23 +00:00
|
|
|
|
2012-02-27 16:14:09 +00:00
|
|
|
// render
|
2012-02-27 16:04:42 +00:00
|
|
|
$(session.web.qweb.render('ThreadMsg', {'record': record})).appendTo(self.$element.find('div.oe_mail_thread_display'));
|
2012-02-27 17:13:23 +00:00
|
|
|
|
2012-02-27 16:04:42 +00:00
|
|
|
// truncated: hide full-text, show summary, add buttons
|
|
|
|
if (record.tr_body_text) {
|
2012-02-27 16:14:09 +00:00
|
|
|
var node = self.$element.find('span.oe_mail_msg_body:last').append(' <a href="#" class="reduce">[ ... Show less]</a>');
|
|
|
|
self.$element.find('p.oe_mail_msg_p:last').append($('<span class="oe_mail_msg_body_short">' + record.tr_body_text + ' <a href="#" class="expand">[ ... Show more]</a></span>'));
|
|
|
|
var new_node = self.$element.find('span.oe_mail_msg_body_short:last');
|
2012-02-27 16:04:42 +00:00
|
|
|
node.hide();
|
2012-02-27 17:13:23 +00:00
|
|
|
node.find('a:last').click(function() { node.hide(); new_node.show(); return false; });
|
|
|
|
new_node.find('a:last').click(function() { new_node.hide(); node.show(); return false; });
|
2012-02-27 16:04:42 +00:00
|
|
|
}
|
2012-02-23 16:32:36 +00:00
|
|
|
});
|
2012-02-27 16:04:42 +00:00
|
|
|
// update offset for "More" buttons
|
|
|
|
this.params.offset += records.length;
|
2012-02-27 11:07:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
display_current_user: function () {
|
|
|
|
$('<div>').html(
|
2012-02-27 17:13:23 +00:00
|
|
|
'<img src="' + this.thread_get_avatar_mini('res.users', 'avatar_mini', this.params.uid) + '"/>'
|
2012-02-27 11:07:54 +00:00
|
|
|
).appendTo(this.$element.find('div.oe_mail_msg_image'));
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_comment: function () {
|
|
|
|
var body_text = this.$element.find('textarea').val();
|
2012-02-27 16:04:42 +00:00
|
|
|
return this.ds.call('message_append_note', [[this.params.res_id], 'Reply comment', body_text, type='comment']).then(
|
|
|
|
this.proxy('init_comments'));
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
2012-02-27 11:07:54 +00:00
|
|
|
do_more: function () {
|
2012-02-27 16:04:42 +00:00
|
|
|
return this.fetch_comments(this.limit, this.offset);
|
2012-02-24 16:09:20 +00:00
|
|
|
},
|
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
do_replace_internal_links: function (string) {
|
|
|
|
var self = this;
|
2012-02-27 11:07:54 +00:00
|
|
|
/* shortcut to user: @login */
|
2012-02-27 16:04:42 +00:00
|
|
|
var regex_login = new RegExp(/(^|\s)@(\w*)/g);
|
2012-02-27 11:07:54 +00:00
|
|
|
var regex_res = regex_login.exec(string);
|
|
|
|
while (regex_res != null) {
|
|
|
|
var login = regex_res[2];
|
2012-02-29 08:59:53 +00:00
|
|
|
string = string.replace(regex_res[0], '<a href="#" class="intlink" data-res-model="res.users" data-res-login = ' + login + '>@' + login + '</a>');
|
2012-02-27 11:07:54 +00:00
|
|
|
regex_res = regex_login.exec(string);
|
|
|
|
}
|
2012-02-27 16:04:42 +00:00
|
|
|
return string;
|
2012-02-27 11:07:54 +00:00
|
|
|
},
|
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
thread_get_avatar_mini: function(model, field, id) {
|
2012-02-27 08:54:42 +00:00
|
|
|
return this.session.prefix + '/web/binary/image?session_id=' + this.session.session_id + '&model=' + model + '&field=' + field + '&id=' + (id || '');
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
2012-02-27 17:13:23 +00:00
|
|
|
|
|
|
|
do_truncate_string: function(string, max_length) {
|
|
|
|
if (string.length <= max_length) return false;
|
|
|
|
else return string.slice(0, max_length);
|
2012-02-27 16:04:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_clean_text: function (string) {
|
|
|
|
var html = $('<div/>').text(string.replace(/\s+/g, ' ')).html().replace(new RegExp('<(/)?b\\s*>', 'gi'), '<$1b>');
|
|
|
|
return html;
|
|
|
|
},
|
2012-02-27 17:13:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* var regex_login = new RegExp(/(^|\s)@(\w*[a-zA-Z_.]+\w*\s)/g);
|
|
|
|
* var regex_login = new RegExp(/(^|\s)@(\w*)/g);
|
|
|
|
* var regex_intlink = new RegExp(/(^|\s)#(\w*[a-zA-Z_]+\w*)\.(\w+[a-zA-Z_]+\w*),(\w+)/g);
|
|
|
|
*/
|
|
|
|
do_check_internal_links: function(string) {
|
|
|
|
/* shortcut to user: @login */
|
|
|
|
var regex_login = new RegExp(/(^|\s)@(\w*)/g);
|
|
|
|
var regex_res = regex_login.exec(string);
|
|
|
|
while (regex_res != null) {
|
|
|
|
var login = regex_res[2];
|
|
|
|
this.map_hash['res.users']['login'].push(login);
|
|
|
|
regex_res = regex_login.exec(string);
|
|
|
|
}
|
|
|
|
/* internal links: #res.model,name */
|
|
|
|
var regex_intlink = new RegExp(/(^|\s)#(\w*[a-zA-Z_]+\w*)\.(\w+[a-zA-Z_]+\w*),(\w+)/g);
|
|
|
|
regex_res = regex_intlink.exec(string);
|
|
|
|
while (regex_res != null) {
|
|
|
|
var res_model = regex_res[2] + '.' + regex_res[3];
|
|
|
|
var res_name = regex_res[4];
|
|
|
|
if (! (res_model in this.map_hash)) { this.map_hash[res_model]['name'] = []; }
|
|
|
|
this.map_hash[res_model]['name'].push(res_name);
|
|
|
|
regex_res = regex_intlink.exec(string);
|
|
|
|
}
|
|
|
|
},
|
2012-02-27 16:04:42 +00:00
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
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-23 16:32:36 +00:00
|
|
|
form_template: 'Thread',
|
|
|
|
|
2012-02-06 09:06:18 +00:00
|
|
|
init: function() {
|
2012-02-22 14:48:35 +00:00
|
|
|
this.is_sub = 0;
|
2012-02-23 16:32:36 +00:00
|
|
|
this.see_sub = 0;
|
2012-02-06 09:06:18 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-02-23 16:32:36 +00:00
|
|
|
this.thread_display = null;
|
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-23 16:32:36 +00:00
|
|
|
|
2012-02-06 11:26:23 +00:00
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
this._super.apply(this, arguments);
|
2012-02-23 16:32:36 +00:00
|
|
|
/* bind and hide buttons */
|
2012-02-16 16:03:02 +00:00
|
|
|
self.$element.find('button.oe_mail_button_followers').bind('click', function () { self.do_toggle_followers(); });
|
2012-02-23 16:32:36 +00:00
|
|
|
self.$element.find('button.oe_mail_button_follow').bind('click', function () { self.do_follow(); }).hide();
|
|
|
|
self.$element.find('button.oe_mail_button_unfollow').bind('click', function () { self.do_unfollow(); }).hide();
|
2012-02-06 15:10:09 +00:00
|
|
|
},
|
2012-02-23 16:32:36 +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-23 16:32:36 +00:00
|
|
|
/* hide follow/unfollow/see followers buttons */
|
2012-02-16 16:03:02 +00:00
|
|
|
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-23 16:32:36 +00:00
|
|
|
/* fetch subscribers */
|
2012-02-16 16:03:02 +00:00
|
|
|
this.fetch_subscribers();
|
2012-02-23 16:32:36 +00:00
|
|
|
/* create ThreadDisplay widget and render it */
|
|
|
|
this.$element.find('div.oe_mail_thread_left').empty();
|
|
|
|
this.thread_display = new mail.ThreadDisplay(this, {'res_model': this.view.model, 'res_id': this.view.datarecord.id, 'uid': this.session.uid});
|
|
|
|
this.thread_display.appendTo(this.$element.find('div.oe_mail_thread_left'));
|
2012-02-16 16:03:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetch_subscribers: function () {
|
|
|
|
var follow_res = this.ds.call('message_get_subscribers', [[this.view.datarecord.id]]).then(
|
2012-02-23 16:32:36 +00:00
|
|
|
this.proxy('display_subscribers'));
|
|
|
|
this.$element.find('div.oe_mail_followers_display').hide();
|
2012-02-10 12:43:48 +00:00
|
|
|
return follow_res;
|
2012-02-02 14:18:43 +00:00
|
|
|
},
|
2012-02-06 09:06:18 +00:00
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
display_subscribers: function (records) {
|
|
|
|
this.$element.find('div.oe_mail_followers_display').empty();
|
2012-02-10 12:43:48 +00:00
|
|
|
var self = this;
|
|
|
|
_(records).each(function (record) {
|
2012-02-22 14:04:03 +00:00
|
|
|
$('<div class="oe_mail_followers_vignette">').html(
|
2012-02-23 16:32:36 +00:00
|
|
|
'<img src="' + self.thread_get_avatar_mini('res.users', 'avatar_mini', record.id) + '" title="' + record.name + '" alt="' + record.name + '"/>'
|
|
|
|
).appendTo(self.$element.find('div.oe_mail_followers_display'));
|
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
|
|
|
},
|
|
|
|
|
2012-02-16 16:03:02 +00:00
|
|
|
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-23 16:32:36 +00:00
|
|
|
this.$element.find('div.oe_mail_followers_display').toggle();
|
2012-02-10 12:43:48 +00:00
|
|
|
},
|
2012-02-23 16:32:36 +00:00
|
|
|
|
|
|
|
thread_get_avatar_mini: function(model, field, id) {
|
2012-02-22 14:04:03 +00:00
|
|
|
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-23 16:32:36 +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({
|
2012-02-23 16:32:36 +00:00
|
|
|
template: 'Wall',
|
2012-02-08 16:42:29 +00:00
|
|
|
|
2012-02-28 08:51:11 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} parent parent
|
|
|
|
* @param {Object} [params]
|
|
|
|
* @param {Number} [params.limit=20] number of messages to show and fetch
|
|
|
|
* @var {Array} sorted_comments records sorted by res_model and res_id
|
|
|
|
* records.res_model = {res_ids}
|
|
|
|
* records.res_model.res_id = [records]
|
|
|
|
*/
|
2012-02-09 10:04:47 +00:00
|
|
|
init: function (parent, params) {
|
|
|
|
this._super(parent);
|
2012-02-28 11:07:40 +00:00
|
|
|
this.params = params || {};
|
2012-02-28 08:51:11 +00:00
|
|
|
this.params.limit = params.limit || 20;
|
2012-02-28 11:07:40 +00:00
|
|
|
this.params.search = {};
|
|
|
|
this.params.domain = [];
|
2012-02-28 08:51:11 +00:00
|
|
|
this.sorted_comments = {};
|
2012-02-08 17:08:08 +00:00
|
|
|
/* DataSets */
|
2012-02-24 15:21:59 +00:00
|
|
|
this.ds_msg = new session.web.DataSet(this, 'mail.message');
|
2012-02-23 17:49:36 +00:00
|
|
|
this.ds_thread = new session.web.DataSet(this, 'mail.thread');
|
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-24 15:21:59 +00:00
|
|
|
this.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); });
|
|
|
|
/* load mail.message search view */
|
|
|
|
var search_view_loaded = this.load_search_view();
|
|
|
|
var search_view_ready = $.when(search_view_loaded).then(function () {
|
|
|
|
self.searchview.on_search.add(self.do_searchview_search);
|
|
|
|
});
|
|
|
|
/* fetch comments */
|
2012-02-28 11:07:40 +00:00
|
|
|
var comments_ready = this.init_comments();
|
2012-02-24 15:21:59 +00:00
|
|
|
return (search_view_ready && comments_ready);
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function () {
|
2012-02-24 15:21:59 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
2012-02-28 08:51:11 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-28 08:56:07 +00:00
|
|
|
* Loads the mail.message search view
|
2012-02-28 08:51:11 +00:00
|
|
|
* @param {Number} view_id id of the search view to load
|
|
|
|
* @param {??} defaults ??
|
|
|
|
* @param {Boolean} hidden ??
|
|
|
|
*/
|
2012-02-24 15:21:59 +00:00
|
|
|
load_search_view: function (view_id, defaults, hidden) {
|
|
|
|
this.searchview = new session.web.SearchView(this, this.ds_msg, view_id || false, defaults || {}, hidden || false);
|
|
|
|
return this.searchview.appendTo(this.$element.find('div.oe_mail_wall_search'));
|
|
|
|
},
|
2012-02-28 08:56:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Aggregate the domains, contexts and groupbys in parameter
|
|
|
|
* with those from search form, and then calls fetch_comments
|
|
|
|
* to actually fetch comments
|
|
|
|
* @param {Array} domains
|
|
|
|
* @param {Array} contexts
|
|
|
|
* @param {Array} groupbys
|
|
|
|
*/
|
2012-02-24 15:21:59 +00:00
|
|
|
do_searchview_search: function(domains, contexts, groupbys) {
|
|
|
|
var self = this;
|
|
|
|
this.rpc('/web/session/eval_domain_and_context', {
|
|
|
|
domains: domains || [],
|
|
|
|
contexts: contexts || [],
|
|
|
|
group_by_seq: groupbys || []
|
|
|
|
}, function (results) {
|
2012-02-28 11:07:40 +00:00
|
|
|
self.params.search['context'] = results.context;
|
|
|
|
self.params.search['domain'] = results.domain;
|
|
|
|
self.params.search['groupby'] = results.group_by;
|
|
|
|
self.init_comments(self.params.search['domain'], self.params.search['context']);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes Wall and calls fetch_comments
|
|
|
|
* @param {Array} domains
|
|
|
|
* @param {Array} contexts
|
|
|
|
* @param {Array} groupbys
|
|
|
|
* @param {Number} limit number of messages to fetch
|
|
|
|
*/
|
|
|
|
init_comments: function(domain, context, offset, limit) {
|
|
|
|
var self = this;
|
|
|
|
this.params.domain = [];
|
|
|
|
this.sorted_comments = {};
|
|
|
|
this.$element.find('div.oe_mail_wall_threads').empty();
|
|
|
|
return this.fetch_comments(domain, context, offset, limit).then(function () {
|
|
|
|
self.$element.find('button.oe_mail_wall_button_more').bind('click', function () { self.do_more(); });
|
2012-02-24 15:21:59 +00:00
|
|
|
});
|
|
|
|
},
|
2012-02-28 08:56:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches Wall comments (mail.thread.get_pushed_messages)
|
|
|
|
* @param {Array} domains
|
|
|
|
* @param {Array} contexts
|
|
|
|
* @param {Array} groupbys
|
|
|
|
* @param {Number} limit number of messages to fetch
|
|
|
|
*/
|
2012-02-24 15:21:59 +00:00
|
|
|
fetch_comments: function (domain, context, offset, limit) {
|
|
|
|
var load_res = this.ds_thread.call('get_pushed_messages',
|
|
|
|
[[this.session.uid], limit = (limit || 100), offset = (offset || 0), domain = (domain || null), context = (context || null) ]).then(
|
|
|
|
this.proxy('display_comments'));
|
2012-02-21 15:39:42 +00:00
|
|
|
return load_res;
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
2012-02-28 08:51:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Array} records records to show in threads
|
|
|
|
*/
|
2012-02-21 15:39:42 +00:00
|
|
|
display_comments: function (records) {
|
2012-02-28 11:07:40 +00:00
|
|
|
var sorted_comments = this.sort_comments(records, this.sorted_comments);
|
2012-02-08 17:08:08 +00:00
|
|
|
var self = this;
|
2012-02-28 11:07:40 +00:00
|
|
|
_(sorted_comments).each(function (rec_models, model) { // each model
|
2012-02-22 14:04:03 +00:00
|
|
|
_(rec_models).each(function (record_id, id) { // each record
|
2012-02-23 16:32:36 +00:00
|
|
|
var template = 'WallThreadContainer';
|
2012-02-22 14:04:03 +00:00
|
|
|
var render_res = session.web.qweb.render(template, {
|
|
|
|
'record_model': model,
|
|
|
|
'record_id': id,
|
2012-02-08 17:08:08 +00:00
|
|
|
});
|
2012-02-23 16:32:36 +00:00
|
|
|
$('<div class="oe_mail_wall_thread">').html(render_res).appendTo(self.$element.find('div.oe_mail_wall_threads'));
|
2012-02-28 11:07:40 +00:00
|
|
|
var thread_display = new mail.ThreadDisplay(self,
|
2012-02-23 17:49:36 +00:00
|
|
|
{'res_model': model, 'res_id': parseInt(id), 'uid': self.session.uid, 'records': record_id}
|
|
|
|
);
|
2012-02-28 11:07:40 +00:00
|
|
|
thread_display.appendTo(self.$element.find('div.oe_mail_wall_thread_content:last'));
|
2012-02-22 14:04:03 +00:00
|
|
|
});
|
2012-02-08 17:08:08 +00:00
|
|
|
});
|
2012-02-28 11:07:40 +00:00
|
|
|
$.extend(true, this.sorted_comments, sorted_comments);
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
|
|
|
|
2012-02-28 08:51:11 +00:00
|
|
|
/**
|
|
|
|
* Add records to sorted_comments array
|
|
|
|
* @param {Array} records records from mail.message
|
2012-02-28 11:07:40 +00:00
|
|
|
* @returns {Object} sorted_comments: dict
|
|
|
|
* sorted_comments.res_model = {res_ids}
|
|
|
|
* sorted_comments.res_model.res_id = [records]
|
2012-02-28 08:51:11 +00:00
|
|
|
*/
|
2012-02-28 11:07:40 +00:00
|
|
|
sort_comments: function (records) {
|
|
|
|
sorted_comments = {}
|
2012-02-22 14:04:03 +00:00
|
|
|
_(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-28 08:51:11 +00:00
|
|
|
/**
|
|
|
|
* Create a domain to fetch new comments according to
|
|
|
|
* comment already present in sorted_comments
|
2012-02-28 11:07:40 +00:00
|
|
|
* @param {Object} sorted_comments (see sort_comments)
|
2012-02-28 08:51:11 +00:00
|
|
|
* @returns {Array} fetch_domain (OpenERP domain style)
|
|
|
|
*/
|
|
|
|
get_fetch_domain: function (sorted_comments) {
|
2012-02-28 11:07:40 +00:00
|
|
|
var domain = [];
|
|
|
|
_(sorted_comments).each(function (rec_models, model) { //each model
|
|
|
|
var ids = [];
|
|
|
|
_(rec_models).each(function (record_id, id) { // each record
|
|
|
|
ids.push(id);
|
|
|
|
});
|
|
|
|
domain.push('|', ['model', '!=', model], ['res_id', 'not in', ids]);
|
|
|
|
});
|
|
|
|
return domain;
|
2012-02-28 08:51:11 +00:00
|
|
|
},
|
|
|
|
|
2012-02-28 11:07:40 +00:00
|
|
|
/**
|
|
|
|
* Action: Posts a comment
|
|
|
|
*/
|
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(
|
2012-02-28 11:23:07 +00:00
|
|
|
//this.proxy('fetch_comments'));
|
|
|
|
this.init_comments());
|
2012-02-22 14:04:03 +00:00
|
|
|
},
|
2012-02-28 11:07:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Action: Shows more discussions
|
|
|
|
*/
|
|
|
|
do_more: function () {
|
|
|
|
var domain = this.get_fetch_domain(this.sorted_comments);
|
|
|
|
return this.fetch_comments(domain);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tools: get avatar mini (TODO: should be moved in some tools ?)
|
|
|
|
*/
|
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-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:
|