2012-02-01 16:22:13 +00:00
|
|
|
openerp.mail = function(session) {
|
2012-03-29 09:18:08 +00:00
|
|
|
var _t = session.web._t,
|
|
|
|
_lt = session.web._lt;
|
2012-02-02 14:18:43 +00:00
|
|
|
|
|
|
|
var mail = session.mail = {};
|
2012-02-08 16:42:29 +00:00
|
|
|
|
2012-06-08 14:24:19 +00:00
|
|
|
/**
|
2012-06-15 08:36:56 +00:00
|
|
|
* ------------------------------------------------------------
|
2012-06-08 14:24:19 +00:00
|
|
|
* Global variables for the Chatter
|
2012-06-15 08:36:56 +00:00
|
|
|
* ------------------------------------------------------------
|
|
|
|
*
|
2012-06-08 14:24:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mail_int_mapping: structure to keep a trace of internal links mapping
|
|
|
|
* mail_int_mapping['model'] = {
|
|
|
|
* 'name_get': [[id,label], [id,label], ...]
|
|
|
|
* 'fetch_ids': [id, id, ...] } */
|
|
|
|
var mail_int_mapping = mail.chatter_internal_mapping = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mail_msg_struct: structure to orrganize chatter messages
|
|
|
|
*/
|
|
|
|
var mail_msg_struct = mail.chatter_message_structure = {}; // TODO: USE IT OR NOT :)
|
|
|
|
|
2012-06-14 08:40:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
* ComposeMessage widget
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* This widget handles the display of a form to compose a new message.
|
2012-06-15 08:36:56 +00:00
|
|
|
* This form is an OpenERP form_view, build on a mail.compose.message
|
|
|
|
* wizard.
|
2012-06-14 08:40:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Add ComposeMessage widget to registry */
|
2012-06-15 08:36:56 +00:00
|
|
|
session.web.form.widgets.add('mail.compose_message', 'openerp.mail.ComposeMessage');
|
2012-06-14 08:40:33 +00:00
|
|
|
|
|
|
|
/* ComposeMessage is an extension of a Widget */
|
2012-06-15 08:36:56 +00:00
|
|
|
mail.ComposeMessage = session.web.Widget.extend({
|
2012-06-14 08:40:33 +00:00
|
|
|
template: 'mail.compose_message',
|
|
|
|
|
|
|
|
init: function(parent, params) {
|
|
|
|
this._super(parent);
|
2012-06-18 15:06:08 +00:00
|
|
|
this.params = params || {};
|
|
|
|
this.params.extended_mode = params.extended_mode || false;
|
2012-06-14 11:44:18 +00:00
|
|
|
this.ds_compose = new session.web.DataSetSearch(this, 'mail.compose.message');
|
2012-06-14 08:40:33 +00:00
|
|
|
this.form_view = new session.web.FormView(this, this.ds_compose, false, {
|
2012-06-15 08:36:56 +00:00
|
|
|
action_buttons: false,
|
|
|
|
pager: false,
|
2012-06-14 08:40:33 +00:00
|
|
|
initial_mode: 'edit',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2012-06-15 08:36:56 +00:00
|
|
|
/**
|
|
|
|
* widget start function
|
|
|
|
* - builds and initializes the form view
|
|
|
|
*/
|
2012-06-14 08:40:33 +00:00
|
|
|
start: function(parent, params) {
|
2012-06-14 11:44:18 +00:00
|
|
|
var self = this;
|
2012-06-14 08:40:33 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-06-18 15:06:08 +00:00
|
|
|
console.log(this.$element);
|
|
|
|
var main_node = this.$element.find('div.oe_mail_msg_content');
|
|
|
|
console.log(main_node);
|
2012-06-15 08:36:56 +00:00
|
|
|
|
|
|
|
return $.when(this.form_view.appendTo(main_node)).pipe(function() {
|
2012-06-15 15:35:15 +00:00
|
|
|
self.bind_events();
|
2012-06-14 11:44:18 +00:00
|
|
|
self.form_view.do_show();
|
2012-06-15 15:35:15 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind events in the widget. Each event is slighty described
|
|
|
|
* in the function. */
|
|
|
|
bind_events: function() {
|
|
|
|
var self = this;
|
|
|
|
// event: click on 'Send a Message' link that toggles the
|
|
|
|
// advanced options for writing a message
|
|
|
|
this.$element.find('a.mail_compose_message_toggle').click(function (event) {
|
|
|
|
event.preventDefault();
|
2012-06-18 15:06:08 +00:00
|
|
|
self.$element.find('span.oe_mail_compose_message_subject').toggleClass('oe_mail_compose_message_invisible');
|
|
|
|
self.$element.find('div.oe_mail_compose_message_body_text').toggleClass('oe_mail_compose_message_invisible');
|
|
|
|
self.$element.find('div.oe_mail_compose_message_body_html').toggleClass('oe_mail_compose_message_invisible');
|
|
|
|
self.$element.find('div.oe_mail_compose_message_partner_ids').toggleClass('oe_mail_compose_message_invisible');
|
|
|
|
|
2012-06-14 11:44:18 +00:00
|
|
|
});
|
2012-06-14 08:40:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function(parent, params) {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
2012-06-15 15:35:15 +00:00
|
|
|
|
2012-03-02 17:07:49 +00:00
|
|
|
/**
|
2012-06-15 08:36:56 +00:00
|
|
|
* ------------------------------------------------------------
|
|
|
|
* Thread Widget
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* This widget handles the display of a thread of messages. The
|
|
|
|
* [thread_level] parameter sets the thread level number:
|
2012-03-02 17:07:49 +00:00
|
|
|
* - root message
|
|
|
|
* - - sub message (parent_id = root message)
|
2012-03-05 17:37:42 +00:00
|
|
|
* - - - sub sub message (parent id = sub message)
|
2012-03-02 17:07:49 +00:00
|
|
|
* - - sub message (parent_id = root message)
|
2012-06-15 08:36:56 +00:00
|
|
|
* This widget has 2 ways of initialization, either you give records
|
|
|
|
* to be rendered, either it will fetch [limit] messages related to
|
|
|
|
* [res_model]:[res_id].
|
2012-03-02 17:07:49 +00:00
|
|
|
*/
|
2012-05-10 12:10:58 +00:00
|
|
|
|
|
|
|
/* Add ThreadDisplay widget to registry */
|
2012-06-15 08:36:56 +00:00
|
|
|
session.web.form.widgets.add('Thread', 'openerp.mail.Thread');
|
2012-05-10 12:10:58 +00:00
|
|
|
|
|
|
|
/* Thread is an extension of a Widget */
|
2012-03-02 17:07:49 +00:00
|
|
|
mail.Thread = session.web.Widget.extend({
|
|
|
|
template: 'Thread',
|
2012-02-27 16:04:42 +00:00
|
|
|
|
|
|
|
/**
|
2012-03-05 17:37:42 +00:00
|
|
|
* @param {Object} parent parent
|
|
|
|
* @param {Object} [params]
|
2012-05-10 12:10:58 +00:00
|
|
|
* @param {String} [params.res_model] res_model of document [REQUIRED]
|
|
|
|
* @param {Number} [params.res_id] res_id of record [REQUIRED]
|
|
|
|
* @param {Number} [params.uid] user id [REQUIRED]
|
|
|
|
* @param {Bool} [params.parent_id=false] parent_id of message
|
2012-03-02 17:07:49 +00:00
|
|
|
* @param {Number} [params.thread_level=0] number of levels in the thread (only 0 or 1 currently)
|
2012-05-10 12:10:58 +00:00
|
|
|
* @param {Bool} [params.wall=false] thread is displayed in the wall
|
|
|
|
* @param {Number} [params.msg_more_limit=150] number of character to display before having a "show more" link;
|
2012-03-02 17:07:49 +00:00
|
|
|
* note that the text will not be truncated if it does not have 110% of
|
|
|
|
* the parameter (ex: 110 characters needed to be truncated and be displayed
|
|
|
|
* as a 100-characters message)
|
2012-05-10 12:10:58 +00:00
|
|
|
* @param {Number} [params.limit=100] maximum number of messages to fetch
|
2012-03-02 17:07:49 +00:00
|
|
|
* @param {Number} [params.offset=0] offset for fetching messages
|
2012-02-27 16:04:42 +00:00
|
|
|
* @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;
|
2012-03-02 17:07:49 +00:00
|
|
|
this.params.parent_id = this.params.parent_id || false;
|
|
|
|
this.params.thread_level = this.params.thread_level || 0;
|
2012-05-14 10:08:20 +00:00
|
|
|
this.params.is_wall = this.params.is_wall || this.params.records || false;
|
2012-05-10 12:10:58 +00:00
|
|
|
this.params.msg_more_limit = this.params.msg_more_limit || 150;
|
2012-03-22 16:32:24 +00:00
|
|
|
this.params.limit = this.params.limit || 100;
|
2012-06-08 14:24:19 +00:00
|
|
|
this.params.limit = 3; // tmp for testing
|
2012-02-27 16:04:42 +00:00
|
|
|
this.params.offset = this.params.offset || 0;
|
|
|
|
this.params.records = this.params.records || null;
|
2012-03-21 17:20:18 +00:00
|
|
|
// datasets and internal vars
|
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');
|
2012-03-13 17:41:05 +00:00
|
|
|
this.ds_msg = new session.web.DataSet(this, 'mail.message');
|
2012-04-05 10:29:17 +00:00
|
|
|
this.comments_structure = {'root_ids': [], 'new_root_ids': [], 'msgs': {}, 'tree_struct': {}, 'model_to_root_ids': {}};
|
2012-03-21 17:20:18 +00:00
|
|
|
// display customization vars
|
2012-03-12 15:25:29 +00:00
|
|
|
this.display = {};
|
2012-03-13 17:06:48 +00:00
|
|
|
this.display.show_post_comment = this.params.show_post_comment || false;
|
2012-05-14 10:08:20 +00:00
|
|
|
this.display.show_msg_menu = this.params.is_wall;
|
2012-05-15 12:23:06 +00:00
|
|
|
this.display.show_reply = (this.params.thread_level > 0 && this.params.is_wall);
|
2012-05-14 10:08:20 +00:00
|
|
|
this.display.show_delete = ! this.params.is_wall;
|
|
|
|
this.display.show_hide = this.params.is_wall;
|
|
|
|
this.display.show_reply_by_email = ! this.params.is_wall;
|
2012-03-12 17:33:23 +00:00
|
|
|
this.display.show_more = (this.params.thread_level == 0);
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
this._super.apply(this, arguments);
|
2012-03-14 08:51:28 +00:00
|
|
|
// add events
|
2012-03-13 17:41:05 +00:00
|
|
|
this.add_events();
|
2012-05-15 07:27:03 +00:00
|
|
|
// display user, fetch comments
|
2012-03-27 09:15:04 +00:00
|
|
|
this.display_current_user();
|
2012-04-04 12:00:53 +00:00
|
|
|
if (this.params.records) var display_done = this.display_comments_from_parameters(this.params.records);
|
2012-03-27 09:15:04 +00:00
|
|
|
else var display_done = this.init_comments();
|
2012-05-14 12:32:31 +00:00
|
|
|
// customize display
|
|
|
|
$.when(display_done).then(this.proxy('do_customize_display'));
|
2012-06-14 08:40:33 +00:00
|
|
|
|
2012-06-15 08:36:56 +00:00
|
|
|
// add message composition form view
|
2012-06-18 15:06:08 +00:00
|
|
|
this.compose_message = new mail.ComposeMessage(this, {'extended_mode': false});
|
2012-06-15 08:36:56 +00:00
|
|
|
var compose_done = this.compose_message.appendTo(this.$element.find('div.oe_mail_thread_act'));
|
2012-06-14 08:40:33 +00:00
|
|
|
|
2012-06-15 08:36:56 +00:00
|
|
|
return display_done && compose_done;
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
2012-05-14 12:32:31 +00:00
|
|
|
do_customize_display: function() {
|
2012-05-15 12:23:06 +00:00
|
|
|
if (this.display.show_post_comment) { this.$element.find('div.oe_mail_thread_act').eq(0).show(); }
|
2012-05-14 12:32:31 +00:00
|
|
|
if (this.display.show_reply_by_email) { this.$element.find('a.oe_mail_compose').show(); }
|
|
|
|
if (! this.display.show_msg_menu) { this.$element.find('img.oe_mail_msg_menu_icon').hide(); }
|
|
|
|
},
|
|
|
|
|
2012-03-13 17:41:05 +00:00
|
|
|
add_events: function() {
|
|
|
|
var self = this;
|
2012-03-21 17:20:18 +00:00
|
|
|
// event: click on 'more' at bottom of thread
|
2012-03-14 08:51:28 +00:00
|
|
|
this.$element.find('button.oe_mail_button_more').click(function () {
|
|
|
|
self.do_more();
|
|
|
|
});
|
|
|
|
// event: writing in textarea
|
|
|
|
this.$element.find('textarea.oe_mail_action_textarea').keyup(function (event) {
|
|
|
|
var charCode = (event.which) ? event.which : window.event.keyCode;
|
|
|
|
if (event.shiftKey && charCode == 13) { this.value = this.value+"\n"; }
|
2012-04-04 16:00:56 +00:00
|
|
|
else if (charCode == 13) { return self.do_comment(); }
|
2012-03-14 08:51:28 +00:00
|
|
|
});
|
2012-03-21 17:20:18 +00:00
|
|
|
// event: click on 'reply' in msg
|
2012-03-13 17:41:05 +00:00
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_reply', 'click', function (event) {
|
2012-03-29 09:18:08 +00:00
|
|
|
var act_dom = $(this).parents('div.oe_mail_thread_display').find('div.oe_mail_thread_act:first');
|
|
|
|
act_dom.toggle();
|
2012-04-05 21:42:41 +00:00
|
|
|
event.preventDefault();
|
2012-03-13 17:41:05 +00:00
|
|
|
});
|
2012-03-21 17:20:18 +00:00
|
|
|
// event: click on 'delete' in msg
|
2012-03-13 17:41:05 +00:00
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_delete', 'click', function (event) {
|
2012-03-29 09:18:08 +00:00
|
|
|
if (! confirm(_t("Do you really want to delete this message?"))) { return false; }
|
|
|
|
var msg_id = event.srcElement.dataset.id;
|
|
|
|
if (! msg_id) return false;
|
|
|
|
var call_defer = self.ds_msg.unlink([parseInt(msg_id)]);
|
|
|
|
$(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).hide();
|
|
|
|
if (self.params.thread_level > 0) {
|
|
|
|
$(event.srcElement).parents('ul.oe_mail_thread').eq(0).hide();
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-13 17:41:05 +00:00
|
|
|
});
|
2012-03-21 17:20:18 +00:00
|
|
|
// event: click on 'hide' in msg
|
2012-03-14 08:51:28 +00:00
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_hide', 'click', function (event) {
|
2012-03-29 09:18:08 +00:00
|
|
|
if (! confirm(_t("Do you really want to hide this thread ?"))) { return false; }
|
|
|
|
var msg_id = event.srcElement.dataset.id;
|
|
|
|
if (! msg_id) return false;
|
2012-04-02 11:50:02 +00:00
|
|
|
var call_defer = self.ds.call('message_remove_pushed_notifications', [[self.params.res_id], [parseInt(msg_id)], true]);
|
2012-03-29 09:18:08 +00:00
|
|
|
$(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).hide();
|
|
|
|
if (self.params.thread_level > 0) {
|
|
|
|
$(event.srcElement).parents('ul.oe_mail_thread').eq(0).hide();
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-14 08:51:28 +00:00
|
|
|
});
|
2012-05-10 12:10:58 +00:00
|
|
|
// event: click on "reply by email" in msg
|
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_reply_by_email', 'click', function (event) {
|
|
|
|
var msg_id = event.srcElement.dataset.id;
|
|
|
|
if (! msg_id) return false;
|
|
|
|
self.do_action({
|
|
|
|
type: 'ir.actions.act_window',
|
|
|
|
res_model: 'mail.compose.message',
|
|
|
|
views: [[false, 'form']],
|
|
|
|
view_type: 'form',
|
|
|
|
view_mode: 'form',
|
|
|
|
target: 'new',
|
|
|
|
context: {'active_model': self.params.res_model, 'active_id': self.params.res_id, 'message_id': msg_id, 'mail.compose.message.mode': 'reply'},
|
|
|
|
key2: 'client_action_multi',
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
2012-04-20 08:42:41 +00:00
|
|
|
// event: click on 'hide this type' in wheel_menu
|
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_hide_type', 'click', function (event) {
|
2012-05-10 12:10:58 +00:00
|
|
|
if (! confirm(_t("Do you really want to hide this type of thread ?"))) { return false; }
|
2012-04-20 12:09:48 +00:00
|
|
|
var subtype = event.srcElement.dataset.subtype;
|
|
|
|
if (! subtype) return false;
|
2012-05-10 12:10:58 +00:00
|
|
|
console.log(subtype);
|
2012-04-20 12:09:48 +00:00
|
|
|
var call_defer = self.ds.call('message_subscription_hide', [[self.params.res_id], subtype]);
|
2012-04-20 08:42:41 +00:00
|
|
|
$(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).hide();
|
|
|
|
if (self.params.thread_level > 0) {
|
|
|
|
$(event.srcElement).parents('ul.oe_mail_thread').eq(0).hide();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2012-03-14 08:51:28 +00:00
|
|
|
// event: click on an internal link
|
|
|
|
this.$element.find('div.oe_mail_thread_display').delegate('a.intlink', 'click', function (event) {
|
|
|
|
// lazy implementation: fetch data and try to redirect
|
|
|
|
if (! event.srcElement.dataset.resModel) return false;
|
|
|
|
else var res_model = event.srcElement.dataset.resModel;
|
|
|
|
var res_login = event.srcElement.dataset.resLogin;
|
|
|
|
var res_id = event.srcElement.dataset.resId;
|
|
|
|
if ((! res_login) && (! res_id)) return false;
|
|
|
|
if (! res_id) {
|
|
|
|
var ds = new session.web.DataSet(self, res_model);
|
|
|
|
var defer = ds.call('search', [[['login', '=', res_login]]]).then(function (records) {
|
|
|
|
if (records[0]) {
|
|
|
|
self.do_action({ type: 'ir.actions.act_window', res_model: res_model, res_id: parseInt(records[0]), views: [[false, 'form']]});
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else self.do_action({ type: 'ir.actions.act_window', res_model: res_model, res_id: parseInt(res_id), views: [[false, 'form']]});
|
|
|
|
});
|
2012-04-20 15:58:27 +00:00
|
|
|
// event: click on "send an email"
|
|
|
|
this.$element.find('div.oe_mail_thread_act').delegate('a.oe_mail_compose', 'click', function (event) {
|
|
|
|
self.do_action({
|
|
|
|
type: 'ir.actions.act_window',
|
|
|
|
res_model: 'mail.compose.message',
|
|
|
|
views: [[false, 'form']],
|
|
|
|
view_type: 'form',
|
|
|
|
view_mode: 'form',
|
|
|
|
target: 'new',
|
2012-05-10 12:10:58 +00:00
|
|
|
context: {'active_model': self.params.res_model, 'active_id': self.params.res_id, 'mail.compose.message.mode': 'document'},
|
2012-04-20 15:58:27 +00:00
|
|
|
key2: 'client_action_multi',
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
2012-03-13 17:41:05 +00:00
|
|
|
},
|
|
|
|
|
2012-03-21 15:31:41 +00:00
|
|
|
destroy: function () {
|
|
|
|
this._super.apply(this, arguments);
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
2012-02-27 16:04:42 +00:00
|
|
|
init_comments: function() {
|
|
|
|
var self = this;
|
|
|
|
this.params.offset = 0;
|
2012-04-05 10:29:17 +00:00
|
|
|
this.comments_structure = {'root_ids': [], 'new_root_ids': [], 'msgs': {}, 'tree_struct': {}, 'model_to_root_ids': {}};
|
2012-02-27 16:04:42 +00:00
|
|
|
this.$element.find('div.oe_mail_thread_display').empty();
|
2012-05-14 15:20:18 +00:00
|
|
|
var domain = this.get_fetch_domain(this.comments_structure);
|
2012-03-07 13:44:09 +00:00
|
|
|
return this.fetch_comments(this.params.limit, this.params.offset, domain).then();
|
2012-02-27 16:04:42 +00:00
|
|
|
},
|
|
|
|
|
2012-03-06 15:09:46 +00:00
|
|
|
fetch_comments: function (limit, offset, domain) {
|
2012-02-29 08:59:53 +00:00
|
|
|
var self = this;
|
2012-05-14 15:20:18 +00:00
|
|
|
var defer = this.ds.call('message_load', [[this.params.res_id], (this.params.thread_level > 0), (this.comments_structure['root_ids']),
|
|
|
|
(limit+1) || (this.params.limit+1), offset||this.params.offset, domain||undefined ]).then(function (records) {
|
2012-03-22 16:32:24 +00:00
|
|
|
if (records.length <= self.params.limit) self.display.show_more = false;
|
2012-04-05 10:29:17 +00:00
|
|
|
else { self.display.show_more = true; records.pop(); }
|
2012-02-29 08:59:53 +00:00
|
|
|
self.display_comments(records);
|
2012-05-14 15:20:18 +00:00
|
|
|
// TODO: move to customize display
|
2012-03-27 09:15:04 +00:00
|
|
|
if (self.display.show_more == true) self.$element.find('div.oe_mail_thread_more:last').show();
|
|
|
|
else self.$element.find('div.oe_mail_thread_more:last').hide();
|
2012-02-29 08:59:53 +00:00
|
|
|
});
|
|
|
|
return defer;
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
2012-03-05 16:59:10 +00:00
|
|
|
|
2012-04-04 12:00:53 +00:00
|
|
|
display_comments_from_parameters: function (records) {
|
|
|
|
if (records.length > 0 && records.length < (records[0].child_ids.length+1) ) this.display.show_more = true;
|
|
|
|
else this.display.show_more = false;
|
|
|
|
var defer = this.display_comments(records);
|
2012-05-14 15:20:18 +00:00
|
|
|
// TODO: move to customize display
|
2012-04-04 12:00:53 +00:00
|
|
|
if (this.display.show_more == true) $('div.oe_mail_thread_more').eq(-2).show();
|
|
|
|
else $('div.oe_mail_thread_more').eq(-2).hide();
|
|
|
|
return defer;
|
|
|
|
},
|
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
display_comments: function (records) {
|
2012-03-05 16:59:10 +00:00
|
|
|
var self = this;
|
2012-05-14 15:20:18 +00:00
|
|
|
tools_sort_comments(this.comments_structure, records, this.params.parent_id);
|
2012-04-05 10:29:17 +00:00
|
|
|
|
2012-03-05 17:37:42 +00:00
|
|
|
/* WIP: map matched regexp -> records to browse with name */
|
|
|
|
//_(records).each(function (record) {
|
|
|
|
//self.do_check_internal_links(record.body_text);
|
|
|
|
//});
|
2012-03-05 16:59:10 +00:00
|
|
|
|
|
|
|
_(records).each(function (record) {
|
2012-03-05 17:37:42 +00:00
|
|
|
var sub_msgs = [];
|
|
|
|
if ((record.parent_id == false || record.parent_id[0] == self.params.parent_id) && self.params.thread_level > 0 ) {
|
2012-05-15 07:27:03 +00:00
|
|
|
var sub_list = self.comments_structure['tree_struct'][record.id]['direct_childs'];
|
2012-03-05 16:59:10 +00:00
|
|
|
_(records).each(function (record) {
|
2012-04-05 13:09:49 +00:00
|
|
|
//if (record.parent_id == false || record.parent_id[0] == self.params.parent_id) return;
|
2012-04-05 10:29:17 +00:00
|
|
|
if (_.indexOf(sub_list, record.id) != -1) {
|
2012-03-05 17:37:42 +00:00
|
|
|
sub_msgs.push(record);
|
2012-03-05 16:59:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
self.display_comment(record);
|
|
|
|
self.thread = new mail.Thread(self, {'res_model': self.params.res_model, 'res_id': self.params.res_id, 'uid': self.params.uid,
|
2012-05-15 12:23:06 +00:00
|
|
|
'records': sub_msgs, 'thread_level': (self.params.thread_level-1), 'parent_id': record.id,
|
|
|
|
'is_wall': self.params.is_wall});
|
2012-04-05 13:09:49 +00:00
|
|
|
self.$element.find('li.oe_mail_thread_msg:last').append('<div class="oe_mail_thread_subthread"/>');
|
2012-03-05 16:59:10 +00:00
|
|
|
self.thread.appendTo(self.$element.find('div.oe_mail_thread_subthread:last'));
|
|
|
|
}
|
|
|
|
else if (self.params.thread_level == 0) {
|
|
|
|
self.display_comment(record);
|
|
|
|
}
|
2012-02-23 16:32:36 +00:00
|
|
|
});
|
2012-02-27 16:04:42 +00:00
|
|
|
// update offset for "More" buttons
|
2012-03-06 15:09:46 +00:00
|
|
|
if (this.params.thread_level == 0) this.params.offset += records.length;
|
2012-02-27 11:07:54 +00:00
|
|
|
},
|
2012-03-02 17:07:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a record
|
|
|
|
*/
|
|
|
|
display_comment: function (record) {
|
2012-05-08 14:54:15 +00:00
|
|
|
record.body = this.do_text_nl2br(record.body, true);
|
2012-03-02 17:07:49 +00:00
|
|
|
if (record.type == 'email') { record.mini_url = ('/mail/static/src/img/email_icon.png'); }
|
2012-05-14 14:53:59 +00:00
|
|
|
else { record.mini_url = tools_get_image(this.session.prefix, this.session.session_id, 'res.users', 'avatar', record.user_id[0]); }
|
2012-03-02 17:07:49 +00:00
|
|
|
// body text manipulation
|
2012-04-06 11:33:50 +00:00
|
|
|
record.body = this.do_clean_text(record.body);
|
|
|
|
record.body = this.do_replace_internal_links(record.body);
|
2012-04-19 08:58:38 +00:00
|
|
|
// format date according to the user timezone
|
|
|
|
record.date = session.web.format_value(record.date, {type:"datetime"});
|
2012-03-02 17:07:49 +00:00
|
|
|
// render
|
2012-03-21 17:20:18 +00:00
|
|
|
$(session.web.qweb.render('ThreadMsg', {'record': record, 'thread': this, 'params': this.params, 'display': this.display})
|
|
|
|
).appendTo(this.$element.children('div.oe_mail_thread_display:first'));
|
2012-05-10 15:15:38 +00:00
|
|
|
// expand feature
|
|
|
|
this.$element.find('span.oe_mail_msg_body:last').expander({slicePoint: this.params.msg_more_limit, moreClass: 'oe_mail_expand', lesClass: 'oe_mail_reduce'});
|
2012-03-02 17:07:49 +00:00
|
|
|
},
|
2012-03-05 16:59:10 +00:00
|
|
|
|
2012-02-27 11:07:54 +00:00
|
|
|
display_current_user: function () {
|
2012-05-14 14:53:59 +00:00
|
|
|
return this.$element.find('img.oe_mail_msg_image').attr('src', tools_get_image(this.session.prefix, this.session.session_id, 'res.users', 'avatar', this.params.uid));
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_comment: function () {
|
2012-03-12 17:33:23 +00:00
|
|
|
var comment_node = this.$element.find('textarea');
|
|
|
|
var body_text = comment_node.val();
|
|
|
|
comment_node.val('');
|
2012-05-07 14:13:58 +00:00
|
|
|
return this.ds.call('message_append_note', [[this.params.res_id], 'Reply', body_text, this.params.parent_id, 'comment', 'html', 'comment']).then(
|
2012-03-07 13:44:09 +00:00
|
|
|
this.proxy('init_comments'));
|
2012-02-23 16:32:36 +00:00
|
|
|
},
|
|
|
|
|
2012-03-06 15:09:46 +00:00
|
|
|
/**
|
|
|
|
* Create a domain to fetch new comments according to
|
2012-05-14 15:20:18 +00:00
|
|
|
* comment already present in comments_structure
|
|
|
|
* @param {Object} comments_structure (see tools_sort_comments)
|
2012-03-06 15:09:46 +00:00
|
|
|
* @returns {Array} fetch_domain (OpenERP domain style)
|
|
|
|
*/
|
2012-05-14 15:20:18 +00:00
|
|
|
get_fetch_domain: function (comments_structure) {
|
2012-03-06 15:09:46 +00:00
|
|
|
var domain = [];
|
2012-05-14 15:20:18 +00:00
|
|
|
var ids = comments_structure.root_ids.slice();
|
2012-04-05 10:29:17 +00:00
|
|
|
var ids2 = [];
|
2012-03-22 16:32:24 +00:00
|
|
|
// must be child of current parent
|
|
|
|
if (this.params.parent_id) { domain.push(['id', 'child_of', this.params.parent_id]); }
|
2012-05-14 15:20:18 +00:00
|
|
|
_(comments_structure.root_ids).each(function (id) { // each record
|
2012-03-06 15:09:46 +00:00
|
|
|
ids.push(id);
|
2012-04-05 10:29:17 +00:00
|
|
|
ids2.push(id);
|
2012-03-06 15:09:46 +00:00
|
|
|
});
|
2012-04-05 10:29:17 +00:00
|
|
|
if (this.params.parent_id != false) {
|
|
|
|
ids2.push(this.params.parent_id);
|
|
|
|
}
|
|
|
|
// must not be children of already fetched messages
|
2012-03-06 15:09:46 +00:00
|
|
|
if (ids.length > 0) {
|
|
|
|
domain.push('&');
|
|
|
|
domain.push('!');
|
|
|
|
domain.push(['id', 'child_of', ids]);
|
|
|
|
}
|
2012-04-05 10:29:17 +00:00
|
|
|
if (ids2.length > 0) {
|
|
|
|
domain.push(['id', 'not in', ids2]);
|
2012-03-06 15:09:46 +00:00
|
|
|
}
|
|
|
|
return domain;
|
|
|
|
},
|
|
|
|
|
2012-02-27 11:07:54 +00:00
|
|
|
do_more: function () {
|
2012-05-14 15:20:18 +00:00
|
|
|
domain = this.get_fetch_domain(this.comments_structure);
|
2012-03-06 15:09:46 +00:00
|
|
|
return this.fetch_comments(this.params.limit, this.params.offset, domain);
|
2012-02-24 16:09:20 +00:00
|
|
|
},
|
2012-05-14 14:47:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CONTENT MANIPULATION
|
2012-06-08 14:24:19 +00:00
|
|
|
*
|
|
|
|
* Regular expressions
|
|
|
|
* - (^|\s)@((\w|@|\.)*): @login@log.log, supports inner '@' for
|
|
|
|
* logins that are emails
|
|
|
|
* 1. '(void')
|
|
|
|
* 2. login@log.log
|
|
|
|
* - (^|\s)\[(\w+).(\w+),(\d)\|*((\w|[@ .,])*)\]: [ir.attachment,3|My Label],
|
|
|
|
* for internal links to model ir.attachment, id=3, and with
|
|
|
|
* optional label 'My Label'. Note that having a '|Label' is not
|
|
|
|
* mandatory, because the regex should still be correct.
|
|
|
|
* 1. '(void)'
|
|
|
|
* 2. 'ir'
|
|
|
|
* 3. 'attachment'
|
|
|
|
* 4. '3'
|
|
|
|
* 5. 'My Label'
|
2012-05-14 14:47:39 +00:00
|
|
|
*/
|
2012-06-08 14:24:19 +00:00
|
|
|
|
2012-04-16 08:18:05 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
*/
|
2012-02-23 16:32:36 +00:00
|
|
|
do_replace_internal_links: function (string) {
|
|
|
|
var self = this;
|
2012-04-06 10:27:59 +00:00
|
|
|
var icon_list = ['al', 'pinky']
|
2012-02-27 11:07:54 +00:00
|
|
|
/* shortcut to user: @login */
|
2012-04-06 10:27:59 +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-04-06 10:27:59 +00:00
|
|
|
string = string.replace(regex_res[0], regex_res[1] + '<a href="#" class="intlink oe_mail_oe_intlink" data-res-model="res.users" data-res-login = ' + login + '>@' + login + '</a>');
|
|
|
|
regex_res = regex_login.exec(string);
|
|
|
|
}
|
|
|
|
/* special shortcut: :name, try to find an icon if in list */
|
|
|
|
var regex_login = new RegExp(/(^|\s):((\w)*)/g);
|
|
|
|
var regex_res = regex_login.exec(string);
|
|
|
|
while (regex_res != null) {
|
|
|
|
var icon_name = regex_res[2];
|
|
|
|
if (_.include(icon_list, icon_name))
|
|
|
|
string = string.replace(regex_res[0], regex_res[1] + '<img src="/mail/static/src/img/_' + icon_name + '.png" width="22px" height="22px" alt="' + icon_name + '"/>');
|
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-27 17:13:23 +00:00
|
|
|
/**
|
2012-04-06 10:27:59 +00:00
|
|
|
* var regex_login = new RegExp(/(^|\s)@((\w|@|\.)*)/g);
|
2012-02-27 17:13:23 +00:00
|
|
|
* var regex_intlink = new RegExp(/(^|\s)#(\w*[a-zA-Z_]+\w*)\.(\w+[a-zA-Z_]+\w*),(\w+)/g);
|
|
|
|
*/
|
2012-05-14 14:47:39 +00:00
|
|
|
do_check_for_internal_links: function(string) {
|
2012-02-27 17:13:23 +00:00
|
|
|
/* shortcut to user: @login */
|
2012-04-06 10:27:59 +00:00
|
|
|
var regex_login = new RegExp(/(^|\s)@((\w|@|\.)*)/g);
|
2012-02-27 17:13:23 +00:00
|
|
|
var regex_res = regex_login.exec(string);
|
|
|
|
while (regex_res != null) {
|
|
|
|
var login = regex_res[2];
|
2012-02-29 09:57:52 +00:00
|
|
|
if (! ('res.users' in this.map_hash)) { this.map_hash['res.users']['name'] = []; }
|
2012-02-27 17:13:23 +00:00
|
|
|
this.map_hash['res.users']['login'].push(login);
|
|
|
|
regex_res = regex_login.exec(string);
|
|
|
|
}
|
2012-05-14 14:47:39 +00:00
|
|
|
/* document link with name_get: [res.model,name] */
|
|
|
|
/* internal link with id: [res.model,id], or [res.model,id|display_name] */
|
2012-02-27 17:13:23 +00:00
|
|
|
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-03-21 17:20:18 +00:00
|
|
|
|
2012-06-08 14:24:19 +00:00
|
|
|
/** Removes html tags, except b, em, br */
|
2012-05-14 14:47:39 +00:00
|
|
|
do_clean_text: function (string) {
|
|
|
|
var html = $('<div/>').text(string.replace(/\s+/g, ' ')).html().replace(new RegExp('<(/)?(b|em|br|br /)\\s*>', 'gi'), '<$1$2>');
|
|
|
|
return html;
|
|
|
|
},
|
|
|
|
|
2012-06-08 14:24:19 +00:00
|
|
|
/** Replaces line bracks by html line breaks (br) */
|
2012-05-14 14:47:39 +00:00
|
|
|
do_text_nl2br: function (str, is_xhtml) {
|
|
|
|
var break_tag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
|
|
|
|
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ break_tag +'$2');
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MISC TOOLS METHODS
|
|
|
|
*/
|
|
|
|
|
2012-03-21 17:20:18 +00:00
|
|
|
/** checks if tue current user is the message author */
|
|
|
|
_is_author: function (id) {
|
|
|
|
return (this.session.uid == id);
|
|
|
|
},
|
2012-02-27 16:04:42 +00:00
|
|
|
|
2012-02-23 16:32:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-06-15 08:36:56 +00:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
* mail_thread Widget
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* This widget handles the display of a thread of messages. The
|
|
|
|
* [thread_level] parameter sets the thread level number:
|
|
|
|
* - root message
|
|
|
|
* - - sub message (parent_id = root message)
|
|
|
|
* - - - sub sub message (parent id = sub message)
|
|
|
|
* - - sub message (parent_id = root message)
|
|
|
|
* This widget has 2 ways of initialization, either you give records
|
|
|
|
* to be rendered, either it will fetch [limit] messages related to
|
|
|
|
* [res_model]:[res_id].
|
|
|
|
*/
|
|
|
|
|
2012-02-08 16:42:29 +00:00
|
|
|
/* Add ThreadView widget to registry */
|
2012-06-15 08:36:56 +00:00
|
|
|
session.web.form.widgets.add('ThreadView', 'openerp.mail.RecordThread');
|
2012-02-06 09:06:18 +00:00
|
|
|
|
2012-02-08 16:42:29 +00:00
|
|
|
/* ThreadView widget: thread of comments */
|
2012-04-16 10:29:54 +00:00
|
|
|
mail.RecordThread = session.web.form.AbstractField.extend({
|
2012-02-06 09:06:18 +00:00
|
|
|
// QWeb template to use when rendering the object
|
2012-06-15 08:36:56 +00:00
|
|
|
template: 'mail.record_thread',
|
2012-02-23 16:32:36 +00:00
|
|
|
|
2012-05-15 11:07:02 +00:00
|
|
|
init: function() {
|
2012-02-06 09:06:18 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-03-21 15:31:41 +00:00
|
|
|
this.see_subscribers = true;
|
2012-03-02 17:07:49 +00:00
|
|
|
this.thread = null;
|
2012-05-15 11:07:02 +00:00
|
|
|
this.params = this.get_definition_options();
|
|
|
|
this.params.thread_level = this.params.thread_level || 0;
|
2012-03-21 15:31:41 +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() {
|
|
|
|
this._super.apply(this, arguments);
|
2012-03-21 15:31:41 +00:00
|
|
|
var self = this;
|
|
|
|
// bind buttons
|
|
|
|
this.$element.find('button.oe_mail_button_followers').click(function () { self.do_toggle_followers(); }).hide();
|
|
|
|
this.$element.find('button.oe_mail_button_follow').click(function () { self.do_follow(); })
|
2012-04-13 11:41:26 +00:00
|
|
|
.mouseover(function () { $(this).html('Follow').removeClass('oe_mail_button_mouseout').addClass('oe_mail_button_mouseover'); })
|
|
|
|
.mouseleave(function () { $(this).html('Not following').removeClass('oe_mail_button_mouseover').addClass('oe_mail_button_mouseout'); });
|
2012-03-21 15:31:41 +00:00
|
|
|
this.$element.find('button.oe_mail_button_unfollow').click(function () { self.do_unfollow(); })
|
2012-04-13 11:41:26 +00:00
|
|
|
.mouseover(function () { $(this).html('Unfollow').removeClass('oe_mail_button_mouseout').addClass('oe_mail_button_mouseover'); })
|
|
|
|
.mouseleave(function () { $(this).html('Following').removeClass('oe_mail_button_mouseover').addClass('oe_mail_button_mouseout'); });
|
2012-03-21 15:31:41 +00:00
|
|
|
this.reinit();
|
2012-02-06 15:10:09 +00:00
|
|
|
},
|
2012-02-23 16:32:36 +00:00
|
|
|
|
2012-03-21 15:31:41 +00:00
|
|
|
destroy: function () {
|
2012-02-16 16:03:02 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-02-02 14:18:43 +00:00
|
|
|
},
|
|
|
|
|
2012-03-21 15:31:41 +00:00
|
|
|
reinit: function() {
|
|
|
|
this.see_subscribers = true;
|
2012-03-08 16:41:38 +00:00
|
|
|
this.$element.find('button.oe_mail_button_followers').html('Hide followers')
|
2012-03-05 09:05:21 +00:00
|
|
|
this.$element.find('button.oe_mail_button_follow').hide();
|
|
|
|
this.$element.find('button.oe_mail_button_unfollow').hide();
|
2012-03-21 15:31:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set_value: function() {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
var self = this;
|
|
|
|
this.reinit();
|
|
|
|
if (! this.view.datarecord.id) { this.$element.find('ul.oe_mail_thread').hide(); return; }
|
|
|
|
// fetch followers
|
2012-03-05 09:05:21 +00:00
|
|
|
var fetch_sub_done = this.fetch_subscribers();
|
2012-03-21 15:31:41 +00:00
|
|
|
// create and render Thread widget
|
2012-06-15 08:36:56 +00:00
|
|
|
this.$element.find('div.oe_mail_recthread_main').empty();
|
2012-03-21 15:31:41 +00:00
|
|
|
if (this.thread) this.thread.destroy();
|
|
|
|
this.thread = new mail.Thread(this, {'res_model': this.view.model, 'res_id': this.view.datarecord.id, 'uid': this.session.uid,
|
2012-05-15 11:07:02 +00:00
|
|
|
'thread_level': this.params.thread_level, 'show_post_comment': true, 'limit': 15});
|
2012-06-15 08:36:56 +00:00
|
|
|
var thread_done = this.thread.appendTo(this.$element.find('div.oe_mail_recthread_main'));
|
2012-03-21 15:31:41 +00:00
|
|
|
return fetch_sub_done && thread_done;
|
2012-02-16 16:03:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetch_subscribers: function () {
|
2012-06-05 12:22:21 +00:00
|
|
|
return this.ds.call('message_read_subscribers', [[this.view.datarecord.id]]).then(this.proxy('display_subscribers'));
|
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) {
|
2012-02-10 12:43:48 +00:00
|
|
|
var self = this;
|
2012-03-28 12:01:26 +00:00
|
|
|
this.is_subscriber = false;
|
2012-06-07 17:53:28 +00:00
|
|
|
var user_list = this.$element.find('ul.oe_mail_followers_display').empty();
|
|
|
|
this.$element.find('div.oe_mail_recthread_followers h4').html('Followers (' + records.length + ')');
|
2012-02-10 12:43:48 +00:00
|
|
|
_(records).each(function (record) {
|
2012-03-21 15:31:41 +00:00
|
|
|
if (record.id == self.session.uid) { self.is_subscriber = true; }
|
2012-06-11 10:22:54 +00:00
|
|
|
var mini_url = tools_get_image(self.session.prefix, self.session.session_id, 'res.users', 'avatar', record.id);
|
2012-06-07 17:53:28 +00:00
|
|
|
$('<li><img class="oe_mail_oe_left oe_mail_msg_image" src="' + mini_url + '"/>' +
|
|
|
|
'<a href="#" class="intlink oe_mail_oe_intlink" data-res-model="res.users" data-res-id="' + record.id + '">' + record.name + '</a></li>').appendTo(user_list);
|
2012-02-10 12:43:48 +00:00
|
|
|
});
|
2012-03-21 15:31:41 +00:00
|
|
|
if (self.is_subscriber) {
|
|
|
|
self.$element.find('button.oe_mail_button_follow').hide();
|
|
|
|
self.$element.find('button.oe_mail_button_unfollow').show(); }
|
|
|
|
else {
|
|
|
|
self.$element.find('button.oe_mail_button_follow').show();
|
|
|
|
self.$element.find('button.oe_mail_button_unfollow').hide(); }
|
2012-02-10 12:43:48 +00:00
|
|
|
},
|
|
|
|
|
2012-02-06 11:26:23 +00:00
|
|
|
do_follow: function () {
|
2012-03-21 15:31:41 +00:00
|
|
|
return this.ds.call('message_subscribe', [[this.view.datarecord.id]]).pipe(this.proxy('fetch_subscribers'));
|
2012-02-06 11:26:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_unfollow: function () {
|
2012-03-21 15:31:41 +00:00
|
|
|
var self = this;
|
|
|
|
return this.ds.call('message_unsubscribe', [[this.view.datarecord.id]]).then(function (record) {
|
2012-03-28 12:01:26 +00:00
|
|
|
if (record == false) self.do_notify("Impossible to unsubscribe", "You are automatically subscribed to this record. You cannot unsubscribe.");
|
2012-03-21 15:31:41 +00:00
|
|
|
}).pipe(this.proxy('fetch_subscribers'));
|
2012-02-06 17:19:11 +00:00
|
|
|
},
|
2012-02-10 12:43:48 +00:00
|
|
|
|
|
|
|
do_toggle_followers: function () {
|
2012-03-21 15:31:41 +00:00
|
|
|
this.see_subscribers = ! this.see_subscribers;
|
|
|
|
if (this.see_subscribers) { this.$element.find('button.oe_mail_button_followers').html('Hide followers'); }
|
2012-02-22 14:48:35 +00:00
|
|
|
else { this.$element.find('button.oe_mail_button_followers').html('Display followers'); }
|
2012-03-21 14:07:26 +00:00
|
|
|
this.$element.find('div.oe_mail_recthread_followers').toggle();
|
2012-02-10 12:43:48 +00:00
|
|
|
},
|
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
|
2012-02-29 10:17:58 +00:00
|
|
|
* @param {Number} [params.search_view_id=false] search view id for messages
|
2012-05-14 15:20:18 +00:00
|
|
|
* @var {Array} comments_structure see tools_sort_comments
|
2012-02-28 08:51:11 +00:00
|
|
|
*/
|
2012-02-09 10:04:47 +00:00
|
|
|
init: function (parent, params) {
|
|
|
|
this._super(parent);
|
2012-04-04 11:20:37 +00:00
|
|
|
this.params = {};
|
2012-04-05 13:11:02 +00:00
|
|
|
this.params.limit = params.limit || 25;
|
2012-03-30 15:11:47 +00:00
|
|
|
this.params.domain = params.domain || [];
|
|
|
|
this.params.context = params.context || {};
|
2012-02-29 10:17:58 +00:00
|
|
|
this.params.search_view_id = params.search_view_id || false;
|
2012-03-07 13:25:20 +00:00
|
|
|
this.params.thread_level = params.thread_level || 1;
|
2012-04-04 11:20:37 +00:00
|
|
|
this.comments_structure = {'root_ids': [], 'new_root_ids': [], 'msgs': {}, 'tree_struct': {}, 'model_to_root_ids': {}};
|
2012-03-05 19:20:52 +00:00
|
|
|
this.display_show_more = true;
|
2012-04-04 11:20:37 +00:00
|
|
|
this.thread_list = [];
|
2012-04-05 11:34:19 +00:00
|
|
|
this.search = {'domain': [], 'context': {}, 'groupby': {}}
|
|
|
|
this.search_results = {'domain': [], 'context': {}, 'groupby': {}}
|
2012-03-30 15:11:47 +00:00
|
|
|
// datasets
|
2012-02-24 15:21:59 +00:00
|
|
|
this.ds_msg = new session.web.DataSet(this, 'mail.message');
|
2012-03-29 08:53:06 +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
|
|
|
},
|
|
|
|
|
2012-03-30 15:11:47 +00:00
|
|
|
start: function () {
|
2012-02-08 17:08:08 +00:00
|
|
|
this._super.apply(this, arguments);
|
2012-03-30 15:11:47 +00:00
|
|
|
var self = this;
|
|
|
|
// add events
|
|
|
|
this.add_event_handlers();
|
|
|
|
// load mail.message search view
|
|
|
|
var search_view_ready = this.load_search_view(this.params.search_view_id, {}, false);
|
|
|
|
// fetch first threads
|
|
|
|
var comments_ready = this.init_and_fetch_comments(this.params.limit, 0);
|
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-03-30 15:11:47 +00:00
|
|
|
|
|
|
|
/** Add events */
|
|
|
|
add_event_handlers: function () {
|
|
|
|
var self = this;
|
|
|
|
// post a comment
|
2012-04-04 16:00:56 +00:00
|
|
|
this.$element.find('button.oe_mail_wall_button_comment').click(function () { return self.do_comment(); });
|
2012-03-30 15:11:47 +00:00
|
|
|
// display more threads
|
2012-04-04 16:00:56 +00:00
|
|
|
this.$element.find('button.oe_mail_wall_button_more').click(function () { return self.do_more(); });
|
2012-03-30 15:11:47 +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
|
2012-02-29 14:23:31 +00:00
|
|
|
* @param {Object} defaults ??
|
2012-03-30 15:11:47 +00:00
|
|
|
* @param {Boolean} hidden some kind of trick we do not care here
|
2012-02-28 08:51:11 +00:00
|
|
|
*/
|
2012-02-24 15:21:59 +00:00
|
|
|
load_search_view: function (view_id, defaults, hidden) {
|
2012-03-30 15:11:47 +00:00
|
|
|
var self = this;
|
2012-02-24 15:21:59 +00:00
|
|
|
this.searchview = new session.web.SearchView(this, this.ds_msg, view_id || false, defaults || {}, hidden || false);
|
2012-05-11 10:37:17 +00:00
|
|
|
var search_view_loaded = this.searchview.appendTo(this.$element.find('.oe_view_manager_view_search'));
|
2012-03-30 15:11:47 +00:00
|
|
|
return $.when(search_view_loaded).then(function () {
|
|
|
|
self.searchview.on_search.add(self.do_searchview_search);
|
|
|
|
});
|
2012-02-24 15:21:59 +00:00
|
|
|
},
|
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-04-05 11:34:19 +00:00
|
|
|
self.search_results['context'] = results.context;
|
|
|
|
self.search_results['domain'] = results.domain;
|
|
|
|
self.search_results['groupby'] = results.group_by;
|
|
|
|
return self.init_and_fetch_comments();
|
2012-02-28 11:07:40 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-03-30 15:11:47 +00:00
|
|
|
* Initializes the wall and calls fetch_comments
|
|
|
|
* @param {Number} limit: number of notifications to fetch
|
|
|
|
* @param {Number} offset: offset in notifications search
|
|
|
|
* @param {Array} domain
|
|
|
|
* @param {Array} context
|
2012-02-28 11:07:40 +00:00
|
|
|
*/
|
2012-04-05 11:34:19 +00:00
|
|
|
init_and_fetch_comments: function() {
|
|
|
|
this.search['domain'] = _.union(this.params.domain, this.search_results.domain);
|
|
|
|
this.search['context'] = _.extend(this.params.context, this.search_results.context);
|
2012-03-05 19:20:52 +00:00
|
|
|
this.display_show_more = true;
|
2012-04-05 07:27:12 +00:00
|
|
|
this.comments_structure = {'root_ids': [], 'new_root_ids': [], 'msgs': {}, 'tree_struct': {}, 'model_to_root_ids': {}};
|
2012-02-28 11:07:40 +00:00
|
|
|
this.$element.find('div.oe_mail_wall_threads').empty();
|
2012-04-05 11:34:19 +00:00
|
|
|
return this.fetch_comments(this.params.limit, 0);
|
2012-02-24 15:21:59 +00:00
|
|
|
},
|
2012-02-28 08:56:07 +00:00
|
|
|
|
|
|
|
/**
|
2012-03-30 15:11:47 +00:00
|
|
|
* Fetches wall messages
|
|
|
|
* @param {Number} limit: number of notifications to fetch
|
|
|
|
* @param {Number} offset: offset in notifications search
|
|
|
|
* @param {Array} domain
|
|
|
|
* @param {Array} context
|
2012-02-28 08:56:07 +00:00
|
|
|
*/
|
2012-03-30 15:11:47 +00:00
|
|
|
fetch_comments: function (limit, offset, additional_domain, additional_context) {
|
2012-03-05 19:20:52 +00:00
|
|
|
var self = this;
|
2012-04-05 11:34:19 +00:00
|
|
|
if (additional_domain) var fetch_domain = this.search['domain'].concat(additional_domain);
|
|
|
|
else var fetch_domain = this.search['domain'];
|
|
|
|
if (additional_context) var fetch_context = _.extend(this.search['context'], additional_context);
|
|
|
|
else var fetch_context = this.search['context'];
|
2012-04-04 16:00:56 +00:00
|
|
|
return this.ds_thread.call('get_pushed_messages',
|
2012-05-03 09:38:43 +00:00
|
|
|
[[this.session.uid], true, [], (limit || 0), (offset || 0), fetch_domain, fetch_context]).then(this.proxy('display_comments'));
|
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-08 17:08:08 +00:00
|
|
|
var self = this;
|
2012-04-04 16:00:56 +00:00
|
|
|
this.do_update_show_more(records.length >= self.params.limit);
|
|
|
|
this.sort_comments(records);
|
2012-04-04 11:20:37 +00:00
|
|
|
_(this.comments_structure['new_root_ids']).each(function (root_id) {
|
|
|
|
var records = self.comments_structure.tree_struct[root_id]['for_thread_msgs'];
|
|
|
|
var model_name = self.comments_structure.msgs[root_id]['model'];
|
|
|
|
var res_id = self.comments_structure.msgs[root_id]['res_id'];
|
|
|
|
var render_res = session.web.qweb.render('WallThreadContainer', {});
|
|
|
|
$('<div class="oe_mail_wall_thread">').html(render_res).appendTo(self.$element.find('div.oe_mail_wall_threads'));
|
|
|
|
var thread = new mail.Thread(self, {
|
|
|
|
'res_model': model_name, 'res_id': res_id, 'uid': self.session.uid, 'records': records,
|
2012-05-15 12:23:06 +00:00
|
|
|
'parent_id': false, 'thread_level': self.params.thread_level, 'show_hide': true, 'is_wall': true}
|
2012-04-04 11:20:37 +00:00
|
|
|
);
|
|
|
|
self.thread_list.push(thread);
|
2012-04-04 16:00:56 +00:00
|
|
|
return thread.appendTo(self.$element.find('div.oe_mail_wall_thread:last'));
|
2012-02-08 17:08:08 +00:00
|
|
|
});
|
2012-03-30 16:02:50 +00:00
|
|
|
// update TODO
|
|
|
|
this.comments_structure['root_ids'] = _.union(this.comments_structure['root_ids'], this.comments_structure['new_root_ids']);
|
|
|
|
this.comments_structure['new_root_ids'] = [];
|
2012-02-08 17:08:08 +00:00
|
|
|
},
|
|
|
|
|
2012-02-28 08:51:11 +00:00
|
|
|
/**
|
2012-04-05 09:48:42 +00:00
|
|
|
* Add records to comments_structure object: see function for details
|
2012-02-28 08:51:11 +00:00
|
|
|
*/
|
2012-04-04 11:20:37 +00:00
|
|
|
sort_comments: function(records) {
|
2012-04-05 09:48:42 +00:00
|
|
|
tools_sort_comments(this.comments_structure, records, false);
|
2012-02-22 14:04:03 +00:00
|
|
|
},
|
|
|
|
|
2012-02-28 08:51:11 +00:00
|
|
|
/**
|
|
|
|
* Create a domain to fetch new comments according to
|
2012-05-14 15:20:18 +00:00
|
|
|
* comments already present in comments_structure
|
2012-03-30 15:11:47 +00:00
|
|
|
* - for each model:
|
|
|
|
* -- should not be child of already displayed ids
|
2012-02-28 08:51:11 +00:00
|
|
|
* @returns {Array} fetch_domain (OpenERP domain style)
|
|
|
|
*/
|
2012-04-04 11:20:37 +00:00
|
|
|
get_fetch_domain: function () {
|
|
|
|
var self = this;
|
|
|
|
var model_to_root = {};
|
|
|
|
var fetch_domain = [];
|
|
|
|
_(this.comments_structure['model_to_root_ids']).each(function (sc_model, model_name) {
|
|
|
|
fetch_domain.push('|', ['model', '!=', model_name], '!', ['id', 'child_of', sc_model]);
|
2012-02-28 11:07:40 +00:00
|
|
|
});
|
2012-04-04 11:20:37 +00:00
|
|
|
return fetch_domain;
|
2012-02-28 08:51:11 +00:00
|
|
|
},
|
2012-03-05 19:20:52 +00:00
|
|
|
|
2012-03-30 15:11:47 +00:00
|
|
|
/** Display update: show more button */
|
|
|
|
do_update_show_more: function (new_value) {
|
|
|
|
if (new_value != undefined) this.display_show_more = new_value;
|
|
|
|
if (this.display_show_more) this.$element.find('div.oe_mail_wall_more:last').show();
|
|
|
|
else this.$element.find('div.oe_mail_wall_more:last').hide();
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Action: Shows more discussions */
|
2012-02-28 11:07:40 +00:00
|
|
|
do_more: function () {
|
2012-04-04 11:20:37 +00:00
|
|
|
var domain = this.get_fetch_domain();
|
2012-03-30 15:11:47 +00:00
|
|
|
return this.fetch_comments(this.params.limit, 0, domain);
|
2012-02-28 11:07:40 +00:00
|
|
|
},
|
2012-03-05 19:20:52 +00:00
|
|
|
|
2012-03-30 15:11:47 +00:00
|
|
|
/** Action: Posts a comment */
|
2012-03-05 19:20:52 +00:00
|
|
|
do_comment: function () {
|
2012-04-06 10:35:45 +00:00
|
|
|
var comment_node = this.$element.find('textarea.oe_mail_wall_action_textarea');
|
|
|
|
var body_text = comment_node.val();
|
|
|
|
comment_node.val('');
|
2012-05-07 14:13:58 +00:00
|
|
|
var call_done = this.ds_users.call('message_append_note', [[this.session.uid], 'Tweet', body_text, false, 'comment', 'html', 'tweet']).then(this.proxy('init_and_fetch_comments'));
|
2012-02-08 16:42:29 +00:00
|
|
|
},
|
|
|
|
});
|
2012-05-07 14:13:58 +00:00
|
|
|
|
2012-05-10 12:10:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TOOLS
|
|
|
|
*/
|
|
|
|
|
2012-05-07 14:13:58 +00:00
|
|
|
/**
|
2012-05-14 15:20:18 +00:00
|
|
|
* Add records to comments_structure array
|
2012-05-07 14:13:58 +00:00
|
|
|
* @param {Array} records records from mail.message sorted by date desc
|
|
|
|
* @returns {Object} cs comments_structure: dict
|
|
|
|
* cs.model_to_root_ids = {model: [root_ids], }
|
|
|
|
* cs.new_root_ids = [new_root_ids]
|
|
|
|
* cs.root_ids = [root_ids]
|
|
|
|
* cs.msgs = {record.id: record,}
|
|
|
|
* cs.tree_struct = {record.id: {
|
|
|
|
* 'level': record_level in hierarchy, 0 is root,
|
|
|
|
* 'msg_nbr': number of childs,
|
|
|
|
* 'direct_childs': [msg_ids],
|
|
|
|
* 'all_childs': [msg_ids],
|
|
|
|
* 'for_thread_msgs': [records],
|
|
|
|
* 'ancestors': [msg_ids], } }
|
|
|
|
*/
|
|
|
|
function tools_sort_comments(cs, records, parent_id) {
|
|
|
|
var cur_iter = 0; var max_iter = 10; var modif = true;
|
|
|
|
while ( modif && (cur_iter++) < max_iter) {
|
|
|
|
modif = false;
|
|
|
|
_(records).each(function (record) {
|
|
|
|
// root and not yet recorded
|
|
|
|
if ( (record.parent_id == false || record.parent_id[0] == parent_id) && ! cs['msgs'][record.id]) {
|
|
|
|
// add to model -> root_list ids
|
|
|
|
if (! cs['model_to_root_ids'][record.model]) cs['model_to_root_ids'][record.model] = [record.id];
|
|
|
|
else cs['model_to_root_ids'][record.model].push(record.id);
|
|
|
|
// add root data
|
|
|
|
cs['new_root_ids'].push(record.id);
|
|
|
|
// add record
|
|
|
|
cs['tree_struct'][record.id] = {'level': 0, 'direct_childs': [], 'all_childs': [], 'for_thread_msgs': [record], 'msg_nbr': -1, 'ancestors': []};
|
|
|
|
cs['msgs'][record.id] = record;
|
|
|
|
modif = true;
|
|
|
|
}
|
|
|
|
// not yet recorded, but parent is recorded
|
|
|
|
else if (! cs['msgs'][record.id] && cs['msgs'][record.parent_id[0]]) {
|
|
|
|
var parent_level = cs['tree_struct'][record.parent_id[0]]['level'];
|
|
|
|
// update parent structure
|
|
|
|
cs['tree_struct'][record.parent_id[0]]['direct_childs'].push(record.id);
|
|
|
|
cs['tree_struct'][record.parent_id[0]]['for_thread_msgs'].push(record);
|
|
|
|
// update ancestors structure
|
|
|
|
for (ancestor_id in cs['tree_struct'][record.parent_id[0]]['ancestors']) {
|
|
|
|
cs['tree_struct'][ancestor_id]['all_childs'].push(record.id);
|
|
|
|
}
|
|
|
|
// add record
|
|
|
|
cs['tree_struct'][record.id] = {'level': parent_level+1, 'direct_childs': [], 'all_childs': [], 'for_thread_msgs': [], 'msg_nbr': -1, 'ancestors': []};
|
|
|
|
cs['msgs'][record.id] = record;
|
|
|
|
modif = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return cs;
|
|
|
|
}
|
2012-05-14 14:53:59 +00:00
|
|
|
|
|
|
|
/** get an image in /web/binary/image?... */
|
|
|
|
function tools_get_image(session_prefix, session_id, model, field, id) {
|
|
|
|
return session_prefix + '/web/binary/image?session_id=' + session_id + '&model=' + model + '&field=' + field + '&id=' + (id || '');
|
|
|
|
}
|
|
|
|
|
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:
|