summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/mail.js
diff options
context:
space:
mode:
authorRajesh Prajapati (OpenERP) <rpr@tinyerp.com>2012-10-05 17:35:03 +0530
committerRajesh Prajapati (OpenERP) <rpr@tinyerp.com>2012-10-05 17:35:03 +0530
commitcdff4611b0a588546afb9b706bbd2d0fbda7bf2f (patch)
tree0a0a99b33e52547987ab69b2a29f3ec23c188142 /addons/mail/static/src/js/mail.js
parent7202b8f7efbc76e2a40b1a4a8d7a95ed0d2ce573 (diff)
parentba97319054728490e4d8a601382dd6fd2ab4f970 (diff)
[MERGE] merge with main addons
bzr revid: rpr@tinyerp.com-20121005120503-07e0zo8ucv8k0q3s
Diffstat (limited to 'addons/mail/static/src/js/mail.js')
-rw-r--r--addons/mail/static/src/js/mail.js1016
1 files changed, 718 insertions, 298 deletions
diff --git a/addons/mail/static/src/js/mail.js b/addons/mail/static/src/js/mail.js
index 57c777441e7..e0c9ac9b7fc 100644
--- a/addons/mail/static/src/js/mail.js
+++ b/addons/mail/static/src/js/mail.js
@@ -22,8 +22,9 @@ openerp.mail = function(session) {
action.context && action.context.redirect == true &&
this.fields && this.fields.message_ids && this.fields.message_ids.view.get("actual_mode") != 'create') {
var thread = this.fields.message_ids.thread;
- thread.refresh_composition_form(action.context);
- return true;
+
+ thread.refresh(action.context);
+ return false;
}
else {
return this._super(action, on_close);
@@ -56,11 +57,6 @@ openerp.mail = function(session) {
return session.origin + '/web/binary/saveas?session_id=' + session.session_id + '&model=ir.attachment&field=datas&filename_field=datas_fname&id=' + attachment['id'];
},
- /** Check if the current user is the message author */
- is_author: function (widget, message_user_id) {
- return (widget.session && widget.session.uid != 0 && widget.session.uid == message_user_id);
- },
-
/** Replaces some expressions
* - :name - shortcut to an image
*/
@@ -115,8 +111,8 @@ openerp.mail = function(session) {
this._super.apply(this, arguments);
// customize display: add avatar, clean previous content
var user_avatar = mail.ChatterUtils.get_image(this.session, 'res.users', 'image_small', this.session.uid);
- this.$el.find('img.oe_mail_icon').attr('src', user_avatar);
- this.$el.find('div.oe_mail_msg_content').empty();
+ this.$('img.oe_mail_icon').attr('src', user_avatar);
+ this.$('div.oe_mail_msg_content').empty();
// create a context for the dataset and default_get of the wizard
var context = _.extend({}, this.options.context);
this.ds_compose = new session.web.DataSetSearch(this, 'mail.compose.message', context);
@@ -144,21 +140,21 @@ openerp.mail = function(session) {
disable_autofocus: true,
});
// add the form, bind events, activate the form
- var msg_node = this.$el.find('div.oe_mail_msg_content');
+ var msg_node = this.$('div.oe_mail_msg_content');
return $.when(this.form_view.appendTo(msg_node)).pipe(this.proxy('postprocess_create_form_view'));
},
postprocess_create_form_view: function () {
// handle attachment button
this.fileupload_id = _.uniqueId('oe_fileupload');
- var button_attach = this.$el.find('button.oe_mail_compose_message_attachment');
+ var button_attach = this.$('button.oe_mail_compose_message_attachment');
var rendered = session.web.qweb.render('mail.compose_message.add_attachment', {'widget': this});
$(rendered).insertBefore(button_attach);
// move the button inside div.oe_hidden_input_file
- var input_node = this.$el.find('input[name=ufile]');
+ var input_node = this.$('input[name=ufile]');
button_attach.detach().insertAfter(input_node);
// set the function called when attachments are added
- this.$el.find('input.oe_form_binary_file').change(this.on_attachment_change);
+ this.$('input.oe_form_binary_file').change(this.on_attachment_change);
this.bind_events();
this.form_view.do_show();
},
@@ -166,7 +162,7 @@ openerp.mail = function(session) {
on_attachment_change: function (event) {
var $target = $(event.target);
if ($target.val() !== '') {
- this.$el.find('form.oe_form_binary_form').submit();
+ this.$('form.oe_form_binary_form').submit();
session.web.blockUI();
}
},
@@ -183,11 +179,11 @@ openerp.mail = function(session) {
},
display_attachments: function () {
- var attach_node = this.$el.find('div.oe_mail_compose_message_attachments');
+ var attach_node = this.$('div.oe_mail_compose_message_attachments');
var rendered = session.web.qweb.render('mail.thread.message.attachments', {'record': this});
attach_node.empty();
$(rendered).appendTo(attach_node);
- this.$el.find('.oe_mail_msg_attachments').show();
+ this.$('.oe_mail_msg_attachments').show();
var composer_attachment_ids = _.pluck(this.attachment_ids, 'id');
var onchange_like = {'value': {'attachment_ids': composer_attachment_ids}}
this.form_view.on_processed_onchange(onchange_like, []);
@@ -233,22 +229,89 @@ openerp.mail = function(session) {
this.$el.on('click', '.oe_mail_attachment_delete', self.on_attachment_delete);
},
}),
+
/**
* ------------------------------------------------------------
- * Thread Widget
+ * Thread Message Expandable Widget
* ------------------------------------------------------------
*
- * This widget handles the display of a thread of messages. The
+ * This widget handles the display the expandable message in a thread. The
* [thread_level] parameter sets the thread level number:
- * - root message
+ * - thread
+ * - - visible message
+ * - - expandable
+ * - - visible message
+ * - - visible message
+ * - - expandable
+ */
+ mail.ThreadExpandable = session.web.Widget.extend({
+ template: 'mail.thread.expandable',
+
+ init: function(parent, options) {
+ this._super(parent);
+ this.domain = options.domain || [];
+ this.context = _.extend({
+ default_model: 'mail.thread',
+ default_res_id: 0,
+ default_parent_id: false }, options.context || {});
+
+ this.id = -1;
+ this.parent_id= options.parameters.parent_id || false;
+ this.nb_messages = options.parameters.nb_messages || 0;
+ this.type = options.parameters.type || false;
+
+ // record options and data
+ this.parent_thread= parent.messages!= undefined ? parent : options.options.thread._parents[0] ;
+
+ },
+
+
+ start: function() {
+ this._super.apply(this, arguments);
+ this.bind_events();
+ },
+
+ /**
+ * Bind events in the widget. Each event is slightly described
+ * in the function. */
+ bind_events: function() {
+ var self = this;
+ // event: click on 'Vote' button
+ this.$el.on('click', 'a.oe_mail_fetch_more', self.on_expandable);
+ },
+
+ /*The selected thread and all childs (messages/thread) became read
+ * @param {object} mouse envent
+ */
+ on_expandable: function (event) {
+ event.stopPropagation();
+ this.parent_thread.message_fletch(false, this.domain, this.context);
+ this.destroy();
+ return false;
+ },
+ });
+
+ /**
+ * ------------------------------------------------------------
+ * Thread Message Widget
+ * ------------------------------------------------------------
+ * This widget handles the display of a messages in a thread.
+ * Displays a record and performs some formatting on the record :
+ * - record.date: formatting according to the user timezone
+ * - record.timerelative: relative time givein by timeago lib
+ * - record.avatar: image url
+ * - record.attachment_ids[].url: url of each attachmentThe
+ * [thread_level] parameter sets the thread level number:
+ * - root thread
* - - sub message (parent_id = root message)
- * - - - sub sub message (parent id = sub message)
+ * - - - sub thread
+ * - - - - sub sub message (parent id = sub thread)
* - - sub message (parent_id = root message)
+ * - - - sub thread
*/
-
- mail.Thread = session.web.Widget.extend({
- template: 'mail.thread',
+ mail.ThreadMessage = session.web.Widget.extend({
+ template: 'mail.thread.message',
/**
* @param {Object} parent parent
@@ -257,66 +320,99 @@ openerp.mail = function(session) {
contain at least default_model, default_res_id. Please refer to
the ComposeMessage widget for more information about it.
* @param {Object} [options]
- * @param {Number} [options.message_ids=null] ids for message_fetch
- * @param {Number} [options.message_data=null] already formatted message
- data, for subthreads getting data from their parent
- * @param {Number} [options.thread_level=0] number of thread levels
- * @param {Boolean} [options.use_composer] use the advanced composer, or
- the default basic textarea if not set
- * @param {Number} [options.truncate_limit=250] number of character to
- * display before having a "show more" link; note that the text
- * will not be truncated if it does not have 110% of the parameter
+ * @param {Object} [thread] read obout mail.Thread object
+ * @param {Object} [message]
+ * @param {Number} [message_ids=null] ids for message_fletch
+ * @param {Number} [message_data=null] already formatted message data,
+ * for subthreads getting data from their parent
+ * @param {Number} [truncate_limit=250] number of character to
+ * display before having a "show more" link; note that the text
+ * will not be truncated if it does not have 110% of the parameter
+ * @param {Boolean} [show_record_name]
+ * @param {Boolean} [show_reply]
+ * @param {Boolean} [show_reply_by_email]
+ * @param {Boolean} [show_dd_delete]
+ * @param {Boolean} [show_dd_hide]
*/
- init: function(parent, domain, context, options) {
+ init: function(parent, options) {
this._super(parent);
- this.domain = domain || [];
+ this.domain = options.domain || [];
this.context = _.extend({
default_model: 'mail.thread',
default_res_id: 0,
- default_parent_id: false }, context || {});
+ default_parent_id: false }, options.context || {});
+
// options
- this.options = {
- message_ids: options.message_ids || null,
- message_data: options.message_data || null,
- thread_level: options.thread_level || 0,
- use_composer: options.use_composer || false,
- show_header_compose: options.show_header_compose != undefined ? options.show_header_compose: true,
- show_record_name: options.show_record_name != undefined ? options.show_record_name: true,
- show_reply: options.show_reply || false,
- show_reply_by_email: options.show_reply_by_email || false,
- show_dd_reply_by_email:options.show_dd_reply_by_email != undefined ? options.show_dd_reply_by_email: true,
- show_dd_delete: options.show_dd_delete || false,
- show_dd_hide: options.show_dd_hide || false,
- show_more: options.show_more || false,
- truncate_limit: options.truncate_limit || 250,
+ this.options={
+ 'thread' : options.options.thread,
+ 'message' : {
+ 'message_ids': options.options.message.message_ids || null,
+ 'message_data': options.options.message.message_data || null,
+ 'show_record_name': options.options.message.show_record_name != undefined ? options.options.message.show_record_name: true,
+ 'show_reply': options.options.message.show_reply || false,
+ 'show_reply_by_email': options.options.message.show_reply_by_email || false,
+ 'show_dd_delete': options.options.message.show_dd_delete || false,
+ 'show_dd_hide': options.options.message.show_dd_hide || false,
+ 'truncate_limit': options.options.message.truncate_limit || 250,
+ }
+ };
+ // record options and data
+ this.parent_thread= parent.messages!= undefined ? parent : options.options.thread._parents[0] ;
+
+ var param = options.parameters;
+ // record parameters
+ this.id = param.id || -1;
+ this.model = param.model || false;
+ this.parent_id= param.parent_id || false;
+ this.res_id = param.res_id || false;
+ this.type = param.type || false;
+ this.is_author = param.is_author || false;
+ this.subject = param.subject || false;
+ this.name = param.name || false;
+ this.record_name = param.record_name || false;
+ this.body = param.body || false;
+ this.vote_user_ids =param.vote_user_ids || [];
+ this.has_voted = param.has_voted || false;
+
+ this.vote_user_ids = param.vote_user_ids || [];
+
+ this.unread = param.unread || false;
+ this._date = param.date;
+ this.author_id = param.author_id || [];
+ this.attachment_ids = param.attachment_ids || [];
+
+ this.thread = false;
+
+ if( param.id > 0 ) {
+ this.formating_data();
}
- // datasets and internal vars
- this.records = {};
- this.ds_thread = new session.web.DataSetSearch(this, this.context.default_model);
+
this.ds_notification = new session.web.DataSetSearch(this, 'mail.notification');
this.ds_message = new session.web.DataSetSearch(this, 'mail.message');
},
+
+ formating_data: function(){
+
+ //formating and add some fields for render
+ this.date = session.web.format_value(this._date, {type:"datetime"});
+ this.timerelative = $.timeago(this.date);
+ if (this.type == 'email') {
+ this.avatar = ('/mail/static/src/img/email_icon.png');
+ } else {
+ this.avatar = mail.ChatterUtils.get_image(this.session, 'res.partner', 'image_small', this.author_id[0]);
+ }
+ for (var l in this.attachment_ids) {
+ var attach = this.attachment_ids[l];
+ attach['url'] = mail.ChatterUtils.get_attachment_url(this.session, attach);
+ }
+ },
start: function() {
- // TDE TODO: check for deferred, not sure it is correct
this._super.apply(this, arguments);
+ this.expender();
+ this.$el.hide().fadeIn(750);
this.bind_events();
- // fetch and display message, using message_ids if set
- var display_done = $.when(this.message_fetch(true, [], {})).then(this.proxy('do_customize_display'));
- // add message composition form view
- if (this.options.show_header_compose && this.options.use_composer) {
- var compose_done = this.instantiate_composition_form();
- }
- return display_done && compose_done;
- },
-
- /** Customize the display
- * - show_header_compose: show the composition form in the header */
- do_customize_display: function() {
- this.display_user_avatar();
- if (this.options.show_header_compose) {
- this.$el.find('div.oe_mail_thread_action').eq(0).show();
- }
+ this.create_thread();
},
/**
@@ -324,281 +420,569 @@ openerp.mail = function(session) {
* in the function. */
bind_events: function() {
var self = this;
- // event: click on 'More' at bottom of thread
- this.$el.on('click', 'button.oe_mail_button_more', this.do_message_fetch_more);
- // event: writing in basic textarea of composition form (quick reply)
- this.$el.find('textarea.oe_mail_compose_textarea').keyup(function (event) {
- var charCode = (event.which) ? event.which : window.event.keyCode;
- if (event.shiftKey && charCode == 13) { this.value = this.value+"\n"; }
- else if (charCode == 13) { return self.message_post(); }
- });
- // event: click on 'Reply' in msg
- this.$el.on('click', 'a.oe_mail_msg_reply', function (event) {
- var act_dom = $(this).parents('li.oe_mail_thread_msg').eq(0).find('div.oe_mail_thread_action:first');
- act_dom.toggle();
- });
// event: click on 'Attachment(s)' in msg
this.$el.on('click', 'a.oe_mail_msg_view_attachments', function (event) {
var act_dom = $(this).parent().parent().parent().find('.oe_mail_msg_attachments');
act_dom.toggle();
});
+ // event: click on icone 'Read' in header
+ this.$el.on('click', 'a.oe_read', this.on_message_read_unread);
+ // event: click on icone 'UnRead' in header
+ this.$el.on('click', 'a.oe_unread', this.on_message_read_unread);
// event: click on 'Delete' in msg side menu
this.$el.on('click', 'a.oe_mail_msg_delete', this.on_message_delete);
- // event: click on 'Hide' in msg side menu
- this.$el.on('click', 'a.oe_mail_msg_hide', this.on_message_read);
+
+ // event: click on 'Reply' in msg
+ this.$el.on('click', 'a.oe_reply', this.on_message_reply);
// event: click on 'Reply by email' in msg side menu
- this.$el.on('click', 'a.oe_mail_msg_reply_by_email', function (event) {
- if (! self.compose_message_widget) return true;
- var msg_id = event.srcElement.dataset.msg_id;
- if (! msg_id) return false;
- self.compose_message_widget.refresh({
- 'default_composition_mode': 'reply',
- 'default_parent_id': parseInt(msg_id),
- 'default_content_subtype': 'html'} );
- });
+ this.$el.on('click', 'a.oe_reply_by_email', this.on_message_reply_by_mail);
// event: click on 'Vote' button
this.$el.on('click', 'button.oe_mail_msg_vote', this.on_vote);
},
+ on_message_reply:function(event){
+ event.stopPropagation();
+ this.thread.on_compose_message($(event.srcElement).hasClass("oe_full_reply"), false);
+ return false;
+ },
+
+ on_message_reply_by_mail:function(event){
+ event.stopPropagation();
+ this.thread.on_compose_message(true, true);
+ return false;
+ },
+
+ expender: function(){
+ this.$('div.oe_mail_msg_body:first').expander({
+ slicePoint: this.options.truncate_limit,
+ expandText: 'read more',
+ userCollapseText: '[^]',
+ detailClass: 'oe_mail_msg_tail',
+ moreClass: 'oe_mail_expand',
+ lessClass: 'oe_mail_reduce',
+ });
+ },
+
+ create_thread: function(){
+ var self=this;
+ if(this.thread){
+ return false;
+ }
+ /*create thread*/
+ self.thread = new mail.Thread(self, {
+ 'domain': self.domain,
+ 'context':{
+ 'default_model': self.model,
+ 'default_res_id': self.res_id,
+ 'default_parent_id': self.id
+ },
+ 'options': {
+ 'thread' : self.options.thread,
+ 'message' : self.options.message
+ },
+ 'parameters':{
+ 'model': self.model,
+ 'id': self.id,
+ 'parent_id': self.id
+ }
+ }
+ );
+ /*insert thread in parent message*/
+ self.thread.appendTo(self.$el.find('div.oe_thread_placeholder'));
+ },
+
+ animated_destroy: function(options) {
+ var self=this;
+ //graphic effects
+ if(options && options.fadeTime) {
+ self.$el.fadeOut(options.fadeTime, function(){
+ self.destroy();
+ });
+ } else {
+ self.destroy();
+ }
+ for(var i in this.thread.messages){
+ this.thread.messages[i].animated_destroy({fadeTime:0});
+ }
+ },
+
on_message_delete: function (event) {
+ event.stopPropagation();
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;
- $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).remove();
- return this.ds_message.unlink([parseInt(msg_id)]);
+
+ this.animated_destroy({fadeTime:250});
+ // delete this message and his childs
+ var ids = [this.id].concat( this.get_child_ids() );
+ this.ds_message.unlink(ids);
+ this.animated_destroy();
+ return false;
},
- on_message_read: function (event) {
- //TDE: TODO
- var msg_id = event.srcElement.dataset.id;
- if (! msg_id) return false;
- $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).remove();
- return this.ds_notification.call('set_message_read', [parseInt(msg_id)]);
+ /*The selected thread and all childs (messages/thread) became read
+ * @param {object} mouse envent
+ */
+ on_message_read_unread: function (event) {
+ event.stopPropagation();
+ this.animated_destroy({fadeTime:250});
+ // if this message is read, all childs message display is read
+ var ids = [this.id].concat( this.get_child_ids() );
+ this.ds_notification.call('set_message_read', [ids,$(event.srcElement).hasClass("oe_read")]);
+ return false;
},
+ /** browse message
+ * @param {object}{int} option.id
+ * @param {object}{string} option.model
+ * @param {object}{boolean} option._go_thread_wall
+ * private for check the top thread
+ * @return thread object
+ */
+ browse_message: function(options){
+ // goto the wall thread for launch browse
+ if(!options._go_thread_wall) {
+ options._go_thread_wall = true;
+ for(var i in this.options.thread._parents[0].messages){
+ var res=this.options.thread._parents[0].messages[i].browse_message(options);
+ if(res) return res;
+ }
+ }
+
+ if(this.id==options.id)
+ return this;
+
+ for(var i in this.thread.messages){
+ if(this.thread.messages[i].thread){
+ var res=this.thread.messages[i].browse_message(options);
+ if(res) return res;
+ }
+ }
+
+ return false;
+ },
+
+ /* get all child message/thread id linked
+ */
+ get_child_ids: function(){
+ var res=[]
+ if(arguments[0]) res.push(this.id);
+ if(this.thread){
+ res = res.concat( this.thread.get_child_ids(true) );
+ }
+ return res;
+ },
+
+
on_vote: function (event) {
event.stopPropagation();
+ var self=this;
+ return this.ds_message.call('vote_toggle', [[self.id]]).pipe(function(vote){
+
+ self.has_voted=vote;
+ if (!self.has_voted) {
+ var votes=[];
+ for(var i in self.vote_user_ids){
+ if(self.vote_user_ids[i][0]!=self.session.uid)
+ vote.push(self.vote_user_ids[i]);
+ }
+ self.vote_user_ids=votes;
+ }
+ else {
+ self.vote_user_ids.push([self.session.uid, 'You']);
+ }
+ self.display_vote();
+ });
+ return false;
+ },
+
+ // Render vote Display template.
+ display_vote: function () {
var self = this;
- var message_id = $(event.srcElement).parent().data().msg_id;
- var vote_node = $(event.srcElement).parents('li').eq(0);
- if (! message_id) { return false; }
- return this.ds_message.call('vote_toggle', [[parseInt(message_id)]]).pipe(
- self.toggle_vote(message_id, vote_node));
+ var vote_element = session.web.qweb.render('mail.thread.message.vote', {'widget': self});
+ self.$(".placeholder-mail-vote").empty();
+ self.$(".placeholder-mail-vote").html(vote_element);
},
+ });
+
+ /**
+ * ------------------------------------------------------------
+ * 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)
+ */
+ mail.Thread = session.web.Widget.extend({
+ template: 'mail.thread',
/**
- * Override-hack of do_action: automatically reload the chatter.
- * Normally it should be called only when clicking on 'Post/Send'
- * in the composition form. */
+ * @param {Object} parent parent
+ * @param {Array} [domain]
+ * @param {Object} [context] context of the thread. It should
+ contain at least default_model, default_res_id. Please refer to
+ the ComposeMessage widget for more information about it.
+ * @param {Object} [options]
+ * @param {Object} [message] read about mail.ThreadMessage object
+ * @param {Object} [thread]
+ * @param {Number} [thread_level=0] number of thread levels
+ * @param {Boolean} [use_composer] use the advanced composer, or
+ * the default basic textarea if not set
+ * @param {Number} [expandable_number=5] number message show
+ * for each click on "show more message"
+ * @param {Number} [expandable_default_number=5] number message show
+ * on begin before the first click on "show more message"
+ * @param {Boolean} [display_on_flat] display all thread
+ * on the wall thread level (no hierarchy)
+ * @param {Array} [parents] liked with the parents thread
+ * use with browse, fletch... [O]= top parent
+ */
+ init: function(parent, options) {
+ this._super(parent);
+ this.domain = options.domain || [];
+ this.context = _.extend({
+ default_model: 'mail.thread',
+ default_res_id: 0,
+ default_parent_id: false }, options.context || {});
+
+ // options
+ this.options={
+ 'thread' : {
+ 'thread_level': options.options.thread.thread_level || 0,
+ 'show_header_compose': (options.options.thread.show_header_compose != undefined ? options.options.thread.show_header_compose: false),
+ 'use_composer': options.options.thread.use_composer || false,
+ 'expandable_number': options.options.thread.expandable_number || 5,
+ 'expandable_default_number': options.options.thread.expandable_default_number || 5,
+ '_expandable_max': options.options.thread.expandable_default_number || 5,
+ 'display_on_flat': options.options.thread.display_on_flat || false,
+ '_parents': (options.options.thread._parents != undefined ? options.options.thread._parents : []).concat( [this] )
+ },
+ 'message' : options.options.message
+ };
+
+ // record options and data
+ this.parent_linked_message= parent.thread!= undefined ? parent : false ;
+
+ var param = options.parameters
+ // datasets and internal vars
+ this.id= param.id || false;
+ this.model= param.model || false;
+ this.parent_id= param.parent_id || false;
+
+ this.messages = [];
+
+ this.ds_thread = new session.web.DataSetSearch(this, this.context.default_model);
+ this.ds_message = new session.web.DataSetSearch(this, 'mail.message');
+ },
+
+ start: function() {
+ // TDE TODO: check for deferred, not sure it is correct
+ this._super.apply(this, arguments);
+
+ this.list_ul=this.$('ul.oe_mail_thread_display:first');
+ this.more_msg=this.$(">.oe_mail_msg_more_message:first");
+
+ this.display_user_avatar();
+ var display_done = compose_done = false;
+
+ // add message composition form view
+ compose_done = this.instantiate_composition_form();
+
+ this.bind_events();
+
+ if(this.options.thread._parents[0]==this){
+ this.on_first_thread();
+ }
+
+ return display_done && compose_done;
+ },
+
+ /**
+ * Override-hack of do_action: automatically load message on the chatter.
+ * Normally it should be called only when clicking on 'Post/Send'
+ * in the composition form. */
do_action: function(action, on_close) {
- //TDE: TODO: instead of reloading, push the message ?
- this.message_clean();
- this.message_fetch();
- if (this.compose_message_widget) {
- this.compose_message_widget.refresh({
- 'default_composition_mode': 'comment',
- 'default_parent_id': this.context.default_parent_id,
- 'default_content_subtype': 'plain'} );
+ this.instantiate_composition_form();
+ this.message_fletch(false, false, false, [action.id]);
+ return this._super(action, on_close);
+ },
+
+ /* this method is runing for first parent thread
+ */
+ on_first_thread: function(){
+ // fetch and display message, using message_ids if set
+ display_done = this.message_fletch(true);
+ //show the first write message
+ this.$(">.oe_mail_thread_action").show();
+ },
+
+ /**
+ * Bind events in the widget. Each event is slightly described
+ * in the function. */
+ bind_events: function() {
+ var self = this;
+ // event: writing in basic textarea of composition form (quick reply)
+ // event: onblur for hide 'Reply'
+ this.$('.oe_mail_compose_textarea:first textarea')
+ .keyup(function (event) {
+ var charCode = (event.which) ? event.which : window.event.keyCode;
+ if (event.shiftKey && charCode == 13) { this.value = this.value+"\n"; }
+ else if (charCode == 13) { return self.message_post(); }
+ })
+ .blur(function (event) {
+ $(this).parents('.oe_mail_thread_action:first').hide();
+ });
+ },
+
+ /* get all child message/thread id linked
+ */
+ get_child_ids: function(){
+ var res=[];
+ for(var i in this.messages){
+ if(this.messages[i].thread){
+ res = res.concat( this.messages[i].get_child_ids(true) );
+ }
+ }
+ return res;
+ },
+
+ /** browse thread
+ * @param {object}{int} option.id
+ * @param {object}{string} option.model
+ * @param {object}{boolean} option._go_thread_wall
+ * private for check the top thread
+ * @param {object}{boolean} option.default_return_top_thread
+ * return the top thread (wall) if no thread found
+ * @return thread object
+ */
+ browse_thread: function(options){
+ // goto the wall thread for launch browse
+ if(!options._go_thread_wall) {
+ options._go_thread_wall = true;
+ return this.options.thread._parents[0].browse_thread(options);
+ }
+
+ if(this.id==options.id){
+ return this;
}
- // return this._super(action, on_close);
+
+ if(options.id)
+ for(var i in this.messages){
+ if(this.messages[i].thread){
+ var res=this.messages[i].thread.browse_thread({'id':options.id, '_go_thread_wall':true});
+ if(res) return res;
+ }
+ }
+
+ //if option default_return_top_thread, return the top if no found thread
+ if(options.default_return_top_thread){
+ return this;
+ }
+
+ return false;
+ },
+
+ /** browse message
+ * @param {object}{int} option.id
+ * @param {object}{string} option.model
+ * @param {object}{boolean} option._go_thread_wall
+ * private for check the top thread
+ * @return thread object
+ */
+ browse_message: function(options){
+ if(this.options.thread._parents[0].messages[0])
+ return this.options.thread._parents[0].messages[0].browse_message(options);
},
/** Instantiate the composition form, with every parameters in context
- or in the widget context. */
+ * or in the widget context.
+ * @param {object}
+ * @param {boolean} show_header_compose, force to instantiate form
+ */
instantiate_composition_form: function(context) {
+ // add message composition form view
+ if ((!context || !context.show_header_compose) &&
+ (!this.options.thread.show_header_compose || !this.options.thread.use_composer ||
+ (this.options.thread.show_header_compose <= this.options.thread._parents.length && this.options.thread._parents[0]!=this))) {
+ this.$("textarea:first").val("");
+ return false;
+ }
if (this.compose_message_widget) {
- this.compose_message_widget.destroy();
+ this.compose_message_widget.refresh();
+ } else {
+ var context = _.extend(context || {}, this.context);
+ this.compose_message_widget = new mail.ComposeMessage(this, {'context': context});
+ var composition_node = this.$('div.oe_mail_thread_action');
+ composition_node.empty();
+ return this.compose_message_widget.appendTo(composition_node);
}
- this.compose_message_widget = new mail.ComposeMessage(this, {
- 'context': _.extend(context || {}, this.context),
- });
- var composition_node = this.$el.find('div.oe_mail_thread_action');
- composition_node.empty();
- var compose_done = this.compose_message_widget.appendTo(composition_node);
- return compose_done;
},
- refresh_composition_form: function (context) {
- if (! this.compose_message_widget) return;
- return this.compose_message_widget.refresh(context);
+ /* this function is launch when a user click on "Reply" button
+ */
+ on_compose_message: function(full_reply, by_mail){
+ if(full_reply){
+ this.instantiate_composition_form({'show_header_compose':true});
+ }
+ if(by_mail){
+ if (!this.compose_message_widget) return true;
+ this.compose_message_widget.refresh({
+ 'default_composition_mode': 'reply',
+ 'default_parent_id': this.id,
+ 'default_content_subtype': 'html'} );
+ }
+ this.$('div.oe_mail_thread_action:first').toggle();
+ return false;
+ },
+
+ refresh: function (action_context) {
+ var self=this;
+ _(this.messages).each(function(){ self.destroy(); });
+ self.message_fletch();
},
- /** Clean the thread */
- message_clean: function() {
- this.$el.find('div.oe_mail_thread_display').empty();
+ /*post a message and fletch the message*/
+ message_post: function (body) {
+ var self = this;
+ if (! body) {
+ var comment_node = this.$('textarea');
+ var body = comment_node.val();
+ comment_node.val('');
+ }
+ if(body.match(/\S+/)) {
+ this.ds_thread.call('message_post_api', [
+ [this.context.default_res_id], body, false, 'comment', false, this.context.default_parent_id, undefined])
+ .then(this.proxy('switch_new_message'));
+ }
+ else {
+ return false;
+ }
},
/** Fetch messages
* @param {Bool} initial_mode: initial mode: try to use message_data or
* message_ids, if nothing available perform a message_read; otherwise
* directly perform a message_read
- * @param {Array} additional_domain: added to this.domain
- * @param {Object} additional_context: added to this.context
+ * @param {Array} replace_domain: added to this.domain
+ * @param {Object} replace_context: added to this.context
*/
- message_fetch: function (initial_mode, additional_domain, additional_context) {
+ message_fletch: function (initial_mode, replace_domain, replace_context, ids) {
var self = this;
- // domain and context: options + additional
- fetch_domain = _.flatten([this.domain, additional_domain || []], true)
- fetch_context = _.extend(this.context, additional_context || {})
+
// initial mode: try to use message_data or message_ids
- if (initial_mode && this.options.message_data) {
- return this.message_display(this.options.message_data);
+ if (initial_mode && this.options.thread.message_data) {
+ return this.create_message_object(this.options.message_data);
}
- message_ids = initial_mode && this.options.message_ids != null && this.options.message_ids || false;
- return this.ds_message.call('message_read', [message_ids, fetch_domain, this.options.thread_level, undefined, fetch_context]
- ).then(this.proxy('message_display'));
+ // domain and context: options + additional
+ fetch_domain = replace_domain ? replace_domain : this.domain;
+ fetch_context = replace_context ? replace_context : this.context;
+ fetch_context.message_loaded= [this.id||0].concat( self.options.thread._parents[0].get_child_ids() );
+
+ return this.ds_message.call('message_read', [ids, fetch_domain, (this.options.thread.thread_level+1), fetch_context, this.context.default_parent_id || undefined]
+ ).then(this.proxy('switch_new_message'));
},
- /* Display a list of records
- * A specific case is done for 'expandable' messages that are messages
- displayed under a 'show more' button form
+ /* create record object and linked him
*/
- message_display: function (records) {
+ create_message_object: function (message) {
var self = this;
- var _expendable = false;
- _(records).each(function (record) {
- if (record.type == 'expandable') {
- _expendable = true;
- self.update_fetch_more(true);
- self.fetch_more_domain = record.domain;
- self.fetch_more_context = record.context;
- }
- else {
- self.display_record(record);
- self.thread = new mail.Thread(self, self.domain,
- { 'default_model': record.model,
- 'default_res_id': record.res_id,
- 'default_parent_id': record.id },
- { 'message_data': record.child_ids,
- 'thread_level': self.options.thread_level - 1,
- 'show_header_compose': false,
- 'show_reply': self.options.show_reply && self.options.thread_level > 1,
- 'show_reply_by_email': self.options.show_reply_by_email,
- 'show_dd_hide': self.options.show_dd_hide,
- 'show_dd_delete': self.options.show_dd_delete });
- self.$el.find('li.oe_mail_thread_msg:last').append('<div class="oe_mail_thread_subthread"/>');
- self.thread.appendTo(self.$el.find('div.oe_mail_thread_subthread:last'));
+
+ // check if the message is already create
+ for(var i in this.messages){
+ if(this.messages[i].id==message.id){
+ this.messages[i].destroy();
+ this.messages[i]=self.insert_message(message);
+ return true;
}
- });
- if (! _expendable) {
- this.update_fetch_more(false);
- }
- },
-
- /** Displays a record and performs some formatting on the record :
- * - record.date: formatting according to the user timezone
- * - record.timerelative: relative time givein by timeago lib
- * - record.avatar: image url
- * - record.attachment_ids[].url: url of each attachment
- * - record.is_author: is the current user the author of the record */
- display_record: function (record) {
- // formatting and additional fields
- record.date = session.web.format_value(record.date, {type:"datetime"});
- record.timerelative = $.timeago(record.date);
- if (record.type == 'email') {
- record.avatar = ('/mail/static/src/img/email_icon.png');
- } else {
- record.avatar = mail.ChatterUtils.get_image(this.session, 'res.partner', 'image_small', record.author_id[0]);
}
- for (var l in record.attachment_ids) {
- var attach = record.attachment_ids[l];
- attach['url'] = mail.ChatterUtils.get_attachment_url(this.session, attach);
- }
- record.is_author = mail.ChatterUtils.is_author(this, record.author_user_id[0]);
- // add to internal storage
- this.records[record.id] = record;
- // render, add the expand feature
- var rendered = session.web.qweb.render('mail.thread.message', {'record': record, 'thread': this, 'options': this.options});
- $(rendered).appendTo(this.$el.children('div.oe_mail_thread_display:first'));
- this.$el.find('div.oe_mail_msg_body').expander({
- slicePoint: this.options.truncate_limit,
- expandText: 'read more',
- userCollapseText: '[^]',
- detailClass: 'oe_mail_msg_tail',
- moreClass: 'oe_mail_expand',
- lessClass: 'oe_mail_reduce',
- });
+
+ self.messages.push( self.insert_message(message) );
+
},
- // Render vote Display template.
- toggle_vote: function (message_id, vote_node) {
- var self = this;
- var record = this.records[message_id];
- if (record.has_voted) {
- var idx = _.map(record.vote_user_ids, function (x) { return x[0]; }).indexOf(message_id);
- record.vote_user_ids.splice(idx, 1);
+ /** Displays a message or an expandable message */
+ insert_message: function (message) {
+ var self=this;
+
+ if(message.type=='expandable'){
+ var message = new mail.ThreadExpandable(self, {
+ 'domain': message.domain,
+ 'context': {
+ 'default_model': message.model,
+ 'default_res_id': message.res_id,
+ 'default_parent_id': message.id },
+ 'parameters': message
+ });
+ } else {
+ var message = new mail.ThreadMessage(self, {
+ 'domain': message.domain,
+ 'context': {
+ 'default_model': message.model,
+ 'default_res_id': message.res_id,
+ 'default_parent_id': message.id },
+ 'options':{
+ 'thread': self.options.thread,
+ 'message': self.options.message
+ },
+ 'parameters': message
+ });
}
- else {
- record.vote_user_ids.push([this.session.uid, 'You']);
+
+ var thread = self.options.thread.display_on_flat ? self.options.thread._parents[0] : this;
+
+ // check older and newer message for insert
+ var parent_newer = false;
+ var parent_older = false;
+ for(var i in thread.messages){
+ if(thread.messages[i].id > message.id){
+ if(!parent_newer || parent_newer.id>thread.messages[i].id)
+ parent_newer = thread.messages[i];
+ } else if(thread.messages[i].id>0 && thread.messages[i].id < message.id) {
+ if(!parent_older || parent_older.id<thread.messages[i].id)
+ parent_older = thread.messages[i];
+ }
}
- record.has_voted = ! record.has_voted;
- var vote_element = session.web.qweb.render('mail.thread.message.vote', {'record': record});
- vote_node.empty();
- vote_node.html(vote_element);
+
+ if(parent_newer)
+ message.insertAfter(parent_newer.$el);
+ else if(parent_older)
+ message.insertBefore(parent_older.$el);
+ else
+ message.prependTo(thread.list_ul);
+
+ return message
},
- /** Display 'show more' button */
- update_fetch_more: function (new_value) {
- if (new_value) {
- this.$el.find('div.oe_mail_thread_more:last').show();
+ /* Hide messages if they are more message that _expandable_max
+ * display "show more messages"
+ */
+ display_expandable: function(){
+ if(this.messages.length>this.options._expandable_max){
+ this.list_ul.find('>li:gt('+(this.options._expandable_max-1)+')').hide();
+ this.more_msg.show();
} else {
- this.$el.find('div.oe_mail_thread_more:last').hide();
+ this.list_ul.find('>li').show();
+ this.more_msg.hide();
}
},
display_user_avatar: function () {
var avatar = mail.ChatterUtils.get_image(this.session, 'res.users', 'image_small', this.session.uid);
- return this.$el.find('img.oe_mail_icon').attr('src', avatar);
+ return this.$('img.oe_mail_icon').attr('src', avatar);
},
- message_post: function (body) {
- var self = this;
- if (! body) {
- var comment_node = this.$el.find('textarea');
- var body = comment_node.val();
- comment_node.val('');
- }
- return this.ds_thread.call('message_post', [
- [this.context.default_res_id], body, false, 'comment', this.context.default_parent_id, undefined]
- ).then(self.message_fetch());
- },
-
- /** Action: 'shows more' to fetch new messages */
- do_message_fetch_more: function () {
- return this.message_fetch(false, this.fetch_more_domain, this.fetch_more_context);
- },
-
- // TDE: keep currently because need something similar
- // /**
- // * Create a domain to fetch new comments according to
- // * comment already present in comments_structure
- // * @param {Object} comments_structure (see chatter utils)
- // * @returns {Array} fetch_domain (OpenERP domain style)
- // */
- // get_fetch_domain: function (comments_structure) {
- // var domain = [];
- // var ids = comments_structure.root_ids.slice();
- // var ids2 = [];
- // // must be child of current parent
- // if (this.options.parent_id) { domain.push(['id', 'child_of', this.options.parent_id]); }
- // _(comments_structure.root_ids).each(function (id) { // each record
- // ids.push(id);
- // ids2.push(id);
- // });
- // if (this.options.parent_id != false) {
- // ids2.push(this.options.parent_id);
- // }
- // // must not be children of already fetched messages
- // if (ids.length > 0) {
- // domain.push('&');
- // domain.push('!');
- // domain.push(['id', 'child_of', ids]);
- // }
- // if (ids2.length > 0) {
- // domain.push(['id', 'not in', ids2]);
- // }
- // return domain;
- // },
+ /* Send the records to his parent thread */
+ switch_new_message: function(records) {
+ var self=this;
+ _(records).each(function(record){
+ self.browse_thread({
+ 'id': record.parent_id,
+ 'default_return_top_thread':true
+ }).create_message_object( record );
+ });
+ },
});
@@ -637,7 +1021,7 @@ openerp.mail = function(session) {
var self = this;
this._super.apply(this, arguments);
if (! this.view.datarecord.id || session.web.BufferedDataSet.virtual_id_regex.test(this.view.datarecord.id)) {
- this.$el.find('oe_mail_thread').hide();
+ this.$('oe_mail_thread').hide();
return;
}
// update context
@@ -647,13 +1031,33 @@ openerp.mail = function(session) {
// update domain
var domain = this.options.domain.concat([['model', '=', this.view.model], ['res_id', '=', this.view.datarecord.id]]);
// create and render Thread widget
- this.$el.find('div.oe_mail_recthread_main').empty();
- var thread = new mail.Thread(self, domain, this.options.context,
- { 'thread_level': this.options.thread_level,
- 'use_composer': true,
- 'show_dd_delete': true,
- 'show_reply_by_email': true });
- return thread.appendTo(this.$el.find('div.oe_mail_recthread_main'));
+ var show_header_compose = this.view.is_action_enabled('edit') ||
+ (this.getParent().fields.message_is_follower && this.getParent().fields.message_is_follower.get_value());
+
+ this.thread = new mail.Thread(self, {
+ 'domain': domain,
+ 'context': this.options.context,
+ 'options':{
+ 'thread':{
+ 'thread_level': this.options.thread_level,
+ 'show_header_compose': show_header_compose,
+ 'use_composer': show_header_compose,
+ 'display_on_flat':true
+ },
+ 'message':{
+ 'show_dd_delete': true,
+ 'show_reply_by_email': show_header_compose,
+ }
+ },
+ 'parameters': {},
+ }
+ );
+
+ this.$('ul.oe_mail_wall_threads').empty();
+ var render_res = session.web.qweb.render('mail.wall_thread_container', {});
+ $(render_res).appendTo(this.$('ul.oe_mail_wall_threads'));
+
+ return this.thread.appendTo( this.$('li.oe_mail_wall_thread:last') );
},
});
@@ -705,7 +1109,7 @@ openerp.mail = function(session) {
load_searchview: function (defaults, hidden) {
var self = this;
this.searchview = new session.web.SearchView(this, this.ds_msg, false, defaults || {}, hidden || false);
- return this.searchview.appendTo(this.$el.find('.oe_view_manager_view_search')).then(function () {
+ return this.searchview.appendTo(this.$('.oe_view_manager_view_search')).then(function () {
self.searchview.on_search.add(self.do_searchview_search);
});
},
@@ -726,25 +1130,41 @@ openerp.mail = function(session) {
}, function (results) {
self.search_results['context'] = results.context;
self.search_results['domain'] = results.domain;
- self.search_results['groupby'] = results.group_by;
+ self.thread.destroy();
return self.message_render();
});
},
/** Clean and display the threads */
- message_render: function () {
- this.$el.find('ul.oe_mail_wall_threads').empty();
+ message_render: function (search) {
var domain = this.options.domain.concat(this.search_results['domain']);
- var render_res = session.web.qweb.render('mail.wall_thread_container', {});
- $(render_res).appendTo(this.$el.find('ul.oe_mail_wall_threads'));
- var thread = new mail.Thread(this, domain, this.options.context,
- { 'thread_level': this.options.thread_level,
- 'use_composer': true,
- 'show_reply': this.options.thread_level > 0,
- 'show_dd_hide': true,
+
+ var domain = _.extend(this.options.domain, search&&search.domain? search.domain : {});
+ var context = _.extend(this.options.context, search&&search.context ? search.context : {});
+
+ this.thread = new mail.Thread(this, {
+ 'domain' : domain,
+ 'context' : context,
+ 'options': {
+ 'thread' :{
+ 'thread_level': this.options.thread_level,
+ 'use_composer': true,
+ 'show_header_compose': 1,
+ },
+ 'message': {
+ 'show_reply': this.options.thread_level > 0,
+ 'show_dd_hide': true,
+ },
+ },
+ 'parameters': {},
}
);
- return thread.appendTo(this.$el.find('li.oe_mail_wall_thread:last'));
+
+ this.$('ul.oe_mail_wall_threads').empty();
+ var render_res = session.web.qweb.render('mail.wall_thread_container', {});
+ $(render_res).appendTo(this.$('ul.oe_mail_wall_threads'));
+
+ return this.thread.appendTo( this.$('li.oe_mail_wall_thread:last') );
},
});
};