[IMP] [FIX] mail_followers widget: fixed buggy button; fixed subtypes displayed only if the current user is following the document; various code cleaning.

bzr revid: tde@openerp.com-20121018091327-bt7dyi7pn9w11lsa
This commit is contained in:
Thibault Delavallée 2012-10-18 11:13:27 +02:00
parent 86868af1f1
commit 1c7859c67a
1 changed files with 68 additions and 56 deletions

View File

@ -27,10 +27,10 @@ openerp_mail_followers = function(session, mail) {
this.options.image = this.node.attrs.image || 'image_small';
this.options.title = this.node.attrs.title || 'Followers';
this.options.comment = this.node.attrs.help || false;
this.options.displayed_nb = this.node.attrs.displayed_nb || 10;
this.ds_model = new session.web.DataSetSearch(this, this.view.model);
this.sub_model = new session.web.DataSetSearch(this,'mail.message.subtype');
this.ds_follow = new session.web.DataSetSearch(this, this.field.relation);
this.follower_model = new session.web.DataSetSearch(this,'mail.followers');
this.ds_users = new session.web.DataSetSearch(this, 'res.users');
},
start: function() {
@ -39,7 +39,6 @@ openerp_mail_followers = function(session, mail) {
this._check_visibility();
this.reinit();
this.bind_events();
this.display_subtypes();
},
_check_visibility: function() {
@ -53,14 +52,17 @@ openerp_mail_followers = function(session, mail) {
bind_events: function() {
var self = this;
this.$('button.oe_follower').on('click', function (event) {
// event: click on '(Un)Follow' button, that toggles the follow for uid
this.$('.oe_follower').on('click', function (event) {
if($(this).hasClass('oe_notfollow'))
self.do_follow();
else
self.do_unfollow();
});
this.$('ul.oe_subtypes input').on('click', self.do_update_subscription);
this.$('button.oe_invite').on('click', function (event) {
// event: click on a subtype, that (un)subscribe for this subtype
this.$el.on('click', 'ul.oe_subtypes input', self.do_update_subscription);
// event: click on 'invite' button, that opens the invite wizard
this.$('.oe_invite').on('click', function (event) {
action = {
type: 'ir.actions.act_window',
res_model: 'mail.wizard.invite',
@ -81,38 +83,55 @@ openerp_mail_followers = function(session, mail) {
});
},
read_value: function() {
read_value: function () {
var self = this;
return this.ds_model.read_ids([this.view.datarecord.id], ['message_follower_ids']).pipe(function (results) {
self.set_value(results[0].message_follower_ids);
});
},
render_value: function() {
set_value: function (value_) {
this._super(value_);
// TDE FIXME: render_value is never called... ask to niv
this.render_value();
},
render_value: function () {
this.reinit();
return this.fetch_followers(this.get("value"));
},
fetch_followers: function (value_) {
this.value = value_ || {};
if (value_)
return this.ds_follow.call('read', [this.value, ['name', 'user_ids']])
.pipe(this.proxy('display_followers'), this.proxy('display_generic'))
.pipe(this.proxy('display_buttons'));
return this.ds_follow.call('read', [this.value, ['name', 'user_ids']])
.pipe(this.proxy('display_followers'), this.proxy('fetch_generic'))
.pipe(this.proxy('display_buttons'))
.pipe(this.proxy('fetch_subtypes'));
},
/** Read on res.partner failed: fall back on a generic case
- fetch current user partner_id (call because no other smart solution currently) FIXME
- then display a generic message about followers */
fetch_generic: function (error, event) {
var self = this;
event.preventDefault();
return this.ds_users.call('read', [this.session.uid, ['partner_id']]).pipe(function (results) {
var pid = results['partner_id'][0];
self.message_is_follower = (_.indexOf(self.get('value'), pid) != -1);
}).pipe(self.proxy('display_generic'));
},
/* Display generic info about follower, for people not having access to res_partner */
display_generic: function (error, event) {
event.preventDefault();
this.message_is_follower = false;
display_generic: function () {
var self = this;
var node_user_list = this.$('ul.oe_mail_followers_display').empty();
// format content: Followers (You and 0 other) // Followers (3)
var content = this.options.title;
if (this.message_is_follower) {
content += ' (You and ' + (this.value.length-1) + ' other)';
content += ' (You and ' + (this.get('value').length-1) + ' other)';
}
else {
content += ' (' + this.value.length + ')'
content += ' (' + this.get('value').length + ')'
}
this.$('div.oe_mail_recthread_followers h4').html(content);
},
@ -121,23 +140,24 @@ openerp_mail_followers = function(session, mail) {
display_followers: function (records) {
var self = this;
this.message_is_follower = this.set_is_follower(records);
// clean and display title
var node_user_list = this.$('ul.oe_mail_followers_display').empty();
this.$('div.oe_mail_recthread_followers h4').html(this.options.title + (records.length>=5 ? ' (' + records.length + ')' : '') );
for(var i=0; i<records.length&&i<5; i++) {
var record=records[i];
this.$('div.oe_mail_recthread_followers h4').html(this.options.title + ' (' + records.length + ')');
// truncate number of displayed followers
truncated = records.splice(0, this.options.displayed_nb);
_(truncated).each(function (record) {
record.avatar_url = mail.ChatterUtils.get_image(self.session, 'res.partner', 'image_small', record.id);
$(session.web.qweb.render('mail.followers.partner', {'record': record})).appendTo(node_user_list);
});
if (truncated.length < records.length) {
$('<li>And ' + (records.length - truncated.length) + ' more.</li>').appendTo(node_user_list);
}
},
/** Computes whether the current user is in the followers */
set_is_follower: function(records) {
for(var i in records) {
if (records[i]['user_ids'][0] == this.session.uid) {
return true;
}
}
return false;
set_is_follower: function (records) {
var user_ids = _.pluck(_.pluck(records, 'user_ids'), 0);
return _.indexOf(user_ids, this.session.uid) != -1;
},
display_buttons: function () {
@ -154,8 +174,18 @@ openerp_mail_followers = function(session, mail) {
this.$('span.oe_mail_invite_wrapper').show();
},
set_subtypes:function(data){
/** Fetch subtypes, only if current user is follower */
fetch_subtypes: function () {
var subtype_list_ul = this.$('.oe_subtypes').empty();
if (! this.message_is_follower) return;
var context = new session.web.CompoundContext(this.build_context(), {});
this.ds_model.call('message_get_subscription_data', [[this.view.datarecord.id], context]).pipe(this.proxy('display_subtypes'));
},
/** Display subtypes: {'name': default, followed} */
display_subtypes:function (data) {
var self = this;
var subtype_list_ul = this.$('.oe_subtypes');
var records = (data[this.view.datarecord.id] || data[null]).message_subtype_data;
_(records).each(function (record, record_name) {
@ -165,27 +195,15 @@ openerp_mail_followers = function(session, mail) {
});
},
/** Display subtypes: {'name': default, followed} */
display_subtypes: function (visible) {
var self = this;
var recthread_subtypes = self.$('.oe_recthread_subtypes');
subtype_list_ul = self.$('ul.oe_subtypes');
if(subtype_list_ul.is(":empty")) {
var context = new session.web.CompoundContext(this.build_context(), {});
this.ds_model.call('message_get_subscription_data',[[self.view.datarecord.id], context]).pipe(this.proxy('set_subtypes'));
}
},
do_follow: function () {
_(this.$('.oe_msg_subtype_check')).each(function(record){
$(record).attr('checked','checked');
_(this.$('.oe_msg_subtype_check')).each(function (record) {
$(record).attr('checked', 'checked');
});
this.do_update_subscription();
},
do_unfollow: function () {
_(this.$('.oe_msg_subtype_check')).each(function(record){
_(this.$('.oe_msg_subtype_check')).each(function (record) {
$(record).attr('checked',false);
});
var context = new session.web.CompoundContext(this.build_context(), {});
@ -196,21 +214,15 @@ openerp_mail_followers = function(session, mail) {
var self = this;
var checklist = new Array();
_(this.$('.oe_mail_recthread_actions input[type="checkbox"]')).each(function(record){
if($(record).is(':checked')) {
checklist.push(parseInt($(record).data('id')))}
_(this.$('.oe_mail_recthread_actions input[type="checkbox"]')).each(function (record) {
if ($(record).is(':checked')) {
checklist.push(parseInt($(record).data('id')))
}
});
if(!checklist.length)
return this.do_unfollow();
else{
var context = new session.web.CompoundContext(this.build_context(), {});
return this.ds_model.call('message_subscribe_users', [[this.view.datarecord.id], [this.session.uid], undefined, context]).pipe(function(value_){
self.read_value(value_);
self.display_subtypes(true);
});
}
var context = new session.web.CompoundContext(this.build_context(), {});
return this.ds_model.call('message_subscribe_users', [[this.view.datarecord.id], [this.session.uid], this.message_is_follower ? checklist:undefined, context])
.pipe(this.proxy('read_value'));
},
});
};