[IMP]mail: read_more with expandable

bzr revid: chm@openerp.com-20121002134843-fzci9tizw6pxsra4
This commit is contained in:
Christophe Matthieu 2012-10-02 15:48:43 +02:00
parent 8cafaceb4a
commit 1ab943d7f0
3 changed files with 205 additions and 145 deletions

View File

@ -314,14 +314,14 @@ class mail_message(osv.Model):
if not ids:
ids = self.search(cr, SUPERUSER_ID, domain, context=context, limit=limit)
# if the user can read a message, he can read all the thread
ids = ids + self.search(cr, SUPERUSER_ID, [['parent_id','in',ids],['id','not in',message_loaded]], None, limit=limit)
messages = self.browse(cr, uid, ids, context=context)
add_expandable = (len(messages) >= limit)
# key: ID, value: record
tree = []
result = []
record = None
for msg in messages:
# if not in record and not in message_loded list
if msg.id not in tree and msg.id not in message_loaded :
@ -339,8 +339,57 @@ class mail_message(osv.Model):
record = self._message_dict_get(cr, uid, msg, context=context)
result.append(record)
# Flatten the result
#result2 = self.message_read_tree_flatten(cr, uid, None, result, [], level, context=context, limit=limit, add_expandable=add_expandable)
result = sorted(result, key=lambda k: k['id'])
# expandable for not show message
for id_msg in tree:
# get all childs
not_loaded_ids = self.search(cr, SUPERUSER_ID, [['parent_id','=',id_msg],['id','not in',message_loaded]], None, limit=limit)
# group childs not read
id_min=None
id_max=None
nb=0
for not_loaded_id in not_loaded_ids:
if not_loaded_id not in tree:
nb+=1
if id_min==None or id_min>not_loaded_id:
id_min=not_loaded_id
if id_max==None or id_max<not_loaded_id:
id_max=not_loaded_id
else:
if nb>0:
result.append({
'domain': [['id','>=',id_min],['id','<=',id_max],['parent_id','=',id_msg]],
'nb_messages': nb,
'type': 'expandable',
'parent_id': id_msg,
'id': id_min
})
nb=0
if nb>0:
result.append({
'domain': [['id','>=',id_min],['parent_id','=',id_msg]],
'nb_messages': nb,
'type': 'expandable',
'parent_id': id_msg,
'id': id_min
})
# expandable for limit max
ids = self.search(cr, SUPERUSER_ID, domain+[['id','not in',message_loaded]], context=context, limit=1)
if len(ids) > 0:
result.append(
{
'domain': domain,
'nb_messages': 0,
'type': 'expandable',
'parent_id': parent_id,
'id': -1
});
result = sorted(result, key=lambda k: k['id'])
return result
#------------------------------------------------------

View File

@ -195,7 +195,6 @@ openerp.mail = function(session) {
* the widget without having to rebuild a complete form view.
* @param {Object} new_context: context of the refresh */
refresh: function (new_context) {
console.log("refresh");
if (! this.form_view) return;
var self = this;
this.attachments = [];
@ -206,21 +205,12 @@ openerp.mail = function(session) {
'use_template', 'template_id', 'model', 'res_id', 'parent_id', 'content_subtype'],
this.ds_compose.get_context(),
]).then( function (result) {
console.log("refresh",result);
self.form_view.on_processed_onchange({'value': result}, []);
self.attachment_ids = [];
self.display_attachments();
});
},
/*
empty: function(){
var self=this;
self.form_view.on_processed_onchange({'value': {
}}, []);
},*/
/**
* Bind events in the widget. Each event is slightly described
* in the function. */
@ -243,28 +233,76 @@ openerp.mail = function(session) {
/**
* ------------------------------------------------------------
* Thread Message Widget
* Thread Message Expandable Widget
* ------------------------------------------------------------
*
* This widget handles the display of a messages in a thread. The
* This widget handles the display the expandable message in a thread. The
* [thread_level] parameter sets the thread level number:
* - root thread
* - - sub message (parent_id = root message)
* - - - sub thread
* - - - - sub sub message (parent id = sub thread)
* - - sub message (parent_id = root message)
* - - - sub thread
* - thread
* - - visible message
* - - expandable
* - - visible message
* - - visible message
* - - expandable
*/
mail.ThreadMessage = session.web.Widget.extend({
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;
this.$('*').unbind();
// 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. The
* 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)
@ -285,7 +323,7 @@ openerp.mail = function(session) {
* @param {Object} [options]
* @param {Object} [thread] read obout mail.Thread object
* @param {Object} [message]
* @param {Number} [message_ids=null] ids for message_fetch
* @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
@ -383,8 +421,7 @@ openerp.mail = function(session) {
* in the function. */
bind_events: function() {
var self = this;
this.$('*').unbind('click');
this.$('*').unbind();
// 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');
@ -407,18 +444,14 @@ openerp.mail = function(session) {
on_message_reply:function(event){
event.stopPropagation();
var message=this.get_object_of_bind_event(event);
if(message){
message.thread.on_compose_message(true, false);
}
this.thread.on_compose_message($(event.srcElement).hasClass("oe_full_reply"), false);
return false;
},
on_message_reply_by_mail:function(event){
event.stopPropagation();
var message=this.get_object_of_bind_event(event);
if(message){
message.thread.on_compose_message(true, true);
}
this.thread.on_compose_message($(event.srcElement).hasClass("oe_full_reply"), true);
return false;
},
expender: function(){
@ -478,14 +511,13 @@ openerp.mail = function(session) {
on_message_delete: function (event) {
event.stopPropagation();
if (! confirm(_t("Do you really want to delete this message?"))) { return false; }
var message=this.get_object_of_bind_event(event);
if(message){
message.animated_destroy({fadeTime:250});
// delete this message and his childs
var ids = [message.id].concat( message.get_child_ids() );
this.ds_message.unlink(ids);
this.animated_destroy();
}
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;
},
/*The selected thread and all childs (messages/thread) became read
@ -493,14 +525,11 @@ openerp.mail = function(session) {
*/
on_message_read_unread: function (event) {
event.stopPropagation();
var message=this.get_object_of_bind_event(event);
if(message){
message.animated_destroy({fadeTime:250});
// if this message is read, all childs message display is read
var ids = [message.id].concat( message.get_child_ids() );
message.ds_notification.call('set_message_read', [ids,$(event.srcElement).hasClass("oe_read")]);
}
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
@ -514,20 +543,17 @@ openerp.mail = function(session) {
// goto the wall thread for launch browse
if(!options._go_thread_wall) {
options._go_thread_wall = true;
var top_thread = this.options.thread._parents[0];
for(var i in top_thread.messages){
var res=top_thread.messages[i].browse_message(options);
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(options.id && options.model){
if(this.id==options.id && this.model==options.model)
return this;
if(this.id==options.id)
return this;
for(var i in this.thread.messages){
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;
}
@ -551,8 +577,7 @@ openerp.mail = function(session) {
on_vote: function (event) {
event.stopPropagation();
var self=this;
var message=this.get_object_of_bind_event(event);
return this.ds_message.call('vote_toggle', [[message.id]]).pipe(function(vote){
return this.ds_message.call('vote_toggle', [[self.id]]).pipe(function(vote){
self.has_voted=vote;
if (!self.has_voted) {
@ -567,8 +592,8 @@ openerp.mail = function(session) {
self.vote_user_ids.push([self.session.uid, 'You']);
}
self.display_vote();
});
return false;
},
// Render vote Display template.
@ -578,14 +603,6 @@ openerp.mail = function(session) {
self.$(".placeholder-mail-vote").empty();
self.$(".placeholder-mail-vote").html(vote_element);
},
get_object_of_bind_event: function(event){
var source = $(event.srcElement).parents('[data-msg_id]:first');
var msg_id = source.data("msg_id");
var msg_model = source.data("msg_model");
if (!msg_id || !msg_model) return false;
return this.browse_message({'id':msg_id, 'model':msg_model});
}
});
/**
@ -690,7 +707,7 @@ openerp.mail = function(session) {
* in the composition form. */
do_action: function(action, on_close) {
this.instantiate_composition_form();
this.message_fetch();
this.message_fletch();
return this._super(action, on_close);
},
@ -698,7 +715,7 @@ openerp.mail = function(session) {
*/
on_first_thread: function(){
// fetch and display message, using message_ids if set
display_done = this.message_fetch(true, [], {});
display_done = this.message_fletch(true);
//show the first write message
this.$(">.oe_mail_thread_action").show();
},
@ -708,8 +725,6 @@ 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', 'a.oe_mail_fetch_more', this.do_message_fetch_more);
// event: writing in basic textarea of composition form (quick reply)
// event: onblur for hide 'Reply'
this.$('.oe_mail_compose_textarea:first textarea')
@ -728,7 +743,9 @@ openerp.mail = function(session) {
get_child_ids: function(){
var res=[];
for(var i in this.messages){
res = res.concat( this.messages[i].get_child_ids(true) );
if(this.messages[i].thread){
res = res.concat( this.messages[i].get_child_ids(true) );
}
}
return res;
},
@ -748,19 +765,22 @@ openerp.mail = function(session) {
options._go_thread_wall = true;
return this.options.thread._parents[0].browse_thread(options);
}
if(options.id && options.model){
if(this.id==options.id)
return this;
for(var i in this.messages){
var res=this.messages[i].thread.browse_thread(options);
if(this.id==options.id){
return this;
}
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.options.thread._parents[0];
return this;
}
return false;
@ -787,7 +807,7 @@ openerp.mail = function(session) {
// 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.options.thread.show_header_compose <= this.options.thread._parents.length && this.options.thread._parents[0]!=this))) {
this.$("textarea:first").val("");
return false;
}
@ -816,12 +836,13 @@ openerp.mail = function(session) {
'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_fetch();
self.message_fletch();
},
/*post a message and fletch the message*/
@ -847,10 +868,10 @@ openerp.mail = function(session) {
* @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) {
var self = this;
// initial mode: try to use message_data or message_ids
@ -858,13 +879,11 @@ openerp.mail = function(session) {
return this.create_message_object(this.options.message_data);
}
// domain and context: options + additional
fetch_domain = _.flatten([this.options.thread._parents[0].domain, additional_domain || []], true);
fetch_context = _.extend({}, this.context, additional_context || {});
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() );
message_ids = this.options.thread.message_ids && this.options.thread.message_ids[0] ? this.options.thread.message_ids : false;
return this.ds_message.call('message_read', [message_ids, fetch_domain, (this.options.thread.thread_level+1), fetch_context, this.context.default_parent_id || undefined]
return this.ds_message.call('message_read', [false, fetch_domain, (this.options.thread.thread_level+1), fetch_context, this.context.default_parent_id || undefined]
).then(this.proxy('switch_new_message'));
},
@ -873,10 +892,6 @@ openerp.mail = function(session) {
create_message_object: function (message) {
var self = this;
if(message.type=='expandable'){
return false;
}
// check if the message is already create
for(var i in this.messages){
if(this.messages[i].id==message.id){
@ -890,27 +905,33 @@ openerp.mail = function(session) {
},
/** 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 */
/** Displays a message or an expandable message */
insert_message: function (message) {
var self=this;
/*create message*/
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
});
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
});
}
// check older and newer message for insert
var parent_newer = false;
@ -957,28 +978,12 @@ openerp.mail = function(session) {
switch_new_message: function(records) {
var self=this;
_(records).each(function(record){
self.browse_thread({'id': (self.options.thread.display_on_flat ? record.parent_id : false), 'default_return_top_thread':true}).create_message_object( record );
self.browse_thread({
'id': (self.options.thread.display_on_flat ? false : record.parent_id),
'default_return_top_thread':true
}).create_message_object( record );
});
},
/** Action: 'shows more' to fetch new messages */
do_message_fetch_more: function (event) {
event.stopPropagation();
var thread=this.get_object_of_bind_event(event);
if(thread){
thread.options._expandable_max+=thread.options.expendable_number;
if(thread.display_expandable())
return thread.message_fetch(false, this.fetch_more_domain, this.fetch_more_context);
}
},
get_object_of_bind_event: function(event){
var source = $(event.srcElement).parents('[data-msg_id]:first');
var msg_id = source.data("msg_id");
var msg_model = source.data("msg_model");
if (!msg_id || !msg_model) return false;
return this.browse_thread({'id':msg_id, 'model':msg_model});
}
});

View File

@ -101,14 +101,10 @@
<ul class="oe_mail_thread_display">
<!-- contains the threads -->
</ul>
<!-- expandable message layout -->
<div class="oe_mail_msg_more_message" style="display:none;">
<a class="oe_mail_fetch_more">Load more messages ...</a>
</div>
</div>
<!-- default layout -->
<li t-name="mail.thread.message" t-attf-class="oe_mail oe_mail_thread_msg #{widget.unread?'oe_mail_unread':'oe_mail_read'}" t-attf-data-msg_model="{widget.model}" t-attf-data-msg_id="{widget.id}">
<li t-name="mail.thread.message" t-attf-class="oe_mail oe_mail_thread_msg #{widget.unread?'oe_mail_unread':'oe_mail_read'}" t-attf-data-msg_id="{widget.id}">
<div t-attf-class="oe_mail_msg_#{widget.type} oe_semantic_html_override">
<!-- message actions (read/unread, reply, delete...) -->
<ul class="oe_header">
@ -116,10 +112,10 @@
<t t-if="!widget.options.thread.display_on_flat">
<li t-if="widget.unread" title="Read"><a class="oe_read oe_e">W</a></li>
<li t-if="!widget.unread" title="Set back to unread"><a class="oe_unread oe_e">h</a></li>
<li t-if="widget.options.thread._parents.length&lt;=widget.options.thread.thread_level and widget.options.message.show_reply" title="Reply"><a class="oe_reply oe_e">)</a></li>
<li t-if="widget.options.message.show_reply" title="Reply"><a class="oe_reply oe_e">)</a></li>
<li t-if="widget.options.message.show_reply_by_email"><a class="oe_reply_by_email oe_e" title="Reply by mail">)</a></li>
</t>
<t t-if="widget.options.thread._parents.length&lt;=widget.options.thread.thread_level and (widget.options.message.show_reply || widget.options.message.show_reply_by_email || (widget.is_author and widget.options.message.show_dd_delete) || widget.type == 'email')">
<t t-if="(widget.options.message.show_reply || widget.options.message.show_reply_by_email || (widget.is_author and widget.options.message.show_dd_delete) || widget.type == 'email')">
<li>
<span class="oe_dropdown_toggle">
<a class="oe_e" title="More options">í</a>
@ -129,8 +125,8 @@
<li t-if="display['show_hide']">
<a href="#" class="oe_mail_msg_hide_type" t-attf-data-subtype='{widget.subtype}'>Hide '<t t-esc="widget.subtype"/>' for this document</a>
</li> -->
<li t-if="widget.options.thread._parents.length&lt;=widget.options.thread.thread_level and widget.options.message.show_reply" title="Reply"><a class="oe_reply oe_full_reply">Full reply</a></li>
<li t-if="widget.options.thread._parents.length&lt;=widget.options.thread.thread_level and widget.options.message.show_reply_by_email"><a class="oe_reply_by_email oe_full_reply" title="Reply by mail">Full reply</a></li>
<li t-if="widget.options.message.show_reply" title="Reply"><a class="oe_reply oe_full_reply">Full reply</a></li>
<li t-if="widget.options.message.show_reply_by_email"><a class="oe_reply_by_email oe_full_reply" title="Reply by mail">Full reply</a></li>
<li t-if="widget.type == 'email'"><a class="oe_mail_msg_details" t-attf-href="#model=mail.message&amp;id=#{widget.id}" >Details</a></li>
</ul>
</span>
@ -161,7 +157,7 @@
</ul>
<div class="oe_clear"/>
<div class="oe_mail_msg_body">
<t t-if="widget.options.message.show_record_name and widget.record_name and (!widget.subject) and widget.options.thread._parents.length&lt;=widget.options.thread.thread_level">
<t t-if="widget.options.message.show_record_name and widget.record_name and (!widget.subject) and widget.options.thread._parents.length&lt;=widget.options.thread.thread_level and widget.model!='res.partner'">
<a class="oe_mail_action_model" t-attf-href="#model=#{widget.model}&amp;id=#{widget.res_id}"><t t-raw="widget.record_name"/></a>
</t>
<t t-raw="widget.body"/>
@ -175,6 +171,16 @@
</div>
<div class="oe_thread_placeholder"></div>
</li>
<!-- expandable message layout -->
<li t-name="mail.thread.expandable" t-attf-class="oe_mail oe_mail_thread_msg oe_mail_unread" t-attf-data-thread_id="{widget.id}">
<div t-attf-class="oe_mail_msg_#{widget.type} oe_semantic_html_override">
<div class="oe_mail_msg_content oe_mail_msg_more_message">
<a class="oe_mail_fetch_more">Load more messages <span t-if="widget.nb_messages>0">(<t t-raw="widget.nb_messages"/> not display)</span>...</a>
</div>
</div>
</li>
<!--
mail.thread.message.attachments template
Template used to display attachments in a mail.message